// File: /workspace/github_cpp_makefile_star2_O3/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 0x295d0
movl $0xa, 0x7c(%rbx)
movb $0x1, %bpl
jmp 0x299fc
movl %esi, 0x44(%rsp)
leaq 0x80(%rbx), %r14
movl 0x480(%rbx), %r15d
leal 0x2(%r15), %eax
leal -0x3fe(%r15), %r12d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r12d
leal -0x3fd(%r12), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x2963d
movl $0x400, %r13d           # imm = 0x400
subl %r12d, %r13d
movl %r12d, %esi
addq %r14, %rsi
leaq 0x8(%rsp), %rdi
movq %r13, %rdx
callq 0x84d0
leaq (%rsp,%r13), %rdi
addq $0x8, %rdi
addl $0xfffffc04, %r12d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r12, %rdx
callq 0x84d0
jmp 0x29648
movl %r12d, %eax
movl (%r14,%rax), %eax
movl %eax, 0x8(%rsp)
leal 0x6(%r15), %eax
addl $0xfffffc06, %r15d      # imm = 0xFFFFFC06
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x296a9
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r14, %rsi
leaq 0xc(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x84d0
leaq (%rsp,%r12), %rdi
addq $0xc, %rdi
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r14, %rsi
movq %r15, %rdx
callq 0x84d0
movl (%r13), %ebp
jmp 0x296b4
movl %r15d, %eax
movl (%r14,%rax), %ebp
movl %ebp, 0xc(%rsp)
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0x2b906
movq %rax, %r14
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2b906
movq %rax, 0x30(%rsp)
cmpq %rax, %r14
je 0x298f9
testq %r14, %r14
je 0x298f9
testq %rax, %rax
je 0x298f9
movl 0x480(%rbx), %eax
leal 0x1(%rax), %ecx
addl $0xfffffc01, %eax       # imm = 0xFFFFFC01
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movzbl 0x80(%rbx,%rax), %r15d
leal -0x2(%r15), %eax
cmpl $0x2, %eax
jae 0x29921
movq %r15, 0x58(%rsp)
movq %rbx, 0x48(%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 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x30(%rsp), %rdx
xorl %esi, %esi
callq 0x2d1fe
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
je 0x29816
leaq 0x60(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x38(%rsp), %r13
movq -0x8(%rcx), %rbp
addq $-0x8, %rcx
movq %rcx, 0x18(%rsp)
movq 0x118(%rbp), %rbx
addq $0x118, %rbp            # imm = 0x118
cmpq %rbp, %rbx
je 0x2980d
movq 0x18(%rbx), %rax
movq %rax, 0x50(%rsp)
cmpq %r14, %rax
je 0x29990
movq %r15, 0x38(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2d33a
testb $0x1, %dl
je 0x297fb
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x297ee
movq 0x50(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x297fb
leaq 0x10(%rsp), %rdi
movq %r12, %rdx
callq 0x2d1fe
movq (%rbx), %rbx
cmpq %rbp, %rbx
jne 0x297a8
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jne 0x29788
leaq 0x70(%rsp), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x29838
movq (%rdi), %r15
movl $0x10, %esi
callq 0x8620
movq %r15, %rdi
testq %r15, %r15
jne 0x29823
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x8350
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x29864
movq %rax, 0x18(%rsp)
movsbl 0x58(%rsp), %edx
movq %r14, %rcx
addq $0x118, %rcx            # imm = 0x118
movq 0x30(%rsp), %rsi
xorl %r15d, %r15d
movq %rcx, %rax
movq 0x48(%rsp), %rbx
movq (%rax), %rax
cmpq %rcx, %rax
je 0x2989a
cmpq %rsi, 0x18(%rax)
jne 0x29883
cmpl %edx, 0x24(%rax)
jne 0x29883
leaq 0x10(%rax), %r15
cmpq %rcx, %rax
jne 0x298b3
movq %r14, %rdi
callq 0x1f58a
movq %rax, %r15
movq 0x70(%rbx), %rdi
callq 0xd37e
cmpb $0x0, 0x44(%rsp)
je 0x298ec
movb 0x2c(%r14), %al
decb %al
cmpb $0x1, %al
ja 0x298ec
movq 0x70(%rbx), %rdi
cmpb $0x1, 0x28(%rdi)
jne 0x298ec
cmpb $0x0, 0x11(%r15)
jne 0x298e7
movq 0x8(%r15), %rdi
callq 0x22c46
movb $0x1, 0x11(%r15)
movq 0x70(%rbx), %rdi
callq 0xd37e
leaq 0x38(%rsp), %rsi
movb $0x32, (%rsi)
jmp 0x2999d
leaq 0x60(%rsp), %rsi
movb $0x34, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
movl %eax, %ebp
testb %al, %al
je 0x299fc
movb $0x1, 0x78(%rbx)
jmp 0x299fc
testl %r15d, %r15d
jne 0x29a10
cmpb $0x0, 0x2c(%r14)
je 0x2971a
movq 0x30(%rsp), %rax
cmpb $0x2, 0x2c(%rax)
je 0x2971a
leaq 0x60(%rsp), %rsi
movb $0x33, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
movl %eax, %ebp
testb %al, %al
je 0x299fc
movl 0x480(%rbx), %eax
leal 0xa(%rax), %ecx
addl $0xfffffc0a, %eax       # imm = 0xFFFFFC0A
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl %eax, 0x480(%rbx)
addl $-0xa, 0x484(%rbx)
movl $0x0, 0x7c(%rbx)
jmp 0x299fc
leaq 0x38(%rsp), %rsi
movb $0x33, (%rsi)
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
movl %eax, %ebp
testb %al, %al
je 0x299db
movl 0x480(%rbx), %eax
leal 0xa(%rax), %ecx
addl $0xfffffc0a, %eax       # imm = 0xFFFFFC0A
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl %eax, 0x480(%rbx)
addl $-0xa, 0x484(%rbx)
movl $0x0, 0x7c(%rbx)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x299f2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8620
leaq 0x60(%rsp), %rdi
callq 0x2bef0
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x60(%rsp), %rsi
movb $0x34, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
testb %al, %al
je 0x29a3b
movb $0x1, 0x78(%rbx)
testb %r15b, %r15b
jne 0x2971a
jmp 0x2992a
xorl %ebp, %ebp
jmp 0x299fc
jmp 0x29a45
jmp 0x29a45
jmp 0x29a45
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x29a5f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8620
leaq 0x60(%rsp), %rdi
callq 0x2bef0
movq %rbx, %rdi
callq 0x8ab0
nop