// File: /workspace/github_c_makefile_star2_O2/hamsternz[P]emulate-risc-v/memorymap.c
// Function: memorymap_write
int memorymap_write(uint32_t address, uint32_t width, uint32_t value) {
   /* Read the memory region */
   switch(address & 3) {
     case 0:
       switch(width) {
	 case 0xFFFFFFFF:
	 case 0xF:
	 case 4:
           if(!memorymap_aligned_write(address+0, 0xF, value))  return 0;
	   return 1;
	 case 0xFFFF:
	 case 0x3:
	 case 2:
           if(!memorymap_aligned_write(address+0, 0x3, value))  return 0;
	   return 1;
	 case 0xFF:
	 case 1:
           if(!memorymap_aligned_write(address+0, 0x1, value))  return 0;
	   return 1;
       }
       break;
     case 1:
       switch(width) {
	 case 0xFFFFFFFF:
	 case 0xF:
	 case 4:
           if(!memorymap_aligned_write(address-1, 0xE, value<<8 ))  return 0;
           if(!memorymap_aligned_write(address+3, 0x1, value>>24))  return 0;
	   return 1;
	 case 0xFFFF:
	 case 3:
	 case 2:
           if(!memorymap_aligned_write(address-1, 0x6, value<<8 ))  return 0;
	   return 1;
	 case 0xFF:
	 case 1:
           if(!memorymap_aligned_write(address-1, 0x2, value<<8 ))  return 0;
	   return 1;
       }
       break;
     case 2:
       switch(width) {
	 case 0xFFFFFFFF:
	 case 0xF:
	 case 4:
           if(!memorymap_aligned_write(address-2, 0xC, value<<16))  return 0;
           if(!memorymap_aligned_write(address+2, 0x3, value>>16))  return 0;
	   return 1;
	 case 0xFFFF:
	 case 3:
	 case 2:
           if(!memorymap_aligned_write(address-2, 0xC, value<<16))  return 0;
	   return 1;
	 case 0xFF:
	 case 1:
           if(!memorymap_aligned_write(address-2, 0x4, value<<16))  return 0;
	   return 1;
       }
       break;
     default:
       switch(width) {
	 case 0xFFFFFFFF:
	 case 0xF:
	 case 4:
           if(!memorymap_aligned_write(address-3, 0x8, value<<24))  return 0;
           if(!memorymap_aligned_write(address+1, 0x7, value>>8 ))  return 0;
	   return 1;
	 case 0xFFFF:
	 case 3:
	 case 2:
           if(!memorymap_aligned_write(address-3, 0x8, value<<24))  return 0;
           if(!memorymap_aligned_write(address+1, 0x1, value>>8 ))  return 0;
	   return 1;
	 case 0xFF:
	 case 1:
           if(!memorymap_aligned_write(address-3, 0x8, value<<24))  return 0;
	   return 1;
       }
       break;
   }
   display_log("Invalid write at address 0x%08x width %i value 0x%08x");
   return 0;
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movl %edx, %ebx
movl %edi, %r14d
movl %edi, %eax
andl $0x3, %eax
leaq 0x42fc(%rip), %rcx      # 0x7da8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leal 0x1(%rsi), %eax
cmpl $0x5, %eax
ja 0x3b9f
leaq 0x4338(%rip), %rcx      # 0x7e00
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leal 0x1(%rsi), %eax
cmpl $0x5, %eax
ja 0x3b1d
leaq 0x42f0(%rip), %rcx      # 0x7dd0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leal 0x1(%rsi), %eax
cmpl $0x5, %eax
ja 0x3b60
leaq 0x42c0(%rip), %rcx      # 0x7db8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leal 0x1(%rsi), %eax
cmpl $0x5, %eax
ja 0x3bc2
leaq 0x42d4(%rip), %rcx      # 0x7de8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3c05
cmpl $0xff, %esi
je 0x3c39
cmpl $0xf, %esi
jne 0x3bf5
leal -0x2(%r14), %edi
movl %ebx, %edx
shll $0x10, %edx
pushq $0xc
popq %rsi
callq 0x3610
testl %eax, %eax
je 0x3c01
addl $0x2, %r14d
shrl $0x10, %ebx
jmp 0x3bbb
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3c10
cmpl $0xff, %esi
je 0x3c44
cmpl $0xf, %esi
jne 0x3bf5
leal -0x3(%r14), %edi
movl %ebx, %edx
shll $0x18, %edx
pushq $0x8
popq %rsi
callq 0x3610
testl %eax, %eax
je 0x3c01
incl %r14d
shrl $0x8, %ebx
pushq $0x7
jmp 0x3c57
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3bbb
cmpl $0xff, %esi
je 0x3c2b
cmpl $0xf, %esi
jne 0x3bf5
pushq $0xf
jmp 0x3c57
pushq $0x3
jmp 0x3c57
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3c2f
cmpl $0xff, %esi
je 0x3c4f
cmpl $0xf, %esi
jne 0x3bf5
leal -0x1(%r14), %edi
movl %ebx, %edx
shll $0x8, %edx
pushq $0xe
popq %rsi
callq 0x3610
testl %eax, %eax
je 0x3c01
addl $0x3, %r14d
shrl $0x18, %ebx
jmp 0x3c2b
leaq 0x4340(%rip), %rdi      # 0x7f3c
callq 0x4b7e
xorl %eax, %eax
jmp 0x3c67
addl $-0x2, %r14d
shll $0x10, %ebx
pushq $0xc
jmp 0x3c57
leal -0x3(%r14), %edi
movl %ebx, %edx
shll $0x18, %edx
pushq $0x8
popq %rsi
callq 0x3610
testl %eax, %eax
je 0x3c01
incl %r14d
shrl $0x8, %ebx
pushq $0x1
jmp 0x3c57
decl %r14d
shll $0x8, %ebx
pushq $0x6
jmp 0x3c57
addl $-0x2, %r14d
shll $0x10, %ebx
pushq $0x4
jmp 0x3c57
addl $-0x3, %r14d
shll $0x18, %ebx
pushq $0x8
jmp 0x3c57
decl %r14d
shll $0x8, %ebx
pushq $0x2
popq %rsi
movl %r14d, %edi
movl %ebx, %edx
callq 0x3610
testl %eax, %eax
setne %al
movzbl %al, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq