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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ _exit(int);
scalar_t__ execv(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid;
  int rstatus;
  char *new_argv[] = {NULL, "3", NULL};
  if (argc != 2) {
    fprintf(stderr, "Usage: %s exepath\n", argv[0]);
    return -1;
  }
  new_argv[0] = argv[1];
  pid = fork();
  if (pid < 0) {
    perror("fork");
    return -1;
  }
  if (pid == 0) {
    if (execv(new_argv[0], new_argv) < 0)
      perror("execv");
    _exit(1);
  }
  if (waitpid(pid, &rstatus, 0) < 0) {
    perror("waitpid");
    return -1;
  }
  if (WIFEXITED(rstatus))
    return WEXITSTATUS(rstatus);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c63(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c60(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c5d(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e9 <main+0x69>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe2a(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <main+0x135>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1223 <main+0xa3>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <main+0x135>
cmpq   $0x0,-0x18(%rbp)
jne    125b <main+0xdb>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <execv@plt>
cmp    $0x0,%rax
jge    1251 <main+0xd1>
lea    0xdce(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jge    128c <main+0x10c>
lea    0xda5(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b5 <main+0x135>
mov    -0x1c(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12ae <main+0x12e>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    12b5 <main+0x135>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    0x2c84(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0x2c68(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x2,%edi
jne    11e6 <main+0x66>
mov    0x8(%rsi),%rbx
mov    %rbx,0x10(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1209 <main+0x89>
je     1220 <main+0xa0>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
js     1212 <main+0x92>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11fe <main+0x7e>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    1203 <main+0x83>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe09(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe05(%rip),%rdi
jmp    1219 <main+0x99>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
jmp    11fe <main+0x7e>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <execv@plt>
test   %rax,%rax
jns    123e <main+0xbe>
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"3\00"
@__const.main.new_argv = [3 x i8*] [i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.1 = [19 x i8] c"Usage: %s exepath\0A\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"execv\00"
@.str.4 = [8 x i8] c"waitpid\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca [3 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [3 x i8*]* %8 to i8*
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %55
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 0
store i8* %21, i8** %22
%23 = call i64 (...) @fork()
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
28: ; preds = %18
%29 = load i64, i64* %6
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 0
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 0
%35 = call i64 @execv(i8* %33, i8** %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %31
%40 = call i32 @_exit(i32 1)
unreachable
41: ; preds = %28
%42 = load i64, i64* %6
%43 = call i64 @waitpid(i64 %42, i32* %7, i32 0)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
47: ; preds = %41
%48 = load i32, i32* %7
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %7
%53 = call i32 @WEXITSTATUS(i32 %52)
store i32 %53, i32* %3
br label %55
54: ; preds = %47
store i32 -1, i32* %3
br label %55
55: ; preds = %54, %51, %45, %26, %12
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"3\00"
@__const.main.new_argv = [3 x i8*] [i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* null]
@stderr = global i32 0
@.str.1 = [19 x i8] c"Usage: %s exepath\0A\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"execv\00"
@.str.4 = [8 x i8] c"waitpid\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [3 x i8*]
%5 = bitcast i32* %3 to i8*
%6 = bitcast [3 x i8*]* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %41
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
store i8* %14, i8** %15
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %41
20: ; preds = %12
%21 = icmp eq i64 %16, 0
br i1 %21, label %22, label %29
22: ; preds = %20
%23 = call i64 @execv(i8* %14, i8** %15)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = call i32 @_exit(i32 1)
unreachable
29: ; preds = %20
%30 = call i64 @waitpid(i64 %16, i32* %3, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %41
34: ; preds = %29
%35 = load i32, i32* %3
%36 = call i64 @WIFEXITED(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = load i32, i32* %3
%40 = call i32 @WEXITSTATUS(i32 %39)
br label %41
41: ; preds = %34, %38, %32, %18, %8
%42 = phi i32 [ -1, %8 ], [ -1, %18 ], [ -1, %32 ], [ %40, %38 ], [ -1, %34 ]
ret i32 %42
}