#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 /*<<< orphan*/ printf(char *);
int wait(int *);
int main() {
  int WaitStatus;
  int Index;
  char *ToolArgumentsArray[10];
  char *ToolName = "ls";
  pid_t pid;
  for (Index = 0; Index < 10; Index++) {
    ToolArgumentsArray[Index] = NULL;
  }
  ToolArgumentsArray[0] = ToolName;
  ToolArgumentsArray[1] = "-l";
  pid = fork();
  if (pid == -1) {
    printf("fork error\n");
  }
  if (pid == 0) {
    return execvp(ToolName, ToolArgumentsArray);
  } else {
    if (pid == wait(&WaitStatus)) {
      if (WaitStatus)
        printf("Child Execute Fail\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x68(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    1186 <main+0x46>
movslq -0xc(%rbp),%rax
movq   $0x0,-0x60(%rbp,%rax,8)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1161 <main+0x21>
mov    -0x68(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    0xe6e(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0xffffffff,-0x6c(%rbp)
jne    11b9 <main+0x79>
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x6c(%rbp)
jne    11d8 <main+0x98>
mov    -0x68(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1040 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    121b <main+0xdb>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x70(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <wait@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%eax
cmp    %ecx,%eax
jne    120f <main+0xcf>
cmpl   $0x0,-0x8(%rbp)
je     120a <main+0xca>
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
jmp    120f <main+0xcf>
jmp    1214 <main+0xd4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
lea    0xe9c(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe93(%rip),%rax
mov    %rax,0x18(%rsp)
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     11b8 <main+0x78>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1193 <main+0x53>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1050 <wait@plt>
xor    %ebx,%ebx
cmp    %eax,%ebp
jne    11cb <main+0x8b>
cmpl   $0x0,0xc(%rsp)
je     11cb <main+0x8b>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
jmp    11cb <main+0x8b>
lea    0xe41(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <execvp@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
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 = [20 x i8] c"Child Execute Fail\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [10 x i8*]
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5
store i32 0, i32* %3
br label %7
7: ; preds = %14, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 10
br i1 %9, label %10, label %17
10: ; preds = %7
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [10 x i8*], [10 x i8*]* %4, i64 0, i64 %12
store i8* null, i8** %13
br label %14
14: ; preds = %10
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
br label %7
17: ; preds = %7
%18 = load i8*, i8** %5
%19 = getelementptr inbounds [10 x i8*], [10 x i8*]* %4, i64 0, i64 0
store i8* %18, i8** %19
%20 = getelementptr inbounds [10 x i8*], [10 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %20
%21 = call i32 (...) @fork()
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = getelementptr inbounds [10 x i8*], [10 x i8*]* %4, i64 0, i64 0
%32 = call i32 @execvp(i8* %30, i8** %31)
store i32 %32, i32* %1
br label %45
33: ; preds = %26
%34 = load i32, i32* %6
%35 = call i32 @wait(i32* %2)
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* %2
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
br label %43
43: ; preds = %42, %33
br label %44
44: ; preds = %43
store i32 0, i32* %1
br label %45
45: ; preds = %44, %29
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str = [3 x i8] c"ls\00"
@.str.1 = [3 x i8] c"-l\00"
@str = [19 x i8] c"Child Execute Fail\00"
@str.4 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8*]
%3 = bitcast [10 x i8*]* %2 to i8*
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 2
%6 = bitcast i8** %5 to i8*
%7 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = 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** %8
%9 = tail call i32 (...) @fork()
switch i32 %9, label %14 [
i32 -1, label %10
i32 0, label %12
]
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
br label %14
12: ; preds = %0
%13 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7)
br label %22
14: ; preds = %10, %0
%15 = call i32 @wait(i32* %1)
%16 = icmp eq i32 %9, %15
%17 = load i32, i32* %1
%18 = icmp ne i32 %17, 0
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %20, %14, %12
%23 = phi i32 [ %13, %12 ], [ 0, %14 ], [ 0, %20 ]
ret i32 %23
}