// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/butterfly_prog.c
// Function: butterfly_verify
static int butterfly_verify(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') ? "verifing flash" : "verifing eeprom";

    uint16_t pos = 0;
    uint8_t comp[256];

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

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

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

        if (memcmp(comp, p_dbuf->data + pos, len) != 0x00)
        {
            p_mboot->progress_cb(p_progress_msg, -1, -1);
            fprintf(stderr, "verify failed at pos 0x%04x!!\n", pos);
            return -1;
        }

        pos += len;
    }

    p_dbuf->length = pos;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
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 0x8b86(%rip), %rax      # 0xb572
leaq 0x8b70(%rip), %rcx      # 0xb563
cmpl $0x46, %edx
cmoveq %rcx, %rax
movq %rax, -0x30(%rbp)
movw $0x0, -0x32(%rbp)
movq -0x28(%rbp), %rdi
movzwl -0x32(%rbp), %esi
callq 0x34b0
cmpl $0x0, %eax
jge 0x2a3a
movq 0xd5db(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x8b23(%rip), %rsi      # 0xb54a
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2b9b
jmp 0x2a3c
movzwl -0x32(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2b70
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdi
movzwl -0x32(%rbp), %esi
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %edx
callq *%rax
movq -0x28(%rbp), %rax
movzwl 0x4c(%rax), %eax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
movzwl -0x32(%rbp), %edx
subl %edx, %ecx
cmpl %ecx, %eax
jae 0x2a8f
movq -0x28(%rbp), %rax
movzwl 0x4c(%rax), %eax
movl %eax, -0x148(%rbp)
jmp 0x2aa2
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movzwl -0x32(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x148(%rbp)
movl -0x148(%rbp), %eax
movw %ax, -0x142(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movw -0x142(%rbp), %cx
movl -0x1c(%rbp), %eax
movzwl %cx, %edx
movzbl %al, %ecx
callq 0x3560
cmpl $0x0, %eax
je 0x2af5
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 0x2b9b
leaq -0x140(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rsi
movzwl -0x142(%rbp), %eax
movl %eax, %edx
callq 0x2140
cmpl $0x0, %eax
je 0x2b5a
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, %esi
callq *%rax
movq 0xd4bc(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzwl -0x32(%rbp), %edx
leaq 0x8a38(%rip), %rsi      # 0xb582
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2b9b
movzwl -0x142(%rbp), %ecx
movzwl -0x32(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x32(%rbp)
jmp 0x2a3c
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
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %edx
callq *%rax
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)