// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rdi, %rbp
cmpl $0x9, 0x484(%rdi)
ja 0x2962d
movl $0xa, 0x7c(%rbp)
movb $0x1, %al
jmp 0x29b1b
movl %esi, %ebx
movl 0x480(%rbp), %r15d
leal 0x2(%r15), %eax
addl $0xfffffc02, %r15d      # imm = 0xFFFFFC02
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leaq 0x80(%rbp), %r14
leal -0x3fd(%r15), %eax
movl %r15d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x29698
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
addq %r14, %rsi
leaq 0x34(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x84d0
addq %r13, %r12
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x84d0
jmp 0x296a0
movl (%r14,%rsi), %eax
movl %eax, 0x34(%rsp)
movl 0x480(%rbp), %r15d
leal 0x6(%r15), %eax
addl $0xfffffc06, %r15d      # imm = 0xFFFFFC06
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
movl %r15d, %esi
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x29702
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
addq %r14, %rsi
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x84d0
addq %r13, %r12
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x84d0
jmp 0x2970a
movl (%r14,%rsi), %eax
movl %eax, 0x30(%rsp)
movl 0x34(%rsp), %esi
movq %rbp, %rdi
callq 0x2ba66
movq %rax, %r14
movl 0x30(%rsp), %esi
movq %rbp, %rdi
callq 0x2ba66
movq %rax, 0x28(%rsp)
cmpq %rax, %r14
je 0x29780
testq %r14, %r14
je 0x29780
testq %rax, %rax
je 0x29780
movl %ebx, 0x4c(%rsp)
movl 0x480(%rbp), %eax
leal 0x1(%rax), %ecx
addl $0xfffffc01, %eax       # imm = 0xFFFFFC01
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movzbl 0x80(%rbp,%rax), %ebx
leaq 0x5c(%rsp), %rax
movq $0x3, -0xc(%rax)
movl $0x2, -0x4(%rax)
testl %ebx, %ebx
je 0x297a6
cmpl $0x3, %ebx
jne 0x297ad
leaq 0x50(%rsp), %rcx
jmp 0x297b9
leaq 0x50(%rsp), %rsi
movb $0x34, (%rsi)
movq %rbp, %rdi
movl $0x1, %edx
callq 0x27fd2
testb %al, %al
je 0x29b1b
movb $0x1, 0x78(%rbp)
jmp 0x29b1b
leaq 0x54(%rsp), %rcx
jmp 0x297b9
leaq 0x58(%rsp), %rcx
cmpb $0x2, %bl
cmovneq %rax, %rcx
cmpq %rax, %rcx
jne 0x297df
leaq 0x50(%rsp), %rsi
movb $0x34, (%rsi)
movq %rbp, %rdi
movl $0x1, %edx
callq 0x27fd2
testb %al, %al
je 0x29b19
movb $0x1, 0x78(%rbp)
testb %bl, %bl
je 0x29834
leaq 0x80(%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)
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x29894
movq %rbp, %r12
movq 0x28(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x298a6
cmpb $0x0, 0x2c(%r14)
je 0x297e3
movq 0x28(%rsp), %rax
cmpb $0x2, 0x2c(%rax)
je 0x297e3
leaq 0x50(%rsp), %rsi
movb $0x33, (%rsi)
movq %rbp, %rdi
movl $0x1, %edx
callq 0x27fd2
testb %al, %al
je 0x29b1b
movl 0x480(%rbp), %edx
leal 0xa(%rdx), %ecx
addl $0xfffffc0a, %edx       # imm = 0xFFFFFC0A
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %edx
movl %edx, 0x480(%rbp)
addl $-0xa, 0x484(%rbp)
movl $0x0, 0x7c(%rbp)
jmp 0x29b1b
movq %rbp, %r12
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x2d40e
movsbl %bl, %eax
movl %eax, 0x48(%rsp)
movl $0x0, 0xc(%rsp)
leaq 0x50(%rsp), %rbp
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x299ae
movq -0x8(%rax), %r15
movl $0x118, %ecx            # imm = 0x118
addq %rcx, %r15
addq $-0x8, %rax
movq %rax, 0x18(%rsp)
movq %r15, %rbx
movq (%rbx), %rbx
cmpq %r15, %rbx
je 0x299a0
movq 0x18(%rbx), %r13
movq %r13, 0x40(%rsp)
cmpq %r14, %r13
je 0x29934
movq %rbp, 0x38(%rsp)
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x2d54a
testb $0x1, %dl
je 0x29997
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x29988
movq 0x40(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x29997
movb $0x33, 0x38(%rsp)
movq %r12, %rdi
leaq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x27fd2
movl %eax, 0xc(%rsp)
testb %al, %al
je 0x29997
movl 0x480(%r12), %eax
leal 0xa(%rax), %ecx
addl $0xfffffc0a, %eax       # imm = 0xFFFFFC0A
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl %eax, 0x480(%r12)
addl $-0xa, 0x484(%r12)
movl $0x0, 0x7c(%r12)
jmp 0x29997
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x2d40e
cmpq %r14, %r13
jne 0x298e2
cmpq %r15, %rbx
je 0x298ba
jmp 0x29af2
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x299cd
movq (%rdi), %rbx
movl $0x10, %esi
callq 0x8620
movq %rbx, %rdi
testq %rbx, %rbx
jne 0x299b8
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x8350
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rax
movups %xmm0, (%rax)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movl 0x48(%rsp), %edx
je 0x29a02
movq %rax, 0x18(%rsp)
movq %r14, %rcx
addq $0x118, %rcx            # imm = 0x118
movq 0x28(%rsp), %r11
xorl %eax, %eax
movl $0x1, %esi
movq %rcx, %rdi
xorl %r15d, %r15d
movl 0x4c(%rsp), %ebx
movq (%rdi), %rdi
cmpq %rcx, %rdi
je 0x29a4c
cmpq %r11, 0x18(%rdi)
setne %r8b
cmpl %edx, 0x24(%rdi)
leaq 0x10(%rdi), %r9
setne %r10b
orb %r8b, %r10b
movzbl %al, %eax
cmovel %esi, %eax
cmoveq %r9, %r15
jne 0x29a22
testb $0x1, %al
jne 0x29a6a
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x1f5c4
movq %rax, %r15
movq 0x70(%r12), %rdi
callq 0xd8da
testb %bl, %bl
je 0x29aa2
movb 0x2c(%r14), %al
decb %al
cmpb $0x1, %al
ja 0x29aa2
movq 0x70(%r12), %rax
cmpb $0x1, 0x28(%rax)
jne 0x29aa2
cmpb $0x0, 0x11(%r15)
jne 0x29a98
movq 0x8(%r15), %rdi
callq 0x22962
movb $0x1, 0x11(%r15)
movq 0x70(%r12), %rdi
callq 0xd8da
leaq 0x38(%rsp), %rsi
movb $0x32, (%rsi)
movq %r12, %rdi
movl $0x1, %edx
callq 0x27fd2
movl %eax, 0xc(%rsp)
testb %al, %al
je 0x29af2
movl 0x480(%r12), %eax
leal 0xa(%rax), %ecx
addl $0xfffffc0a, %eax       # imm = 0xFFFFFC0A
cmpl $0x400, %ecx            # imm = 0x400
cmovbl %ecx, %eax
movl %eax, 0x480(%r12)
addl $-0xa, 0x484(%r12)
movl $0x0, 0x7c(%r12)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x29b09
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8620
leaq 0x50(%rsp), %rdi
callq 0x2c0d6
movl 0xc(%rsp), %eax
jmp 0x29b1b
xorl %eax, %eax
andb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29b35
jmp 0x29b35
jmp 0x29b35
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x29b4f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x8620
leaq 0x50(%rsp), %rdi
callq 0x2c0d6
movq %rbx, %rdi
callq 0x8ab0
nop