#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define AND 0
#define OR 1
#define NAND 2
#define NOR 3
#define XOR 4
#define NXOR 5

/*
* Evaluates logical expression.
*/
char evaluateLogicGate(int ops[]) {
    int result=0;

    switch (ops[2]) {
    case NOR:
        result = !(ops[0] | ops[1]);
        break;
    case XOR:
        result = ops[0] ^ ops[1];
        break;
    case NAND:
        result = !(ops[0] & ops[1]);
        break;
    case AND:
        result = ops[0] & ops[1];
        break;
    case OR:
        result = ops[0] | ops[1];
        break;
    case NXOR:
        result = !(ops[0] ^ ops[1]);
        break;
    }

    char output = result + '0';
    return output;

}

/*
* Sequential computation of list of logical expressions.
* Takes input file, file length and output file name as arguments.
*/
int main(int argc, char* argv[]) {
    if (argc != 4) {
        printf("Error: Please enter the input file path, input file length and output file path when running.\n");
        return 1;
    }
    char line[10];
    char* inputFileName = argv[1];
    int inputLength = atoi(argv[2]);
    char* outputFileName = argv[3];

    FILE* inputFile = fopen(inputFileName, "r");
    FILE* outputFile = fopen(outputFileName, "w");

    if (inputFile == NULL) {
        fprintf(stderr, "Error opening file.\n");
        return 1;
    }

    clock_t start = clock();

    int operands[3] = { 0 };
    char result = NULL;
    while (fgets(line, 10, inputFile)) {
        operands[0] = atoi(line);
        operands[1] = atoi(line + 2);
        operands[2] = atoi(line + 4);
        result = evaluateLogicGate(operands);
        fprintf(outputFile, "%c\n", result);

    }
    clock_t end = clock();
    int duration = ((end - start) * 1000) / CLOCKS_PER_SEC;

    fclose(outputFile);
    fclose(inputFile);

    printf("Completed\nTime: %ds %dms\n", duration / 1000, duration % 1000);

    return 0;

}
	.file	"tmpxft_003336c2_00000000-6_sequential.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3027:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3027:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z17evaluateLogicGatePi
	.type	_Z17evaluateLogicGatePi, @function
_Z17evaluateLogicGatePi:
.LFB3023:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	cmpl	$5, 8(%rdi)
	ja	.L3
	movl	8(%rdi), %edx
	leaq	.L5(%rip), %rax
	movslq	(%rax,%rdx,4), %rdx
	addq	%rax, %rdx
	movl	(%rdi), %eax
	notrack jmp	*%rdx
	.section	.rodata
	.align 4
	.align 4
.L5:
	.long	.L10-.L5
	.long	.L9-.L5
	.long	.L8-.L5
	.long	.L7-.L5
	.long	.L6-.L5
	.long	.L4-.L5
	.text
.L7:
	orl	4(%rdi), %eax
	jmp	.L12
.L6:
	xorl	4(%rdi), %eax
	jmp	.L3
.L8:
	andl	4(%rdi), %eax
	jmp	.L12
.L10:
	andl	4(%rdi), %eax
	jmp	.L3
.L9:
	orl	4(%rdi), %eax
	jmp	.L3
.L4:
	cmpl	4(%rdi), %eax
.L12:
	sete	%al
	movzbl	%al, %eax
.L3:
	addl	$48, %eax
	ret
	.cfi_endproc
.LFE3023:
	.size	_Z17evaluateLogicGatePi, .-_Z17evaluateLogicGatePi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Error: Please enter the input file path, input file length and output file path when running.\n"
.LC1:
	.string	"r"
.LC2:
	.string	"w"
.LC3:
	.string	"Error opening file.\n"
.LC4:
	.string	"%c\n"
