#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 */

/* Variables and functions */
int /*<<< orphan*/ execve(char *, char **, char **);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[], char *envp[]) {
  printf("\nIN Hello\n");
  if (argc > 1) {
    printf("\nIN Hello2\n");
    int pid1 = fork();
    if (pid1 == 0) {
      printf("In child Hello2\n");
      char *en[] = {"PATH=/rootfs/bin", "CWD=/rootfs", NULL};
      execve("bin/ps", argv, en);
    } else {
      waitpid(pid1, 0, 0);
      printf("Back in hello2 parent pid: %d\n", getpid());
    }
  } else {
    int pid = fork();
    if (pid == 0) {
      printf("In child Hello\n");
      char *args[] = {"bin/hello", "bin/ps", NULL};
      char *en[] = {"PATH=/rootfs/bin", "CWD=/rootfs", NULL};
      execve("bin/hello", args, en);
    } else {
      waitpid(pid, 0, 0);
      printf("Back in hello parent pid: %d\n", getpid());
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1217 <main+0xc7>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11ef <main+0x9f>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c04(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c01(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2bfe(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    0xe60(%rip),%rdi
call   1050 <execve@plt>
jmp    1212 <main+0xc2>
mov    -0x1c(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bc <main+0x16c>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1294 <main+0x144>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ba0(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2b9d(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0x2b9a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2b9f(%rip),%rax
mov    %rax,-0x80(%rbp)
mov    0x2b9c(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    0x2b99(%rip),%rax
mov    %rax,-0x70(%rbp)
lea    -0x60(%rbp),%rsi
lea    -0x80(%rbp),%rdx
lea    0xdf1(%rip),%rdi
call   1050 <execve@plt>
jmp    12b7 <main+0x167>
mov    -0x44(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bc <main+0x16c>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xef9(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jl     11ae <main+0x4e>
lea    0xf01(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11e0 <main+0x80>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rdi
jmp    11d2 <main+0x72>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1212 <main+0xb2>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1261 <main+0x101>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c1d(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c01(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe13(%rip),%rdi
mov    %rsp,%rdx
mov    %rbx,%rsi
jmp    125c <main+0xfc>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2bcb(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2baf(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    0x2bd4(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0x2bb8(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xdef(%rip),%rdi
mov    %rsp,%rsi
lea    0x20(%rsp),%rdx
call   1060 <execve@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"\0AIN Hello\0A\00"
@.str.1 = [12 x i8] c"\0AIN Hello2\0A\00"
@.str.2 = [17 x i8] c"In child Hello2\0A\00"
@.str.3 = [17 x i8] c"PATH=/rootfs/bin\00"
@.str.4 = [12 x i8] c"CWD=/rootfs\00"
@__const.main.en = [3 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str.5 = [7 x i8] c"bin/ps\00"
@.str.6 = [31 x i8] c"Back in hello2 parent pid: %d\0A\00"
@.str.7 = [16 x i8] c"In child Hello\0A\00"
@.str.8 = [10 x i8] c"bin/hello\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i32 0, i32 0), i8* null]
@__const.main.en.9 = [3 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str.10 = [30 x i8] c"Back in hello parent pid: %d\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 i32
%9 = alloca [3 x i8*]
%10 = alloca i32
%11 = alloca [3 x i8*]
%12 = alloca [3 x i8*]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %33
16: ; preds = %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 (...) @fork()
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%23 = bitcast [3 x i8*]* %9 to i8*
%24 = load i8**, i8*** %6
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %9, i64 0, i64 0
%26 = call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %24, i8** %25)
br label %32
27: ; preds = %16
%28 = load i32, i32* %8
%29 = call i32 @waitpid(i32 %28, i32 0, i32 0)
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %27, %21
br label %50
33: ; preds = %3
%34 = call i32 (...) @fork()
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%39 = bitcast [3 x i8*]* %11 to i8*
%40 = bitcast [3 x i8*]* %12 to i8*
%41 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%42 = getelementptr inbounds [3 x i8*], [3 x i8*]* %12, i64 0, i64 0
%43 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8** %41, i8** %42)
br label %49
44: ; preds = %33
%45 = load i32, i32* %10
%46 = call i32 @waitpid(i32 %45, i32 0, i32 0)
%47 = call i32 (...) @getpid()
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %44, %37
br label %50
50: ; preds = %49, %32
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [17 x i8] c"PATH=/rootfs/bin\00"
@.str.4 = [12 x i8] c"CWD=/rootfs\00"
@.str.5 = [7 x i8] c"bin/ps\00"
@.str.6 = [31 x i8] c"Back in hello2 parent pid: %d\0A\00"
@.str.8 = [10 x i8] c"bin/hello\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i32 0, i32 0), i8* null]
@__const.main.en.9 = [3 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str.10 = [30 x i8] c"Back in hello parent pid: %d\0A\00"
@str = [10 x i8] c"\0AIN Hello\00"
@str.11 = [15 x i8] c"In child Hello\00"
@str.12 = [11 x i8] c"\0AIN Hello2\00"
@str.13 = [16 x i8] c"In child Hello2\00"
define i32 @main(i32 %0, i8** %1, i8** nocapture readnone %2) {
%4 = alloca [3 x i8*]
%5 = alloca [3 x i8*]
%6 = alloca [3 x i8*]
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %22
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.12, i64 0, i64 0))
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.13, i64 0, i64 0))
%15 = bitcast [3 x i8*]* %4 to i8*
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%17 = call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %1, i8** %16)
br label %36
18: ; preds = %9
%19 = tail call i32 @waitpid(i32 %11, i32 0, i32 0)
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %20)
br label %36
22: ; preds = %3
%23 = tail call i32 (...) @fork()
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
%27 = bitcast [3 x i8*]* %5 to i8*
%28 = bitcast [3 x i8*]* %6 to i8*
%29 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%31 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8** %29, i8** %30)
br label %36
32: ; preds = %22
%33 = tail call i32 @waitpid(i32 %23, i32 0, i32 0)
%34 = tail call i32 (...) @getpid()
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.10, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %25, %32, %13, %18
ret i32 0
}