// File: 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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movb %sil, %al
movq %rdi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x150(%rbp)
movl $0xa, -0x18(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0xa, %eax
jae 0x5a101
movq -0x150(%rbp), %rax
movl $0xa, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5a74f
movq -0x150(%rbp), %rdi
addq $0x80, %rdi
leaq -0x1c(%rbp), %rsi
movl $0x2, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0x150(%rbp), %rdi
addq $0x80, %rdi
leaq -0x20(%rbp), %rsi
movl $0x6, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0x150(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x5e970
movq -0x150(%rbp), %rdi
movq %rax, -0x28(%rbp)
movl -0x20(%rbp), %esi
callq 0x5e970
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5a181
cmpq $0x0, -0x30(%rbp)
je 0x5a181
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x5a1c0
movq -0x150(%rbp), %rdi
movb 0x1f2c0(%rip), %al      # 0x7944e
movb %al, -0x31(%rbp)
leaq -0x31(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a1ac
movb $0x0, -0x1(%rbp)
jmp 0x5a74f
movq -0x150(%rbp), %rax
movb $0x1, 0x78(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5a74f
movq -0x150(%rbp), %rdi
addq $0x80, %rdi
movl $0x1, %esi
callq 0x5e160
movsbl %al, %eax
movl %eax, -0x38(%rbp)
movl $0x3, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x2, -0x3c(%rbp)
movl -0x38(%rbp), %esi
leaq -0x44(%rbp), %rdi
callq 0x5c630
xorb $-0x1, %al
testb $0x1, %al
jne 0x5a207
jmp 0x5a23d
movq -0x150(%rbp), %rdi
movb 0x1f23b(%rip), %al      # 0x7944f
movb %al, -0x45(%rbp)
leaq -0x45(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a232
movb $0x0, -0x1(%rbp)
jmp 0x5a74f
movq -0x150(%rbp), %rax
movb $0x1, 0x78(%rax)
movl -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x5a2bf
movq -0x28(%rbp), %rdi
callq 0x2f0d0
cmpb $0x0, %al
je 0x5a2bd
movq -0x30(%rbp), %rdi
callq 0x2f0d0
cmpb $0x2, %al
je 0x5a2bd
movq -0x150(%rbp), %rdi
movb 0x1f1e0(%rip), %al      # 0x79450
movb %al, -0x4d(%rbp)
leaq -0x4d(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a28e
movb $0x0, -0x1(%rbp)
jmp 0x5a74f
movq -0x150(%rbp), %rdi
addq $0x80, %rdi
movl $0xa, %esi
callq 0x5e1a0
movq -0x150(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5a74f
jmp 0x5a2bf
leaq -0x88(%rbp), %rdi
callq 0x5f020
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x5f040
movq -0x158(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x5f060
jmp 0x5a2f0
jmp 0x5a2f2
leaq -0xa0(%rbp), %rdi
callq 0x5f0d0
xorb $-0x1, %al
testb $0x1, %al
jne 0x5a309
jmp 0x5a4ff
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x5f110
movq -0x168(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
callq 0x5f150
movq -0xb8(%rbp), %rdi
callq 0x30440
movq %rax, -0x160(%rbp)
jmp 0x5a347
movq -0x160(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x30240
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x30270
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x302a0
testb $0x1, %al
jne 0x5a397
jmp 0x5a4fa
leaq -0xc8(%rbp), %rdi
callq 0x302d0
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x3cda0
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x5a471
movq -0x150(%rbp), %rdi
movb 0x1f076(%rip), %al      # 0x79451
movb %al, -0xe1(%rbp)
leaq -0xe1(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a438
movb $0x0, -0x1(%rbp)
movl $0x1, -0xe8(%rbp)
jmp 0x5a737
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5f260
leaq -0x88(%rbp), %rdi
callq 0x5f2c0
jmp 0x5a75d
movq -0x150(%rbp), %rdi
addq $0x80, %rdi
movl $0xa, %esi
callq 0x5e1a0
movq -0x150(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0xe8(%rbp)
jmp 0x5a737
leaq -0x88(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x5f180
movb %dl, -0x171(%rbp)
movq %rax, -0x170(%rbp)
jmp 0x5a493
movb -0x171(%rbp), %al
movq -0x170(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movb %al, -0x100(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xf8(%rbp)
movb -0x100(%rbp), %al
movb %al, -0xf0(%rbp)
testb $0x1, -0xf0(%rbp)
je 0x5a4e7
leaq -0xa0(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x5f060
jmp 0x5a4e5
jmp 0x5a4e7
jmp 0x5a4e9
leaq -0xc8(%rbp), %rdi
callq 0x30330
jmp 0x5a37b
jmp 0x5a2f2
leaq -0x88(%rbp), %rdi
callq 0x5f1c0
leaq -0xa0(%rbp), %rdi
callq 0x5f1e0
movb $0x0, -0x109(%rbp)
movq $0x0, -0x118(%rbp)
movq -0x28(%rbp), %rdi
callq 0x30440
movq %rax, -0x180(%rbp)
jmp 0x5a53b
movq -0x180(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x30240
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x30270
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x302a0
testb $0x1, %al
jne 0x5a588
jmp 0x5a5f4
leaq -0x128(%rbp), %rdi
callq 0x302d0
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
callq 0x3cda0
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x5a5e1
movq -0x138(%rbp), %rax
movl 0x14(%rax), %eax
cmpl -0x4c(%rbp), %eax
jne 0x5a5e1
movb $0x1, -0x109(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x118(%rbp)
jmp 0x5a5f4
jmp 0x5a5e3
leaq -0x128(%rbp), %rdi
callq 0x30330
jmp 0x5a56f
testb $0x1, -0x109(%rbp)
jne 0x5a634
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x4c(%rbp), %edx
callq 0x5f200
movq %rax, -0x188(%rbp)
jmp 0x5a616
movq -0x150(%rbp), %rax
movq -0x188(%rbp), %rcx
movq %rcx, -0x118(%rbp)
movq 0x70(%rax), %rdi
callq 0x173c0
xorl %eax, %eax
testb $0x1, -0x11(%rbp)
movb %al, -0x189(%rbp)
je 0x5a67e
movb $0x2, -0x142(%rbp)
movb $0x1, -0x141(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2f0d0
movzbl %al, %esi
leaq -0x142(%rbp), %rdi
callq 0x5c690
movb %al, -0x18a(%rbp)
jmp 0x5a670
movb -0x18a(%rbp), %al
movb %al, -0x189(%rbp)
jmp 0x5a67e
movb -0x189(%rbp), %al
testb $0x1, %al
jne 0x5a68a
jmp 0x5a6cb
movq -0x150(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x4e280
testb $0x1, %al
jne 0x5a6c9
movq -0x118(%rbp), %rdi
callq 0x30420
movq %rax, %rdi
movq -0x118(%rbp), %rsi
callq 0x5ea40
movq -0x150(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x173c0
jmp 0x5a6cb
movq -0x150(%rbp), %rdi
movb 0x1ed7a(%rip), %al      # 0x79452
movb %al, -0x143(%rbp)
leaq -0x143(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a703
movb $0x0, -0x1(%rbp)
movl $0x1, -0xe8(%rbp)
jmp 0x5a737
movq -0x150(%rbp), %rdi
addq $0x80, %rdi
movl $0xa, %esi
callq 0x5e1a0
movq -0x150(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0xe8(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5f260
leaq -0x88(%rbp), %rdi
callq 0x5f2c0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0xa8(%rbp), %rdi
callq 0xacc0
nopl (%rax)