// File: /workspace/github_c_makefile_star2_O3/orempel[P]multiboot_tool/twiboot.c
// Function: twi_write
static int twi_write(struct multiboot *mboot,
                     struct databuf *dbuf,
                     int memtype)
{
    struct twi_privdata *twi = (struct twi_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);

        uint8_t len = (memtype == MEMTYPE_FLASH) ? twi->pagesize : WRITE_BLOCK_SIZE;

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

        if (twi_write_memory(twi, 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, %r14d
movq %rdi, 0x10(%rsp)
cmpl $0x1, %edx
leaq 0x942(%rip), %rax       # 0x84d8
leaq 0x949(%rip), %rcx       # 0x84e6
cmoveq %rax, %rcx
movq %rcx, 0x20(%rsp)
movl 0x4(%rsi), %eax
movl $0x0, %ebx
movl $0x0, %edx
testl %eax, %eax
je 0x7d18
movq %rsi, %r12
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x8(%rsi), %rcx
movq %rcx, 0x30(%rsp)
xorl %ebx, %ebx
movl %eax, %edx
xorl %eax, %eax
movl %r14d, 0x2c(%rsp)
movq %rsi, 0x40(%rsp)
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rax
callq *0x18(%rax)
movzwl 0xc(%rsp), %ecx
cmpl $0x1, %r14d
jne 0x7c23
movq 0x18(%rsp), %rax
movzbl 0x18(%rax), %r13d
movl 0x4(%r12), %r15d
subl %ebx, %r15d
cmpl %r13d, %r15d
cmovael %r13d, %r15d
addq 0x30(%rsp), %rcx
jmp 0x7c4d
movl 0x4(%r12), %r15d
subl %ebx, %r15d
cmpl $0x10, %r15d
movl $0x10, %eax
cmovael %eax, %r15d
addq 0x30(%rsp), %rcx
cmpb $0x1, %r14b
jne 0x7c66
movq 0x18(%rsp), %rax
movzbl 0x18(%rax), %r13d
leal 0xffff(%r13), %eax
movb $0x1, %bpl
testl %ebx, %eax
jne 0x7d2b
movq %rcx, 0x38(%rsp)
jmp 0x7c70
movq %rcx, 0x38(%rsp)
xorl %ebp, %ebp
movl %r15d, %r13d
addl $0x4, %r13d
movq %r13, %rdi
callq 0x21d0
testq %rax, %rax
je 0x7d48
movq %rax, %rbx
movb $0x2, (%rax)
movb %r14b, 0x1(%rax)
movl 0xc(%rsp), %eax
movb %ah, 0x2(%rbx)
movb %al, 0x3(%rbx)
movq %rbx, %r14
addq $0x4, %r14
movzbl %r15b, %r12d
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x21b0
testb %bpl, %bpl
je 0x7cd5
addq %r12, %r14
movq 0x18(%rsp), %rax
movzbl 0x18(%rax), %edx
subq %r12, %rdx
movq %r14, %rdi
movl $0xff, %esi
callq 0x2120
movq 0x18(%rsp), %rax
movl 0xc(%rax), %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x7eb0
movl %eax, %ebp
movq %rbx, %rdi
callq 0x2030
cmpl %r13d, %ebp
movl 0x2c(%rsp), %r14d
movq 0x40(%rsp), %r12
jne 0x7d48
movl 0xc(%rsp), %eax
addl %r15d, %eax
movzwl %ax, %ebx
movl 0x4(%r12), %edx
cmpl %ebx, %edx
ja 0x7be5
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movq 0x10(%rsp), %rax
callq *0x18(%rax)
xorl %ebx, %ebx
jmp 0x7d64
movq 0x52c6(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x1bc5(%rip), %rsi      # 0x9901
movl %ebx, %edx
movl %r13d, %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 0x10(%rsp), %rax
callq *0x18(%rax)
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq