// File: /workspace/github_c_makefile_star2_O2/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, %r12d
leaq 0xa(%rsp), %rsi
pushq $0x2
popq %rdx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x37f6
cmpl $0x1, %eax
jle 0x39d6
movl %ebp, 0xc(%rsp)
movb 0xb(%rsp), %r15b
movzbl %r14b, %ebp
xorl %r13d, %r13d
movb %r15b, 0x9(%rsp)
movzbl %r13b, %esi
testb %r15b, %r15b
je 0x39b8
movzbl %r15b, %edx
testq %rbx, %rbx
je 0x398f
cmpb %r14b, %r13b
jae 0x398f
movl %ebp, %eax
subl %esi, %eax
cmpl %edx, %eax
cmovgel %edx, %eax
addq %rbx, %rsi
movzbl %al, %edx
movl %r12d, %edi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x37f6
testl %eax, %eax
jle 0x3a25
addb %al, %r13b
jmp 0x39b3
cmpb $0x40, %r15b
pushq $0x40
popq %rax
cmovael %eax, %edx
movl %r12d, %edi
leaq 0x10(%rsp), %rsi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x37f6
testl %eax, %eax
jle 0x3a3a
subb %al, %r15b
jmp 0x394f
movzbl 0xa(%rsp), %edx
movl 0xc(%rsp), %eax
cmpb %al, %dl
jne 0x39eb
movl %esi, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x961b(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4e46(%rip), %rdi      # 0x882d
pushq $0x20
jmp 0x3a4d
movzbl %al, %ecx
leal -0x80(%rdx), %eax
movb 0x9(%rsp), %sil
xorb $0x1, %sil
orb %al, %sil
jne 0x3a58
testq %rbx, %rbx
leaq 0x10(%rsp), %rax
cmovneq %rbx, %rax
movzbl (%rax), %eax
movq 0x95e3(%rip), %rdx      # 0xcff8
movq (%rdx), %rdi
leaq 0x4e71(%rip), %rsi      # 0x8890
movl %ecx, %edx
movl %eax, %ecx
jmp 0x3a69
movq 0x95cc(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4e18(%rip), %rdi      # 0x884e
pushq $0x21
jmp 0x3a4d
movq 0x95b7(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4e25(%rip), %rdi      # 0x8870
pushq $0x1f
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x3a70
movq 0x9599(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x4e61(%rip), %rsi      # 0x88ca
xorl %eax, %eax
callq 0x2180
pushq $-0x1
popq %rsi
jmp 0x39c5