// File: /workspace/github_c_makefile_star2_O1/orempel[P]multiboot_tool/mpmboot.c
// Function: mpm_write
static int mpm_write(struct multiboot *mboot,
                     struct databuf *dbuf, int memtype)
{
    struct mpm_privdata *mpm = (struct mpm_privdata *)mboot->privdata;
    char *progress_msg = (memtype == MEMTYPE_FLASH) ? "writing flash" : "writing eeprom";

    uint16_t pos = 0;
    while (pos < dbuf->length)
    {
        mboot->progress_cb(progress_msg, pos, dbuf->length);

        uint16_t len = (memtype == MEMTYPE_FLASH) ? mpm->flashpage : WRITE_BLOCK_SIZE;

        len = MIN(len, dbuf->length - pos);
        if (mpm_write_memory(mpm, dbuf->data + pos, len, memtype, pos))
        {
            mboot->progress_cb(progress_msg, -1, -1);
            return -1;
        }

        pos += len;
    }

    mboot->progress_cb(progress_msg, pos, dbuf->length);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
movq 0x8(%rdi), %r13
movl %edx, 0x4(%rsp)
cmpl $0x1, %edx
leaq 0x1f3d(%rip), %rax      # 0x84d8
leaq 0x1f44(%rip), %rcx      # 0x84e6
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
movq %rsi, 0x8(%rsp)
movq %r13, 0x30(%rsp)
movzwl %ax, %ebx
movl 0x4(%r12), %edx
cmpl %ebx, %edx
jbe 0x673c
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movq 0x18(%rsp), %rax
callq *0x18(%rax)
movl $0x10, %eax
cmpl $0x1, 0x4(%rsp)
jne 0x65f5
movzbl 0x14(%r13), %eax
movl 0x4(%r12), %r15d
subl %ebx, %r15d
cmpl %r15d, %eax
cmovbl %eax, %r15d
movl %r15d, %r14d
cmpb $0x1, 0x4(%rsp)
jne 0x661f
movzbl 0x14(%r13), %r14d
leal -0x1(%r14), %eax
testl %ebx, %eax
jne 0x671b
leal 0x5(%r14), %r12d
movq %r12, %rdi
callq 0x21c0
testq %rax, %rax
je 0x66de
movq %rax, %rbp
movq 0x28(%rsp), %rax
leaq (%rax,%rbx), %rsi
movl 0x4(%rsp), %r13d
movb %r13b, (%rbp)
movb %bh, 0x1(%rbp)
movb %bl, 0x2(%rbp)
movb $0x0, 0x3(%rbp)
movb %r14b, 0x4(%rbp)
movq %rbp, %rbx
addq $0x5, %rbx
movl %r15d, %r14d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x21a0
cmpb $0x1, %r13b
movq 0x30(%rsp), %r13
jne 0x668f
addq %r14, %rbx
movzbl 0x14(%r13), %edx
subq %r14, %rdx
movq %rbx, %rdi
movl $0xff, %esi
callq 0x2120
movzwl %r12w, %ecx
movq %r13, %rdi
movl $0x12, %esi
movq %rbp, %rdx
callq 0x6882
testl %eax, %eax
js 0x66de
movq %rbp, %rdi
callq 0x2030
movb $0x0, 0x3(%rsp)
movq %r13, %rdi
movl $0x12, %esi
leaq 0x3(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x69a0
testl %eax, %eax
setns %al
cmpb $0x0, 0x3(%rsp)
sete %bpl
andb %al, %bpl
jmp 0x66e0
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
testb %bpl, %bpl
je 0x66f4
movq 0x20(%rsp), %rax
addl %r15d, %eax
jmp 0x6710
movq 0x10(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rax
callq *0x18(%rax)
movq 0x20(%rsp), %rax
testb %bpl, %bpl
jne 0x65c0
jmp 0x674f
movq 0x68d6(%rip), %rax      # 0xcff8
movq (%rax), %rdi
xorl %ebp, %ebp
leaq 0x2c31(%rip), %rsi      # 0x935f
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2170
jmp 0x66e5
movq 0x10(%rsp), %rdi
movl %ebx, %esi
movq 0x18(%rsp), %rax
callq *0x18(%rax)
xorl %eax, %eax
jmp 0x6754
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq