#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGCONT;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ wait_for_death(int /*<<< orphan*/, int /*<<< orphan*/, long);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void ensure_death(pid_t pid) {
  int status;
  switch (waitpid(pid, &status, WNOHANG)) {
  case -1:
    return;
  case 0:
    break;
  default:
    return;
  }
  (void)kill(pid, SIGTERM);
  if (wait_for_death(pid, 0, 500000L))
    return;
  (void)kill(pid, SIGKILL);
  (void)kill(pid, SIGCONT);
  (void)waitpid(pid, &status, 0);
}


x86 assembly (clang, -O0):
<ensure_death>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     1173 <ensure_death+0x43>
jmp    1163 <ensure_death+0x33>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     1178 <ensure_death+0x48>
jmp    117d <ensure_death+0x4d>
jmp    11e1 <ensure_death+0xb1>
jmp    1182 <ensure_death+0x52>
jmp    11e1 <ensure_death+0xb1>
mov    -0x4(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
mov    $0x7a120,%edx
call   1030 <wait_for_death@plt>
cmp    $0x0,%rax
je     11b1 <ensure_death+0x81>
jmp    11e1 <ensure_death+0xb1>
mov    -0x4(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
mov    -0x4(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_death>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e9a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
call   1050 <waitpid@plt>
test   %eax,%eax
jne    119f <ensure_death+0x6f>
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <kill@plt>
mov    $0x7a120,%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <wait_for_death@plt>
test   %rax,%rax
jne    119f <ensure_death+0x6f>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <kill@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <kill@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@SIGKILL = global i32 0
@SIGCONT = global i32 0
define void @ensure_death(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* @WNOHANG
%6 = call i32 @waitpid(i32 %4, i32* %3, i32 %5)
switch i32 %6, label %9 [
i32 -1, label %7
i32 0, label %8
]
7: ; preds = %1
br label %27
8: ; preds = %1
br label %10
9: ; preds = %1
br label %27
10: ; preds = %8
%11 = load i32, i32* %2
%12 = load i32, i32* @SIGTERM
%13 = call i32 @kill(i32 %11, i32 %12)
%14 = load i32, i32* %2
%15 = call i64 @wait_for_death(i32 %14, i32 0, i64 500000)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %10
br label %27
18: ; preds = %10
%19 = load i32, i32* %2
%20 = load i32, i32* @SIGKILL
%21 = call i32 @kill(i32 %19, i32 %20)
%22 = load i32, i32* %2
%23 = load i32, i32* @SIGCONT
%24 = call i32 @kill(i32 %22, i32 %23)
%25 = load i32, i32* %2
%26 = call i32 @waitpid(i32 %25, i32* %3, i32 0)
br label %27
27: ; preds = %18, %17, %9, %7
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@SIGKILL = global i32 0
@SIGCONT = global i32 0
define void @ensure_death(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @WNOHANG
%5 = call i32 @waitpid(i32 %0, i32* %2, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %1
%8 = load i32, i32* @SIGTERM
%9 = call i32 @kill(i32 %0, i32 %8)
%10 = call i64 @wait_for_death(i32 %0, i32 0, i64 500000)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = load i32, i32* @SIGKILL
%14 = call i32 @kill(i32 %0, i32 %13)
%15 = load i32, i32* @SIGCONT
%16 = call i32 @kill(i32 %0, i32 %15)
%17 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
br label %18
18: ; preds = %1, %7, %12
ret void
}