// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/funkboot.c
// Function: funk_read_version
static int funk_read_version(struct funk_privdata *funk,
                             uint8_t *version,
                             uint16_t length)
{
    struct rfm12_pkt packet;

    packet.dest_address         = funk->address;
    packet.source_address       = 0xCC;             // TODO: changed in bridge
    packet.data_length          = 0x03;
    packet.header_checksum      = 0xCC;             // TODO: calced in bridge
    packet.msg.command          = MSG_CMD_VERSION_REQUEST;
    packet.msg.seqnum           = ++funk->seqnum;   // TODO: retransmit in bridge?
    packet.msg.cause            = CAUSE_SUCCESS;

    int ret = funk_send_packet(funk, &packet, 4 + packet.data_length);
    if (ret < 0)
    {
        fprintf(stderr, "funk_read_version(): funk_send_packet()\n");
        return ret;
    }

    int response_size = sizeof(packet);
    ret = funk_recv_packet(funk, &packet, &response_size);
    if (ret < 0)
    {
        fprintf(stderr, "funk_read_version(): funk_recv_packet()\n");
        return ret;
    }

    if ((packet.msg.command != MSG_CMD_VERSION_RESPONSE) ||
        (packet.msg.cause != CAUSE_SUCCESS)
       )
    {
        return -1;
    }

    int i;
    for (i = 0; i < MIN(length, packet.data_length -3); i++)
    {
        version[i] = packet.msg.p.version.data[i] & 0x7F;
    }

    version[i] = '\0';

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movw %dx, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %ax, -0x1a(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movb %al, -0x45(%rbp)
movb $-0x34, -0x44(%rbp)
movb $0x3, -0x43(%rbp)
movb $-0x34, -0x42(%rbp)
movb $0x21, -0x41(%rbp)
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x18(%rcx)
movb %al, -0x40(%rbp)
movb $0x0, -0x3f(%rbp)
movq -0x10(%rbp), %rdi
movzbl -0x43(%rbp), %edx
addl $0x4, %edx
leaq -0x45(%rbp), %rsi
callq 0x7030
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x6985
movq 0x968f(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x5653(%rip), %rsi      # 0xbfc6
movb $0x0, %al
callq 0x2160
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6a56
movl $0x2b, -0x50(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x45(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x70d0
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x69c9
movq 0x964b(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x5638(%rip), %rsi      # 0xbfef
movb $0x0, %al
callq 0x2160
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6a56
movzbl -0x41(%rbp), %eax
cmpl $0xe1, %eax
jne 0x69dd
movzbl -0x3f(%rbp), %eax
cmpl $0x0, %eax
je 0x69e6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6a56
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
movzwl -0x1a(%rbp), %eax
movzbl -0x43(%rbp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jge 0x6a0b
movzwl -0x1a(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x6a15
movzbl -0x43(%rbp), %eax
subl $0x3, %eax
movl %eax, -0x5c(%rbp)
movl -0x58(%rbp), %eax
movl -0x5c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x6a43
movslq -0x54(%rbp), %rax
movzbl -0x3e(%rbp,%rax), %eax
andl $0x7f, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x69ed
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop