// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/filedata.c
// Function: hexfile_write
static int hexfile_write(const char *filename, struct databuf *dbuf)
{
    FILE *stream = fopen(filename, "w");
    if (stream == NULL)
    {
        perror("hexfile_write(): fopen()");
        return -1;
    }

    uint32_t i;
    uint32_t addr_min = dbuf->length;
    uint32_t addr_max = 0;
    for (i = 0; i < dbuf->length; i++)
    {
        if (dbuf->data[i] == 0xFF)
        {
            continue;
        }

        if (addr_min > i)
        {
            addr_min = i;
        }

        if (addr_max < i)
        {
            addr_max = i;
        }
    }

    if (addr_min >= addr_max)
    {
        addr_min = 0;
        addr_max = dbuf->length;
    }

    addr_min = addr_min & ~0x0F;
    addr_max = (addr_max + 0x0F) & ~0x0F;

    struct ihex_record record;
    for (i = addr_min; i < addr_max; i += 0x10)
    {
        record.byte_count = 0x10;
        record.address = i;
        record.type = 0x00;
        record.data = &dbuf->data[i];

        hexfile_putrecord(stream, &record);
    }

    record.byte_count = 0x00;
    record.address = addr_min;
    record.type = 0x01;
    record.data = NULL;
    hexfile_putrecord(stream, &record);

    fclose(stream);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x6048(%rip), %rsi      # 0xb5e3
callq 0x21f0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x55c3
leaq 0x65a6(%rip), %rdi      # 0xbb58
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x56ce
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x5625
movq -0x18(%rbp), %rax
movl -0x24(%rbp), %ecx
movzbl 0x8(%rax,%rcx), %eax
cmpl $0xff, %eax
jne 0x55fc
jmp 0x561a
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jbe 0x560a
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x5618
movl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x561a
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x55db
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jb 0x563e
movl $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
andl $-0x10, %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
addl $0xf, %eax
andl $-0x10, %eax
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x569a
movb $0x10, -0x48(%rbp)
movl -0x24(%rbp), %eax
movw %ax, -0x46(%rbp)
movb $0x0, -0x44(%rbp)
movq -0x18(%rbp), %rax
addq $0x8, %rax
movl -0x24(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x5ab0
movl -0x24(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x24(%rbp)
jmp 0x5659
movb $0x0, -0x48(%rbp)
movl -0x28(%rbp), %eax
movw %ax, -0x46(%rbp)
movb $0x1, -0x44(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x5ab0
movq -0x20(%rbp), %rdi
callq 0x2080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)