// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/funkboot.c
// Function: funk_read_memory
static int funk_read_memory(struct funk_privdata *funk,
                            uint8_t *buffer,
                            uint16_t size,
                            uint8_t memtype,
                            uint16_t address)
{
    struct rfm12_pkt packet;

    packet.dest_address         = funk->address;
    packet.source_address       = 0xCC;             // TODO: changed in bridge
    packet.data_length          = 0x07;
    packet.header_checksum      = 0xCC;             // TODO: calced in bridge
    packet.msg.command          = MSG_CMD_READ_REQUEST;
    packet.msg.seqnum           = ++funk->seqnum;   // TODO: retransmit in bridge?
    packet.msg.cause            = CAUSE_SUCCESS;
    packet.msg.p.read_req.address   = address;
    packet.msg.p.read_req.mem_type  = memtype;
    packet.msg.p.read_req.size      = size;

    int ret = funk_send_packet(funk, &packet, 4 + packet.data_length);
    if (ret < 0) {
        fprintf(stderr, "funk_read_memory(): 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_memory(): funk_recv_packet()\n");
        return ret;
    }

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

    memcpy(buffer, packet.msg.p.read_rsp.data, MIN(packet.data_length -3, size));

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %dx, -0x1a(%rbp)
movb %cl, -0x1b(%rbp)
movw %ax, -0x1e(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movb %al, -0x49(%rbp)
movb $-0x34, -0x48(%rbp)
movb $0x7, -0x47(%rbp)
movb $-0x34, -0x46(%rbp)
movb $0x23, -0x45(%rbp)
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x18(%rcx)
movb %al, -0x44(%rbp)
movb $0x0, -0x43(%rbp)
movw -0x1e(%rbp), %ax
movw %ax, -0x42(%rbp)
movb -0x1b(%rbp), %al
movb %al, -0x40(%rbp)
movw -0x1a(%rbp), %ax
movb %al, -0x3f(%rbp)
movq -0x10(%rbp), %rdi
movzbl -0x47(%rbp), %edx
addl $0x4, %edx
leaq -0x49(%rbp), %rsi
callq 0x7030
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jge 0x7262
movq 0x8db2(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4e1c(%rip), %rsi      # 0xc06c
movb $0x0, %al
callq 0x2160
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x7314
movl $0x2b, -0x54(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x49(%rbp), %rsi
leaq -0x54(%rbp), %rdx
callq 0x70d0
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jge 0x72a3
movq 0x8d6e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4e00(%rip), %rsi      # 0xc094
movb $0x0, %al
callq 0x2160
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x7314
movzbl -0x45(%rbp), %eax
cmpl $0xe3, %eax
jne 0x72b7
movzbl -0x43(%rbp), %eax
cmpl $0x0, %eax
je 0x72c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7314
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x49(%rbp), %rax
addq $0x4, %rax
addq $0x3, %rax
movq %rax, -0x60(%rbp)
movzbl -0x47(%rbp), %eax
subl $0x3, %eax
movzwl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x72f3
movzbl -0x47(%rbp), %eax
subl $0x3, %eax
movl %eax, -0x6c(%rbp)
jmp 0x72fa
movzwl -0x1a(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdi
movl -0x6c(%rbp), %eax
movslq %eax, %rdx
callq 0x2190
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)