// File: /workspace/github_cpp_makefile_star2_O2/akshayrajput[P]ARMV8_Simulator/./instructions.cpp
// Function: ADD(int, int, int, int, int)
void ADD(int sf, int sw,int RD,int RN,int immRM)
{
	
	//ADD stub
	printf("[log] - add instructions\n");

	assert(RD >= 0 && RD <= 31);
	assert(RN >= 0 && RN <= 31);
	
	//TODO - Add shifted operands also
	if(sf == 1 && sw == 0)
	{
		printf("[log] - adding 64 bit immediate\n");
		uint64_t result = R[RN].getData() + immRM ;
		
		if(result < R[RN].getData() )
		{
			printf("Addition overflow!!\n");
			return;
		}

		R[RD].setData(result);

	}
	else if(sf == 0 && sw == 0)
	{
		printf("[log] - adding 32 bit immediate\n");
		uint32_t result = R[RN].getLowerData() + immRM ;
		
		if(result < R[RN].getLowerData() )
		{
			printf("Addition overflow!!\n");
			return;
		}

		R[RD].setLowerData(result);

	}
	else if(sf == 1 && sw == 1)
	{
		printf("[log] - adding 64 bit register\n");
		assert(immRM >= 0 && immRM <= 31);
		uint64_t result = R[RN].getData() + R[immRM].getData() ;
		
		if(result < R[RN].getData() )
		{
			printf("Addition overflow!!\n");
			return;
		}

		R[RD].setData(result);

	}
	else if(sf == 0 && sw == 1)
	{
		printf("[log] - adding 32 bit register\n");
		assert(immRM >= 0 && immRM <= 31);
		uint32_t result = R[RN].getLowerData() + R[immRM].getData() ;
		
		if(result < R[RN].getLowerData() )
		{
			printf("Addition overflow!!\n");
			return;
		}

		R[RD].setLowerData(result);

	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movl %ecx, %r14d
movl %edx, %ebx
movl %esi, %r15d
movl %edi, %r12d
leaq 0x4d7b(%rip), %rdi      # 0x63b0
callq 0x1120
cmpl $0x20, %ebx
jae 0x1826
cmpl $0x20, %r14d
jae 0x1845
movl %r12d, %eax
xorl $0x1, %eax
movl %eax, %ecx
orl %r15d, %ecx
jne 0x169e
leaq 0x4dc6(%rip), %rdi      # 0x6427
callq 0x1120
movl %r14d, %eax
leaq 0x8a70(%rip), %r12      # 0xa0e0
leaq (%r12,%rax,8), %r15
movq %r15, %rdi
callq 0x5b22
movslq %ebp, %r14
addq %rax, %r14
movq %r15, %rdi
callq 0x5b22
cmpq %rax, %r14
jb 0x17da
movl %ebx, %eax
leaq (%r12,%rax,8), %rdi
jmp 0x1722
movl %r15d, %ecx
orl %r12d, %ecx
je 0x1738
xorl $0x1, %r15d
orl %r15d, %eax
jne 0x177b
leaq 0x4d2a(%rip), %rdi      # 0x63e8
callq 0x1120
cmpl $0x20, %ebp
jae 0x1864
movl %r14d, %eax
leaq 0x8a0a(%rip), %r13      # 0xa0e0
leaq (,%rax,8), %r15
addq %r13, %r15
movq %r15, %rdi
callq 0x5b22
movq %rax, %r12
movl %ebp, %eax
leaq (,%rax,8), %rdi
addq %r13, %rdi
callq 0x5b22
movq %rax, %r14
addq %r12, %r14
movq %r15, %rdi
callq 0x5b22
cmpq %rax, %r14
jb 0x17da
movl %ebx, %eax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5b2a
leaq 0x4cc8(%rip), %rdi      # 0x6407
callq 0x1120
movl %r14d, %eax
leaq 0x8992(%rip), %r12      # 0xa0e0
leaq (%r12,%rax,8), %r15
movq %r15, %rdi
callq 0x5b26
movl %eax, %r14d
addl %ebp, %r14d
movq %r15, %rdi
callq 0x5b26
cmpl %eax, %r14d
jb 0x17da
movl %ebx, %eax
leaq (%r12,%rax,8), %rdi
movl %r14d, %esi
jmp 0x1813
orl %r15d, %r12d
jne 0x17f4
leaq 0x4c42(%rip), %rdi      # 0x63c9
callq 0x1120
cmpl $0x20, %ebp
jae 0x1883
movl %r14d, %eax
leaq 0x8941(%rip), %r13      # 0xa0e0
leaq (,%rax,8), %r15
addq %r13, %r15
movq %r15, %rdi
callq 0x5b26
movl %eax, %r12d
movl %ebp, %eax
leaq (,%rax,8), %rdi
addq %r13, %rdi
callq 0x5b22
movq %rax, %r14
addl %r12d, %r14d
movq %r15, %rdi
callq 0x5b26
cmpl %eax, %r14d
jae 0x1803
leaq 0x4c66(%rip), %rdi      # 0x6447
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1120
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %eax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movl %r14d, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5b30
leaq 0x48d9(%rip), %rdi      # 0x6106
leaq 0x485b(%rip), %rsi      # 0x608f
leaq 0x48df(%rip), %rcx      # 0x611a
movl $0x97, %edx
callq 0x1070
leaq 0x48f0(%rip), %rdi      # 0x613c
leaq 0x483c(%rip), %rsi      # 0x608f
leaq 0x48c0(%rip), %rcx      # 0x611a
movl $0x98, %edx
callq 0x1070
leaq 0x48e5(%rip), %rdi      # 0x6150
leaq 0x481d(%rip), %rsi      # 0x608f
leaq 0x48a1(%rip), %rcx      # 0x611a
movl $0xba, %edx
callq 0x1070
leaq 0x48c6(%rip), %rdi      # 0x6150
leaq 0x47fe(%rip), %rsi      # 0x608f
leaq 0x4882(%rip), %rcx      # 0x611a
movl $0xc9, %edx
callq 0x1070