// File: /workspace/github_cpp_makefile_star2_O2/davmac314[P]dinit/src/control.cc
// Function: control_conn_t::add_service_dep(bool)
bool control_conn_t::add_service_dep(bool do_enable)
{
    // 1 byte packet type
    // 1 byte dependency type
    // handle: "from"
    // handle: "to"

    constexpr int pkt_size = 2 + sizeof(handle_t) * 2;

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

    handle_t from_handle;
    handle_t to_handle;
    rbuf.extract((char *) &from_handle, 2, sizeof(from_handle));
    rbuf.extract((char *) &to_handle, 2 + sizeof(from_handle), sizeof(to_handle));

    service_record *from_service = find_service_for_key(from_handle);
    service_record *to_service = find_service_for_key(to_handle);
    if (from_service == nullptr || to_service == nullptr || from_service == to_service) {
        // 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;
    }

    // Check dependency type is valid:
    int dep_type_int = rbuf[1];
    if (!contains({dependency_type::MILESTONE, dependency_type::REGULAR,
            dependency_type::WAITS_FOR}, dep_type_int)) {
        char badreqRep[] = { (char)cp_rply::BADREQ };
        if (!queue_packet(badreqRep, 1)) return false;
        bad_conn_close = true;
    }
    dependency_type dep_type = static_cast<dependency_type>(dep_type_int);

    // Check current service states are valid for given dep type
    if (dep_type == dependency_type::REGULAR) {
        if (from_service->get_state() != service_state_t::STOPPED &&
                to_service->get_state() != service_state_t::STARTED) {
            // Cannot create dependency now since it would be contradicted:
            char nak_rep[] = { (char)cp_rply::NAK };
            if (! queue_packet(nak_rep, 1)) return false;
            rbuf.consume(pkt_size);
            chklen = 0;
            return true;
        }
    }

    // Check for creation of circular dependency chain
    std::unordered_set<service_record *> dep_marks;
    std::vector<service_record *> dep_queue;
    dep_queue.push_back(to_service);
    while (! dep_queue.empty()) {
        service_record * sr = dep_queue.back();
        dep_queue.pop_back();
        // iterate deps; if dep == from, abort; otherwise add to set/queue
        // (only add to queue if not already in set)
        for (auto &dep : sr->get_dependencies()) {
            service_record * dep_to = dep.get_to();
            if (dep_to == from_service) {
                // fail, circular dependency!
                char nak_rep[] = { (char)cp_rply::NAK };
                if (! queue_packet(nak_rep, 1)) return false;
                rbuf.consume(pkt_size);
                chklen = 0;
                return true;
            }
            if (dep_marks.insert(dep_to).second) {
                dep_queue.push_back(dep_to);
            }
        }
    }
    dep_marks.clear();
    dep_queue.clear();

    bool dep_exists = false;
    service_dep * dep_record = nullptr;

    // Prevent creation of duplicate dependency:
    for (auto &dep : from_service->get_dependencies()) {
        service_record * dep_to = dep.get_to();
        if (dep_to == to_service && dep.dep_type == dep_type) {
            // Dependency already exists
            dep_exists = true;
            dep_record = &dep;
            break;
        }
    }

    if (! dep_exists) {
        // Create dependency:
        dep_record = &(from_service->add_dep(to_service, dep_type));
        services->process_queues();
    }

    if (do_enable && contains({service_state_t::STARTED, service_state_t::STARTING},
            from_service->get_state())) {
        // The dependency record is activated: mark it as holding acquisition of the dependency, and start
        // the dependency.
        if (!services->is_shutting_down()) {
            dep_record->get_from()->start_dep(*dep_record);
            services->process_queues();
        }
    }

    char ack_rep[] = { (char)cp_rply::ACK };
    if (! queue_packet(ack_rep, 1)) return false;
    rbuf.consume(pkt_size);
    chklen = 0;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
cmpl $0x9, 0x484(%rdi)
ja 0x2448a
movl $0xa, 0x7c(%rbx)
movb $0x1, %bpl
jmp 0x24653
movl %esi, %r13d
leaq 0x80(%rbx), %r14
leaq 0x5c(%rsp), %r15
pushq $0x2
popq %rdx
pushq $0x4
popq %rbp
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x25e18
leaq 0x58(%rsp), %r12
pushq $0x6
popq %rdx
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %ecx
callq 0x25e18
movl (%r15), %esi
movq %rbx, %rdi
callq 0x26056
movq %rax, %r15
movl (%r12), %esi
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x26056
movq %rax, 0x20(%rsp)
cmpq %rax, %r15
je 0x245ec
testq %r15, %r15
je 0x245ec
testq %rax, %rax
je 0x245ec
movq 0x8(%rsp), %rdx
movl 0x480(%rdx), %eax
leal 0x1(%rax), %ecx
addl $0xfffffc01, %eax       # imm = 0xFFFFFC01
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movzbl 0x80(%rdx,%rax), %r12d
leal -0x2(%r12), %eax
cmpl $0x2, %eax
jae 0x24667
movl %r13d, 0x2c(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x30(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x20(%rsp), %rsi
callq 0x2626c
movsbl %r12b, %eax
movl %eax, 0x28(%rsp)
leaq 0x60(%rsp), %r13
leaq 0x50(%rsp), %r12
leaq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rax
cmpq %rax, 0x30(%rsp)
je 0x246e9
movq -0x8(%rax), %rbx
movl $0x118, %ecx            # imm = 0x118
addq %rcx, %rbx
addq $-0x8, %rax
movq %rax, 0x38(%rsp)
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x24591
movq 0x18(%r14), %rax
movq %rax, 0x50(%rsp)
cmpq %r15, %rax
je 0x24610
movq %r13, %rdi
movq %r12, %rsi
callq 0x27c84
testb $0x1, %dl
je 0x245b9
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2626c
jmp 0x245b9
leaq 0x60(%rsp), %rsi
movb $0x34, (%rsi)
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x234ec
movl %eax, %ebp
testb %al, %al
je 0x24653
movb $0x1, 0x78(%rbx)
jmp 0x24653
leaq 0x17(%rsp), %rsi
movb $0x33, (%rsi)
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x234ec
movl %eax, %ebp
testb %al, %al
je 0x2463f
pushq $0xa
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x25df2
andl $0x0, 0x7c(%rbx)
leaq 0x30(%rsp), %rdi
callq 0x27b0c
leaq 0x60(%rsp), %rdi
callq 0x267b6
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
jne 0x246b7
cmpb $0x0, 0x2c(%r15)
je 0x24538
movq 0x20(%rsp), %rax
cmpb $0x2, 0x2c(%rax)
je 0x24538
leaq 0x60(%rsp), %rsi
movb $0x33, (%rsi)
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x234ec
movl %eax, %ebp
testb %al, %al
je 0x24653
pushq $0xa
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x25df2
andl $0x0, 0x7c(%rbx)
jmp 0x24653
leaq 0x60(%rsp), %rsi
movb $0x34, (%rsi)
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x234ec
testb %al, %al
je 0x246e2
movb $0x1, 0x78(%rbx)
testb %r12b, %r12b
jne 0x24538
jmp 0x2466c
xorl %ebp, %ebp
jmp 0x24653
leaq 0x60(%rsp), %rdi
callq 0x267d2
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x24704
movq %rax, 0x38(%rsp)
movq %r15, %rax
addq $0x118, %rax            # imm = 0x118
movq 0x20(%rsp), %rsi
xorl %r13d, %r13d
movq %rax, %rcx
movl 0x28(%rsp), %edx
movq (%rcx), %rcx
cmpq %rax, %rcx
je 0x24733
cmpq %rsi, 0x18(%rcx)
jne 0x2471c
cmpl %edx, 0x24(%rcx)
jne 0x2471c
leaq 0x10(%rcx), %r13
cmpq %rax, %rcx
jne 0x24751
movq %r15, %rdi
callq 0x2628a
movq %rax, %r13
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rdi
callq 0xcdd2
cmpb $0x0, 0x2c(%rsp)
je 0x2478b
movb 0x2c(%r15), %al
decb %al
cmpb $0x1, %al
ja 0x2478b
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rax
cmpb $0x1, 0x28(%rax)
jne 0x2478b
movq (%r13), %rdi
movq %r13, %rsi
callq 0x26090
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rdi
callq 0xcdd2
leaq 0x50(%rsp), %rsi
movb $0x32, (%rsi)
jmp 0x24618
jmp 0x2479e
jmp 0x2479e
jmp 0x2479e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x27b0c
leaq 0x60(%rsp), %rdi
callq 0x267b6
movq %rbx, %rdi
callq 0x8a90
nop