// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/twiboot.c
// Function: twi_verify
static int twi_verify(struct multiboot *mboot, struct databuf *dbuf, int memtype)
{
    struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
    char *progress_msg = (memtype == MEMTYPE_FLASH) ? "verifing flash" : "verifing eeprom";

    uint16_t pos = 0;
    uint8_t comp[READ_BLOCK_SIZE];

    while (pos < dbuf->length)
    {
        mboot->progress_cb(progress_msg, pos, dbuf->length);

        int len = MIN(READ_BLOCK_SIZE, dbuf->length - pos);
        if (twi_read_memory(twi, comp, len, memtype, pos))
        {
            mboot->progress_cb(progress_msg, -1, -1);
            return -1;
        }

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

        pos += len;
    }

    dbuf->length = pos;

    mboot->progress_cb(progress_msg, pos, dbuf->length);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %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 0x1226(%rip), %rax      # 0xb572
leaq 0x1210(%rip), %rcx      # 0xb563
cmpl $0x1, %edx
cmoveq %rcx, %rax
movq %rax, -0x30(%rbp)
movw $0x0, -0x32(%rbp)
movzwl -0x32(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0xa494
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 -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movzwl -0x32(%rbp), %eax
subl %eax, %ecx
movl $0x80, %eax
cmpl %ecx, %eax
jae 0xa3b1
movl $0x80, %eax
movl %eax, -0xc8(%rbp)
jmp 0xa3c4
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movzwl -0x32(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0xc4(%rbp)
movq -0x28(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movl -0xc4(%rbp), %eax
movb %al, %cl
movl -0x1c(%rbp), %eax
movzbl %cl, %edx
movzbl %al, %ecx
movzwl -0x32(%rbp), %r8d
callq 0xaa50
cmpl $0x0, %eax
je 0xa41c
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 0xa4bf
leaq -0xc0(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0xc4(%rbp), %rdx
callq 0x2140
cmpl $0x0, %eax
je 0xa47f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, %esi
callq *%rax
movq 0x5b97(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzwl -0x32(%rbp), %edx
leaq 0x1c4d(%rip), %rsi      # 0xc0bc
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa4bf
movl -0xc4(%rbp), %ecx
movzwl -0x32(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x32(%rbp)
jmp 0xa364
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 $0xd0, %rsp
popq %rbp
retq
nopl (%rax,%rax)