// File: davmac314[P]dinit/src/proc-service.cc
// Function: process_service::bring_down()
void process_service::bring_down() noexcept
{
    if (stop_pid != -1 || stop_issued) {
        // waiting for stop command to complete (or for process to die after it has complete);
        // can't do anything here.
        return;
    }
    if (waiting_for_execstat) {
        // The process is still starting. This should be uncommon, but can occur during
        // smooth recovery (or it may mean the stop command process is still starting). We can't
        // do much now; we have to wait until we get the status, and then act appropriately.
        return;
    }
    else if (pid != -1) {
        // The process is still kicking on - must actually kill it.
        if (!stop_command.empty() && !stop_issued) {
            if (start_stop_process(stop_arg_parts)) {
                goto arm_timer;
            }

            // stop-command failed, need to try something else:
            if (term_signal != 0) {
                kill_pg(term_signal);
            }
            else {
                kill_pg(SIGKILL);
            }
        }
        else if (term_signal != 0) {
            // We signal the process group (-pid) rather than just the process as there's less
            // risk then of creating an orphaned process group:
            kill_pg(term_signal);
        }

        if (stop_pid == -1 && !tracking_child) {
            // If we have no way of tracking when the child terminates, assume stopped now
            stopped();
            return;
        }

        arm_timer:

        stop_issued = true; // (don't try again)

        // If there's a stop timeout, arm the timer now:
        if (stop_timeout != time_val(0,0)) {
            process_timer.arm_timer_rel(event_loop, stop_timeout);
            waiting_stopstart_timer = true;
        }

        // The rest is done in handle_exit_status.
    }
    else {
        // The process is already dead (possibly, we are in smooth recovery waiting for timer)
        doing_smooth_recovery = false;
        if (waiting_restart_timer) {
            process_timer.stop_timer(event_loop);
            waiting_restart_timer = false;
        }
        stopped();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $-0x1, 0x504(%rax)
jne 0x515ef
movq -0x30(%rbp), %rax
movb 0x500(%rax), %al
shrb %al
andb $0x1, %al
testb $0x1, %al
je 0x515f4
jmp 0x517c1
movq -0x30(%rbp), %rax
movl 0x10c(%rax), %eax
shrl $0x7, %eax
andl $0x1, %eax
testl $0x1, %eax
je 0x51610
jmp 0x517c1
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x4d8(%rax)
je 0x51772
movq -0x30(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0x4b180
testb $0x1, %al
jne 0x51690
movq -0x30(%rbp), %rax
movb 0x500(%rax), %al
shrb %al
andb $0x1, %al
testb $0x1, %al
jne 0x51690
movq -0x30(%rbp), %rdi
movq %rdi, %rsi
addq $0x240, %rsi            # imm = 0x240
callq 0x517d0
testb $0x1, %al
jne 0x51660
jmp 0x51662
jmp 0x516de
movq -0x30(%rbp), %rax
cmpl $0x0, 0x194(%rax)
je 0x51680
movq -0x30(%rbp), %rdi
movl 0x194(%rdi), %esi
callq 0x56c90
jmp 0x5168e
movq -0x30(%rbp), %rdi
movl $0x9, %esi
callq 0x56c90
jmp 0x516ae
movq -0x30(%rbp), %rax
cmpl $0x0, 0x194(%rax)
je 0x516ac
movq -0x30(%rbp), %rdi
movl 0x194(%rdi), %esi
callq 0x56c90
jmp 0x516ae
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x504(%rax)
jne 0x516dc
movq -0x30(%rbp), %rax
movb 0x4f4(%rax), %al
shrb $0x3, %al
andb $0x1, %al
testb $0x1, %al
jne 0x516dc
movq -0x30(%rbp), %rdi
callq 0x4bb20
jmp 0x517c1
jmp 0x516de
movq -0x30(%rbp), %rax
movb 0x500(%rax), %cl
andb $-0x3, %cl
orb $0x2, %cl
movb %cl, 0x500(%rax)
addq $0x488, %rax            # imm = 0x488
movq %rax, -0x38(%rbp)
leaq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x1c100
movq -0x38(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x52e50
testb $0x1, %al
jne 0x51721
jmp 0x51770
movq -0x30(%rbp), %rdi
movq %rdi, %rax
addq $0x3b0, %rax            # imm = 0x3B0
movq %rax, -0x40(%rbp)
addq $0x488, %rdi            # imm = 0x488
callq 0x153b0
movq -0x40(%rbp), %rdi
movq %rax, -0x28(%rbp)
movq %rdx, -0x20(%rbp)
leaq 0x5740f(%rip), %rsi     # 0xa8b60
leaq -0x28(%rbp), %rdx
callq 0x18220
movq -0x30(%rbp), %rax
movb 0x4f4(%rax), %cl
andb $-0x3, %cl
orb $0x2, %cl
movb %cl, 0x4f4(%rax)
jmp 0x517bf
movq -0x30(%rbp), %rax
movb $0x0, 0x5b8(%rax)
movb 0x4f4(%rax), %al
andb $0x1, %al
testb $0x1, %al
je 0x517b6
movq -0x30(%rbp), %rdi
addq $0x3b0, %rdi            # imm = 0x3B0
leaq 0x573c5(%rip), %rsi     # 0xa8b60
callq 0x529f0
movq -0x30(%rbp), %rax
movb 0x4f4(%rax), %cl
andb $-0x2, %cl
orb $0x0, %cl
movb %cl, 0x4f4(%rax)
movq -0x30(%rbp), %rdi
callq 0x4bb20
jmp 0x517c1
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)