// File: /workspace/github_c_makefile_star2_O2/orempel[P]multiboot_tool/funkboot.c
// Function: funk_bridge_recv
static int funk_bridge_recv(struct funk_privdata *funk,
                            uint8_t command,
                            uint8_t *cause,
                            uint8_t *buffer,
                            int buffersize)
{
    uint8_t response[3];
    int len;

    len = funk_serial_read(funk->fd, response, sizeof(response));
    if (len != sizeof(response))
    {
        fprintf(stderr, "short read() from device\n");
        return -1;
    }

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

        return -1;
    }

    *cause = response[1];
    uint16_t length = response[2];
    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 = funk_serial_read(funk->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 = funk_serial_read(funk->fd, dummy, size);
            if (len <= 0)
            {
                fprintf(stderr, "short read() from device (%d != %d)\n",
                        len, size);

                return -1;
            }

            length -= len;
        }
    }

#if (FUNK_BRIDGE_DEBUG == 1)
    char *dump = funk_print_data(buffer, bufferpos);
    printf("funk_bridge_recv() cmd=0x%02x cause=0x%02x length=0x%02x\n%s\n",
           command, *cause, length, dump);

    free(dump);
#endif

    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 0xd(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x4f26
cmpl $0x3, %eax
jne 0x4ec0
movzbl 0xd(%rsp), %edx
cmpb %bpl, %dl
jne 0x4ede
movb 0xe(%rsp), %al
movb %al, (%r12)
movzbl 0xf(%rsp), %ecx
xorl %r13d, %r13d
movzwl %r13w, %eax
testw %cx, %cx
je 0x4f14
movzwl %cx, %r12d
testq %r14, %r14
je 0x4e96
movl %ebx, %edx
subl %eax, %edx
jle 0x4e96
cmpl %r12d, %edx
cmovgel %r12d, %edx
movl 0x8(%r15), %edi
movzwl %r13w, %esi
addq %r14, %rsi
movzwl %dx, %ebp
movl %ebp, %edx
callq 0x4f26
testl %eax, %eax
jle 0x4ef5
addl %eax, %r13d
jmp 0x4eb8
cmpw $0x100, %cx             # imm = 0x100
movl $0x100, %ebp            # imm = 0x100
cmovbl %r12d, %ebp
movl 0x8(%r15), %edi
leaq 0x10(%rsp), %rsi
movl %ebp, %edx
callq 0x4f26
testl %eax, %eax
jle 0x4ef5
subl %eax, %r12d
movl %r12d, %ecx
jmp 0x4e55
movq 0x8131(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3f6f(%rip), %rdi      # 0x8e40
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x4f11
movzbl %bpl, %ecx
movq 0x810f(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x3f67(%rip), %rsi      # 0x8e5a
jmp 0x4f0a
movq 0x80fc(%rip), %rcx      # 0xcff8
movq (%rcx), %rdi
leaq 0x3f81(%rip), %rsi      # 0x8e87
movl %eax, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2180
pushq $-0x1
popq %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq