// File: davmac314[P]dinit/src/control.cc
// Function: control_conn_t::rm_service_dep()
bool control_conn_t::rm_service_dep()
{
    // 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);

    // Remove dependency:
    bool did_remove = from_service->rm_dep(to_service, dep_type);
    services->process_queues();

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x58(%rbp)
movl $0xa, -0x14(%rbp)
addq $0x80, %rdi
callq 0x5e200
cmpl $0xa, %eax
jae 0x5a7b0
movq -0x58(%rbp), %rax
movl $0xa, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5a95a
movq -0x58(%rbp), %rdi
addq $0x80, %rdi
leaq -0x18(%rbp), %rsi
movl $0x2, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0x58(%rbp), %rdi
addq $0x80, %rdi
leaq -0x1c(%rbp), %rsi
movl $0x6, %edx
movl $0x4, %ecx
callq 0x5e220
movq -0x58(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x5e970
movq -0x58(%rbp), %rdi
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %esi
callq 0x5e970
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5a824
cmpq $0x0, -0x30(%rbp)
je 0x5a824
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0x5a85d
movq -0x58(%rbp), %rdi
movb 0x1ec25(%rip), %al      # 0x79453
movb %al, -0x31(%rbp)
leaq -0x31(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a84c
movb $0x0, -0x1(%rbp)
jmp 0x5a95a
movq -0x58(%rbp), %rax
movb $0x1, 0x78(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x5a95a
movq -0x58(%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 0x5a8a1
jmp 0x5a8d1
movq -0x58(%rbp), %rdi
movb 0x1eba9(%rip), %al      # 0x79454
movb %al, -0x45(%rbp)
leaq -0x45(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a8c9
movb $0x0, -0x1(%rbp)
jmp 0x5a95a
movq -0x58(%rbp), %rax
movb $0x1, 0x78(%rax)
movl -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x4c(%rbp), %edx
callq 0x5f2e0
movb %al, %cl
movq -0x58(%rbp), %rax
andb $0x1, %cl
movb %cl, -0x4d(%rbp)
movq 0x70(%rax), %rdi
callq 0x173c0
movb -0x4d(%rbp), %cl
movb $0x32, %al
movb $0x33, %dl
movb %dl, -0x5a(%rbp)
testb $0x1, %cl
movb %al, -0x59(%rbp)
jne 0x5a914
movb -0x5a(%rbp), %al
movb %al, -0x59(%rbp)
movq -0x58(%rbp), %rdi
movb -0x59(%rbp), %al
movb %al, -0x4e(%rbp)
leaq -0x4e(%rbp), %rsi
movl $0x1, %edx
callq 0x58340
testb $0x1, %al
jne 0x5a936
movb $0x0, -0x1(%rbp)
jmp 0x5a95a
movq -0x58(%rbp), %rdi
addq $0x80, %rdi
movl $0xa, %esi
callq 0x5e1a0
movq -0x58(%rbp), %rax
movl $0x0, 0x7c(%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)