#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__ ECHILD;
scalar_t__ EINTR;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ PR_SET_CHILD_SUBREAPER;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ prctl(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int wait(int *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("This program runs a specified program and waits for all of its "
           "decendants\n"
           "Usage: %s program [program args]\n",
           argv[0]);
    return EXIT_SUCCESS;
  }
  prctl(PR_SET_CHILD_SUBREAPER);
  pid_t child = fork();
  if (child == -1) {
    perror("fork");
    return EXIT_FAILURE;
  } else if (child == 0) {
    execvp(argv[1], argv + 1);
    perror("execvp");
    return EXIT_FAILURE;
  }
  int last_status = 0;
  while (1) {
    int status;
    child = wait(&status);
    if (child == -1) {
      if (errno == EINTR)
        continue;
      if (errno == ECHILD)
        break;
      perror("wait");
      return EXIT_FAILURE;
    }
    last_status = status;
  }
  if (last_status == 0 || WIFEXITED(last_status))
    return WEXITSTATUS(last_status);
  return EXIT_FAILURE;
}


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    11c4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <main+0x17f>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1040 <prctl@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1203 <main+0x83>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <main+0x17f>
cmpl   $0x0,-0x14(%rbp)
jne    123f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1070 <execvp@plt>
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <main+0x17f>
jmp    1244 <main+0xc4>
movl   $0x0,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12bc <main+0x13c>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
jne    1280 <main+0x100>
jmp    124b <main+0xcb>
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%rax
jne    129f <main+0x11f>
jmp    12c7 <main+0x147>
lea    0xdd2(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <main+0x17f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    124b <main+0xcb>
cmpl   $0x0,-0x18(%rbp)
je     12e3 <main+0x163>
mov    -0x18(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12f3 <main+0x173>
mov    -0x18(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    12ff <main+0x17f>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11aa <main+0x2a>
mov    (%rbx),%rsi
lea    0xe67(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e1b(%rip),%rax
jmp    1253 <main+0xd3>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1040 <prctl@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     121e <main+0x9e>
test   %eax,%eax
je     1227 <main+0xa7>
xor    %ebp,%ebp
lea    0x4(%rsp),%rbx
mov    0x2e0a(%rip),%r14
mov    0x2df3(%rip),%r15
jmp    11e4 <main+0x64>
nop
mov    0x4(%rsp),%ebp
mov    %rbx,%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x60>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11e4 <main+0x64>
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <main+0xc0>
test   %ebp,%ebp
je     1215 <main+0x95>
mov    %ebp,%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     124c <main+0xcc>
mov    %ebp,%edi
call   1050 <WEXITSTATUS@plt>
jmp    1255 <main+0xd5>
lea    0xe47(%rip),%rdi
jmp    1247 <main+0xc7>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1070 <execvp@plt>
lea    0xe33(%rip),%rdi
jmp    1247 <main+0xc7>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [108 x i8] c"This program runs a specified program and waits for all of its decendants\0AUsage: %s program [program args]\0A\00"
@EXIT_SUCCESS = global i32 0
@PR_SET_CHILD_SUBREAPER = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"execvp\00"
@errno = global i64 0
@EINTR = global i64 0
@ECHILD = global i64 0
@.str.3 = [5 x i8] c"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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_SUCCESS
store i32 %16, i32* %3
br label %71
17: ; preds = %2
%18 = load i32, i32* @PR_SET_CHILD_SUBREAPER
%19 = call i32 @prctl(i32 %18)
%20 = call i32 (...) @fork()
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %71
26: ; preds = %17
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = call i32 @execvp(i8* %32, i8** %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %71
38: ; preds = %26
br label %39
39: ; preds = %38
store i32 0, i32* %7
br label %40
40: ; preds = %39, %48, %57
%41 = call i32 @wait(i32* %8)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %57
44: ; preds = %40
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
br label %40
49: ; preds = %44
%50 = load i64, i64* @errno
%51 = load i64, i64* @ECHILD
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
br label %59
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
store i32 %56, i32* %3
br label %71
57: ; preds = %40
%58 = load i32, i32* %8
store i32 %58, i32* %7
br label %40
59: ; preds = %53
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %59
%63 = load i32, i32* %7
%64 = call i64 @WIFEXITED(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62, %59
%67 = load i32, i32* %7
%68 = call i32 @WEXITSTATUS(i32 %67)
store i32 %68, i32* %3
br label %71
69: ; preds = %62
%70 = load i32, i32* @EXIT_FAILURE
store i32 %70, i32* %3
br label %71
71: ; preds = %69, %66, %54, %29, %23, %11
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [108 x i8] c"This program runs a specified program and waits for all of its decendants\0AUsage: %s program [program args]\0A\00"
@EXIT_SUCCESS = global i32 0
@PR_SET_CHILD_SUBREAPER = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"execvp\00"
@errno = global i64 0
@EINTR = global i64 0
@ECHILD = global i64 0
@.str.3 = [5 x i8] c"wait\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_SUCCESS
br label %51
9: ; preds = %2
%10 = load i32, i32* @PR_SET_CHILD_SUBREAPER
%11 = tail call i32 @prctl(i32 %10)
%12 = tail call i32 (...) @fork()
switch i32 %12, label %13 [
i32 -1, label %15
i32 0, label %18
]
13: ; preds = %9
%14 = bitcast i32* %3 to i8*
br label %24
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
br label %51
18: ; preds = %9
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @execvp(i8* %20, i8** %19)
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
br label %51
24: ; preds = %13, %40
%25 = phi i32 [ %41, %40 ], [ 0, %13 ]
%26 = call i32 @wait(i32* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %40, label %32
32: ; preds = %28
%33 = load i64, i64* @ECHILD
%34 = icmp eq i64 %29, %33
br i1 %34, label %42, label %35
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
br label %51
38: ; preds = %24
%39 = load i32, i32* %3
br label %40
40: ; preds = %28, %38
%41 = phi i32 [ %39, %38 ], [ %25, %28 ]
br label %24
42: ; preds = %32
%43 = icmp eq i32 %25, 0
br i1 %43, label %47, label %44
44: ; preds = %42
%45 = call i64 @WIFEXITED(i32 %25)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44, %42
%48 = call i32 @WEXITSTATUS(i32 %25)
br label %51
49: ; preds = %44
%50 = load i32, i32* @EXIT_FAILURE
br label %51
51: ; preds = %35, %15, %18, %49, %47, %5
%52 = phi i32 [ %8, %5 ], [ %17, %15 ], [ %23, %18 ], [ %48, %47 ], [ %50, %49 ], [ %37, %35 ]
ret i32 %52
}