#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void execute_External(char **argv) {
  pid_t pid;
  int status;
  switch (pid = fork()) {
  case -1:
    perror("fork");
    break;
  case 0:
    execvp(argv[0], argv);
    perror("execvp");
    exit(1);
  default:
    break;
  }
  if (waitpid(pid, &status, 0) == -1) {
    perror("waitpid");
    return;
  }
  if (WIFSIGNALED(status))
    printf("Terminated by signal %d.\n", WTERMSIG(status));
  return;
}


x86 assembly (clang, -O0):
<execute_External>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11b9 <execute_External+0x39>
jmp    11a9 <execute_External+0x29>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11ca <execute_External+0x4a>
jmp    11f0 <execute_External+0x70>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
jmp    11f5 <execute_External+0x75>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11f5 <execute_External+0x75>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    121d <execute_External+0x9d>
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
jmp    124a <execute_External+0xca>
mov    -0x10(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1245 <execute_External+0xc5>
mov    -0x10(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xdd4(%rip),%rdi
call   1030 <printf@plt>
jmp    124a <execute_External+0xca>
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   10a0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11e7 <execute_External+0x67>
test   %ebx,%ebx
je     11f5 <execute_External+0x75>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11d9 <execute_External+0x59>
mov    0x4(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     11d1 <execute_External+0x51>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
lea    0xe4a(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
jmp    11d1 <execute_External+0x51>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
jmp    1199 <execute_External+0x19>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1080 <execvp@plt>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [7 x i8] c"execvp\00"
@.str.2 = [8 x i8] c"waitpid\00"
@.str.3 = [26 x i8] c"Terminated by signal %d.\0A\00"
define void @execute_External(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %12 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
7: ; preds = %1
%8 = load i8*, i8** %0
%9 = tail call i32 @execvp(i8* %8, i8** %0)
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1, %5
%13 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %25
17: ; preds = %12
%18 = load i32, i32* %2
%19 = call i64 @WIFSIGNALED(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i32, i32* %2
%23 = call i32 @WTERMSIG(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %17, %21, %15
ret void
}