// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/butterfly_prog.c
// Function: butterfly_read
static int butterfly_read(struct multiboot * p_mboot,
                          struct databuf * p_dbuf,
                          int memtype)
{
    bfly_privdata_t * p_priv = (bfly_privdata_t *)p_mboot->privdata;
    char * p_progress_msg = (memtype == 'F') ? "reading flash" : "reading eeprom";

    uint16_t pos = 0;
    uint16_t size = (memtype == 'F') ? p_priv->flashsize :
                                       p_priv->eepromsize;

    if (butterfly_set_address(p_priv, pos) < 0)
    {
        fprintf(stderr, "failed to set address\n");
        return -1;
    }

    while (pos < size)
    {
        p_mboot->progress_cb(p_progress_msg, pos, size);

        uint16_t len = MIN(p_priv->buffersize, size - pos);
        if (butterfly_read_data(p_priv, p_dbuf->data + pos, len, memtype))
        {
            p_mboot->progress_cb(p_progress_msg, -1, -1);
            return -1;
        }

        pos += len;
    }

    p_dbuf->length = pos;

    p_mboot->progress_cb(p_progress_msg, pos, size);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %edx
leaq 0x8ce2(%rip), %rax      # 0xb53b
leaq 0x8ccd(%rip), %rcx      # 0xb52d
cmpl $0x46, %edx
cmoveq %rcx, %rax
movq %rax, -0x30(%rbp)
movw $0x0, -0x32(%rbp)
cmpl $0x46, -0x1c(%rbp)
jne 0x2884
movq -0x28(%rbp), %rax
movzwl 0x4e(%rax), %eax
movl %eax, -0x3c(%rbp)
jmp 0x288f
movq -0x28(%rbp), %rax
movzwl 0x50(%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movw %ax, -0x34(%rbp)
movq -0x28(%rbp), %rdi
movzwl -0x32(%rbp), %esi
callq 0x34b0
cmpl $0x0, %eax
jge 0x28cc
movq 0xd749(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8c91(%rip), %rsi      # 0xb54a
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29b0
jmp 0x28ce
movzwl -0x32(%rbp), %eax
movzwl -0x34(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2988
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdi
movzwl -0x32(%rbp), %esi
movzwl -0x34(%rbp), %edx
callq *%rax
movq -0x28(%rbp), %rax
movzwl 0x4c(%rax), %eax
movzwl -0x34(%rbp), %ecx
movzwl -0x32(%rbp), %edx
subl %edx, %ecx
cmpl %ecx, %eax
jge 0x2917
movq -0x28(%rbp), %rax
movzwl 0x4c(%rax), %eax
movl %eax, -0x40(%rbp)
jmp 0x2924
movzwl -0x34(%rbp), %eax
movzwl -0x32(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movw %ax, -0x36(%rbp)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rsi
movw -0x36(%rbp), %cx
movl -0x1c(%rbp), %eax
movzwl %cx, %edx
movzbl %al, %ecx
callq 0x3560
cmpl $0x0, %eax
je 0x2975
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, %esi
callq *%rax
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29b0
movzwl -0x36(%rbp), %ecx
movzwl -0x32(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x32(%rbp)
jmp 0x28ce
movzwl -0x32(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdi
movzwl -0x32(%rbp), %esi
movzwl -0x34(%rbp), %edx
callq *%rax
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)