#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);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  const char *filename = argv[1];
  pid_t cpid;
  if ((cpid = fork()) == 0) {
    execv(argv[1], &argv[1]);
    exit(1);
  } else if (cpid > 0) {
    int status;
    wait(&status);
    printf("Child status is %d\n", WEXITSTATUS(status));
  } else {
    perror("fork() failed");
  }
  exit(0);
}


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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c6 <main+0x56>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execv@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    11f5 <main+0x85>
lea    -0x24(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x24(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe15(%rip),%rdi
call   1030 <printf@plt>
jmp    1201 <main+0x91>
lea    0xe18(%rip),%rdi
call   1050 <perror@plt>
jmp    1206 <main+0x96>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    119d <main+0x2d>
add    $0x8,%rbx
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execv@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jg     11b2 <main+0x42>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe34(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Child status is %d\0A\00"
@.str.1 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
store i8* %11, i8** %6
%12 = call i64 (...) @fork()
store i64 %12, i64* %7
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = call i32 @execv(i8* %17, i8** %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i64, i64* %7
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = call i32 @wait(i32* %8)
%27 = load i32, i32* %8
%28 = call i32 @WEXITSTATUS(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %32
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
br label %33
33: ; preds = %32
%34 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [20 x i8] c"Child status is %d\0A\00"
@.str.1 = [14 x i8] c"fork() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = tail call i32 @execv(i8* %8, i8** %4)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp sgt i64 %5, 0
br i1 %12, label %13, label %19
13: ; preds = %11
%14 = bitcast i32* %3 to i8*
%15 = call i32 @wait(i32* %3)
%16 = load i32, i32* %3
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %21
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %13, %19
%22 = call i32 @exit(i32 0)
unreachable
}