#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 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);
    printf("pid: %d -- %s has completed with status: %d\n", (int)my_id,
           file_name, 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    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11bf <main+0x3f>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <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   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1255 <main+0xd5>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x1030(%rbp),%rcx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x2c(%rbp),%rdi
call   1090 <wait@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    -0x1030(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ba <main+0x13a>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    -0x1030(%rbp),%rdx
lea    0xe08(%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    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
cmp    $0x1,%edi
jg     11b9 <main+0x29>
lea    0xf24(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <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   10b0 <fork@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%r15
test   %r12,%r12
jne    1233 <main+0xa3>
add    $0x8,%rbx
lea    0xe4a(%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>
mov    %eax,%ecx
lea    0xe56(%rip),%rdi
jmp    1261 <main+0xd1>
lea    0xdc6(%rip),%rdi
lea    0x10(%rsp),%rcx
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
lea    0xdc8(%rip),%rdi
mov    0xc(%rsp),%ecx
lea    0x10(%rsp),%rdx
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3e(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <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 %42
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 i64, i64* %9
%38 = trunc i64 %37 to i32
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%40 = load i32, i32* %10
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %38, i8* %39, i32 %40)
br label %58
42: ; preds = %19
%43 = call i64 (...) @getpid()
store i64 %43, i64* %9
%44 = load i64, i64* %9
%45 = trunc i64 %44 to i32
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %45, i8* %46)
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%49 = load i8**, i8*** %6
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8**, i8*** %7
%52 = call i32 @execve(i8* %48, i8** %50, i8** %51)
store i32 %52, i32* %13
%53 = load i64, i64* %9
%54 = trunc i64 %53 to i32
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%56 = load i32, i32* %13
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i32 %54, i8* %55, i32 %56)
br label %58
58: ; preds = %42, %30
%59 = load i64, i64* %9
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %59)
%61 = 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 %25, 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 = trunc i64 %19 to i32
%24 = load i32, i32* %4
br label %29
25: ; preds = %12
%26 = trunc i64 %19 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %26, i8* %7)
%28 = call i32 @execve(i8* %7, i8** %14, i8** %2)
br label %29
29: ; preds = %25, %20
%30 = phi i32 [ %28, %25 ], [ %24, %20 ]
%31 = phi i32 [ %26, %25 ], [ %23, %20 ]
%32 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), %25 ], [ getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), %20 ]
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %32, i32 %31, i8* %7, i32 %30)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %19)
%35 = call i32 @exit(i32 0)
unreachable
}