#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 */
int execvp(char *, char **);
int fork();
int getpid();
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int wait(int *);
int main() {
  int WaitStatus;
  pid_t pid;
  int WaitSec = 0;
  char *ToolArgumentsArray[10];
  char *ToolName = "ls";
  memset((void *)ToolArgumentsArray, 0x00, 10);
  ToolArgumentsArray[0] = ToolName;
  ToolArgumentsArray[1] = "-l";
  pid = fork();
  if (pid == -1) {
    printf("fork error\n");
  }
  if (pid == 0) {
    printf("I am child\n");
    printf("In c pid = %d\n", pid);
    printf("Child PID = %d\n", getpid());
    return execvp(ToolName, ToolArgumentsArray);
  } else {
    printf("In p pid = %d\n", pid);
    if (pid != wait(&WaitStatus)) {
      printf("Parent Wait %d Second\n", ++WaitSec);
    }
    if (WaitStatus)
      printf("Child Execute Fail\n");
  }
  printf("Parent Exit\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
mov    -0x68(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c6 <main+0x66>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    121b <main+0xbb>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x68(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1060 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    1291 <main+0x131>
mov    -0xc(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x8(%rbp),%rdi
call   1070 <wait@plt>
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
cmp    %ecx,%eax
je     125f <main+0xff>
mov    -0x10(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
je     1277 <main+0x117>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127c <main+0x11c>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x60,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <memset@plt>
lea    0xe73(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe6a(%rip),%rax
mov    %rax,0x18(%rsp)
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1210 <main+0xa0>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bc <main+0x4c>
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
lea    0xe62(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
cmp    %eax,%ebx
je     11ed <main+0x7d>
lea    0xe53(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0xc(%rsp)
je     1200 <main+0x90>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1254 <main+0xe4>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
lea    0xde3(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xddb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdb6(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
add    $0x60,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.2 = [12 x i8] c"fork error\0A\00"
@.str.3 = [12 x i8] c"I am child\0A\00"
@.str.4 = [15 x i8] c"In c pid = %d\0A\00"
@.str.5 = [16 x i8] c"Child PID = %d\0A\00"
@.str.6 = [15 x i8] c"In p pid = %d\0A\00"
@.str.7 = [23 x i8] c"Parent Wait %d Second\0A\00"
@.str.8 = [20 x i8] c"Child Execute Fail\0A\00"
@.str.9 = [13 x i8] c"Parent Exit\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [10 x i8*]
%6 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
%8 = bitcast i8** %7 to i8*
%9 = call i32 @memset(i8* %8, i32 0, i32 10)
%10 = load i8*, i8** %6
%11 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
store i8* %10, i8** %11
%12 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %12
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %6
%28 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
%29 = call i32 @execvp(i8* %27, i8** %28)
store i32 %29, i32* %1
br label %48
30: ; preds = %18
%31 = load i32, i32* %3
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %3
%34 = call i32 @wait(i32* %2)
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %36, %30
%41 = load i32, i32* %2
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
br label %46
46: ; preds = %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %1
br label %48
48: ; preds = %46, %21
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@.str.4 = [15 x i8] c"In c pid = %d\0A\00"
@.str.5 = [16 x i8] c"Child PID = %d\0A\00"
@.str.6 = [15 x i8] c"In p pid = %d\0A\00"
@.str.7 = [23 x i8] c"Parent Wait %d Second\0A\00"
@str = [12 x i8] c"Parent Exit\00"
@str.10 = [19 x i8] c"Child Execute Fail\00"
@str.11 = [11 x i8] c"I am child\00"
@str.12 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8*]
%3 = bitcast i32* %1 to i8*
%4 = bitcast [10 x i8*]* %2 to i8*
%5 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 0
%6 = call i32 @memset(i8* %4, i32 0, i32 10)
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %7
%8 = call i32 (...) @fork()
switch i32 %8, label %17 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.12, i64 0, i64 0))
br label %17
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.11, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 0)
%14 = call i32 (...) @getpid()
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %14)
%16 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5)
br label %30
17: ; preds = %9, %0
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %8)
%19 = call i32 @wait(i32* %1)
%20 = icmp eq i32 %8, %19
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i32 1)
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* %1
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %28
28: ; preds = %23, %26
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %28, %11
%31 = phi i32 [ %16, %11 ], [ 0, %28 ]
ret i32 %31
}