#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 */

/* Variables and functions */
scalar_t__ ECHILD;
scalar_t__ EINTR;
int EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ prctl(int, int);
int /*<<< orphan*/ stderr;
int wait(int *);
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "usage: fork-wrapper command...\n");
    exit(EXIT_FAILURE);
  }
  if (prctl(36, 1) < 0) {
    perror("could not set subreaper process attribute");
    exit(EXIT_FAILURE);
  }
  int rc = fork();
  if (rc < 0) {
    perror("could not fork");
    exit(EXIT_FAILURE);
  }
  if (rc == 0) {
    execvp(argv[1], argv + 1);
    perror("could not exec");
    exit(EXIT_FAILURE);
  }
  int last_exit_status = 0;
  while (1) {
    int stat;
    int pid = wait(&stat);
    if (pid < 0) {
      if (errno == EINTR) {
        continue;
      }
      if (errno == ECHILD) {
        exit(last_exit_status);
      }
      perror("could not wait");
      exit(EXIT_FAILURE);
    }
    last_exit_status = WEXITSTATUS(stat);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c3 <main+0x43>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x24,%edi
mov    $0x1,%esi
call   1040 <prctl@plt>
cmp    $0x0,%rax
jge    11f6 <main+0x76>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1224 <main+0xa4>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    125d <main+0xdd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1070 <execvp@plt>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12d5 <main+0x155>
mov    0x2d57(%rip),%rax
mov    (%rax),%rax
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%rax
jne    1299 <main+0x119>
jmp    1264 <main+0xe4>
mov    0x2d38(%rip),%rax
mov    (%rax),%rax
mov    0x2d16(%rip),%rcx
cmp    (%rcx),%rax
jne    12bb <main+0x13b>
mov    -0x18(%rbp),%edi
call   1080 <exit@plt>
lea    0xda6(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x18(%rbp)
jmp    1264 <main+0xe4>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11a7 <main+0x27>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1030 <fprintf@plt>
jmp    124f <main+0xcf>
mov    %rsi,%rbx
mov    $0x24,%edi
mov    $0x1,%esi
call   1040 <prctl@plt>
test   %rax,%rax
jns    11ca <main+0x4a>
lea    0xe5b(%rip),%rdi
jmp    124a <main+0xca>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jns    11de <main+0x5e>
lea    0xe6e(%rip),%rdi
jmp    124a <main+0xca>
jne    11f9 <main+0x79>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1070 <execvp@plt>
lea    0xe62(%rip),%rdi
jmp    124a <main+0xca>
xor    %ebx,%ebx
lea    0x4(%rsp),%r14
mov    0x2dd1(%rip),%r15
mov    0x2dba(%rip),%r12
jmp    121b <main+0x9b>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1090 <wait@plt>
test   %eax,%eax
jns    1210 <main+0x90>
mov    (%r15),%rax
cmp    (%r12),%rax
je     121b <main+0x9b>
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
jne    1243 <main+0xc3>
mov    %ebx,%edi
call   1080 <exit@plt>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"usage: fork-wrapper command...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [42 x i8] c"could not set subreaper process attribute\00"
@.str.2 = [15 x i8] c"could not fork\00"
@.str.3 = [15 x i8] c"could not exec\00"
@errno = global i64 0
@EINTR = global i64 0
@ECHILD = global i64 0
@.str.4 = [15 x i8] c"could not wait\00"
define i32 @main(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* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = call i64 @prctl(i32 36, i32 1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %17
%25 = call i32 (...) @fork()
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %45
35: ; preds = %32
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = call i32 @execvp(i8* %38, i8** %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %32
store i32 0, i32* %7
br label %46
46: ; preds = %45, %54, %66
%47 = call i32 @wait(i32* %8)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %66
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = load i64, i64* @EINTR
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %46
55: ; preds = %50
%56 = load i64, i64* @errno
%57 = load i64, i64* @ECHILD
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %7
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %55
%63 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %46
%67 = load i32, i32* %8
%68 = call i32 @WEXITSTATUS(i32 %67)
store i32 %68, i32* %7
br label %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"usage: fork-wrapper command...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [42 x i8] c"could not set subreaper process attribute\00"
@.str.2 = [15 x i8] c"could not fork\00"
@.str.3 = [15 x i8] c"could not exec\00"
@errno = global i64 0
@EINTR = global i64 0
@ECHILD = global i64 0
@.str.4 = [15 x i8] c"could not wait\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i64 @prctl(i32 36, i32 1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = tail call i32 (...) @fork()
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %17
%25 = icmp eq i32 %18, 0
br i1 %25, label %28, label %26
26: ; preds = %24
%27 = bitcast i32* %3 to i8*
br label %35
28: ; preds = %24
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = tail call i32 @execvp(i8* %30, i8** %29)
%32 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %26, %55
%36 = phi i32 [ %56, %55 ], [ 0, %26 ]
%37 = call i32 @wait(i32* %3)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %55, label %43
43: ; preds = %39
%44 = load i64, i64* @ECHILD
%45 = icmp eq i64 %40, %44
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @exit(i32 %36)
unreachable
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %35
%53 = load i32, i32* %3
%54 = call i32 @WEXITSTATUS(i32 %53)
br label %55
55: ; preds = %39, %52
%56 = phi i32 [ %54, %52 ], [ %36, %39 ]
br label %35
}