// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/funkboot.c
// Function: _funk_write_memory
static int __funk_write_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 + size;
    packet.header_checksum      = 0xCC;             // TODO: calced in bridge
    packet.msg.command          = MSG_CMD_WRITE_REQUEST;
    packet.msg.seqnum           = ++funk->seqnum;   // TODO: retransmit in bridge?
    packet.msg.cause            = CAUSE_SUCCESS;
    packet.msg.p.write_req.address   = address;
    packet.msg.p.write_req.mem_type  = memtype;
    packet.msg.p.write_req.size      = size;

    memcpy(packet.msg.p.write_req.data, buffer, size);

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

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

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %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)
movzwl -0x1a(%rbp), %eax
addl $0x7, %eax
movb %al, -0x47(%rbp)
movb $-0x34, -0x46(%rbp)
movb $0x24, -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)
leaq -0x49(%rbp), %rdi
addq $0x4, %rdi
addq $0x3, %rdi
addq $0x4, %rdi
movq -0x18(%rbp), %rsi
movzwl -0x1a(%rbp), %eax
movl %eax, %edx
callq 0x2190
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 0x7564
movq 0x8aad(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4bcb(%rip), %rsi      # 0xc120
movb $0x0, %al
callq 0x2160
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x75c9
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 0x75a5
movq 0x8a6c(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x4bb3(%rip), %rsi      # 0xc149
movb $0x0, %al
callq 0x2160
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x75c9
movzbl -0x45(%rbp), %eax
cmpl $0xe4, %eax
jne 0x75b9
movzbl -0x43(%rbp), %eax
cmpl $0x0, %eax
je 0x75c2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x75c9
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)