// File: /workspace/github_cpp_makefile_star2_O3/akshayrajput[P]ARMV8_Simulator/simulo.cpp
// Function: printRegMem(char*)
void printRegMem(char* arg)
{
	//TODO - parse args
	while(*arg == ' ')arg++;
	
	char c = *arg;
	arg++ ;
	//TODO - if x or d then print register
	while(*arg == ' ') arg++ ;
		
	if(c == 'x' || c == 'd')
	{
		if(*arg != 'r' && *arg != 'R')
		{
			printf("Please enter a valid command\n");
			printf("Type help or h for details\n");
			return;
		}	
		assert(*arg == 'R' || *arg == 'r');
		arg++;
		while(*arg == ' ') arg++ ;

		int regno = (int)strtol(arg,NULL,10);
		assert(regno >= 0 && regno <= 31);

		if(c == 'x')
			printf("0x%02lx\n",R[regno].getData());
		else if(c == 'd')
			printf("%ld\n",(int64_t)R[regno].getData());
	}
	//TODO- else print memory
	else
	{
		assert(isdigit(c));
		int n = c - '0';
		while(isdigit(*arg))
		 {
			 n = n*10 + (*arg - '0') ;
			 arg++;
		 }

		while(*arg == ' ')arg++;
		
		if(*arg != 'w' && *arg != 'b' && *arg!= 'd')
		{
			printf("Please enter a valid command\n");
			printf("Type help or h for details\n");
			return;
		}
		
		assert(*arg == 'w' || *arg == 'b' || *arg == 'd' );
		
		int offset;
		if(*arg == 'w')
			offset = 4;
		else if(*arg == 'd')
			offset = 8;
		else if(*arg == 'b')
			offset = 1;
		
		arg++;
		while(*arg == ' ') arg++;

		if(*arg != 'x' && *arg != 'd')
		{
			printf("Please enter a valid command\n");
			printf("Type help or h for details\n");
			return;
		}
		assert(*arg == 'x' || *arg == 'd');
		c = *arg;

		arg++;

		while(*arg == ' ')arg++;

		uint64_t addr = strtoul(arg,NULL,16);
		printMem(addr,n * offset,c);
		
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
decq %rdi
movsbl 0x1(%rdi), %ebx
incq %rdi
cmpl $0x20, %ebx
je 0x4d12
movsbl 0x1(%rdi), %r14d
incq %rdi
cmpl $0x20, %r14d
je 0x4d1e
cmpb $0x78, %bl
je 0x4d39
movzbl %bl, %eax
cmpl $0x64, %eax
jne 0x4d9e
movzbl %r14b, %eax
orl $0x20, %eax
cmpl $0x72, %eax
jne 0x4e50
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x4d49
xorl %esi, %esi
movl $0xa, %edx
callq 0x1120
testl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
jne 0x4ea5
cmpb $0x64, %bl
je 0x4e70
movzbl %bl, %ecx
cmpl $0x78, %ecx
jne 0x4e9c
andl $0x1f, %eax
leaq 0x5367(%rip), %rcx      # 0xa0f0
leaq (%rcx,%rax,8), %rdi
callq 0x5cae
leaq 0x1466(%rip), %rdi      # 0x61ff
jmp 0x4e8a
addl $-0x30, %ebx
cmpl $0x9, %ebx
ja 0x4ec4
movl %r14d, %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x4dd5
leal (%rbx,%rbx,4), %ecx
leal (%rax,%rcx,2), %ebx
movsbl 0x1(%rdi), %r14d
incq %rdi
leal -0x30(%r14), %eax
cmpl $0xa, %eax
jb 0x4db5
jmp 0x4dd5
movb 0x1(%rdi), %r14b
incq %rdi
cmpb $0x20, %r14b
je 0x4dce
movzbl %r14b, %eax
addl $-0x62, %eax
cmpl $0x15, %eax
ja 0x4e50
movl $0x200005, %ecx         # imm = 0x200005
btl %eax, %ecx
jae 0x4e50
movzbl 0x1(%rdi), %ebp
incq %rdi
cmpl $0x20, %ebp
je 0x4df1
cmpl $0x78, %ebp
je 0x4e07
cmpl $0x64, %ebp
jne 0x4e50
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x4e07
cmpb $0x64, %r14b
sete %r15b
xorl %r12d, %r12d
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
orb $0x2, %r15b
cmpb $0x62, %r14b
movzbl %r15b, %ecx
cmovel %r12d, %ecx
shll %cl, %ebx
movsbl %bpl, %edx
movq %rax, %rdi
movl %ebx, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4c61
leaq 0x1f34(%rip), %rdi      # 0x6d8b
callq 0x1140
leaq 0x1dee(%rip), %rdi      # 0x6c51
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1140
andl $0x1f, %eax
leaq 0x5276(%rip), %rcx      # 0xa0f0
leaq (%rcx,%rax,8), %rdi
callq 0x5cae
leaq 0x137e(%rip), %rdi      # 0x6208
movq %rax, %rsi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1040
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1339(%rip), %rdi      # 0x61e5
leaq 0x12e3(%rip), %rsi      # 0x6196
leaq 0x1312(%rip), %rcx      # 0x61cc
movl $0x53, %edx
callq 0x1080
leaq 0x1342(%rip), %rdi      # 0x620d
leaq 0x12c4(%rip), %rsi      # 0x6196
leaq 0x12f3(%rip), %rcx      # 0x61cc
movl $0x5d, %edx
callq 0x1080