// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_reload_service()
bool control_conn_t::process_reload_service()
{
    using std::string;

    constexpr int pkt_size = 1 + sizeof(handle_t);

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

    // 1 byte: packet type
    // 4 bytes: service handle

    handle_t handle;
    rbuf.extract((char *) &handle, 1, sizeof(handle));

    service_record *service = find_service_for_key(handle);
    if (service == nullptr) {
        // Service handle is bad
        char badreq_rep[] = { (char)cp_rply::BADREQ };
        if (! queue_packet(badreq_rep, 1)) return false;
        bad_conn_close = true;
        return true;
    }

    if (!service->has_lone_ref(false)) {
        // Cannot unload: has other references
        char nak_rep[] = { (char)cp_rply::NAK };
        if (! queue_packet(nak_rep, 1)) return false;
    }
    else {
        try {
            // drop handle
            key_service_map.erase(handle);
            service_key_map.erase(service);

            // reload
            service->remove_listener(this);
            services->reload_service(service);
            services->process_queues();

            // send ack
            char ack_buf[] = { (char) cp_rply::ACK };
            if (! queue_packet(ack_buf, 1)) return false;
        }
        catch (service_load_exc &slexc) {
            log(loglevel_t::ERROR, "Could not reload service ", slexc.service_name, ": ",
                    slexc.exc_description);
            char nak_rep[] = { (char)cp_rply::NAK };
            if (! queue_packet(nak_rep, 1)) return false;
        }
    }

    // Clear the packet from the buffer
    rbuf.consume(pkt_size);
    chklen = 0;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x50(%rbp)
movl $0x5, -0x14(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0x5, %eax
jae 0x594c0
movq -0x50(%rbp), %rax
movl $0x5, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x596ad
movq -0x50(%rbp), %rdi
addq $0x80, %rdi
leaq -0x18(%rbp), %rsi
movl $0x1, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0x50(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x5e970
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x5952e
movq -0x50(%rbp), %rdi
movb 0x1ff47(%rip), %al      # 0x79446
movb %al, -0x21(%rbp)
leaq -0x21(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5951d
movb $0x0, -0x1(%rbp)
jmp 0x596ad
movq -0x50(%rbp), %rax
movb $0x1, 0x78(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x596ad
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x5ea80
testb $0x1, %al
jne 0x5956a
movq -0x50(%rbp), %rdi
movb 0x1ff00(%rip), %al      # 0x79447
movb %al, -0x22(%rbp)
leaq -0x22(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x59565
movb $0x0, -0x1(%rbp)
jmp 0x596ad
jmp 0x59689
movq -0x50(%rbp), %rdi
addq $0x4c0, %rdi            # imm = 0x4C0
leaq -0x18(%rbp), %rsi
callq 0x5eee0
jmp 0x59580
movq -0x50(%rbp), %rdi
addq $0x488, %rdi            # imm = 0x488
leaq -0x20(%rbp), %rsi
callq 0x5eeb0
jmp 0x59596
movq -0x50(%rbp), %rsi
movq -0x20(%rbp), %rdi
callq 0x5e8d0
movq -0x50(%rbp), %rax
movq 0x70(%rax), %rdi
movq -0x20(%rbp), %rsi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x595ba
movq -0x50(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x173c0
movq -0x50(%rbp), %rdi
movb 0x1fe77(%rip), %al      # 0x79448
movb %al, -0x35(%rbp)
leaq -0x35(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5966c
movb $0x0, -0x1(%rbp)
jmp 0x596ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x596b8
movq -0x30(%rbp), %rdi
callq 0xa240
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdx
movq -0x40(%rbp), %r8
addq $0x20, %r8
movl $0x3, %edi
leaq 0x1fe45(%rip), %rsi     # 0x79479
leaq 0x1d5cb(%rip), %rcx     # 0x76c06
callq 0x5c270
movq -0x50(%rbp), %rdi
movb 0x1fdff(%rip), %al      # 0x79449
movb %al, -0x41(%rbp)
leaq -0x41(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5966e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x59675
jmp 0x59687
movl $0x0, -0x48(%rbp)
callq 0xac30
movl -0x48(%rbp), %eax
testl %eax, %eax
je 0x59685
jmp 0x59683
jmp 0x596ad
jmp 0x59687
jmp 0x59689
movq -0x50(%rbp), %rdi
addq $0x80, %rdi
movl $0x5, %esi
callq 0x5e1a0
movq -0x50(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0xacc0
nopw %cs:(%rax,%rax)