#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 pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ SIGTERM;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ alarm(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setpgid(int, int);
int /*<<< orphan*/ stdout;
int waitpid(int, int *, int /*<<< orphan*/);
int run_test(int(test_function)(void), char *name) {
  bool terminated;
  int rc, status;
  pid_t pid;
  fflush(stdout);
  pid = fork();
  if (pid == 0) {
    setpgid(0, 0);
    exit(test_function());
  } else if (pid == -1) {
    perror("fork");
    return 1;
  }
  setpgid(pid, pid);
  alarm(120);
  terminated = false;
wait:
  rc = waitpid(pid, &status, 0);
  if (rc == -1) {
    if (errno != EINTR) {
      printf("unknown error from waitpid\n");
      return 1;
    }
    if (terminated) {
      printf("!! force killing %s\n", name);
      kill(-pid, SIGKILL);
      return 1;
    } else {
      printf("!! killing %s\n", name);
      kill(-pid, SIGTERM);
      terminated = true;
      alarm(5);
      goto wait;
    }
  }
  kill(-pid, SIGTERM);
  if (WIFEXITED(status))
    status = WEXITSTATUS(status);
  else {
    if (WIFSIGNALED(status))
      printf("!! child died by signal %d\n", WTERMSIG(status));
    else
      printf("!! child died by unknown cause\n");
    status = 1;
  }
  return status;
}


