// File: davmac314[P]dinit/src/dinitctl.cc
// Function: signal_send(int, cpbuffer<1024u>&, char const*, int)
static int signal_send(int socknum, cpbuffer_t &rbuffer, const char *service_name, sig_num_t sig_num)
{
    using namespace std;

    handle_t handle;

    if (!load_service(socknum, rbuffer, service_name, &handle, nullptr, true)) {
        return 1;
    }

    // Issue SIGNAL command.
    auto m = membuf()
            .append((char)cp_cmd::SIGNAL)
            .append(sig_num)
            .append(handle);
    write_all_x(socknum, m);

    wait_for_reply(rbuffer, socknum);
    cp_rply reply_pkt_h = (cp_rply)rbuffer[0];
    if (reply_pkt_h == cp_rply::NAK) {
        cerr << "dinitctl: cannot send signal to service.\n";
        return 1;
    }
    if (reply_pkt_h == cp_rply::SIGNAL_NOPID) {
        cerr << "dinitctl: could not get vaild PID of service; service is not process, "
        "service in wrong state." << endl;
        return 1;
    }
    if (reply_pkt_h == cp_rply::SIGNAL_BADSIG) {
        cerr << "dinitctl: provided signal was invalid.\n";
        return 1;
    }
    if (reply_pkt_h == cp_rply::SIGNAL_KILLERR) {
        cerr << "dinitctl: failed sending signal to service.\n";
        return 1;
    }
    if (reply_pkt_h != cp_rply::ACK) {
        cerr << "dinitctl: protocol error.\n";
        return 1;
    }
    rbuffer.consume(1);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
movl $0x1, %r9d
callq 0xc5e0
testb $0x1, %al
jne 0xb975
movl $0x1, -0x4(%rbp)
jmp 0xbabb
movb $0x15, -0x31(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x31(%rbp), %rsi
callq 0x11b60
movb %al, -0x2f(%rbp)
leaq -0x2f(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x1cb60
movl %eax, %ecx
movl %ecx, -0x2e(%rbp)
shrq $0x20, %rax
movb %al, -0x2a(%rbp)
leaq -0x2e(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1cba0
movb %dl, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x29(%rbp)
movb -0x40(%rbp), %al
movb %al, -0x21(%rbp)
movl -0x8(%rbp), %edi
leaq -0x29(%rbp), %rsi
callq 0x1cbe0
movq -0x10(%rbp), %rdi
movl -0x8(%rbp), %esi
callq 0x11350
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x113e0
movb %al, -0x49(%rbp)
cmpb $0x33, -0x49(%rbp)
jne 0xba0f
movq 0x265f1(%rip), %rdi     # 0x31fe8
leaq 0x197b1(%rip), %rsi     # 0x251af
callq 0x53d0
movl $0x1, -0x4(%rbp)
jmp 0xbabb
cmpb $0x4a, -0x49(%rbp)
jne 0xba40
movq 0x265cc(%rip), %rdi     # 0x31fe8
leaq 0x197b6(%rip), %rsi     # 0x251d9
callq 0x53d0
movq %rax, %rdi
movq 0x26536(%rip), %rsi     # 0x31f68
callq 0x5410
movl $0x1, -0x4(%rbp)
jmp 0xbabb
cmpb $0x4b, -0x49(%rbp)
jne 0xba62
movq 0x2659b(%rip), %rdi     # 0x31fe8
leaq 0x197e3(%rip), %rsi     # 0x25237
callq 0x53d0
movl $0x1, -0x4(%rbp)
jmp 0xbabb
cmpb $0x4c, -0x49(%rbp)
jne 0xba84
movq 0x26579(%rip), %rdi     # 0x31fe8
leaq 0x197e9(%rip), %rsi     # 0x2525f
callq 0x53d0
movl $0x1, -0x4(%rbp)
jmp 0xbabb
cmpb $0x32, -0x49(%rbp)
je 0xbaa6
movq 0x26557(%rip), %rdi     # 0x31fe8
leaq 0x196fc(%rip), %rsi     # 0x25194
callq 0x53d0
movl $0x1, -0x4(%rbp)
jmp 0xbabb
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x115f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)