// File: /workspace/github_c_makefile_star2_O3/orempel[P]multiboot_tool/funkboot.c
// Function: funk_write
static int funk_write(struct multiboot *mboot,
                      struct databuf *dbuf,
                      int memtype)
{
    struct funk_privdata *funk = (struct funk_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) ? funk->flashpage : WRITE_BLOCK_SIZE;

        len = MIN(len, dbuf->length - pos);

        if (funk_write_memory(funk, 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
movl %edx, %r12d
movq %rdi, 0x10(%rsp)
cmpl $0x1, %edx
leaq 0x2d8b(%rip), %rax      # 0x84d8
leaq 0x2d92(%rip), %rcx      # 0x84e6
cmoveq %rax, %rcx
movq %rcx, 0x18(%rsp)
movl 0x4(%rsi), %eax
movl $0x0, %ebx
movl $0x0, %edx
testl %eax, %eax
je 0x58e0
movq %rsi, %r15
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %r13
leaq 0x8(%rsi), %rcx
movq %rcx, 0x28(%rsp)
xorl %ebx, %ebx
movzbl %r12b, %ecx
movl %ecx, 0x24(%rsp)
movl %eax, %edx
xorl %r14d, %r14d
movq %rsi, 0x30(%rsp)
movl %r12d, 0x20(%rsp)
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rax
callq *0x18(%rax)
cmpl $0x1, %r12d
jne 0x57da
movl %r14d, %ecx
movl 0x20(%r13), %r14d
movzwl %r14w, %eax
movl 0x4(%r15), %ebp
subl %ebx, %ebp
cmpl %ebp, %eax
cmovbl %eax, %ebp
movl %ecx, 0xc(%rsp)
movzwl %cx, %r15d
addq 0x28(%rsp), %r15
jmp 0x5822
movl 0x4(%r15), %ebp
subl %ebx, %ebp
cmpl $0x20, %ebp
movl $0x20, %eax
cmovael %eax, %ebp
movzwl %r14w, %r8d
movq 0x28(%rsp), %rax
leaq (%rax,%r8), %r15
cmpb $0x2, %r12b
jne 0x5819
movzwl %bp, %edx
movq %r13, %rdi
movq %r15, %rsi
movl $0x2, %ecx
callq 0x5ebc
movl %eax, %r15d
jmp 0x58c1
movl %r14d, 0xc(%rsp)
movl 0x20(%r13), %r14d
leal 0xffff(%r14), %eax
testl %ebx, %eax
jne 0x58f3
movslq %r14d, %rdi
callq 0x21d0
testq %rax, %rax
je 0x5912
movq %rax, %rbx
movzwl %bp, %r12d
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21b0
leaq (%rbx,%r12), %rdi
movl %r14d, %eax
subl %r12d, %eax
movslq %eax, %rdx
movl $0xff, %esi
callq 0x2120
xorl %r15d, %r15d
testl %r14d, %r14d
jle 0x58af
movzwl 0xc(%rsp), %r14d
xorl %r12d, %r12d
leaq (%rbx,%r12), %rsi
leal (%r14,%r12), %eax
movzwl %ax, %r8d
movq %r13, %rdi
movl $0x20, %edx
movl 0x24(%rsp), %ecx
callq 0x5ebc
testl %eax, %eax
js 0x58ac
addq $0x20, %r12
cmpl %r12d, 0x20(%r13)
jg 0x587f
jmp 0x58af
movl %eax, %r15d
movl 0xc(%rsp), %r14d
movq %rbx, %rdi
callq 0x2030
movl 0x20(%rsp), %r12d
testl %r15d, %r15d
jne 0x591e
addl %r14d, %ebp
movzwl %bp, %ebx
movq 0x30(%rsp), %r15
movl 0x4(%r15), %edx
movl %ebp, %r14d
cmpl %ebx, %edx
ja 0x57a0
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rax
callq *0x18(%rax)
xorl %ebx, %ebx
jmp 0x593a
movq 0x76fe(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x372e(%rip), %rsi      # 0x9032
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2180
jmp 0x591e
leaq 0x312c(%rip), %rdi      # 0x8a45
callq 0x2240
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rax
callq *0x18(%rax)
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq