// File: /workspace/github_c_makefile_star2_O0/hamsternz[P]emulate-risc-v/memorymap.c
// Function: memorymap_read
int memorymap_read(uint32_t address, uint8_t width, uint32_t *value) {
   uint32_t v, v1, mask;

   /* Mask out data we don't want */
   switch(width) {
     case 1:
       mask = 0xFF;
       break; 
     case 2:
       mask = 0xFFFF;
       break; 
     case 4:
       mask = 0xFFFFFFFF;
       break; 
     default:
       display_log("Invalid read width at address 0x%08x");
       return 0;
   }

   /* Read the memory region */
   switch(address & 3) {
     case 0:
       if(!memorymap_aligned_read(address+0, &v))  return 0;
       break;
     case 1:
       if(!memorymap_aligned_read(address-1, &v))  return 0;
       if(!memorymap_aligned_read(address+3, &v1)) return 0;
       v = (v>>8) | (v1 <<24);
       break;
     case 2:
       if(!memorymap_aligned_read(address-2, &v))  return 0;
       if(!memorymap_aligned_read(address+2, &v1)) return 0;
       v = (v>>16) | (v1 <<16);
       break;
     default:
       if(!memorymap_aligned_read(address-3, &v))  return 0;
       if(!memorymap_aligned_read(address+1, &v1)) return 0;
       v = (v>>24) | (v1 <<8);
       break;
   }
   /* Set the return value */
   *value = v & mask;
   return 1;
}

// Assembly:
subq $0x38, %rsp
movb %sil, %al
movl %edi, 0x30(%rsp)
movb %al, 0x2f(%rsp)
movq %rdx, 0x20(%rsp)
movzbl 0x2f(%rsp), %eax
movl %eax, 0x10(%rsp)
subl $0x1, %eax
je 0x483a
jmp 0x4824
movl 0x10(%rsp), %eax
subl $0x2, %eax
je 0x4844
jmp 0x482f
movl 0x10(%rsp), %eax
subl $0x4, %eax
je 0x484e
jmp 0x4858
movl $0xff, 0x14(%rsp)
jmp 0x4871
movl $0xffff, 0x14(%rsp)     # imm = 0xFFFF
jmp 0x4871
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
jmp 0x4871
leaq 0x664a(%rip), %rdi      # 0xaea9
callq 0x6b20
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x30(%rsp), %eax
andl $0x3, %eax
movl %eax, 0xc(%rsp)
je 0x489d
jmp 0x4880
movl 0xc(%rsp), %eax
subl $0x1, %eax
je 0x48c5
jmp 0x488b
movl 0xc(%rsp), %eax
subl $0x2, %eax
je 0x4924
jmp 0x4980
movl 0x30(%rsp), %edi
addl $0x0, %edi
leaq 0x1c(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x48c0
movl $0x0, 0x34(%rsp)
jmp 0x49eb
jmp 0x49d4
movl 0x30(%rsp), %edi
subl $0x1, %edi
leaq 0x1c(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x48e8
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x30(%rsp), %edi
addl $0x3, %edi
leaq 0x18(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x490b
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x1c(%rsp), %eax
shrl $0x8, %eax
movl 0x18(%rsp), %ecx
shll $0x18, %ecx
orl %ecx, %eax
movl %eax, 0x1c(%rsp)
jmp 0x49d4
movl 0x30(%rsp), %edi
subl $0x2, %edi
leaq 0x1c(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x4947
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x30(%rsp), %edi
addl $0x2, %edi
leaq 0x18(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x496a
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x1c(%rsp), %eax
shrl $0x10, %eax
movl 0x18(%rsp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, 0x1c(%rsp)
jmp 0x49d4
movl 0x30(%rsp), %edi
subl $0x3, %edi
leaq 0x1c(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x49a0
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x30(%rsp), %edi
addl $0x1, %edi
leaq 0x18(%rsp), %rsi
callq 0x4100
cmpl $0x0, %eax
jne 0x49c0
movl $0x0, 0x34(%rsp)
jmp 0x49eb
movl 0x1c(%rsp), %eax
shrl $0x18, %eax
movl 0x18(%rsp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %ecx
andl 0x14(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
movl $0x1, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)