// File: /workspace/github_c_makefile_star2_O3/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 0x414b(%rip), %rcx      # 0x7da8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0xe, %esi
jg 0x3d86
incl %esi
cmpl $0x5, %esi
ja 0x3df2
leaq 0x417f(%rip), %rax      # 0x7e00
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xe, %esi
jg 0x3cf2
incl %esi
cmpl $0x5, %esi
ja 0x3df2
leaq 0x412f(%rip), %rax      # 0x7dd0
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xe, %esi
jg 0x3d37
incl %esi
cmpl $0x5, %esi
ja 0x3df2
leaq 0x40f3(%rip), %rax      # 0x7db8
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xe, %esi
jg 0x3db9
incl %esi
cmpl $0x5, %esi
ja 0x3df2
leaq 0x40ff(%rip), %rax      # 0x7de8
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3e00
cmpl $0xff, %esi
je 0x3e4c
cmpl $0xf, %esi
jne 0x3df2
leal -0x2(%r14), %edi
movl %ebx, %edx
shll $0x10, %edx
movl $0xc, %esi
callq 0x37b9
testl %eax, %eax
je 0x3e38
addl $0x2, %r14d
shrl $0x10, %ebx
jmp 0x3dac
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3e11
cmpl $0xff, %esi
je 0x3e5d
cmpl $0xf, %esi
jne 0x3df2
leal -0x3(%r14), %edi
movl %ebx, %edx
shll $0x18, %edx
movl $0x8, %esi
callq 0x37b9
testl %eax, %eax
je 0x3e38
incl %r14d
shrl $0x8, %ebx
movl %r14d, %edi
movl $0x7, %esi
jmp 0x3e7c
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3dac
cmpl $0xff, %esi
je 0x3e2e
cmpl $0xf, %esi
jne 0x3df2
movl %r14d, %edi
movl $0xf, %esi
jmp 0x3e7c
movl %r14d, %edi
movl $0x3, %esi
jmp 0x3e7c
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3e3c
cmpl $0xff, %esi
je 0x3e6e
cmpl $0xf, %esi
jne 0x3df2
leal -0x1(%r14), %edi
movl %ebx, %edx
shll $0x8, %edx
movl $0xe, %esi
callq 0x37b9
testl %eax, %eax
je 0x3e38
addl $0x3, %r14d
shrl $0x18, %ebx
jmp 0x3e2e
leaq 0x4143(%rip), %rdi      # 0x7f3c
callq 0x4d36
jmp 0x3e38
addl $-0x2, %r14d
shll $0x10, %ebx
movl %r14d, %edi
movl $0xc, %esi
jmp 0x3e7c
leal -0x3(%r14), %edi
movl %ebx, %edx
shll $0x18, %edx
movl $0x8, %esi
callq 0x37b9
testl %eax, %eax
je 0x3e38
incl %r14d
shrl $0x8, %ebx
movl %r14d, %edi
movl $0x1, %esi
jmp 0x3e7c
xorl %eax, %eax
jmp 0x3e88
decl %r14d
shll $0x8, %ebx
movl %r14d, %edi
movl $0x6, %esi
jmp 0x3e7c
addl $-0x2, %r14d
shll $0x10, %ebx
movl %r14d, %edi
movl $0x4, %esi
jmp 0x3e7c
addl $-0x3, %r14d
shll $0x18, %ebx
movl %r14d, %edi
movl $0x8, %esi
jmp 0x3e7c
decl %r14d
shll $0x8, %ebx
movl %r14d, %edi
movl $0x2, %esi
movl %ebx, %edx
callq 0x37b9
testl %eax, %eax
setne %al
movzbl %al, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq