// File: /workspace/github_c_makefile_star2_O1/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 %rsi, %r14
movq %rdi, %r13
movq 0x8(%rdi), %r15
cmpl $0x1, %edx
leaq 0x2d4a(%rip), %rax      # 0x84d8
leaq 0x2d51(%rip), %rcx      # 0x84e6
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, 0x28(%rsp)
movzbl %r12b, %eax
movl %eax, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
movl %edx, 0x4(%rsp)
movq %rsi, 0x30(%rsp)
movzwl 0x8(%rsp), %ebx
movl 0x4(%r14), %edx
cmpl %ebx, %edx
jbe 0x592d
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq *0x18(%r13)
movl $0x20, %eax
cmpl $0x1, %r12d
jne 0x57f1
movzwl 0x20(%r15), %eax
movl 0x4(%r14), %ebp
subl %ebx, %ebp
cmpl %ebp, %eax
cmovbl %eax, %ebp
movq 0x28(%rsp), %rax
leaq (%rax,%rbx), %rsi
cmpb $0x2, %r12b
jne 0x5825
movzwl %bp, %edx
movq %r15, %rdi
movl 0xc(%rsp), %ecx
movl %ebx, %r8d
callq 0x5ee8
movl %eax, %r14d
jmp 0x58c6
movslq 0x20(%r15), %r14
leal -0x1(%r14), %eax
testl %ebx, %eax
jne 0x58f4
movq %rsi, 0x20(%rsp)
movq %r14, %rdi
callq 0x21c0
testq %rax, %rax
je 0x5919
movq %rax, %r12
movl %ebp, %r13d
movq %rax, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x21a0
addq %r12, %r13
movl %r14d, %eax
subl %ebp, %eax
movslq %eax, %rdx
movq %r13, %rdi
movl $0xff, %esi
callq 0x2120
testl %r14d, %r14d
jle 0x58b1
xorl %r13d, %r13d
leaq (%r12,%r13), %rsi
leal (%rbx,%r13), %eax
movzwl %ax, %r8d
movq %r15, %rdi
movl $0x20, %edx
movl 0xc(%rsp), %ecx
callq 0x5ee8
movl %eax, %r14d
testl %eax, %eax
js 0x58b4
addq $0x20, %r13
cmpl %r13d, 0x20(%r15)
jg 0x5881
jmp 0x58b4
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x2030
movq 0x18(%rsp), %r13
movl 0x4(%rsp), %r12d
testl %r14d, %r14d
je 0x58e0
movq 0x10(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq *0x18(%r13)
jmp 0x58e4
addl %ebp, 0x8(%rsp)
testl %r14d, %r14d
movq 0x30(%rsp), %r14
je 0x57c5
jmp 0x593c
movq 0x76fd(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x371b(%rip), %rsi      # 0x9020
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2170
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x58c6
leaq 0x3113(%rip), %rdi      # 0x8a33
callq 0x2230
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x58c1
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq *0x18(%r13)
xorl %eax, %eax
jmp 0x5941
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq