#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;

/* Variables and functions */
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
char **parseCommandAndFillArgs(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ wait(int *);
int executeNonBuiltInCommand(char *command) {
  char **argsToExec = parseCommandAndFillArgs(command);
  int childStatus;
  pid_t waitPid;
  pid_t childPid = fork();
  if (childPid >= 0) {
    if (childPid == 0) {
      int execRetval = execvp(argsToExec[0], argsToExec);
      if (execRetval == -1) {
        return 1;
      }
    } else {
      waitPid = wait(&childStatus);
      if (waitPid == -1) {
        perror("wait() failed");
        exit(1);
      } else {
        printf("Command executed by child with pid=%d\n", waitPid);
      }
    }
  } else {
    printf("fork() failed.\n");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<executeNonBuiltInCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <parseCommandAndFillArgs@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jl     1222 <executeNonBuiltInCommand+0xb2>
cmpq   $0x0,-0x30(%rbp)
jne    11d8 <executeNonBuiltInCommand+0x68>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <execvp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11d3 <executeNonBuiltInCommand+0x63>
movl   $0x1,-0x4(%rbp)
jmp    1241 <executeNonBuiltInCommand+0xd1>
jmp    121d <executeNonBuiltInCommand+0xad>
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1206 <executeNonBuiltInCommand+0x96>
lea    0xe09(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121d <executeNonBuiltInCommand+0xad>
jmp    123a <executeNonBuiltInCommand+0xca>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executeNonBuiltInCommand>:
push   %rbx
sub    $0x10,%rsp
call   1050 <parseCommandAndFillArgs@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11dd <executeNonBuiltInCommand+0x5d>
je     11be <executeNonBuiltInCommand+0x3e>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffffffffffff,%rax
je     11f3 <executeNonBuiltInCommand+0x73>
lea    0xe5c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11ce <executeNonBuiltInCommand+0x4e>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
je     11d6 <executeNonBuiltInCommand+0x56>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%eax
jmp    11d0 <executeNonBuiltInCommand+0x50>
lea    0xe51(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe06(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"wait() failed\00"
@.str.1 = [39 x i8] c"Command executed by child with pid=%d\0A\00"
@.str.2 = [16 x i8] c"fork() failed.\0A\00"
define i32 @executeNonBuiltInCommand(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i8** @parseCommandAndFillArgs(i8* %9)
store i8** %10, i8*** %4
%11 = call i64 (...) @fork()
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = icmp sge i64 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %1
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = load i8**, i8*** %4
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %4
%22 = call i32 @execvp(i8* %20, i8** %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 1, i32* %2
br label %43
26: ; preds = %17
br label %38
27: ; preds = %14
%28 = call i64 @wait(i32* %5)
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = icmp eq i64 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i64, i64* %6
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %34
br label %38
38: ; preds = %37, %26
br label %42
39: ; preds = %1
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %38
store i32 0, i32* %2
br label %43
43: ; preds = %42, %25
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [14 x i8] c"wait() failed\00"
@.str.1 = [39 x i8] c"Command executed by child with pid=%d\0A\00"
@str = [15 x i8] c"fork() failed.\00"
define i32 @executeNonBuiltInCommand(i8* %0) {
%2 = alloca i32
%3 = tail call i8** @parseCommandAndFillArgs(i8* %0)
%4 = bitcast i32* %2 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp sgt i64 %5, -1
br i1 %6, label %7, label %21
7: ; preds = %1
%8 = icmp eq i64 %5, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = load i8*, i8** %3
%11 = tail call i32 @execvp(i8* %10, i8** %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %25, label %24
13: ; preds = %7
%14 = call i64 @wait(i32* %2)
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %14)
br label %24
21: ; preds = %1
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %19, %9
br label %25
25: ; preds = %9, %24
%26 = phi i32 [ 0, %24 ], [ 1, %9 ]
ret i32 %26
}