// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movl 0x484(%rdi), %ebp
cmpl $0x3, %ebp
ja 0x28164
movl $0x4, 0x7c(%rbx)
movb $0x1, %r14b
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %r13d
movl 0x480(%rbx), %r14d
leal 0x1(%r14), %eax
movl $0xfffffc01, %ecx       # imm = 0xFFFFFC01
addl %ecx, %r14d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r14d
leaq 0x80(%rbx), %r15
addl %r14d, %ecx
movl %r14d, %esi
cmpl $0xfffffbfe, %ecx       # imm = 0xFFFFFBFE
ja 0x281d5
movl $0x400, %r12d           # imm = 0x400
subl %r14d, %r12d
addq %r15, %rsi
leaq 0xa(%rsp), %rdi
movq %r12, %rdx
callq 0x84d0
leaq (%rsp,%r12), %rdi
addq $0xa, %rdi
addl $0xfffffc02, %r14d      # imm = 0xFFFFFC02
movq %r15, %rsi
movq %r14, %rdx
callq 0x84d0
leaq 0xa(%rsp), %rax
movzwl (%rax), %eax
jmp 0x281df
movzwl (%r15,%rsi), %eax
movw %ax, 0xa(%rsp)
leal -0x3fe(%rax), %ecx
movzwl %cx, %ecx
cmpl $0xfc02, %ecx           # imm = 0xFC02
ja 0x28219
leaq 0x38(%rsp), %rsi
movb $0x34, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
movl %eax, %r14d
testb %al, %al
je 0x28152
movb $0x1, 0x78(%rbx)
jmp 0x28152
leal 0x3(%rax), %ecx
movzwl %cx, %ecx
movl %ecx, 0x7c(%rbx)
movb $0x1, %r14b
cmpl %ecx, %ebp
jb 0x28152
movzwl %ax, %ecx
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
movl $0x3, %edx
callq 0x2b760
movl 0x7c(%rbx), %eax
movl 0x480(%rbx), %ecx
leal (%rcx,%rax), %edx
cmpl $0x400, %edx            # imm = 0x400
leal -0x400(%rcx,%rax), %ecx
cmovbl %edx, %ecx
movl %ecx, 0x480(%rbx)
subl %eax, 0x484(%rbx)
movl $0x0, 0x7c(%rbx)
movq 0x70(%rbx), %r14
cmpb $0x2, %r13b
jne 0x2828e
movq 0x38(%rsp), %rsi
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %rax, %r14
jmp 0x282d1
movq 0x38(%rsp), %rsi
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x1a43c
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x225e4
movq %rax, %r14
movq (%r15), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x282d1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x8620
testq %r14, %r14
je 0x2837c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2ad9c
movl %eax, 0xc(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x7, %esi
callq 0x2b854
leaq 0x9(%rsp), %rsi
movb $0x3b, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x2cbf6
movb 0x2c(%r14), %al
leaq 0x9(%rsp), %rsi
movb %al, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x2cbf6
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
leaq (%rsp,%r12), %rdx
addq $0xc, %rdx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x28354
movb (%rdx), %al
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0x2835c
movq %r15, %rdi
callq 0x2cd1c
incq %r12
cmpq $0x4, %r12
jne 0x28335
movb 0x2d(%r14), %al
leaq 0x9(%rsp), %rsi
movb %al, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x2cbf6
jmp 0x2839b
movb $0x3c, %bpl
leaq 0xc(%rsp), %rsi
movb %bpl, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0x9(%rsp), %rcx
movl $0x1, %edx
callq 0x2b810
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2acb2
movl %eax, %r14d
movq (%r15), %rdi
testq %rdi, %rdi
je 0x283c3
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8620
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28152
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x8620
jmp 0x28152
jmp 0x28482
jmp 0x28482
movq %rax, %r14
cmpl $0x3, %edx
jne 0x28416
movq %r14, %rdi
callq 0x8210
movq %rax, %rdi
callq 0xb651
movb $0x47, %bpl
callq 0x8a50
jmp 0x2837f
cmpl $0x2, %edx
jne 0x2843c
movq %r14, %rdi
callq 0x8210
leaq 0x20(%rax), %rsi
movq %rax, %rdi
callq 0x2ac74
movb $0x3c, %bpl
callq 0x8a50
jmp 0x2837f
cmpl $0x1, %edx
jne 0x284a1
movq %r14, %rdi
callq 0x8210
leaq 0x20(%rax), %rsi
movq %rax, %rdi
callq 0x2ac74
movb $0x48, %bpl
callq 0x8a50
jmp 0x2837f
jmp 0x28482
jmp 0x28482
jmp 0x28482
movq %rax, %r14
callq 0x8a50
jmp 0x284a1
movq %rax, %rdi
callq 0xb74e
jmp 0x28487
jmp 0x28487
jmp 0x28487
jmp 0x28487
movq %rax, %r14
jmp 0x284a1
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x284a1
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8620
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x284bc
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x8620
movq %r14, %rdi
callq 0x8ab0