.LC5:
	.string	"Completed\nTime: %ds %dms\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3024:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	cmpl	$4, %edi
	je	.L15
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L16
.L15:
	movq	24(%rsi), %rbp
	movq	8(%rsi), %rdi
	leaq	.LC1(%rip), %rsi
	call	fopen@PLT
	movq	%rbp, %rdi
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rbx
	call	fopen@PLT
	movq	%rax, %rbp
	testq	%rbx, %rbx
	jne	.L17
	movq	stderr(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
.L16:
	movl	$1, %eax
	jmp	.L14
.L17:
	call	clock@PLT
	xorl	%edx, %edx
	leaq	.LC4(%rip), %r13
	movq	%rax, %r12
	xorl	%eax, %eax
	movl	%edx, 8(%rsp)
	movq	%rax, (%rsp)
.L19:
	leaq	14(%rsp), %r14
	movq	%rbx, %rcx
	movl	$10, %edx
	movl	$10, %esi
	movq	%r14, %rdi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	je	.L23
	movq	%r14, %rdi
	call	atoi@PLT
	leaq	16(%rsp), %rdi
	movl	%eax, (%rsp)
	call	atoi@PLT
	leaq	18(%rsp), %rdi
	movl	%eax, 4(%rsp)
	call	atoi@PLT
	movq	%rsp, %rdi
	movl	$2, %esi
	movl	%eax, 8(%rsp)
	call	_Z17evaluateLogicGatePi
	movq	%r13, %rdx
	movq	%rbp, %rdi
	movsbl	%al, %ecx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L19
.L23:
	call	clock@PLT
	movl	$1000, %ecx
	movq	%rbp, %rdi
	subq	%r12, %rax
	cqto
	idivq	%rcx
	movq	%rax, %r14
	call	fclose@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movl	%r14d, %eax
	movl	$1000, %esi
	movl	$2, %edi
	cltd
	idivl	%esi
	leaq	.LC5(%rip), %rsi
	movl	%edx, %ecx
	movl	%eax, %edx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
.L14:
	movq	24(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3024:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3050:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3050:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define AND 0
#define OR 1
#define NAND 2
#define NOR 3
#define XOR 4
#define NXOR 5

/*
* Evaluates logical expression.
*/
char evaluateLogicGate(int ops[]) {
    int result=0;

    switch (ops[2]) {
    case NOR:
        result = !(ops[0] | ops[1]);
        break;
    case XOR:
        result = ops[0] ^ ops[1];
        break;
    case NAND:
        result = !(ops[0] & ops[1]);
        break;
    case AND:
        result = ops[0] & ops[1];
        break;
    case OR:
        result = ops[0] | ops[1];
        break;
    case NXOR:
        result = !(ops[0] ^ ops[1]);
        break;
    }

    char output = result + '0';
    return output;

}

/*
* Sequential computation of list of logical expressions.
* Takes input file, file length and output file name as arguments.
*/
int main(int argc, char* argv[]) {
    if (argc != 4) {
        printf("Error: Please enter the input file path, input file length and output file path when running.\n");
        return 1;
    }
    char line[10];
    char* inputFileName = argv[1];
    int inputLength = atoi(argv[2]);
    char* outputFileName = argv[3];

    FILE* inputFile = fopen(inputFileName, "r");
    FILE* outputFile = fopen(outputFileName, "w");

    if (inputFile == NULL) {
        fprintf(stderr, "Error opening file.\n");
        return 1;
    }

    clock_t start = clock();

    int operands[3] = { 0 };
    char result = NULL;
    while (fgets(line, 10, inputFile)) {
        operands[0] = atoi(line);
        operands[1] = atoi(line + 2);
        operands[2] = atoi(line + 4);
        result = evaluateLogicGate(operands);
        fprintf(outputFile, "%c\n", result);

    }
    clock_t end = clock();
    int duration = ((end - start) * 1000) / CLOCKS_PER_SEC;

    fclose(outputFile);
    fclose(inputFile);

    printf("Completed\nTime: %ds %dms\n", duration / 1000, duration % 1000);

    return 0;

}
	.text
	.file	"sequential.hip"
	.globl	_Z17evaluateLogicGatePi         # -- Begin function _Z17evaluateLogicGatePi
	.type	_Z17evaluateLogicGatePi,@function
_Z17evaluateLogicGatePi:                # @_Z17evaluateLogicGatePi
	.cfi_startproc
# %bb.0:
	movl	8(%rdi), %eax
	cmpq	$5, %rax
	ja	.LBB0_1
# %bb.2:
	jmpq	*.LJTI0_0(,%rax,8)
.LBB0_6:
	movl	4(%rdi), %eax
	andl	(%rdi), %eax
	jmp	.LBB0_10
.LBB0_4:
	movl	4(%rdi), %eax
	xorl	(%rdi), %eax
	jmp	.LBB0_10
.LBB0_5:
	movl	4(%rdi), %ecx
	xorl	%eax, %eax
	testl	%ecx, (%rdi)
	jmp	.LBB0_9
.LBB0_3:
	movl	4(%rdi), %ecx
	xorl	%eax, %eax
	orl	(%rdi), %ecx
	jmp	.LBB0_9
.LBB0_7:
	movl	4(%rdi), %eax
	orl	(%rdi), %eax
	jmp	.LBB0_10
.LBB0_8:
	movl	(%rdi), %ecx
	xorl	%eax, %eax
	cmpl	4(%rdi), %ecx
.LBB0_9:
	sete	%al
	jmp	.LBB0_10
.LBB0_1:
	xorl	%eax, %eax
.LBB0_10:
	addb	$48, %al
                                        # kill: def $al killed $al killed $eax
	retq
.Lfunc_end0:
	.size	_Z17evaluateLogicGatePi, .Lfunc_end0-_Z17evaluateLogicGatePi
	.cfi_endproc
	.section	.rodata,"a",@progbits
	.p2align	3, 0x0
.LJTI0_0:
	.quad	.LBB0_6
	.quad	.LBB0_7
	.quad	.LBB0_5
	.quad	.LBB0_3
	.quad	.LBB0_4
	.quad	.LBB0_8
                                        # -- End function
	.text
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$4, %edi
	jne	.LBB1_6
# %bb.1:
	movq	8(%rsi), %rdi
	movq	24(%rsi), %r14
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	movl	$.L.str.2, %esi
	movq	%r14, %rdi
	callq	fopen
	testq	%rbx, %rbx
	je	.LBB1_9
# %bb.2:
	movq	%rax, %r14
	callq	clock
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	leaq	10(%rsp), %rdi
	movl	$10, %esi
	movq	%rbx, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB1_5
# %bb.3:                                # %.lr.ph
	leaq	12(%rsp), %r12
	leaq	14(%rsp), %r13
	leaq	10(%rsp), %rbp
	leaq	20(%rsp), %r15
.LBB1_4:                                # =>This Inner Loop Header: Depth=1
	movq	%rbp, %rdi
	callq	atoi
	movl	%eax, 20(%rsp)
	movq	%r12, %rdi
	callq	atoi
	movl	%eax, 24(%rsp)
	movq	%r13, %rdi
	callq	atoi
	movl	%eax, 28(%rsp)
	movq	%r15, %rdi
	callq	_Z17evaluateLogicGatePi
	movsbl	%al, %edx
	movl	$.L.str.4, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movq	%rbp, %rdi
	movl	$10, %esi
	movq	%rbx, %rdx
	callq	fgets
	testq	%rax, %rax
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge
	callq	clock
	subq	32(%rsp), %rax                  # 8-byte Folded Reload
	movabsq	$2361183241434822607, %rcx      # imm = 0x20C49BA5E353F7CF
	imulq	%rcx
	movq	%rdx, %r15
	movq	%rdx, %rax
	shrq	$63, %rax
	sarq	$7, %r15
	addq	%rax, %r15
	movq	%r14, %rdi
	callq	fclose
	movq	%rbx, %rdi
	callq	fclose
	movslq	%r15d, %rdx
	imulq	$274877907, %rdx, %rsi          # imm = 0x10624DD3
	movq	%rsi, %rax
	shrq	$63, %rax
	sarq	$38, %rsi
	addl	%eax, %esi
	imull	$1000, %esi, %eax               # imm = 0x3E8
	subl	%eax, %edx
	xorl	%ebx, %ebx
	movl	$.L.str.5, %edi
                                        # kill: def $esi killed $esi killed $rsi
                                        # kill: def $edx killed $edx killed $rdx
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB1_8
.LBB1_6:
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB1_7:
	movl	$1, %ebx
.LBB1_8:
	movl	%ebx, %eax
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB1_9:
	.cfi_def_cfa_offset 96
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$20, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	jmp	.LBB1_7
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"r"
	.size	.L.str.1, 2

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"w"
	.size	.L.str.2, 2

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Error opening file.\n"
	.size	.L.str.3, 21

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"%c\n"
	.size	.L.str.4, 4

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Completed\nTime: %ds %dms\n"
	.size	.L.str.5, 26

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Error: Please enter the input file path, input file length and output file path when running."
	.size	.Lstr, 94

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
