// File: /workspace/github_c_makefile_star2_O1/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 0x4130(%rip), %rcx      # 0x7da8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0xe, %esi
jg 0x3da1
incl %esi
cmpl $0x5, %esi
ja 0x3e0d
leaq 0x4164(%rip), %rax      # 0x7e00
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xe, %esi
jg 0x3d0d
incl %esi
cmpl $0x5, %esi
ja 0x3e0d
leaq 0x4114(%rip), %rax      # 0x7dd0
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xe, %esi
jg 0x3d52
incl %esi
cmpl $0x5, %esi
ja 0x3e0d
leaq 0x40d8(%rip), %rax      # 0x7db8
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xe, %esi
jg 0x3dd4
incl %esi
cmpl $0x5, %esi
ja 0x3e0d
leaq 0x40e4(%rip), %rax      # 0x7de8
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3e1b
cmpl $0xff, %esi
je 0x3e67
cmpl $0xf, %esi
jne 0x3e0d
leal -0x2(%r14), %edi
movl %ebx, %edx
shll $0x10, %edx
movl $0xc, %esi
callq 0x37c1
testl %eax, %eax
je 0x3e53
addl $0x2, %r14d
shrl $0x10, %ebx
jmp 0x3dc7
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3e2c
cmpl $0xff, %esi
je 0x3e78
cmpl $0xf, %esi
jne 0x3e0d
leal -0x3(%r14), %edi
movl %ebx, %edx
shll $0x18, %edx
movl $0x8, %esi
callq 0x37c1
testl %eax, %eax
je 0x3e53
incl %r14d
shrl $0x8, %ebx
movl %r14d, %edi
movl $0x7, %esi
jmp 0x3e97
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3dc7
cmpl $0xff, %esi
je 0x3e49
cmpl $0xf, %esi
jne 0x3e0d
movl %r14d, %edi
movl $0xf, %esi
jmp 0x3e97
movl %r14d, %edi
movl $0x3, %esi
jmp 0x3e97
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x3e57
cmpl $0xff, %esi
je 0x3e89
cmpl $0xf, %esi
jne 0x3e0d
leal -0x1(%r14), %edi
movl %ebx, %edx
shll $0x8, %edx
movl $0xe, %esi
callq 0x37c1
testl %eax, %eax
je 0x3e53
addl $0x3, %r14d
shrl $0x18, %ebx
jmp 0x3e49
leaq 0x4128(%rip), %rdi      # 0x7f3c
callq 0x4d86
jmp 0x3e53
addl $-0x2, %r14d
shll $0x10, %ebx
movl %r14d, %edi
movl $0xc, %esi
jmp 0x3e97
leal -0x3(%r14), %edi
movl %ebx, %edx
shll $0x18, %edx
movl $0x8, %esi
callq 0x37c1
testl %eax, %eax
je 0x3e53
incl %r14d
shrl $0x8, %ebx
movl %r14d, %edi
movl $0x1, %esi
jmp 0x3e97
xorl %eax, %eax
jmp 0x3ea3
decl %r14d
shll $0x8, %ebx
movl %r14d, %edi
movl $0x6, %esi
jmp 0x3e97
addl $-0x2, %r14d
shll $0x10, %ebx
movl %r14d, %edi
movl $0x4, %esi
jmp 0x3e97
addl $-0x3, %r14d
shll $0x18, %ebx
movl %r14d, %edi
movl $0x8, %esi
jmp 0x3e97
decl %r14d
shll $0x8, %ebx
movl %r14d, %edi
movl $0x2, %esi
movl %ebx, %edx
callq 0x37c1
testl %eax, %eax
setne %al
movzbl %al, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq