// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %edx, %r15d
movq %rdi, 0x18(%rsp)
cmpl $0x1, %edx
leaq 0x1f6f(%rip), %rax      # 0x84d8
leaq 0x1f76(%rip), %rcx      # 0x84e6
cmoveq %rax, %rcx
movq %rcx, 0x20(%rsp)
movl 0x4(%rsi), %eax
movl $0x0, %ebp
movl $0x0, %edx
testl %eax, %eax
je 0x6710
movq %rsi, %r12
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rsp)
leaq 0x8(%rsi), %rcx
movq %rcx, 0x30(%rsp)
xorl %ebp, %ebp
movl %eax, %edx
xorl %ebx, %ebx
movq %rsi, 0x40(%rsp)
movl %r15d, 0x2c(%rsp)
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq 0x18(%rsp), %rax
callq *0x18(%rax)
movl %ebx, 0x14(%rsp)
movzwl %bx, %ecx
cmpl $0x1, %r15d
jne 0x65f3
movq 0x8(%rsp), %rax
movzbl 0x14(%rax), %r14d
movl 0x4(%r12), %ebx
subl %ebp, %ebx
cmpl %r14d, %ebx
cmovael %r14d, %ebx
addq 0x30(%rsp), %rcx
jmp 0x661a
movl 0x4(%r12), %ebx
subl %ebp, %ebx
cmpl $0x10, %ebx
movl $0x10, %eax
cmovael %eax, %ebx
addq 0x30(%rsp), %rcx
cmpb $0x1, %r15b
jne 0x6633
movq 0x8(%rsp), %rax
movzbl 0x14(%rax), %r14d
leal 0xffff(%r14), %eax
movb $0x1, %r13b
testl %ebp, %eax
jne 0x6723
movq %rcx, 0x38(%rsp)
jmp 0x663e
movq %rcx, 0x38(%rsp)
xorl %r13d, %r13d
movl %ebx, %r14d
leal 0x5(%r14), %r12d
movq %r12, %rdi
callq 0x21d0
testq %rax, %rax
je 0x6740
movq %rax, %rbp
movb %r15b, (%rax)
movl 0x14(%rsp), %eax
movb %ah, 0x1(%rbp)
movb %al, 0x2(%rbp)
movb $0x0, 0x3(%rbp)
movb %r14b, 0x4(%rbp)
movq %rbp, %r14
addq $0x5, %r14
movzwl %bx, %r15d
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
callq 0x21b0
testb %r13b, %r13b
je 0x66a7
addq %r15, %r14
movq 0x8(%rsp), %rax
movzbl 0x14(%rax), %edx
subq %r15, %rdx
movq %r14, %rdi
movl $0xff, %esi
callq 0x2120
movzwl %r12w, %ecx
movq 0x8(%rsp), %rdi
movl $0x12, %esi
movq %rbp, %rdx
callq 0x6893
testl %eax, %eax
movl 0x2c(%rsp), %r15d
js 0x6740
movq %rbp, %rdi
callq 0x2030
movb $0x0, 0x7(%rsp)
movq 0x8(%rsp), %rdi
movl $0x12, %esi
leaq 0x7(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x69b1
testl %eax, %eax
js 0x6740
cmpb $0x0, 0x7(%rsp)
jne 0x6740
addl 0x14(%rsp), %ebx
movzwl %bx, %ebp
movq 0x40(%rsp), %r12
movl 0x4(%r12), %edx
cmpl %ebp, %edx
ja 0x65b8
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq 0x18(%rsp), %rax
callq *0x18(%rax)
xorl %ebx, %ebx
jmp 0x675c
movq 0x68ce(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2c63(%rip), %rsi      # 0x9397
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2180
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rax
callq *0x18(%rax)
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq