// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_query_name()
bool control_conn_t::process_query_name()
{
    // 1 byte packet type
    // 1 byte reserved
    // handle: service
    constexpr int pkt_size = 2 + sizeof(handle_t);

    if (rbuf.get_length() < pkt_size) {
        chklen = pkt_size;
        return true;
    }

    handle_t handle;
    rbuf.extract(&handle, 2, sizeof(handle));
    rbuf.consume(pkt_size);
    chklen = 0;

    service_record *service = find_service_for_key(handle);
    if (service == nullptr || service->get_name().length() > std::numeric_limits<uint16_t>::max()) {
        char nak_rep[] = { (char)cp_rply::NAK };
        return queue_packet(nak_rep, 1);
    }

    // Reply:
    // 1 byte packet type = cp_rply::SERVICENAME
    // 1 byte reserved
    // uint16_t length
    // N bytes name

    std::vector<char> reply;
    const std::string &name = service->get_name();
    uint16_t name_length = name.length();
    reply.resize(2 + sizeof(uint16_t) + name_length);
    reply[0] = (char)cp_rply::SERVICENAME;
    memcpy(reply.data() + 2, &name_length, sizeof(name_length));
    memcpy(reply.data() + 2 + sizeof(uint16_t), name.c_str(), name_length);

    return queue_packet(std::move(reply));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x68(%rbp)
movl $0x6, -0x14(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0x6, %eax
jae 0x5ad23
movq -0x68(%rbp), %rax
movl $0x6, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5ae83
movq -0x68(%rbp), %rdi
addq $0x80, %rdi
leaq -0x18(%rbp), %rsi
movl $0x2, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0x68(%rbp), %rdi
addq $0x80, %rdi
movl $0x6, %esi
callq 0x5e1a0
movq -0x68(%rbp), %rdi
movl $0x0, 0x7c(%rdi)
movl -0x18(%rbp), %esi
callq 0x5e970
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x5ad9d
movq -0x20(%rbp), %rdi
callq 0x2d420
movq %rax, %rdi
callq 0xab20
movq %rax, -0x70(%rbp)
callq 0x5f010
movw %ax, %cx
movq -0x70(%rbp), %rax
movzwl %cx, %ecx
cmpq %rcx, %rax
jbe 0x5adc2
movq -0x68(%rbp), %rdi
movb 0x1e6ae(%rip), %al      # 0x79455
movb %al, -0x21(%rbp)
leaq -0x21(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5ae83
leaq -0x40(%rbp), %rdi
movq %rdi, -0x78(%rbp)
callq 0x30d60
movq -0x20(%rbp), %rdi
callq 0x2d420
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0xab20
movq -0x78(%rbp), %rdi
movw %ax, -0x4a(%rbp)
movzwl -0x4a(%rbp), %eax
movl %eax, %esi
addq $0x4, %rsi
callq 0x31490
jmp 0x5adfe
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5e950
movb $0x42, (%rax)
leaq -0x40(%rbp), %rdi
callq 0x31180
movw -0x4a(%rbp), %cx
movw %cx, 0x2(%rax)
leaq -0x40(%rbp), %rdi
callq 0x31180
addq $0x2, %rax
addq $0x2, %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rdi
callq 0xa250
movq -0x80(%rbp), %rdi
movq %rax, %rsi
movzwl -0x4a(%rbp), %eax
movl %eax, %edx
callq 0xa5b0
movq -0x68(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x5bc70
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x13f20
jmp 0x5ae83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x13f20
jmp 0x5ae91
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0xacc0
nopw (%rax,%rax)