x86 assembly (clang, -O0):
<run_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1215 <run_test+0x45>
xor    %esi,%esi
mov    %esi,%edi
call   10c0 <setpgid@plt>
call   *-0x10(%rbp)
mov    %eax,%edi
call   10e0 <exit@plt>
cmpl   $0xffffffff,-0x28(%rbp)
jne    1237 <run_test+0x67>
lea    0xdda(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139c <run_test+0x1cc>
jmp    123c <run_test+0x6c>
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
call   10c0 <setpgid@plt>
mov    $0x78,%edi
call   1040 <alarm@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    131d <run_test+0x14d>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%rax
je     12a7 <run_test+0xd7>
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139c <run_test+0x1cc>
cmpl   $0x0,-0x1c(%rbp)
je     12e2 <run_test+0x112>
mov    -0x18(%rbp),%rsi
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
sub    -0x28(%rbp),%edi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x1,-0x4(%rbp)
jmp    139c <run_test+0x1cc>
mov    -0x18(%rbp),%rsi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
sub    -0x28(%rbp),%edi
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x1,-0x1c(%rbp)
mov    $0x5,%edi
call   1040 <alarm@plt>
jmp    1258 <run_test+0x88>
xor    %edi,%edi
sub    -0x28(%rbp),%edi
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x24(%rbp),%edi
call   10d0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1352 <run_test+0x182>
mov    -0x24(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,-0x24(%rbp)
jmp    1396 <run_test+0x1c6>
mov    -0x24(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1381 <run_test+0x1b1>
mov    -0x24(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xcd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    138f <run_test+0x1bf>
lea    0xcd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     1349 <run_test+0x169>
mov    %eax,%ebp
test   %eax,%eax
je     1357 <run_test+0x177>
mov    %ebp,%edi
mov    %ebp,%esi
call   10d0 <setpgid@plt>
mov    $0x78,%edi
call   1050 <alarm@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    %ebp,%ebx
neg    %ebx
cmp    $0xffffffff,%eax
je     1274 <run_test+0x94>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <kill@plt>
mov    0xc(%rsp),%edi
call   10e0 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     12fd <run_test+0x11d>
call   10a0 <WEXITSTATUS@plt>
jmp    133c <run_test+0x15c>
mov    0x2d5d(%rip),%r15
mov    (%r15),%rax
mov    0x2d4b(%rip),%r12
cmp    (%r12),%rax
jne    1322 <run_test+0x142>
lea    0xd84(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <kill@plt>
mov    $0x5,%edi
call   1050 <alarm@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1244 <run_test+0x64>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1322 <run_test+0x142>
lea    0xd24(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <kill@plt>
jmp    1337 <run_test+0x157>
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     132b <run_test+0x14b>
mov    0xc(%rsp),%edi
call   1090 <WTERMSIG@plt>
lea    0xd12(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1337 <run_test+0x157>
lea    0xd3b(%rip),%rdi
jmp    1332 <run_test+0x152>
lea    0xd13(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb0(%rip),%rdi
call   10c0 <perror@plt>
jmp    1337 <run_test+0x157>
xor    %edi,%edi
xor    %esi,%esi
call   10d0 <setpgid@plt>
call   *%rbx
mov    %eax,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [5 x i8] c"fork\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [28 x i8] c"unknown error from waitpid\0A\00"
@.str.2 = [21 x i8] c"!! force killing %s\0A\00"
@SIGKILL = global i32 0
@.str.3 = [15 x i8] c"!! killing %s\0A\00"
@SIGTERM = global i32 0
@.str.4 = [28 x i8] c"!! child died by signal %d\0A\00"
@.str.5 = [32 x i8] c"!! child died by unknown cause\0A\00"
define i32 @run_test(i32 ()* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32 ()*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 ()* %0, i32 ()** %4
store i8* %1, i8** %5
%10 = load i32, i32* @stdout
%11 = call i32 @fflush(i32 %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = call i32 @setpgid(i32 0, i32 0)
%17 = load i32 ()*, i32 ()** %4
%18 = call i32 %17()
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %84
25: ; preds = %20
br label %26
26: ; preds = %25
%27 = load i32, i32* %9
%28 = load i32, i32* %9
%29 = call i32 @setpgid(i32 %27, i32 %28)
%30 = call i32 @alarm(i32 120)
store i32 0, i32* %6
br label %31
31: ; preds = %52, %26
%32 = load i32, i32* %9
%33 = call i32 @waitpid(i32 %32, i32* %8, i32 0)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %60
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %84
42: ; preds = %36
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load i8*, i8** %5
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %9
%49 = sub nsw i32 0, %48
%50 = load i32, i32* @SIGKILL
%51 = call i32 @kill(i32 %49, i32 %50)
store i32 1, i32* %3
br label %84
52: ; preds = %42
%53 = load i8*, i8** %5
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %9
%56 = sub nsw i32 0, %55
%57 = load i32, i32* @SIGTERM
%58 = call i32 @kill(i32 %56, i32 %57)
store i32 1, i32* %6
%59 = call i32 @alarm(i32 5)
br label %31
60: ; preds = %31
%61 = load i32, i32* %9
%62 = sub nsw i32 0, %61
%63 = load i32, i32* @SIGTERM
%64 = call i32 @kill(i32 %62, i32 %63)
%65 = load i32, i32* %8
%66 = call i64 @WIFEXITED(i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %60
%69 = load i32, i32* %8
%70 = call i32 @WEXITSTATUS(i32 %69)
store i32 %70, i32* %8
br label %82
71: ; preds = %60
%72 = load i32, i32* %8
%73 = call i64 @WIFSIGNALED(i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %71
%76 = load i32, i32* %8
%77 = call i32 @WTERMSIG(i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %77)
br label %81
79: ; preds = %71
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %81
81: ; preds = %79, %75
store i32 1, i32* %8
br label %82
82: ; preds = %81, %68
%83 = load i32, i32* %8
store i32 %83, i32* %3
br label %84
84: ; preds = %82, %45, %40, %23
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [5 x i8] c"fork\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [21 x i8] c"!! force killing %s\0A\00"
@SIGKILL = global i32 0
@.str.3 = [15 x i8] c"!! killing %s\0A\00"
@SIGTERM = global i32 0
@.str.4 = [28 x i8] c"!! child died by signal %d\0A\00"
@str = [31 x i8] c"!! child died by unknown cause\00"
@str.6 = [27 x i8] c"unknown error from waitpid\00"
define i32 @run_test(i32 ()* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
%7 = tail call i32 (...) @fork()
switch i32 %7, label %14 [
i32 0, label %8
i32 -1, label %12
]
8: ; preds = %2
%9 = tail call i32 @setpgid(i32 0, i32 0)
%10 = tail call i32 %0()
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %59
14: ; preds = %2
%15 = tail call i32 @setpgid(i32 %7, i32 %7)
%16 = tail call i32 @alarm(i32 120)
%17 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
%18 = icmp eq i32 %17, -1
%19 = sub nsw i32 0, %7
br i1 %18, label %20, label %41
20: ; preds = %14
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %37, %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %59
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%28 = load i32, i32* @SIGTERM
%29 = call i32 @kill(i32 %19, i32 %28)
%30 = call i32 @alarm(i32 5)
%31 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %37, label %41
33: ; preds = %37
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%35 = load i32, i32* @SIGKILL
%36 = call i32 @kill(i32 %19, i32 %35)
br label %59
37: ; preds = %26
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br i1 %40, label %33, label %24
41: ; preds = %14, %26
%42 = load i32, i32* @SIGTERM
%43 = call i32 @kill(i32 %19, i32 %42)
%44 = load i32, i32* %3
%45 = call i64 @WIFEXITED(i32 %44)
%46 = icmp eq i64 %45, 0
%47 = load i32, i32* %3
br i1 %46, label %50, label %48
48: ; preds = %41
%49 = call i32 @WEXITSTATUS(i32 %47)
br label %59
50: ; preds = %41
%51 = call i64 @WIFSIGNALED(i32 %47)
%52 = icmp eq i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %50
%54 = load i32, i32* %3
%55 = call i32 @WTERMSIG(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %55)
br label %59
57: ; preds = %50
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %59
59: ; preds = %48, %57, %53, %33, %24, %12
%60 = phi i32 [ 1, %12 ], [ 1, %24 ], [ 1, %33 ], [ %49, %48 ], [ 1, %57 ], [ 1, %53 ]
ret i32 %60
}