// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movb %sil, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movw %ax, -0x2a(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x34(%rbp), %rsi
movl $0x4, %edx
callq 0x8720
movl %eax, -0x30(%rbp)
movslq -0x30(%rbp), %rax
cmpq $0x4, %rax
je 0x8549
movq 0x7acc(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x3ebe(%rip), %rsi      # 0xc3f4
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x870f
movzbl -0x34(%rbp), %eax
movzbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
je 0x8581
movq 0x7a9c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x34(%rbp), %edx
movzbl -0x11(%rbp), %ecx
leaq 0x39a6(%rip), %rsi      # 0xbf14
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x870f
movb -0x33(%rbp), %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
movzbl -0x32(%rbp), %eax
shll $0x8, %eax
movzbl -0x31(%rbp), %ecx
orl %ecx, %eax
movw %ax, -0x36(%rbp)
movw $0x0, -0x38(%rbp)
movzwl -0x36(%rbp), %eax
cmpl $0x0, %eax
jle 0x8708
movzwl -0x38(%rbp), %eax
movzwl -0x2a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x8671
cmpq $0x0, -0x28(%rbp)
je 0x8671
movzwl -0x2a(%rbp), %eax
movzwl -0x38(%rbp), %ecx
subl %ecx, %eax
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
jge 0x85ed
movzwl -0x2a(%rbp), %eax
movzwl -0x38(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x148(%rbp)
jmp 0x85f7
movzwl -0x36(%rbp), %eax
movl %eax, -0x148(%rbp)
movl -0x148(%rbp), %eax
movw %ax, -0x3a(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x28(%rbp), %rsi
movzwl -0x38(%rbp), %eax
cltq
addq %rax, %rsi
movzwl -0x3a(%rbp), %edx
callq 0x8720
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jg 0x8652
movq 0x79ca(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x30(%rbp), %edx
movzwl -0x3a(%rbp), %ecx
leaq 0x3902(%rip), %rsi      # 0xbf41
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x870f
movl -0x30(%rbp), %ecx
movzwl -0x38(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x38(%rbp)
movl -0x30(%rbp), %ecx
movzwl -0x36(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x36(%rbp)
jmp 0x8703
movzwl -0x36(%rbp), %eax
movl %eax, %ecx
movl $0x100, %eax            # imm = 0x100
cmpq %rcx, %rax
jae 0x868f
movl $0x100, %eax            # imm = 0x100
movq %rax, -0x150(%rbp)
jmp 0x869a
movzwl -0x36(%rbp), %eax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movw %ax, -0x142(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x140(%rbp), %rsi
movzwl -0x142(%rbp), %edx
callq 0x8720
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jg 0x86f6
movq 0x7926(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x30(%rbp), %edx
movzwl -0x142(%rbp), %ecx
leaq 0x385b(%rip), %rsi      # 0xbf41
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x870f
movl -0x30(%rbp), %ecx
movzwl -0x36(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x36(%rbp)
jmp 0x85a1
movzwl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax,%rax)