// File: /workspace/github_c_makefile_star2_O3/orempel[P]multiboot_tool/eprom_prog.c
// Function: eprog_recv
static int eprog_recv(int fd, uint8_t msgtype, uint8_t * data, uint8_t length)
{
    uint8_t header[2];
    uint8_t dummy[64];

    int ret = eprog_serial_read(fd, header, sizeof(header), SERIAL_TIMEOUT);
    if (ret < (int)sizeof(header))
    {
        fprintf(stderr, "short read from device (header)\n");
        return -1;
    }

    uint8_t pos = 0;
    uint8_t msgsize = header[1];

    while (msgsize > 0)
    {
        if ((data != NULL) && (pos < length))
        {
            uint8_t readsize = MIN(msgsize, length - pos);

            ret = eprog_serial_read(fd, &data[pos], readsize, SERIAL_TIMEOUT);
            if (ret <= 0)
            {
                fprintf(stderr, "short read from device (payload)\n");
                return -1;
            }

            pos += ret;
        }
        else
        {
            uint8_t readsize = MIN(msgsize, sizeof(dummy));

            ret = eprog_serial_read(fd, &dummy, readsize, SERIAL_TIMEOUT);
            if (ret <= 0)
            {
                fprintf(stderr, "short read from device (dummy)\n");
                return -1;
            }
        }

        msgsize -= ret;
    }

    if (header[0] != msgtype)
    {
        if ((header[0] == MSGTYPE_ERROR_RSP) && (header[1] == 1))
        {
            uint8_t error_code = (data != NULL) ? data[0] : dummy[0];

            fprintf(stderr, "expected msgtype 0x%x received ERROR_RSP error_code 0x%x\n",
                    msgtype, error_code);
        }
        else
        {
            fprintf(stderr, "wrong msgtype received (0x%x != 0x%x)\n",
                    header[0], msgtype);
        }

        return -1;
    }

    return pos;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, %r14d
movq %rdx, %rbx
movl %esi, %ebp
movl %edi, %r15d
leaq 0xa(%rsp), %rsi
movl $0x2, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x43eb
cmpl $0x1, %eax
jle 0x4608
movb 0xb(%rsp), %r12b
testb %r12b, %r12b
movb %r12b, 0x9(%rsp)
je 0x45ed
movl %ebp, 0xc(%rsp)
movzbl %r14b, %r13d
xorl %ebp, %ebp
movzbl %r12b, %eax
testq %rbx, %rbx
je 0x45b7
cmpb %r14b, %bpl
jae 0x45b7
movzbl %bpl, %esi
movl %r13d, %ecx
subl %esi, %ecx
cmpl %eax, %ecx
cmovgel %eax, %ecx
addq %rbx, %rsi
movzbl %cl, %edx
movl %r15d, %edi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x43eb
testl %eax, %eax
jle 0x466b
addb %al, %bpl
jmp 0x45de
cmpb $0x40, %al
movl $0x40, %ecx
cmovael %ecx, %eax
movzbl %al, %edx
movl %r15d, %edi
leaq 0x10(%rsp), %rsi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x43eb
testl %eax, %eax
jle 0x4664
subb %al, %r12b
jne 0x457b
movzbl %bpl, %eax
movl 0xc(%rsp), %ebp
jmp 0x45ef
xorl %eax, %eax
movzbl 0xa(%rsp), %edx
cmpb %bpl, %dl
jne 0x460f
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x269a
jmp 0x4670
movzbl %bpl, %ecx
leal -0x80(%rdx), %eax
movb 0x9(%rsp), %sil
xorb $0x1, %sil
orb %al, %sil
jne 0x464a
testq %rbx, %rbx
leaq 0x10(%rsp), %rax
cmovneq %rbx, %rax
movzbl (%rax), %eax
movq 0x89be(%rip), %rdx      # 0xcff8
movq (%rdx), %rdi
leaq 0x424c(%rip), %rsi      # 0x8890
movl %ecx, %edx
movl %eax, %ecx
jmp 0x465b
movq 0x89a7(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x426f(%rip), %rsi      # 0x88ca
xorl %eax, %eax
callq 0x2180
jmp 0x4670
callq 0x2662
jmp 0x4670
callq 0x267e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x45f9