// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movb %cl, %al
movb %sil, %cl
movl %edi, -0x8(%rbp)
movb %cl, -0x9(%rbp)
movq %rdx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movl -0x8(%rbp), %edi
leaq -0x1b(%rbp), %rsi
movl $0x2, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x4710
movl %eax, -0x64(%rbp)
cmpl $0x2, -0x64(%rbp)
jge 0x4970
movq 0xb6a5(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6f85(%rip), %rsi      # 0xb8e2
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4b34
movb $0x0, -0x65(%rbp)
movb -0x1a(%rbp), %al
movb %al, -0x66(%rbp)
movzbl -0x66(%rbp), %eax
cmpl $0x0, %eax
jle 0x4aa2
cmpq $0x0, -0x18(%rbp)
je 0x4a27
movzbl -0x65(%rbp), %eax
movzbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4a27
movzbl -0x66(%rbp), %eax
movzbl -0x19(%rbp), %ecx
movzbl -0x65(%rbp), %edx
subl %edx, %ecx
cmpl %ecx, %eax
jge 0x49bd
movzbl -0x66(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x49ca
movzbl -0x19(%rbp), %eax
movzbl -0x65(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movb %al, -0x67(%rbp)
movl -0x8(%rbp), %edi
movq -0x18(%rbp), %rsi
movzbl -0x65(%rbp), %eax
addq %rax, %rsi
movzbl -0x67(%rbp), %edx
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x4710
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jg 0x4a19
movq 0xb5fc(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6efd(%rip), %rsi      # 0xb903
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4b34
movl -0x64(%rbp), %ecx
movzbl -0x65(%rbp), %eax
addl %ecx, %eax
movb %al, -0x65(%rbp)
jmp 0x4a91
movzbl -0x66(%rbp), %eax
cmpq $0x40, %rax
jae 0x4a3b
movzbl -0x66(%rbp), %eax
movq %rax, -0x78(%rbp)
jmp 0x4a46
movl $0x40, %eax
movq %rax, -0x78(%rbp)
jmp 0x4a46
movq -0x78(%rbp), %rax
movb %al, -0x68(%rbp)
movl -0x8(%rbp), %edi
movzbl -0x68(%rbp), %edx
leaq -0x60(%rbp), %rsi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x4710
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jg 0x4a8f
movq 0xb586(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x6ea9(%rip), %rsi      # 0xb925
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4b34
jmp 0x4a91
movl -0x64(%rbp), %ecx
movzbl -0x66(%rbp), %eax
subl %ecx, %eax
movb %al, -0x66(%rbp)
jmp 0x497a
movzbl -0x1b(%rbp), %eax
movzbl -0x9(%rbp), %ecx
cmpl %ecx, %eax
je 0x4b2d
movzbl -0x1b(%rbp), %eax
cmpl $0x80, %eax
jne 0x4b04
movzbl -0x1a(%rbp), %eax
cmpl $0x1, %eax
jne 0x4b04
cmpq $0x0, -0x18(%rbp)
je 0x4ad5
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x7c(%rbp)
jmp 0x4adc
movzbl -0x60(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movb %al, -0x69(%rbp)
movq 0xb50f(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x9(%rbp), %edx
movzbl -0x69(%rbp), %ecx
leaq 0x6e4a(%rip), %rsi      # 0xb945
movb $0x0, %al
callq 0x2160
jmp 0x4b24
movq 0xb4ed(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x1b(%rbp), %edx
movzbl -0x9(%rbp), %ecx
leaq 0x6e62(%rip), %rsi      # 0xb97f
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4b34
movzbl -0x65(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq