// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/dinitctl.cc
// Function: add_remove_dependency(int, cpbuffer<1024u>&, bool, char const*, char const*, dependency_type, bool)
static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
        const char *service_from, const char *service_to, dependency_type dep_type, bool verbose)
{
    using std::cout;
    using std::cerr;

    handle_t from_handle;
    handle_t to_handle;

    if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr)
            || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) {
        return 1;
    }

    if (from_handle == to_handle) {
        cerr << "dinitctl: can not add/remove a dependency from a service to itself\n";
        return 1;
    }

    auto m = membuf()
            .append<char>(add ? (char)cp_cmd::ADD_DEP : (char)cp_cmd::REM_DEP)
            .append(static_cast<char>(dep_type))
            .append(from_handle)
            .append(to_handle);
    write_all_x(socknum, m);

    wait_for_reply(rbuffer, socknum);

    // check reply
    if (rbuffer[0] == (char)cp_rply::NAK) {
        if (add) {
            cerr << "dinitctl: could not add dependency: circular dependency or wrong state\n";
        }
        else {
            cerr << "dinitctl: no such dependency to remove\n";
        }
        return 1;
    }
    if (rbuffer[0] != (char)cp_rply::ACK) {
        cerr << "dinitctl: control socket protocol error\n";
        return 1;
    }

    if (verbose) {
        std::cout << "Service '" << service_from << "': dependency '" << service_to << "' "
                << (add ? "added" : "removed") << "\n";
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebx
movq %r8, %r15
movq %rcx, %rax
movl %edx, %r14d
movq %rsi, %r12
movl %edi, %ebp
leaq 0x14(%rsp), %rcx
movl $0x1, %r13d
movq %rax, 0x20(%rsp)
movq %rax, %rdx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xd2a8
testb %al, %al
je 0xb837
leaq 0x10(%rsp), %rcx
movl $0x1, %r13d
movl %ebp, %edi
movq %r12, %rsi
movq %r15, 0x18(%rsp)
movq %r15, %rdx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xd2a8
testb %al, %al
je 0xb837
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %eax
cmpl %eax, %ecx
jne 0xb64e
movq 0x119b0(%rip), %rdi     # 0x1cfe8
leaq 0xb2b0(%rip), %rsi      # 0x168ef
movl $0x43, %edx
callq 0x4330
jmp 0xb837
movb 0x70(%rsp), %r15b
shll $0x8, %ebx
movl %r14d, 0xc(%rsp)
movzbl %r14b, %edx
subl %edx, %ebx
addl $0xc, %ebx
movl %ecx, %edx
shrl $0x10, %edx
shlq $0x10, %rcx
movzwl %bx, %esi
orq %rcx, %rsi
leaq 0x28(%rsp), %r13
movw %dx, 0x4(%r13)
movl %esi, (%r13)
movl %eax, 0x6(%r13)
movq (%r13), %rcx
shrl $0x10, %eax
movq %rcx, (%r13)
movw %ax, 0x8(%r13)
movl $0xa, %r14d
xorl %ebx, %ebx
movl %r14d, %edx
movl %ebp, %edi
movq %r13, %rsi
callq 0x4100
cmpl $-0x1, %eax
je 0xb6bc
addl %eax, %ebx
movslq %eax, %rcx
addq %rcx, %r13
subl %eax, %r14d
jmp 0xb6ca
callq 0x4040
cmpl $0x4, (%rax)
jne 0xb7f1
testl %r14d, %r14d
jg 0xb69d
cmpl $-0x1, %ebx
je 0xb7f9
movq %r12, %rdi
movl %ebp, %esi
callq 0xed47
movl 0x400(%r12), %eax
leal -0x400(%rax), %ecx
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %ecx
movzbl (%r12,%rcx), %eax
cmpl $0x32, %eax
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %r14
je 0xb735
movq 0x118d6(%rip), %rdi     # 0x1cfe8
cmpl $0x33, %eax
jne 0xb7e3
testb %bpl, %bpl
je 0xb820
leaq 0xb208(%rip), %rsi      # 0x16933
movl $0x47, %edx
jmp 0xb82c
xorl %r13d, %r13d
testb %r15b, %r15b
je 0xb837
movq 0x11860(%rip), %rbx     # 0x1cfa8
leaq 0xa92e(%rip), %rsi      # 0x1607d
movl $0x9, %edx
movq %rbx, %rdi
callq 0x4330
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x42d0
movq %rax, %rbx
leaq 0xb230(%rip), %rsi      # 0x169a3
movl $0xf, %edx
movq %rax, %rdi
callq 0x4330
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42d0
movq %rax, %rbx
leaq 0xabad(%rip), %rsi      # 0x16342
movl $0x2, %edx
movq %rax, %rdi
callq 0x4330
leaq 0xb20a(%rip), %rax      # 0x169b3
leaq 0xb209(%rip), %rsi      # 0x169b9
testb %bpl, %bpl
cmovneq %rax, %rsi
xorb $0x1, %bpl
movzbl %bpl, %eax
leaq 0x5(,%rax,2), %rdx
movq %rbx, %rdi
callq 0x4330
leaq 0xb32a(%rip), %rsi      # 0x16b00
movl $0x1, %edx
movq %rbx, %rdi
jmp 0xb644
leaq 0xaf3f(%rip), %rsi      # 0x16729
movl $0x28, %edx
jmp 0xb82c
testl %ebx, %ebx
jg 0xb6d8
movl $0x4, %edi
callq 0x4190
movq %rax, %rbx
callq 0x4040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x1133a(%rip), %rsi     # 0x1cb50
movq %rbx, %rdi
xorl %edx, %edx
callq 0x44d0
leaq 0xb154(%rip), %rsi      # 0x1697b
movl $0x27, %edx
callq 0x4330
movl $0x1, %r13d
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq