#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 */
scalar_t__ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
void executeExecutable(char **argv, int wait) {
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    printf("ERROR: forking child process failed\n");
  } else if (pid == 0) {
    if (execvp(*argv, argv) < 0) {
      printf("ERROR: command exec failed\n");
    }
  } else {
    if (wait == 1) {
      waitpid(pid, NULL, 0);
    }
  }
}


x86 assembly (clang, -O0):
<executeExecutable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1175 <executeExecutable+0x35>
lea    0xe95(%rip),%rdi
call   1030 <printf@plt>
jmp    11ce <executeExecutable+0x8e>
cmpq   $0x0,-0x18(%rbp)
jne    11ab <executeExecutable+0x6b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <execvp@plt>
cmp    $0x0,%rax
jge    11a6 <executeExecutable+0x66>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
jmp    11c9 <executeExecutable+0x89>
cmpl   $0x1,-0xc(%rbp)
jne    11c4 <executeExecutable+0x84>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
jmp    11c9 <executeExecutable+0x89>
jmp    11ce <executeExecutable+0x8e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executeExecutable>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     116d <executeExecutable+0x2d>
je     1176 <executeExecutable+0x36>
cmp    $0x1,%ebp
jne    1186 <executeExecutable+0x46>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <waitpid@plt>
lea    0xea7(%rip),%rdi
jmp    1194 <executeExecutable+0x54>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1050 <execvp@plt>
test   %rax,%rax
js     118d <executeExecutable+0x4d>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6c(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"ERROR: forking child process failed\0A\00"
@.str.1 = [28 x i8] c"ERROR: command exec failed\0A\00"
define void @executeExecutable(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%7 = call i64 (...) @fork()
store i64 %7, i64* %5
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %31
11: ; preds = %2
%12 = load i64, i64* %5
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = load i8**, i8*** %3
%18 = call i64 @execvp(i8* %16, i8** %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
br label %30
23: ; preds = %11
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i64, i64* %5
%28 = call i32 @waitpid(i64 %27, i32* null, i32 0)
br label %29
29: ; preds = %26, %23
br label %30
30: ; preds = %29, %22
br label %31
31: ; preds = %30, %9
ret void
}

LLVM IR (-O2):
@str = [27 x i8] c"ERROR: command exec failed\00"
@str.2 = [36 x i8] c"ERROR: forking child process failed\00"
define void @executeExecutable(i8** %0, i32 %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.2, i64 0, i64 0))
br label %19
7: ; preds = %2
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %15
9: ; preds = %7
%10 = load i8*, i8** %0
%11 = tail call i64 @execvp(i8* %10, i8** %0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %19
15: ; preds = %7
%16 = icmp eq i32 %1, 1
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @waitpid(i64 %3, i32* null, i32 0)
br label %19
19: ; preds = %13, %9, %17, %15, %5
ret void
}