// File: /workspace/github_cpp_makefile_star2_O1/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 0xd198
testb %al, %al
je 0xb68b
leaq 0x10(%rsp), %rcx
movl $0x1, %r13d
movl %ebp, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0xd198
testb %al, %al
je 0xb68b
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %eax
cmpl %eax, %ecx
jne 0xb4af
movq 0x11b4f(%rip), %rdi     # 0x1cfe8
leaq 0xb464(%rip), %rsi      # 0x16904
movl $0x43, %edx
callq 0x4330
jmp 0xb68b
movq %r15, 0x18(%rsp)
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), %rax
movzwl 0x8(%r13), %ecx
movq %rax, (%r13)
movw %cx, 0x8(%r13)
movl $0xa, %r14d
xorl %ebx, %ebx
movl %r14d, %edx
movl %ebp, %edi
movq %r13, %rsi
callq 0x4100
cmpl $-0x1, %eax
je 0xb521
addl %eax, %ebx
movslq %eax, %rcx
addq %rcx, %r13
subl %eax, %r14d
xorl %ecx, %ecx
jmp 0xb541
callq 0x4040
movl $0x2, %ecx
cmpl $0x4, (%rax)
je 0xb541
testl %ebx, %ebx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmovgl %ebx, %r15d
movl $0x1, %ecx
testb $0x1, %cl
jne 0xb54d
testl %r14d, %r14d
jg 0xb500
jmp 0xb550
movl %r15d, %ebx
cmpl $-0x1, %ebx
je 0xb69d
movq %r12, %rdi
movl %ebp, %esi
callq 0xedbd
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 0xb5b6
movq 0x11a55(%rip), %rdi     # 0x1cfe8
cmpl $0x33, %eax
jne 0xb666
testb %bpl, %bpl
je 0xb674
leaq 0xb39c(%rip), %rsi      # 0x16948
movl $0x47, %edx
jmp 0xb680
xorl %r13d, %r13d
cmpb $0x0, 0x70(%rsp)
je 0xb68b
movq 0x119dd(%rip), %rbx     # 0x1cfa8
leaq 0xaac0(%rip), %rsi      # 0x16092
movl $0x9, %edx
movq %rbx, %rdi
callq 0x4330
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x42d0
movq %rax, %rbx
leaq 0xb3c2(%rip), %rsi      # 0x169b8
movl $0xf, %edx
movq %rax, %rdi
callq 0x4330
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42d0
movq %rax, %rbx
leaq 0xad3f(%rip), %rsi      # 0x16357
movl $0x2, %edx
movq %rax, %rdi
callq 0x4330
leaq 0xb39c(%rip), %rax      # 0x169c8
leaq 0xb39b(%rip), %rsi      # 0x169ce
testb %bpl, %bpl
cmovneq %rax, %rsi
xorb $0x1, %bpl
movzbl %bpl, %eax
leaq 0x5(,%rax,2), %rdx
movq %rbx, %rdi
callq 0x4330
leaq 0xb4bc(%rip), %rsi      # 0x16b15
movl $0x1, %edx
movq %rbx, %rdi
jmp 0xb4a5
leaq 0xb0d1(%rip), %rsi      # 0x1673e
movl $0x28, %edx
jmp 0xb680
leaq 0xb315(%rip), %rsi      # 0x16990
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
movl $0x4, %edi
callq 0x4190
movq %rax, %rbx
callq 0x4040
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x11496(%rip), %rsi     # 0x1cb50
movq %rbx, %rdi
xorl %edx, %edx
callq 0x44d0