// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/filedata.c
// Function: hexfile_getrecord
static int hexfile_getrecord(FILE *stream, struct ihex_record *record)
{
    char *hexline = NULL;
    size_t size;

    ssize_t length = getline(&hexline, &size, stream);
    if (length == -1)
    {
        if (!feof(stream))
        {
            perror("hexfile_getrecord(): getline()");
        }

        return -1;
    }

    if (length < 12)
    {
        fprintf(stderr, "record too short (%ld)\n", length);
        free(hexline);
        return -1;
    }

    int pos = 0;
    if (hexline[pos] != ':')
    {
        fprintf(stderr, "invalid startcode\n");
        free(hexline);
        return -1;
    }

    pos++;

    uint8_t chksum = 0x00;

    record->byte_count = hex2byte(&hexline[pos]);
    chksum += record->byte_count;
    pos += 2;

    if (record->byte_count > 0)
    {
        record->data = malloc(record->byte_count);
        if (record->data == NULL)
        {
            perror("hexfile_getrecord(): malloc()");
            free(hexline);
            return -1;
        }
    }

    uint8_t hiaddr = hex2byte(&hexline[pos]);
    uint8_t loaddr = hex2byte(&hexline[pos +2]);
    record->address = (hiaddr << 8) + loaddr;
    chksum += hiaddr + loaddr;
    pos += 4;

    record->type = hex2byte(&hexline[pos]);
    chksum += record->type;
    pos += 2;

    int i;
    for (i = 0; i < record->byte_count; i++)
    {
        record->data[i] = hex2byte(&hexline[pos]);
        chksum += record->data[i];
        pos += 2;
    }

    record->chksum = hex2byte(&hexline[pos]);
    chksum += record->chksum;
    pos += 2;

    if (chksum != 0x00)
    {
        fprintf(stderr, "invalid checksum (0x%02X)\n", chksum);
        if (record->byte_count > 0)
        {
            free(record->data);
        }

        free(hexline);
        return -1;
    }

    free(hexline);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2230
movq %rax, -0x30(%rbp)
cmpq $-0x1, -0x30(%rbp)
jne 0x573a
movq -0x10(%rbp), %rdi
callq 0x2170
cmpl $0x0, %eax
jne 0x572e
leaq 0x6362(%rip), %rdi      # 0xba8b
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x59cf
cmpq $0xc, -0x30(%rbp)
jge 0x5772
movq 0xa8b0(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq -0x30(%rbp), %rdx
leaq 0x6354(%rip), %rsi      # 0xbaaa
movb $0x0, %al
callq 0x2160
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x59cf
movl $0x0, -0x34(%rbp)
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x3a, %eax
je 0x57b7
movq 0xa867(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6327(%rip), %rsi      # 0xbac2
movb $0x0, %al
callq 0x2160
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x59cf
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movb $0x0, -0x35(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
callq 0x59e0
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, (%rax)
movq -0x18(%rbp), %rax
movzbl (%rax), %ecx
movzbl -0x35(%rbp), %eax
addl %ecx, %eax
movb %al, -0x35(%rbp)
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jle 0x5848
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %edi
callq 0x21b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5846
leaq 0x62a9(%rip), %rdi      # 0xbad5
callq 0x2210
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x59cf
jmp 0x5848
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
callq 0x59e0
movb %al, -0x36(%rbp)
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %eax
addl $0x2, %eax
cltq
addq %rax, %rdi
callq 0x59e0
movb %al, -0x37(%rbp)
movzbl -0x36(%rbp), %eax
shll $0x8, %eax
movzbl -0x37(%rbp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x2(%rax)
movzbl -0x36(%rbp), %ecx
movzbl -0x37(%rbp), %eax
addl %eax, %ecx
movzbl -0x35(%rbp), %eax
addl %ecx, %eax
movb %al, -0x35(%rbp)
movl -0x34(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
callq 0x59e0
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %ecx
movzbl -0x35(%rbp), %eax
addl %ecx, %eax
movb %al, -0x35(%rbp)
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
jge 0x593c
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
callq 0x59e0
movb %al, %dl
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
movzbl -0x35(%rbp), %eax
addl %ecx, %eax
movb %al, -0x35(%rbp)
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x58e0
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
addq %rax, %rdi
callq 0x59e0
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x10(%rax)
movq -0x18(%rbp), %rax
movzbl 0x10(%rax), %ecx
movzbl -0x35(%rbp), %eax
addl %ecx, %eax
movb %al, -0x35(%rbp)
movl -0x34(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movzbl -0x35(%rbp), %eax
cmpl $0x0, %eax
je 0x59bf
movq 0xa679(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x35(%rbp), %edx
leaq 0x6166(%rip), %rsi      # 0xbaf3
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jle 0x59ad
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2030
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x59cf
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)