// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq %rsp, %rdi
movq $0x0, (%rdi)
leaq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x2260
cmpq $-0x1, %rax
je 0x4f17
cmpq $0xb, %rax
jle 0x4f36
movq (%rsp), %r14
cmpb $0x3a, (%r14)
jne 0x4f43
movb $0x1, %cl
xorl %eax, %eax
xorl %edx, %edx
xorl %r15d, %r15d
movb 0x1(%r14,%rdx), %dl
leal -0x30(%rdx), %esi
cmpb $0xa, %sil
jb 0x4d07
movl %edx, %esi
andb $-0x21, %sil
addb $-0x41, %sil
addb $-0x37, %dl
cmpb $0x6, %sil
movzbl %dl, %esi
cmovael %eax, %esi
shlb $0x4, %r15b
orb %sil, %r15b
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0x4ce2
movb %r15b, (%rbx)
testb %r15b, %r15b
je 0x4d3b
movzbl %r15b, %edi
callq 0x21d0
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x4f5d
movb $0x1, %dl
xorl %ecx, %ecx
xorl %edi, %edi
xorl %eax, %eax
movb 0x3(%r14,%rdi), %sil
leal -0x30(%rsi), %edi
cmpb $0xa, %dil
jb 0x4d6a
movl %esi, %edi
andb $-0x21, %dil
addb $-0x41, %dil
addb $-0x37, %sil
cmpb $0x6, %dil
movzbl %sil, %edi
cmovael %ecx, %edi
shlb $0x4, %al
orb %dil, %al
movl $0x1, %edi
xorl %esi, %esi
testb $0x1, %dl
movl $0x0, %edx
jne 0x4d43
movb $0x1, %dil
xorl %r8d, %r8d
xorl %ecx, %ecx
movb 0x5(%r14,%r8), %dl
leal -0x30(%rdx), %r8d
cmpb $0xa, %r8b
jb 0x4db2
movl %edx, %r8d
andb $-0x21, %r8b
addb $-0x41, %r8b
addb $-0x37, %dl
cmpb $0x6, %r8b
movzbl %dl, %r8d
cmovael %esi, %r8d
shlb $0x4, %cl
orb %r8b, %cl
movl $0x1, %r8d
xorl %edx, %edx
testb $0x1, %dil
movl $0x0, %edi
jne 0x4d89
movzbl %al, %esi
shll $0x8, %esi
movzbl %cl, %edi
orl %esi, %edi
movw %di, 0x2(%rbx)
movb $0x1, %dil
xorl %r8d, %r8d
xorl %esi, %esi
movb 0x7(%r14,%r8), %r8b
leal -0x30(%r8), %r9d
cmpb $0xa, %r9b
jb 0x4e0c
movl %r8d, %r9d
andb $-0x21, %r9b
addb $-0x41, %r9b
addb $-0x37, %r8b
cmpb $0x6, %r9b
movzbl %r8b, %r9d
cmovael %edx, %r9d
shlb $0x4, %sil
orb %r9b, %sil
movl $0x1, %r8d
testb $0x1, %dil
movl $0x0, %edi
jne 0x4de2
addb %r15b, %al
addb %cl, %al
addb %sil, %al
movb %sil, 0x4(%rbx)
movl $0x9, %ecx
testb %r15b, %r15b
je 0x4eb4
xorl %edx, %edx
xorl %esi, %esi
movq (%rsp), %rdi
addq %rcx, %rdi
movb $0x1, %r8b
xorl %r10d, %r10d
xorl %r9d, %r9d
movb (%rdi,%r10), %r10b
leal -0x30(%r10), %r11d
cmpb $0xa, %r11b
jb 0x4e77
movl %r10d, %r11d
andb $-0x21, %r11b
addb $-0x41, %r11b
addb $-0x37, %r10b
cmpb $0x6, %r11b
movzbl %r10b, %r11d
cmovael %edx, %r11d
shlb $0x4, %r9b
orb %r11b, %r9b
movl $0x1, %r10d
testb $0x1, %r8b
movl $0x0, %r8d
jne 0x4e4e
movq 0x8(%rbx), %rdi
movb %r9b, (%rdi,%rsi)
movq 0x8(%rbx), %rdi
addb (%rdi,%rsi), %al
addq $0x2, %rcx
incq %rsi
movzbl (%rbx), %edi
cmpq %rdi, %rsi
jb 0x4e3e
movq (%rsp), %r14
movl %ecx, %ecx
addq %r14, %rcx
movb $0x1, %sil
xorl %edx, %edx
xorl %r8d, %r8d
xorl %edi, %edi
movb (%rcx,%r8), %r8b
leal -0x30(%r8), %r9d
cmpb $0xa, %r9b
jb 0x4eea
movl %r8d, %r9d
andb $-0x21, %r9b
addb $-0x41, %r9b
addb $-0x37, %r8b
cmpb $0x6, %r9b
movzbl %r8b, %r9d
cmovael %edx, %r9d
shlb $0x4, %dil
orb %r9b, %dil
movl $0x1, %r8d
testb $0x1, %sil
movl $0x0, %esi
jne 0x4ec1
movb %dil, 0x10(%rbx)
addb %dil, %al
jne 0x4f4d
movq %r14, %rdi
callq 0x2030
xorl %ebx, %ebx
jmp 0x4f6a
movq %r14, %rdi
callq 0x2190
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x4f6a
leaq 0x3ab7(%rip), %rdi      # 0x89e6
callq 0x2240
jmp 0x4f6a
movq %rsp, %rsi
movq %rax, %rdi
callq 0x275b
jmp 0x4f65
movq %rsp, %rdi
callq 0x26d2
jmp 0x4f65
movzbl %al, %edi
movq %rsp, %rdx
movq %rbx, %rsi
callq 0x2716
jmp 0x4f65
movq %rsp, %rdi
callq 0x26fc
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %r15
retq