#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 */
int /*<<< orphan*/ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ execvp(char *, char **);
int finished(char *, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int args, char **argv) {
  pid_t pid;
  int status;
  int FAILURE = 6;
  if (args < 2) {
    fprintf(stderr, "Need args\n");
    return FAILURE;
  }
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "Could not fork\n");
    return FAILURE;
  } else if (pid == 0) {
    execvp(argv[1], &argv[1]);
    return FAILURE;
  } else {
    waitpid(pid, &status, 0);
    if (!WIFEXITED(status)) {
      if (WIFSIGNALED(status))
        fprintf(stderr, "Process %d died of signal %d.\n", pid,
                WTERMSIG(status));
      return FAILURE;
    }
    return finished(argv[0], WEXITSTATUS(status));
  }
  return FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x6,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d9 <main+0x49>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <main+0x136>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120f <main+0x7f>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <main+0x136>
cmpl   $0x0,-0x14(%rbp)
jne    1239 <main+0xa9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1090 <execvp@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <main+0x136>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x18(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    12a5 <main+0x115>
mov    -0x18(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     129a <main+0x10a>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <main+0x136>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1050 <finished@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11af <main+0x1f>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
jmp    11d4 <main+0x44>
mov    %rsi,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11e5 <main+0x55>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11fc <main+0x6c>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x6,%ebx
jmp    126c <main+0xdc>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1090 <execvp@plt>
mov    $0x6,%ebx
jmp    126c <main+0xdc>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %eax,%eax
je     1231 <main+0xa1>
mov    (%rbx),%rbx
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <finished@plt>
mov    %eax,%ebx
jmp    126c <main+0xdc>
mov    0xc(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
mov    $0x6,%ebx
test   %rax,%rax
je     126c <main+0xdc>
mov    0x2d95(%rip),%rax
mov    (%rax),%r14d
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xdbd(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"Need args\0A\00"
@.str.1 = [16 x i8] c"Could not fork\0A\00"
@.str.2 = [31 x i8] c"Process %d died of signal %d.\0A\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
store i32 6, i32* %8
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %8
store i32 %14, i32* %3
br label %59
15: ; preds = %2
%16 = call i32 (...) @fork()
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %8
store i32 %22, i32* %3
br label %59
23: ; preds = %15
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = call i32 @execvp(i8* %29, i8** %31)
%33 = load i32, i32* %8
store i32 %33, i32* %3
br label %59
34: ; preds = %23
%35 = load i32, i32* %6
%36 = call i32 @waitpid(i32 %35, i32* %7, i32 0)
%37 = load i32, i32* %7
%38 = call i32 @WIFEXITED(i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %52, label %40
40: ; preds = %34
%41 = load i32, i32* %7
%42 = call i64 @WIFSIGNALED(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = load i32, i32* %6
%47 = load i32, i32* %7
%48 = call i32 @WTERMSIG(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %46, i32 %48)
br label %50
50: ; preds = %44, %40
%51 = load i32, i32* %8
store i32 %51, i32* %3
br label %59
52: ; preds = %34
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 0
%55 = load i8*, i8** %54
%56 = load i32, i32* %7
%57 = call i32 @WEXITSTATUS(i32 %56)
%58 = call i32 @finished(i8* %55, i32 %57)
store i32 %58, i32* %3
br label %59
59: ; preds = %52, %50, %26, %19, %11
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"Need args\0A\00"
@.str.1 = [16 x i8] c"Could not fork\0A\00"
@.str.2 = [31 x i8] c"Process %d died of signal %d.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %37
9: ; preds = %2
%10 = tail call i32 (...) @fork()
switch i32 %10, label %18 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %9
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %37
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @execvp(i8* %16, i8** %15)
br label %37
18: ; preds = %9
%19 = call i32 @waitpid(i32 %10, i32* %3, i32 0)
%20 = load i32, i32* %3
%21 = call i32 @WIFEXITED(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %18
%24 = load i32, i32* %3
%25 = call i64 @WIFSIGNALED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i32, i32* %3
%30 = call i32 @WTERMSIG(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %30)
br label %37
32: ; preds = %18
%33 = load i8*, i8** %1
%34 = load i32, i32* %3
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = call i32 @finished(i8* %33, i32 %35)
br label %37
37: ; preds = %23, %27, %32, %14, %11, %6
%38 = phi i32 [ 6, %6 ], [ 6, %11 ], [ 6, %14 ], [ %36, %32 ], [ 6, %27 ], [ 6, %23 ]
ret i32 %38
}