// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/mpmboot.c
// Function: mpm_write_memory
static int mpm_write_memory(struct mpm_privdata *mpm,
                            uint8_t *buffer, uint16_t size,
                            uint8_t memtype, uint16_t address)
{
    int bufsize;

    if (memtype == MEMTYPE_FLASH)
    {
        if ((address & (mpm->flashpage -1)) != 0x00)
        {
            fprintf(stderr, "mpm_write_memory(): address 0x%04x not aligned to pagesize 0x%02x\n", address, mpm->flashpage);
            return -1;
        }

        bufsize = 5 + mpm->flashpage;

    }
    else
    {
        bufsize = 5 + size;
    }

    uint8_t *cmd = malloc(bufsize);
    if (cmd == NULL)
    {
        return -1;
    }

    cmd[0] = memtype;
    cmd[1] = (address >> 8) & 0xFF;
    cmd[2] = (address & 0xFF);
    cmd[3] = ((bufsize -5) >> 8) & 0xFF;
    cmd[4] = ((bufsize -5) & 0xFF);
    memcpy(cmd +5, buffer, size);

    if (memtype == MEMTYPE_FLASH)
    {
        memset(cmd +5 +size, 0xFF, mpm->flashpage - size);
    }

    int ret = mpm_send(mpm, CMD_WRITE_MEMORY, cmd, bufsize);
    if (ret < 0)
    {
        return ret;
    }

    free(cmd);

    uint8_t cause = CAUSE_SUCCESS;
    ret = mpm_recv(mpm, CMD_WRITE_MEMORY, &cause, NULL, 0);
    if (ret < 0)
    {
        return ret;
    }

    return (cause != CAUSE_SUCCESS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %dx, -0x1a(%rbp)
movb %cl, -0x1b(%rbp)
movw %ax, -0x1e(%rbp)
movzbl -0x1b(%rbp), %eax
cmpl $0x1, %eax
jne 0x8a6e
movzwl -0x1e(%rbp), %eax
movq -0x10(%rbp), %rcx
movzbl 0x14(%rcx), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x8a5e
movq 0x75c3(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzwl -0x1e(%rbp), %edx
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %ecx
leaq 0x39d4(%rip), %rsi      # 0xc41f
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bb1
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
addl $0x5, %eax
movl %eax, -0x24(%rbp)
jmp 0x8a78
movzwl -0x1a(%rbp), %eax
addl $0x5, %eax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rdi
callq 0x21b0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x8a98
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bb1
movb -0x1b(%rbp), %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movzwl -0x1e(%rbp), %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x1(%rax)
movzwl -0x1e(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x24(%rbp), %eax
subl $0x5, %eax
sarl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x3(%rax)
movl -0x24(%rbp), %eax
subl $0x5, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x30(%rbp), %rdi
addq $0x5, %rdi
movq -0x18(%rbp), %rsi
movzwl -0x1a(%rbp), %eax
movl %eax, %edx
callq 0x2190
movzbl -0x1b(%rbp), %eax
cmpl $0x1, %eax
jne 0x8b3f
movq -0x30(%rbp), %rdi
addq $0x5, %rdi
movzwl -0x1a(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movzbl 0x14(%rax), %eax
movzwl -0x1a(%rbp), %ecx
subl %ecx, %eax
movslq %eax, %rdx
movl $0xff, %esi
callq 0x2100
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rdx
movl -0x24(%rbp), %eax
movl $0x12, %esi
movzwl %ax, %ecx
callq 0x8320
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x8b68
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8bb1
movq -0x30(%rbp), %rdi
callq 0x2030
movb $0x0, -0x35(%rbp)
movq -0x10(%rbp), %rdi
movl $0x12, %esi
leaq -0x35(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x84e0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x8b9f
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8bb1
movzbl -0x35(%rbp), %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)