#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;
typedef int /*<<< orphan*/ file_name;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv, char **envp) {
  pid_t child_id;
  pid_t my_id;
  int child_status;
  int i;
  char file_name[4096];
  int err;
  if (argc < 2) {
    printf("must specify file name as first argument\n");
    exit(1);
  }
  memset(file_name, 0, sizeof(file_name));
  strncpy(file_name, argv[1], sizeof(file_name));
  child_id = fork();
  if (child_id != 0) {
    my_id = getpid();
    printf("pid: %d -- I forked pid: %d for: %s\n", my_id, child_id, file_name);
    wait(&child_status);
    if (WIFEXITED(child_status)) {
      printf("pid: %d -- %s has completed with status: %d\n", (int)my_id,
             file_name, WEXITSTATUS(child_status));
    }
  } else {
    my_id = getpid();
    printf("pid: %d -- I am the child and I am going to exec %s\n", (int)my_id,
           file_name);
    err = execve(file_name, &(argv[1]), envp);
    printf("pid: %d -- execve of %s failed with error %d\n", (int)my_id,
           file_name, err);
  }
  printf("pid: %d -- I am exiting\n", my_id);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11df <main+0x3f>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12a6 <main+0x106>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x1030(%rbp),%rcx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x2c(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x2c(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12a1 <main+0x101>
mov    -0x28(%rbp),%rax
mov    %eax,-0x1044(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    -0x1044(%rbp),%esi
mov    -0x1040(%rbp),%rdx
mov    %eax,%ecx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    130b <main+0x16b>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    -0x1030(%rbp),%rdx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%rdx
call   1070 <execve@plt>
mov    %eax,-0x1034(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    -0x1030(%rbp),%rdx
mov    -0x1034(%rbp),%ecx
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
cmp    $0x1,%edi
jg     11d9 <main+0x29>
lea    0xf04(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    0x8(%rbx),%rsi
mov    %r15,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%r15
test   %r12,%r12
jne    1251 <main+0xa1>
add    $0x8,%rbx
lea    0xe2a(%rip),%rdi
lea    0x10(%rsp),%r12
mov    %r15d,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <execve@plt>
lea    0xe38(%rip),%rdi
jmp    1292 <main+0xe2>
lea    0xda8(%rip),%rdi
lea    0x10(%rsp),%rcx
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
je     12a3 <main+0xf3>
mov    0xc(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
lea    0xd93(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r15d,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe0b(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"must specify file name as first argument\0A\00"
@.str.1 = [37 x i8] c"pid: %d -- I forked pid: %d for: %s\0A\00"
@.str.2 = [45 x i8] c"pid: %d -- %s has completed with status: %d\0A\00"
@.str.3 = [53 x i8] c"pid: %d -- I am the child and I am going to exec %s\0A\00"
@.str.4 = [46 x i8] c"pid: %d -- execve of %s failed with error %d\0A\00"
@.str.5 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca [4096 x i8]
%13 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 4096)
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%23 = load i8**, i8*** %6
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strncpy(i8* %22, i8* %25, i32 4096)
%27 = call i64 (...) @fork()
store i64 %27, i64* %8
%28 = load i64, i64* %8
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %19
%31 = call i64 (...) @getpid()
store i64 %31, i64* %9
%32 = load i64, i64* %9
%33 = load i64, i64* %8
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %32, i64 %33, i8* %34)
%36 = call i32 @wait(i32* %10)
%37 = load i32, i32* %10
%38 = call i64 @WIFEXITED(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %30
%41 = load i64, i64* %9
%42 = trunc i64 %41 to i32
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%44 = load i32, i32* %10
%45 = call i32 @WEXITSTATUS(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %42, i8* %43, i32 %45)
br label %47
47: ; preds = %40, %30
br label %64
48: ; preds = %19
%49 = call i64 (...) @getpid()
store i64 %49, i64* %9
%50 = load i64, i64* %9
%51 = trunc i64 %50 to i32
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %51, i8* %52)
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%55 = load i8**, i8*** %6
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8**, i8*** %7
%58 = call i32 @execve(i8* %54, i8** %56, i8** %57)
store i32 %58, i32* %13
%59 = load i64, i64* %9
%60 = trunc i64 %59 to i32
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%62 = load i32, i32* %13
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %60, i8* %61, i32 %62)
br label %64
64: ; preds = %48, %47
%65 = load i64, i64* %9
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %65)
%67 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [37 x i8] c"pid: %d -- I forked pid: %d for: %s\0A\00"
@.str.2 = [45 x i8] c"pid: %d -- %s has completed with status: %d\0A\00"
@.str.3 = [53 x i8] c"pid: %d -- I am the child and I am going to exec %s\0A\00"
@.str.4 = [46 x i8] c"pid: %d -- execve of %s failed with error %d\0A\00"
@.str.5 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
@str = [41 x i8] c"must specify file name as first argument\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = call i32 @memset(i8* %7, i32 0, i32 4096)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @strncpy(i8* %7, i8* %15, i32 4096)
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
%19 = call i64 (...) @getpid()
br i1 %18, label %30, label %20
20: ; preds = %12
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %19, i64 %17, i8* %7)
%22 = call i32 @wait(i32* %4)
%23 = load i32, i32* %4
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %39, label %26
26: ; preds = %20
%27 = trunc i64 %19 to i32
%28 = load i32, i32* %4
%29 = call i32 @WEXITSTATUS(i32 %28)
br label %34
30: ; preds = %12
%31 = trunc i64 %19 to i32
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %31, i8* %7)
%33 = call i32 @execve(i8* %7, i8** %14, i8** %2)
br label %34
34: ; preds = %30, %26
%35 = phi i32 [ %29, %26 ], [ %33, %30 ]
%36 = phi i32 [ %27, %26 ], [ %31, %30 ]
%37 = phi i8* [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), %30 ]
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %37, i32 %36, i8* %7, i32 %35)
br label %39
39: ; preds = %34, %20
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %19)
%41 = call i32 @exit(i32 0)
unreachable
}