#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 */
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
char *getenv(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    exit(-1);
  } else if (pid == 0) {
    argv[1] = getenv("USER");
    if (execvp("./hello02", argv) < 0) {
      printf("fail :(\n");
      exit(-1);
    }
  } else {
    printf("child pid = %d\n", pid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1184 <main+0x34>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11dc <main+0x8c>
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1050 <execvp@plt>
cmp    $0x0,%rax
jge    11d7 <main+0x87>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    11ed <main+0x9d>
mov    -0x14(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f2 <main+0xa2>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1186 <main+0x26>
cmp    $0xffffffff,%eax
je     11ae <main+0x4e>
lea    0xe94(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11aa <main+0x4a>
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x8(%rbx)
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
call   1060 <execvp@plt>
test   %rax,%rax
js     11b8 <main+0x58>
xor    %eax,%eax
pop    %rbx
ret
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe60(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"USER\00"
@.str.1 = [10 x i8] c"./hello02\00"
@.str.2 = [9 x i8] c"fail :(\0A\00"
@.str.3 = [16 x i8] c"child pid = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
store i8* %16, i8** %18
%19 = load i8**, i8*** %5
%20 = call i64 @execvp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %15
br label %29
26: ; preds = %12
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %25
br label %30
30: ; preds = %29
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"USER\00"
@.str.1 = [10 x i8] c"./hello02\00"
@.str.3 = [16 x i8] c"child pid = %d\0A\00"
@str = [8 x i8] c"fail :(\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 (...) @fork()
switch i32 %3, label %14 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %2
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %2
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds i8*, i8** %1, i64 1
store i8* %7, i8** %8
%9 = tail call i64 @execvp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %3)
br label %16
16: ; preds = %14, %6
ret i32 0
}