#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*/ 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) {
    exit(test_function());
  } else if (pid == -1) {
    perror("fork");
    return 1;
  }
  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;
    }
  }
  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    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11fc <run_test+0x3c>
call   *-0x10(%rbp)
mov    %eax,%edi
call   10d0 <exit@plt>
cmpl   $0xffffffff,-0x28(%rbp)
jne    121e <run_test+0x5e>
lea    0xdf3(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <run_test+0x1a1>
jmp    1223 <run_test+0x63>
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    12f5 <run_test+0x135>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
je     1283 <run_test+0xc3>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <run_test+0x1a1>
cmpl   $0x0,-0x1c(%rbp)
je     12bc <run_test+0xfc>
mov    -0x18(%rbp),%rsi
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <run_test+0x1a1>
mov    -0x18(%rbp),%rsi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%edi
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0x1,-0x1c(%rbp)
mov    $0x5,%edi
call   1040 <alarm@plt>
jmp    1234 <run_test+0x74>
mov    -0x24(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1317 <run_test+0x157>
mov    -0x24(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,-0x24(%rbp)
jmp    135b <run_test+0x19b>
mov    -0x24(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1346 <run_test+0x186>
mov    -0x24(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1354 <run_test+0x194>
lea    0xd14(%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   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
je     130f <run_test+0x13f>
mov    %eax,%ebp
test   %eax,%eax
je     131d <run_test+0x14d>
mov    $0x78,%edi
call   1050 <alarm@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1242 <run_test+0x72>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     12c5 <run_test+0xf5>
call   10a0 <WEXITSTATUS@plt>
jmp    1304 <run_test+0x134>
mov    0x2d8f(%rip),%r15
mov    (%r15),%rax
mov    0x2d7d(%rip),%rbx
cmp    (%rbx),%rax
jne    12ea <run_test+0x11a>
lea    0xdb7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <kill@plt>
mov    $0x5,%edi
call   1050 <alarm@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1222 <run_test+0x52>
mov    (%r15),%rax
cmp    (%rbx),%rax
jne    12ea <run_test+0x11a>
lea    0xd5c(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <kill@plt>
jmp    12ff <run_test+0x12f>
call   1060 <WIFSIGNALED@plt>
test   %rax,%rax
je     12f3 <run_test+0x123>
mov    0x4(%rsp),%edi
call   1090 <WTERMSIG@plt>
lea    0xd4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12ff <run_test+0x12f>
lea    0xd73(%rip),%rdi
jmp    12fa <run_test+0x12a>
lea    0xd4b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcea(%rip),%rdi
call   10c0 <perror@plt>
jmp    12ff <run_test+0x12f>
call   *%rbx
mov    %eax,%edi
call   10e0 <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 %19
15: ; preds = %2
%16 = load i32 ()*, i32 ()** %4
%17 = call i32 %16()
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %74
24: ; preds = %19
br label %25
25: ; preds = %24
%26 = call i32 @alarm(i32 120)
store i32 0, i32* %6
br label %27
27: ; preds = %47, %25
%28 = load i32, i32* %9
%29 = call i32 @waitpid(i32 %28, i32* %8, i32 0)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %54
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = 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 %74
38: ; preds = %32
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i8*, i8** %5
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %9
%45 = load i32, i32* @SIGKILL
%46 = call i32 @kill(i32 %44, i32 %45)
store i32 1, i32* %3
br label %74
47: ; preds = %38
%48 = load i8*, i8** %5
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %9
%51 = load i32, i32* @SIGTERM
%52 = call i32 @kill(i32 %50, i32 %51)
store i32 1, i32* %6
%53 = call i32 @alarm(i32 5)
br label %27
54: ; preds = %27
%55 = load i32, i32* %8
%56 = call i64 @WIFEXITED(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i32, i32* %8
%60 = call i32 @WEXITSTATUS(i32 %59)
store i32 %60, i32* %8
br label %72
61: ; preds = %54
%62 = load i32, i32* %8
%63 = call i64 @WIFSIGNALED(i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = load i32, i32* %8
%67 = call i32 @WTERMSIG(i32 %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %67)
br label %71
69: ; preds = %61
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %71
71: ; preds = %69, %65
store i32 1, i32* %8
br label %72
72: ; preds = %71, %58
%73 = load i32, i32* %8
store i32 %73, i32* %3
br label %74
74: ; preds = %72, %41, %36, %22
%75 = load i32, i32* %3
ret i32 %75
}

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 %13 [
i32 0, label %8
i32 -1, label %11
]
8: ; preds = %2
%9 = tail call i32 %0()
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %54
13: ; preds = %2
%14 = tail call i32 @alarm(i32 120)
%15 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %38
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %34, %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %54
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%25 = load i32, i32* @SIGTERM
%26 = call i32 @kill(i32 %7, i32 %25)
%27 = call i32 @alarm(i32 5)
%28 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %34, label %38
30: ; preds = %34
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%32 = load i32, i32* @SIGKILL
%33 = call i32 @kill(i32 %7, i32 %32)
br label %54
34: ; preds = %23
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %30, label %21
38: ; preds = %23, %13
%39 = load i32, i32* %3
%40 = call i64 @WIFEXITED(i32 %39)
%41 = icmp eq i64 %40, 0
%42 = load i32, i32* %3
br i1 %41, label %45, label %43
43: ; preds = %38
%44 = call i32 @WEXITSTATUS(i32 %42)
br label %54
45: ; preds = %38
%46 = call i64 @WIFSIGNALED(i32 %42)
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %45
%49 = load i32, i32* %3
%50 = call i32 @WTERMSIG(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %54
52: ; preds = %45
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %54
54: ; preds = %43, %52, %48, %30, %21, %11
%55 = phi i32 [ 1, %11 ], [ 1, %21 ], [ 1, %30 ], [ %44, %43 ], [ 1, %52 ], [ 1, %48 ]
ret i32 %55
}