#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*/ sigset_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ do_log(int /*<<< orphan*/, char *, int);
int killpg(int, int /*<<< orphan*/);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int *, int /*<<< orphan*/);
void kill_script(pid_t pid) {
  pid_t ret;
  int status;
  sigset_t mask, origmask;
  if (pid == -1)
    return;
  assert(pid > 0);
  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  sigprocmask(SIG_BLOCK, &mask, &origmask);
  if (killpg(pid, SIGTERM) == -1) {
    do_log(LOG_ERR, "Can't kill script pgrp %d: %m", pid);
    goto done;
  }
  sleep(1);
  ret = waitpid(pid, &status, WNOHANG);
  if (ret == -1) {
    do_log(LOG_ERR, "Failed to wait for %d: %m?!", pid);
    goto done;
  } else if (ret == 0) {
    if (killpg(pid, SIGKILL) == -1) {
      do_log(LOG_ERR, "2nd kill %d failed: %m?!", pid);
      goto done;
    }
    ret = waitpid(pid, &status, 0);
  }
  assert(ret == pid);
done:
  sigprocmask(SIG_SETMASK, &origmask, NULL);
}


x86 assembly (clang, -O0):
<kill_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    119a <kill_script+0x1a>
jmp    12dc <kill_script+0x15c>
cmpl   $0x0,-0x4(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x10(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   10a0 <sigaddset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    -0x4(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1070 <killpg@plt>
cmp    $0xffffffff,%eax
jne    1213 <kill_script+0x93>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xdf7(%rip),%rsi
call   1040 <do_log@plt>
jmp    12c6 <kill_script+0x146>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1080 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    125c <kill_script+0xdc>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xdcc(%rip),%rsi
call   1040 <do_log@plt>
jmp    12c6 <kill_script+0x146>
cmpl   $0x0,-0x8(%rbp)
jne    12ae <kill_script+0x12e>
mov    -0x4(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
call   1070 <killpg@plt>
cmp    $0xffffffff,%eax
jne    129d <kill_script+0x11d>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xda7(%rip),%rsi
call   1040 <do_log@plt>
jmp    12c6 <kill_script+0x146>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,-0x8(%rbp)
jmp    12b3 <kill_script+0x133>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kill_script>:
cmp    $0xffffffff,%edi
je     1287 <kill_script+0x107>
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1050 <assert@plt>
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1060 <sigemptyset@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <sigaddset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %r14,%rsi
call   1030 <sigprocmask@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <killpg@plt>
cmp    $0xffffffff,%eax
je     1222 <kill_script+0xa2>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1080 <waitpid@plt>
test   %eax,%eax
je     123b <kill_script+0xbb>
cmp    $0xffffffff,%eax
jne    125e <kill_script+0xde>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
jmp    1232 <kill_script+0xb2>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
mov    %ebx,%edx
call   1040 <do_log@plt>
jmp    126b <kill_script+0xeb>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <killpg@plt>
cmp    $0xffffffff,%eax
je     1288 <kill_script+0x108>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
jmp    1232 <kill_script+0xb2>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@SIGTERM = global i32 0
@LOG_ERR = global i32 0
@.str = [30 x i8] c"Can't kill script pgrp %d: %m\00"
@WNOHANG = global i32 0
@.str.1 = [28 x i8] c"Failed to wait for %d: %m?!\00"
@SIGKILL = global i32 0
@.str.2 = [25 x i8] c"2nd kill %d failed: %m?!\00"
@SIG_SETMASK = global i32 0
define void @kill_script(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
br label %64
10: ; preds = %1
%11 = load i32, i32* %2
%12 = icmp sgt i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32 @sigemptyset(i32* %5)
%16 = load i32, i32* @SIGCHLD
%17 = call i32 @sigaddset(i32* %5, i32 %16)
%18 = load i32, i32* @SIG_BLOCK
%19 = call i32 @sigprocmask(i32 %18, i32* %5, i32* %6)
%20 = load i32, i32* %2
%21 = load i32, i32* @SIGTERM
%22 = call i32 @killpg(i32 %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %10
%25 = load i32, i32* @LOG_ERR
%26 = load i32, i32* %2
%27 = call i32 @do_log(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %61
28: ; preds = %10
%29 = call i32 @sleep(i32 1)
%30 = load i32, i32* %2
%31 = load i32, i32* @WNOHANG
%32 = call i32 @waitpid(i32 %30, i32* %4, i32 %31)
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @LOG_ERR
%37 = load i32, i32* %2
%38 = call i32 @do_log(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %61
39: ; preds = %28
%40 = load i32, i32* %3
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %39
%43 = load i32, i32* %2
%44 = load i32, i32* @SIGKILL
%45 = call i32 @killpg(i32 %43, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @LOG_ERR
%49 = load i32, i32* %2
%50 = call i32 @do_log(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %61
51: ; preds = %42
%52 = load i32, i32* %2
%53 = call i32 @waitpid(i32 %52, i32* %4, i32 0)
store i32 %53, i32* %3
br label %54
54: ; preds = %51, %39
br label %55
55: ; preds = %54
%56 = load i32, i32* %3
%57 = load i32, i32* %2
%58 = icmp eq i32 %56, %57
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
br label %61
61: ; preds = %55, %47, %35, %24
%62 = load i32, i32* @SIG_SETMASK
%63 = call i32 @sigprocmask(i32 %62, i32* %6, i32* null)
br label %64
64: ; preds = %61, %9
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@SIGTERM = global i32 0
@LOG_ERR = global i32 0
@.str = [30 x i8] c"Can't kill script pgrp %d: %m\00"
@WNOHANG = global i32 0
@.str.1 = [28 x i8] c"Failed to wait for %d: %m?!\00"
@SIGKILL = global i32 0
@.str.2 = [25 x i8] c"2nd kill %d failed: %m?!\00"
@SIG_SETMASK = global i32 0
define void @kill_script(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = icmp eq i32 %0, -1
br i1 %8, label %48, label %9
9: ; preds = %1
%10 = icmp sgt i32 %0, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = call i32 @sigemptyset(i32* %3)
%14 = load i32, i32* @SIGCHLD
%15 = call i32 @sigaddset(i32* %3, i32 %14)
%16 = load i32, i32* @SIG_BLOCK
%17 = call i32 @sigprocmask(i32 %16, i32* %3, i32* %4)
%18 = load i32, i32* @SIGTERM
%19 = call i32 @killpg(i32 %0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %9
%22 = load i32, i32* @LOG_ERR
%23 = call i32 @do_log(i32 %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %45
24: ; preds = %9
%25 = call i32 @sleep(i32 1)
%26 = load i32, i32* @WNOHANG
%27 = call i32 @waitpid(i32 %0, i32* %2, i32 %26)
switch i32 %27, label %40 [
i32 -1, label %28
i32 0, label %31
]
28: ; preds = %24
%29 = load i32, i32* @LOG_ERR
%30 = call i32 @do_log(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %45
31: ; preds = %24
%32 = load i32, i32* @SIGKILL
%33 = call i32 @killpg(i32 %0, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @LOG_ERR
%37 = call i32 @do_log(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %45
38: ; preds = %31
%39 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
br label %40
40: ; preds = %24, %38
%41 = phi i32 [ %39, %38 ], [ %27, %24 ]
%42 = icmp eq i32 %41, %0
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
br label %45
45: ; preds = %40, %35, %28, %21
%46 = load i32, i32* @SIG_SETMASK
%47 = call i32 @sigprocmask(i32 %46, i32* %4, i32* null)
br label %48
48: ; preds = %1, %45
ret void
}