// File: /workspace/github_cpp_makefile_star2_O2/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 0x4be3
movzbl 0x1(%rdi), %ebp
incq %rdi
cmpl $0x20, %ebp
je 0x4bef
cmpb $0x78, %bl
je 0x4c08
movzbl %bl, %eax
cmpl $0x64, %eax
jne 0x4c67
orl $0x20, %ebp
cmpl $0x72, %ebp
jne 0x4d13
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x4c14
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x1100
testl $0xffffffe0, %eax       # imm = 0xFFFFFFE0
jne 0x4d68
cmpb $0x64, %bl
je 0x4d33
movzbl %bl, %ecx
cmpl $0x78, %ecx
jne 0x4d5f
andl $0x1f, %eax
leaq 0x548e(%rip), %rcx      # 0xa0e0
leaq (%rcx,%rax,8), %rdi
callq 0x5b22
leaq 0x159d(%rip), %rdi      # 0x61ff
jmp 0x4d4d
addl $-0x30, %ebx
cmpl $0x9, %ebx
ja 0x4d81
movsbl %bpl, %eax
addl $-0x30, %eax
cmpl $0xa, %eax
jae 0x4c96
imull $0xa, %ebx, %ecx
addl %ecx, %eax
movb 0x1(%rdi), %bpl
incq %rdi
movl %eax, %ebx
jmp 0x4c73
movb 0x1(%rdi), %bpl
incq %rdi
cmpb $0x20, %bpl
je 0x4c8f
movzbl %bpl, %eax
addl $-0x62, %eax
cmpl $0x15, %eax
ja 0x4d13
movl $0x200005, %ecx         # imm = 0x200005
btl %eax, %ecx
jae 0x4d13
movzbl 0x1(%rdi), %r14d
incq %rdi
cmpl $0x20, %r14d
je 0x4cb2
cmpl $0x78, %r14d
je 0x4ccc
cmpl $0x64, %r14d
jne 0x4d13
cmpb $0x20, 0x1(%rdi)
leaq 0x1(%rdi), %rdi
je 0x4ccc
cmpb $0x64, %bpl
sete %r15b
xorl %r12d, %r12d
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x1050
orb $0x2, %r15b
cmpb $0x62, %bpl
movzbl %r15b, %ecx
cmovel %r12d, %ecx
shll %cl, %ebx
movsbl %r14b, %edx
movq %rax, %rdi
movl %ebx, %esi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x4b36
leaq 0x2071(%rip), %rdi      # 0x6d8b
callq 0x1120
leaq 0x1f2b(%rip), %rdi      # 0x6c51
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1120
andl $0x1f, %eax
leaq 0x53a3(%rip), %rcx      # 0xa0e0
leaq (%rcx,%rax,8), %rdi
callq 0x5b22
leaq 0x14bb(%rip), %rdi      # 0x6208
movq %rax, %rsi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1030
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1476(%rip), %rdi      # 0x61e5
leaq 0x1420(%rip), %rsi      # 0x6196
leaq 0x144f(%rip), %rcx      # 0x61cc
pushq $0x53
jmp 0x4d98
leaq 0x1485(%rip), %rdi      # 0x620d
leaq 0x1407(%rip), %rsi      # 0x6196
leaq 0x1436(%rip), %rcx      # 0x61cc
pushq $0x5d
popq %rdx
callq 0x1070