// File: /workspace/github_c_makefile_star2_O1/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 0x6(%rsp), %rsi
movl $0x2, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x443a
cmpl $0x1, %eax
jle 0x46d1
movb 0x7(%rsp), %r12b
testb %r12b, %r12b
je 0x468f
movl %ebp, 0x8(%rsp)
movzbl %r14b, %eax
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
movb %r12b, 0x5(%rsp)
movzbl %r12b, %eax
testq %rbx, %rbx
je 0x4648
cmpb %r14b, %bpl
jae 0x4648
movzbl %bpl, %esi
movl 0xc(%rsp), %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 0x443a
movl %eax, %r13d
testl %eax, %eax
jle 0x4679
addb %r13b, %bpl
testl %r13d, %r13d
jg 0x4672
jmp 0x46f7
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 0x443a
movl %eax, %r13d
testl %eax, %eax
jle 0x46f2
subb %r13b, %r12b
jne 0x4604
jmp 0x4680
callq 0x26e3
jmp 0x463e
movzbl %bpl, %eax
movl 0x8(%rsp), %ebp
movb 0x5(%rsp), %r12b
jmp 0x4691
xorl %eax, %eax
movzbl 0x6(%rsp), %edx
cmpb %bpl, %dl
je 0x46fc
movzbl %bpl, %ecx
leal -0x80(%rdx), %eax
xorb $0x1, %r12b
orb %al, %r12b
jne 0x46d8
testq %rbx, %rbx
leaq 0x10(%rsp), %rax
cmovneq %rbx, %rax
movzbl (%rax), %eax
movq 0x8937(%rip), %rdx      # 0xcff8
movq (%rdx), %rdi
leaq 0x41c5(%rip), %rsi      # 0x8890
movl %ecx, %edx
movl %eax, %ecx
jmp 0x46e9
callq 0x26ff
jmp 0x46f7
movq 0x8919(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x41e1(%rip), %rsi      # 0x88ca
xorl %eax, %eax
callq 0x2170
jmp 0x46f7
callq 0x26c7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq