// File: /workspace/github_cpp_makefile_star2_O1/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 0x4cd1
movzbl 0x1(%rdi), %eax
incq %rdi
cmpl $0x20, %eax
je 0x4cdd
cmpb $0x78, %bl
je 0x4cf6
movzbl %bl, %ecx
cmpl $0x64, %ecx
jne 0x4d57
orl $0x20, %eax
cmpl $0x72, %eax
jne 0x4e04
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x4d02
xorl %esi, %esi
movl $0xa, %edx
callq 0x1120
testl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
jne 0x4e59
cmpb $0x64, %bl
je 0x4e24
movzbl %bl, %ecx
cmpl $0x78, %ecx
jne 0x4e50
andl $0x1f, %eax
leaq 0x53ae(%rip), %rcx      # 0xa0f0
leaq (%rcx,%rax,8), %rdi
callq 0x5c86
leaq 0x14ad(%rip), %rdi      # 0x61ff
jmp 0x4e3e
addl $-0x30, %ebx
cmpl $0x9, %ebx
ja 0x4e78
movsbl (%rdi), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x4d88
leal (%rbx,%rbx,4), %ecx
leal (%rax,%rcx,2), %ebx
movsbl 0x1(%rdi), %eax
incq %rdi
addl $-0x30, %eax
cmpl $0xa, %eax
jb 0x4d6e
jmp 0x4d88
incq %rdi
movzbl (%rdi), %r14d
cmpl $0x20, %r14d
je 0x4d85
leal -0x62(%r14), %eax
cmpl $0x15, %eax
ja 0x4e04
movl $0x200005, %ecx         # imm = 0x200005
btl %eax, %ecx
jae 0x4e04
movzbl 0x1(%rdi), %ebp
incq %rdi
cmpl $0x20, %ebp
je 0x4da5
cmpl $0x78, %ebp
je 0x4dbb
cmpl $0x64, %ebp
jne 0x4e04
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x4dbb
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 0x4c20
leaq 0x1f80(%rip), %rdi      # 0x6d8b
callq 0x1140
leaq 0x1e3a(%rip), %rdi      # 0x6c51
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1140
andl $0x1f, %eax
leaq 0x52c2(%rip), %rcx      # 0xa0f0
leaq (%rcx,%rax,8), %rdi
callq 0x5c86
leaq 0x13ca(%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 0x1385(%rip), %rdi      # 0x61e5
leaq 0x132f(%rip), %rsi      # 0x6196
leaq 0x135e(%rip), %rcx      # 0x61cc
movl $0x53, %edx
callq 0x1080
leaq 0x138e(%rip), %rdi      # 0x620d
leaq 0x1310(%rip), %rsi      # 0x6196
leaq 0x133f(%rip), %rcx      # 0x61cc
movl $0x5d, %edx
callq 0x1080