// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_find_load(cp_cmd)
bool control_conn_t::process_find_load(cp_cmd pktType)
{
    using std::string;
    
    constexpr int pkt_size = 4;
    
    if (rbuf.get_length() < pkt_size) {
        chklen = pkt_size;
        return true;
    }
    
    srvname_len_t srvname_len;
    rbuf.extract(&srvname_len, 1, sizeof(srvname_len));
    if (srvname_len <= 0 || srvname_len > (1024 - 3)) {
        // Queue error response / mark connection bad
        char badreqRep[] = { (char)cp_rply::BADREQ };
        if (! queue_packet(badreqRep, 1)) return false;
        bad_conn_close = true;
        return true;
    }
    chklen = srvname_len + 3; // packet type + (2 byte) length + service name
    
    if (rbuf.get_length() < chklen) {
        // packet not complete yet; read more
        return true;
    }
    
    service_record * record = nullptr;
    
    string service_name = rbuf.extract_string(3, srvname_len);

    // Clear the packet from the buffer
    rbuf.consume(chklen);
    chklen = 0;
    
    cp_rply fail_code = cp_rply::NOSERVICE;

    if (pktType == cp_cmd::LOADSERVICE) {
        // LOADSERVICE
        try {
            record = services->load_service(service_name.c_str());
        }
        catch (service_description_exc &sdexc) {
            log_service_load_failure(sdexc);
            fail_code = cp_rply::SERVICE_DESC_ERR;
        }
        catch (service_not_found &snf) {
            log(loglevel_t::ERROR, "Could not load service ", snf.service_name, ": ",
                    snf.exc_description);
            // fail_code = cp_rply::NOSERVICE;   (already set)
        }
        catch (service_load_exc &slexc) {
            log(loglevel_t::ERROR, "Could not load service ", slexc.service_name, ": ",
                    slexc.exc_description);
            fail_code = cp_rply::SERVICE_LOAD_ERR;
        }
    }
    else {
        // FINDSERVICE
        record = services->find_service(service_name.c_str());
    }
    
    if (record == nullptr) {
        std::vector<char> rp_buf = { (char)fail_code };
        if (! queue_packet(std::move(rp_buf))) return false;
        return true;
    }

    // Allocate a service handle
    handle_t handle = allocate_service_handle(record);
    std::vector<char> rp_buf;
    rp_buf.reserve(7);
    rp_buf.push_back((char)cp_rply::SERVICERECORD);
    rp_buf.push_back(static_cast<char>(record->get_state()));
    for (int i = 0; i < (int) sizeof(handle); i++) {
        rp_buf.push_back(*(((char *) &handle) + i));
    }
    rp_buf.push_back(static_cast<char>(record->get_target_state()));
    if (! queue_packet(std::move(rp_buf))) return false;
    
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x108(%rbp)
movl $0x4, -0x18(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0x4, %eax
jae 0x5854f
movq -0x108(%rbp), %rax
movl $0x4, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x58a77
movq -0x108(%rbp), %rdi
addq $0x80, %rdi
leaq -0x1a(%rbp), %rsi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x5e220
movzwl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jle 0x58584
movzwl -0x1a(%rbp), %eax
cmpl $0x3fd, %eax            # imm = 0x3FD
jle 0x585c3
movq -0x108(%rbp), %rdi
movb 0x20eab(%rip), %al      # 0x7943c
movb %al, -0x1b(%rbp)
leaq -0x1b(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x585af
movb $0x0, -0x1(%rbp)
jmp 0x58a77
movq -0x108(%rbp), %rax
movb $0x1, 0x78(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x58a77
movq -0x108(%rbp), %rdi
movzwl -0x1a(%rbp), %eax
addl $0x3, %eax
movl %eax, 0x7c(%rdi)
addq $0x80, %rdi
callq 0x5e200
movq -0x108(%rbp), %rcx
cmpl 0x7c(%rcx), %eax
jae 0x585f5
movb $0x1, -0x1(%rbp)
jmp 0x58a77
movq -0x108(%rbp), %rsi
movq $0x0, -0x28(%rbp)
addq $0x80, %rsi
movzwl -0x1a(%rbp), %ecx
leaq -0x48(%rbp), %rdi
movl $0x3, %edx
callq 0x5e2d0
movq -0x108(%rbp), %rax
movq %rax, %rdi
addq $0x80, %rdi
movl 0x7c(%rax), %esi
callq 0x5e1a0
movq -0x108(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x3c, -0x49(%rbp)
cmpb $0x2, -0x11(%rbp)
jne 0x587a2
movq -0x108(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x118(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xa250
movq -0x118(%rbp), %rdi
movq %rax, %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, -0x110(%rbp)
jmp 0x58689
movq -0x110(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x586d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x11c(%rbp)
movl $0x3, %ecx
cmpl %ecx, %eax
jne 0x586de
movq -0x58(%rbp), %rdi
callq 0xa240
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0xf8b0
jmp 0x586cc
movb $0x47, -0x49(%rbp)
callq 0xac30
jmp 0x586d7
jmp 0x586d9
jmp 0x58843
movl -0x11c(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0x58726
movq -0x58(%rbp), %rdi
callq 0xa240
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdx
movq %rdx, %r8
addq $0x20, %r8
leaq 0x1fd42(%rip), %rsi     # 0x7844e
leaq 0x1e4f3(%rip), %rcx     # 0x76c06
movl $0x3, %edi
callq 0x5bc30
callq 0xac30
jmp 0x58724
jmp 0x586d9
movl -0x11c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x58a6c
movq -0x58(%rbp), %rdi
callq 0xa240
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdx
movq %rdx, %r8
addq $0x20, %r8
leaq 0x1fcf6(%rip), %rsi     # 0x7844e
leaq 0x1e4a7(%rip), %rcx     # 0x76c06
movl $0x3, %edi
callq 0x5bc30
movb $0x48, -0x49(%rbp)
callq 0xac30
jmp 0x58774
jmp 0x586d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x58a6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
callq 0xac30
jmp 0x5879d
jmp 0x58a6c
movq -0x108(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x138(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xa250
movq %rax, -0x130(%rbp)
leaq -0x99(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0xace0
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0x16e00
jmp 0x587f3
movq -0x138(%rbp), %rdi
leaq -0x98(%rbp), %rsi
xorl %edx, %edx
callq 0x4b790
movq %rax, -0x28(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xade8
leaq -0x99(%rbp), %rdi
callq 0xa790
jmp 0x58843
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x99(%rbp), %rdi
callq 0xa790
jmp 0x58a6c
cmpq $0x0, -0x28(%rbp)
jne 0x58915
movb -0x49(%rbp), %al
movb %al, -0xc9(%rbp)
leaq -0xc9(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq $0x1, -0xc0(%rbp)
leaq -0xca(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0xace0
movq -0x140(%rbp), %rcx
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
leaq -0xb8(%rbp), %rdi
callq 0x5e4a0
jmp 0x588a6
leaq -0xca(%rbp), %rdi
callq 0xa790
movq -0x108(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x5bc70
testb $0x1, %al
jne 0x588f6
movb $0x0, -0x1(%rbp)
movl $0x1, -0xd0(%rbp)
jmp 0x58904
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xca(%rbp), %rdi
callq 0xa790
jmp 0x58a6c
movb $0x1, -0x1(%rbp)
movl $0x1, -0xd0(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x13f20
jmp 0x58a61
movq -0x108(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5be40
movl %eax, -0x144(%rbp)
jmp 0x5892d
movl -0x144(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x30d60
movq -0x150(%rbp), %rdi
movl $0x7, %esi
callq 0x5e520
jmp 0x5895f
movb $0x3b, -0xf1(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf1(%rbp), %rsi
callq 0x5e600
jmp 0x5897b
movq -0x28(%rbp), %rdi
callq 0x2f0d0
movb %al, -0xf2(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf2(%rbp), %rsi
callq 0x5e600
jmp 0x5899f
movl $0x0, -0xf8(%rbp)
cmpl $0x4, -0xf8(%rbp)
jge 0x589fc
movslq -0xf8(%rbp), %rax
leaq -0xd4(%rbp,%rax), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x5e630
jmp 0x589cf
jmp 0x589d1
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0x589a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x13f20
jmp 0x58a6c
movq -0x28(%rbp), %rdi
callq 0x52d20
movb %al, -0xf9(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf9(%rbp), %rsi
callq 0x5e600
jmp 0x58a20
movq -0x108(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x5bc70
testb $0x1, %al
jne 0x58a47
movb $0x0, -0x1(%rbp)
movl $0x1, -0xd0(%rbp)
jmp 0x58a55
movb $0x1, -0x1(%rbp)
movl $0x1, -0xd0(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x13f20
leaq -0x48(%rbp), %rdi
callq 0xade8
jmp 0x58a77
leaq -0x48(%rbp), %rdi
callq 0xade8
jmp 0x58a85
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0xacc0
movq %rax, %rdi
callq 0xfa30
nopw %cs:(%rax,%rax)