// File: /workspace/github_c_makefile_star2_O2/orempel[P]multiboot_tool/mpmboot.c
// Function: mpm_recv
static int mpm_recv(struct mpm_privdata *mpm,
                    uint8_t command,
                    uint8_t *cause,
                    uint8_t *buffer,
                    uint16_t buffersize)
{
    int     len;
    uint8_t header[4];

    len = mpm_serial_read(mpm->fd, header, sizeof(header));
    if (len != sizeof(header))
    {
        fprintf(stderr, "short read() from device (not addressed?)\n");
        return -1;
    }

    if (header[0] != command)
    {
        fprintf(stderr, "invalid command response (0x%02x != 0x%02x)\n", header[0], command);
        return -1;
    }

    *cause = header[1];

    uint16_t length = (header[2] << 8) | header[3];
//     printf("mpm_recv() cmd=0x%02x cause=0x%02x length=0x%04x\n", command, *cause, length);

    uint16_t bufferpos = 0;
    while (length > 0)
    {

        /* free space in output buffer? */
        if ((bufferpos < buffersize) && (buffer != NULL))
        {
            uint16_t size = MIN(buffersize - bufferpos, length);

            len = mpm_serial_read(mpm->fd, buffer + bufferpos, size);
            if (len <= 0)
            {
                fprintf(stderr, "short read() from device (%d != %d)\n", len, size);
                return -1;
            }

            bufferpos += len;
            length    -= len;
        }
        else
        {
            uint8_t dummy[256];

            /* no space in output buffer, but device still sends data -> do dummy read */
            uint16_t size = MIN(sizeof(dummy), length);

            len = mpm_serial_read(mpm->fd, dummy, size);
            if (len <= 0)
            {
                fprintf(stderr, "short read() from device (%d != %d)\n", len, size);
                return -1;
            }

            length -= len;
        }
    }

    return bufferpos;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %r8d, %ebx
movq %rcx, %r14
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r15
movl 0x8(%rdi), %edi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x5db0
cmpl $0x4, %eax
jne 0x5d48
movzbl 0x8(%rsp), %edx
cmpb %bpl, %dl
jne 0x5d66
movb 0x9(%rsp), %al
movb %al, (%r12)
movzwl 0xa(%rsp), %eax
rolw $0x8, %ax
movzwl %bx, %ecx
movl %ecx, 0xc(%rsp)
xorl %r12d, %r12d
movzwl %r12w, %esi
testw %ax, %ax
je 0x5d9c
movzwl %ax, %r13d
testq %r14, %r14
je 0x5d1f
cmpw %bx, %r12w
jae 0x5d1f
movl 0xc(%rsp), %eax
subl %esi, %eax
cmpl %r13d, %eax
cmovgel %r13d, %eax
movl 0x8(%r15), %edi
addq %r14, %rsi
movzwl %ax, %ebp
movl %ebp, %edx
callq 0x5db0
testl %eax, %eax
jle 0x5d7d
addl %eax, %r12d
jmp 0x5d40
cmpw $0x100, %ax             # imm = 0x100
movl $0x100, %ebp            # imm = 0x100
cmovbl %r13d, %ebp
movl 0x8(%r15), %edi
leaq 0x10(%rsp), %rsi
movl %ebp, %edx
callq 0x5db0
testl %eax, %eax
jle 0x5d7d
subl %eax, %r13d
movl %r13d, %eax
jmp 0x5cdc
movq 0x72a9(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x35db(%rip), %rdi      # 0x9334
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x5d99
movzbl %bpl, %ecx
movq 0x7287(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x30df(%rip), %rsi      # 0x8e5a
jmp 0x5d92
movq 0x7274(%rip), %rcx      # 0xcff8
movq (%rcx), %rdi
leaq 0x30f9(%rip), %rsi      # 0x8e87
movl %eax, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2180
pushq $-0x1
popq %rsi
movl %esi, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq