#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 */

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int run_external(char **command) {
  int pid = fork();
  if (pid == -1) {
    perror("fork");
    return 0;
  } else if (pid == 0) {
    execvp(command[0], command);
    perror(command[0]);
    exit(0);
  } else {
    int status;
    waitpid(pid, &status, 0);
    return status;
  }
}


x86 assembly (clang, -O0):
<run_external>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1188 <run_external+0x38>
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <run_external+0x79>
cmpl   $0x0,-0x14(%rbp)
jne    11b5 <run_external+0x65>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_external>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     1183 <run_external+0x33>
test   %eax,%eax
je     1191 <run_external+0x41>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    0x4(%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
jmp    117a <run_external+0x2a>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1050 <execvp@plt>
mov    (%rbx),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
define i32 @run_external(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
store i8** %0, i8*** %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %29
11: ; preds = %1
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = load i8**, i8*** %3
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = load i8**, i8*** %3
%19 = call i32 @execvp(i8* %17, i8** %18)
%20 = load i8**, i8*** %3
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 @perror(i8* %22)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %11
%26 = load i32, i32* %4
%27 = call i32 @waitpid(i32 %26, i32* %5, i32 0)
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %25, %9
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
define i32 @run_external(i8** %0) {
%2 = alloca i32
%3 = tail call i32 (...) @fork()
switch i32 %3, label %12 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i32 @execvp(i8* %7, i8** %0)
%9 = load i8*, i8** %0
%10 = tail call i32 @perror(i8* %9)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %1
%13 = bitcast i32* %2 to i8*
%14 = call i32 @waitpid(i32 %3, i32* %2, i32 0)
%15 = load i32, i32* %2
br label %16
16: ; preds = %12, %4
%17 = phi i32 [ 0, %4 ], [ %15, %12 ]
ret i32 %17
}