// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x2f(%rbp), %rsi
movl $0x3, %edx
callq 0x6e70
movl %eax, -0x34(%rbp)
movslq -0x34(%rbp), %rax
cmpq $0x3, %rax
je 0x6ca7
movq 0x936e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x5266(%rip), %rsi      # 0xbefa
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e5f
movzbl -0x2f(%rbp), %eax
movzbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
je 0x6cdf
movq 0x933e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzbl -0x2f(%rbp), %edx
movzbl -0x11(%rbp), %ecx
leaq 0x5248(%rip), %rsi      # 0xbf14
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e5f
movb -0x2e(%rbp), %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
movzbl -0x2d(%rbp), %eax
movw %ax, -0x36(%rbp)
movw $0x0, -0x38(%rbp)
movzwl -0x36(%rbp), %eax
cmpl $0x0, %eax
jle 0x6e58
movzwl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x6dc1
cmpq $0x0, -0x28(%rbp)
je 0x6dc1
movl -0x2c(%rbp), %eax
movzwl -0x38(%rbp), %ecx
subl %ecx, %eax
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
jge 0x6d3d
movl -0x2c(%rbp), %eax
movzwl -0x38(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x148(%rbp)
jmp 0x6d47
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 0x6e70
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jg 0x6da2
movq 0x927a(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x34(%rbp), %edx
movzwl -0x3a(%rbp), %ecx
leaq 0x51b2(%rip), %rsi      # 0xbf41
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e5f
movl -0x34(%rbp), %ecx
movzwl -0x38(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x38(%rbp)
movl -0x34(%rbp), %ecx
movzwl -0x36(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x36(%rbp)
jmp 0x6e53
movzwl -0x36(%rbp), %eax
movl %eax, %ecx
movl $0x100, %eax            # imm = 0x100
cmpq %rcx, %rax
jae 0x6ddf
movl $0x100, %eax            # imm = 0x100
movq %rax, -0x150(%rbp)
jmp 0x6dea
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 0x6e70
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jg 0x6e46
movq 0x91d6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl -0x34(%rbp), %edx
movzwl -0x142(%rbp), %ecx
leaq 0x510b(%rip), %rsi      # 0xbf41
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e5f
movl -0x34(%rbp), %ecx
movzwl -0x36(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x36(%rbp)
jmp 0x6cf6
movzwl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax,%rax)