// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_query_dsc_dir()
bool control_conn_t::process_query_dsc_dir()
{
    // packet contains command byte, spare byte, and service handle
    constexpr int pkt_size = 2 + sizeof(handle_t);

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

    bool spare_ok = (rbuf[1] == 0);
    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 || !spare_ok) {
        char nak_rep[] = { (char)cp_rply::NAK };
        return queue_packet(nak_rep, 1);
    }

    // Reply:
    // 1 byte packet type = cp_rply::SVCDSCDIR
    // 4 bytes (uint32_t) = directory length (no nul terminator)
    // N bytes            = directory (no nul)
    std::vector<char> reppkt;
    auto sdir_len = static_cast<uint32_t>(strlen(service->get_service_dsc_dir()));
    reppkt.resize(1 + sizeof(uint32_t) + sdir_len);  // packet type, dir length, dir
    reppkt[0] = (char)cp_rply::SVCDSCDIR;
    std::memcpy(&reppkt[1], &sdir_len, sizeof(sdir_len));
    std::memcpy(&reppkt[1 + sizeof(uint32_t)], service->get_service_dsc_dir(), sdir_len);

    if (! queue_packet(std::move(reppkt))) return false;
    return true;
}

// 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 0x5baa3
movq -0x68(%rbp), %rax
movl $0x6, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5bc0f
movq -0x68(%rbp), %rdi
addq $0x80, %rdi
movl $0x1, %esi
callq 0x5e160
movq -0x68(%rbp), %rdi
movsbl %al, %eax
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movb %al, -0x15(%rbp)
addq $0x80, %rdi
leaq -0x1c(%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 -0x1c(%rbp), %esi
callq 0x5e970
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5bb1d
testb $0x1, -0x15(%rbp)
jne 0x5bb42
movq -0x68(%rbp), %rdi
movb 0x1d93c(%rip), %al      # 0x79463
movb %al, -0x29(%rbp)
leaq -0x29(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x5bc0f
leaq -0x48(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x30d60
movq -0x28(%rbp), %rdi
callq 0x5f810
movq %rax, %rdi
callq 0xa280
movq -0x70(%rbp), %rdi
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, %esi
addq $0x5, %rsi
callq 0x31490
jmp 0x5bb77
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5e950
movb $0x4d, (%rax)
leaq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0x5e950
movl -0x4c(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x48(%rbp), %rdi
movl $0x5, %esi
callq 0x5e950
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5f810
movq -0x78(%rbp), %rdi
movq %rax, %rsi
movl -0x4c(%rbp), %eax
movl %eax, %edx
callq 0xa5b0
movq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x5bc70
testb $0x1, %al
jne 0x5bbfb
movb $0x0, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0x5bc06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x13f20
jmp 0x5bc1d
movb $0x1, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x13f20
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0xacc0
nopw %cs:(%rax,%rax)