#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 *, char *);
scalar_t__ wait(int *);
int execute_external(char *args[]) {
  pid_t pid = fork();
  int status = 0;
  if (pid < 0) {
    printf("Process unable to be created. PID : %d.\n", pid);
    return -1;
  } else if (pid == 0) {
    if (execvp(*args, args) < 0) {
      printf("Running %s failed. Are you sure the program exists?\nExiting.\n",
             args[0]);
      return -1;
    }
  } else {
    while (wait(&status) != pid) {
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<execute_external>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1185 <execute_external+0x45>
mov    -0x18(%rbp),%rsi
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <execute_external+0xbc>
cmpq   $0x0,-0x18(%rbp)
jne    11ce <execute_external+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <execvp@plt>
cmp    $0x0,%rax
jge    11c9 <execute_external+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <execute_external+0xbc>
jmp    11f0 <execute_external+0xb0>
jmp    11d3 <execute_external+0x93>
lea    -0x1c(%rbp),%rdi
call   1050 <wait@plt>
cmp    -0x18(%rbp),%rax
je     11eb <execute_external+0xab>
jmp    11d3 <execute_external+0x93>
jmp    11f0 <execute_external+0xb0>
jmp    11f5 <execute_external+0xb5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_external>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%rbx
movl   $0x0,0x4(%rsp)
test   %rax,%rax
js     1181 <execute_external+0x41>
je     118d <execute_external+0x4d>
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1050 <wait@plt>
cmp    %rbx,%rax
jne    1170 <execute_external+0x30>
xor    %eax,%eax
jmp    11b6 <execute_external+0x76>
lea    0xe78(%rip),%rdi
mov    %rbx,%rsi
jmp    11ac <execute_external+0x6c>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1040 <execvp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    11b6 <execute_external+0x76>
mov    (%r14),%rsi
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Process unable to be created. PID : %d.\0A\00"
@.str.1 = [62 x i8] c"Running %s failed. Are you sure the program exists?\0AExiting.\0A\00"
define i32 @execute_external(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
store i8** %0, i8*** %3
%6 = call i64 (...) @fork()
store i64 %6, i64* %4
store i32 0, i32* %5
%7 = load i64, i64* %4
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i64, i64* %4
%11 = inttoptr i64 %10 to i8*
%12 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
br label %37
13: ; preds = %1
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = load i8**, i8*** %3
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %3
%20 = call i64 @execvp(i8* %18, i8** %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8**, i8*** %3
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 -1, i32* %2
br label %37
27: ; preds = %16
br label %35
28: ; preds = %13
br label %29
29: ; preds = %33, %28
%30 = call i64 @wait(i32* %5)
%31 = load i64, i64* %4
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %29
34: ; preds = %29
br label %35
35: ; preds = %34, %27
br label %36
36: ; preds = %35
store i32 0, i32* %2
br label %37
37: ; preds = %36, %22, %9
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [41 x i8] c"Process unable to be created. PID : %d.\0A\00"
@.str.1 = [62 x i8] c"Running %s failed. Are you sure the program exists?\0AExiting.\0A\00"
define i32 @execute_external(i8** %0) {
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = icmp slt i64 %3, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = inttoptr i64 %3 to i8*
%8 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %21
9: ; preds = %1
%10 = icmp eq i64 %3, 0
br i1 %10, label %11, label %18
11: ; preds = %9
%12 = load i8*, i8** %0
%13 = tail call i64 @execvp(i8* %12, i8** %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i8*, i8** %0
%17 = tail call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %21
18: ; preds = %9, %18
%19 = call i64 @wait(i32* %2)
%20 = icmp eq i64 %19, %3
br i1 %20, label %21, label %18
21: ; preds = %18, %11, %15, %6
%22 = phi i32 [ -1, %6 ], [ -1, %15 ], [ 0, %11 ], [ 0, %18 ]
ret i32 %22
}