// File: /workspace/github_cpp_makefile_star2_O3/davmac314[P]dinit/src/control.cc
// Function: control_conn_t::process_signal()
bool control_conn_t::process_signal()
{
    // packet contains signal number and process handle
    constexpr int pkt_size = 1 + sizeof(int) + sizeof(handle_t);
    if (rbuf.get_length() < pkt_size) {
        chklen = pkt_size;
        return true;
    }

    sig_num_t sig_num;
    rbuf.extract(&sig_num, 1, sizeof(sig_num));
    handle_t handle;
    rbuf.extract(&handle, 1 + sizeof(sig_num), sizeof(handle));
    rbuf.consume(pkt_size);
    chklen = 0;

    service_record *service = find_service_for_key(handle);
    if (service == nullptr) {
        char nak_rep[] = { (char)cp_rply::NAK };
        return queue_packet(nak_rep, 1);
    }

    // Reply:
    // 1 byte packet type = cp_rply::*

    pid_t spid = service->get_pid();
    // we probably don't want to kill/signal every process (in the current group),
    // but get_pid() sometimes returns -1 if e.g. service is not 'started'
    if (spid == -1 || spid == 0) {
        char nak_rep[] = { (char)cp_rply::SIGNAL_NOPID };
        return queue_packet(nak_rep, 1);
    }
    else {
        if (bp_sys::kill(spid, sig_num) != 0) {
            if (errno == EINVAL) {
                log(loglevel_t::ERROR, "Requested signal not in valid signal range.");
                char nak_rep[] = { (char)cp_rply::SIGNAL_BADSIG };
                return queue_packet(nak_rep, 1);
            }
            log(loglevel_t::ERROR, "Error sending signal to process: ", strerror(errno));
            char nak_rep[] = { (char)cp_rply::SIGNAL_KILLERR };
            return queue_packet(nak_rep, 1);
        }
    }
    char ack_rep[] = { (char)cp_rply::ACK };
    return queue_packet(ack_rep, 1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0x484(%rdi), %ebp
cmpl $0x8, %ebp
ja 0x2a8da
movl $0x9, 0x7c(%rbx)
movb $0x1, %al
jmp 0x2aa8d
leaq 0x80(%rbx), %r13
movl 0x480(%rbx), %r14d
leal 0x1(%r14), %eax
leal -0x3ff(%r14), %r15d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x2a93c
movl $0x400, %r12d           # imm = 0x400
subl %r15d, %r12d
movl %r15d, %esi
addq %r13, %rsi
movq %rsp, %rdi
movq %r12, %rdx
callq 0x84d0
leaq (%rsp,%r12), %rdi
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq %r13, %rsi
movq %r15, %rdx
callq 0x84d0
jmp 0x2a947
movl %r15d, %eax
movl (%r13,%rax), %eax
movl %eax, (%rsp)
leal 0x5(%r14), %eax
leal -0x3fb(%r14), %r15d
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r15d
leal -0x3fd(%r15), %eax
cmpl $0xfffffbfe, %eax       # imm = 0xFFFFFBFE
ja 0x2a9ae
movq %r13, %rax
movq %r13, 0x10(%rsp)
movl $0x400, %r13d           # imm = 0x400
subl %r15d, %r13d
movl %r15d, %esi
addq %rax, %rsi
leaq 0xc(%rsp), %r12
movq %r12, %rdi
movq %r13, %rdx
callq 0x84d0
leaq (%rsp,%r13), %rdi
addq $0xc, %rdi
addl $0xfffffc04, %r15d      # imm = 0xFFFFFC04
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x84d0
jmp 0x2a9b7
movl %r15d, %eax
addq %rax, %r13
movq %r13, %r12
movl (%r12), %esi
leal 0x9(%r14), %eax
addl $0xfffffc09, %r14d      # imm = 0xFFFFFC09
cmpl $0x400, %eax            # imm = 0x400
cmovbl %eax, %r14d
movl %r14d, 0x480(%rbx)
addl $-0x9, %ebp
movl %ebp, 0x484(%rbx)
movl $0x0, 0x7c(%rbx)
movq %rbx, %rdi
callq 0x2b906
testq %rax, %rax
je 0x2aa0e
movq (%rax), %rcx
movq %rax, %rdi
callq *0x48(%rcx)
leal 0x1(%rax), %ecx
cmpl $0x1, %ecx
ja 0x2aa18
leaq 0xa(%rsp), %rsi
movb $0x4a, (%rsi)
jmp 0x2aa80
leaq 0xb(%rsp), %rsi
movb $0x33, (%rsi)
jmp 0x2aa80
movl (%rsp), %esi
movl %eax, %edi
callq 0x88d0
testl %eax, %eax
je 0x2aa4d
callq 0x8050
movl (%rax), %edi
cmpl $0x16, %edi
jne 0x2aa57
leaq 0xba2b(%rip), %rsi      # 0x36464
movl $0x3, %edi
callq 0x2e33a
leaq 0x9(%rsp), %rsi
movb $0x4b, (%rsi)
jmp 0x2aa80
leaq 0x7(%rsp), %rsi
movb $0x32, (%rsi)
jmp 0x2aa80
callq 0x81d0
movq %rax, %r14
leaq 0xba2a(%rip), %rsi      # 0x36490
movl $0x3, %edi
callq 0x2e412
movq %r14, %rdi
callq 0x2e53c
leaq 0x8(%rsp), %rsi
movb $0x4c, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x28036
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq