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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ iface_restore();
int /*<<< orphan*/ iface_temporary_exit();
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void run_external_cmd(char **args, const int arg_num) {
  pid_t child;
  assert(args != NULL);
  assert(arg_num >= 1);
  iface_temporary_exit();
  child = fork();
  if (child == -1)
    error("fork() failed");
  else {
    int status;
    if (child == 0) {
      putchar('\n');
      execvp(args[0], args);
      fprintf(stderr, "\nError executing %s: %s\n", args[0], strerror(errno));
      sleep(2);
      exit(1);
    }
    waitpid(child, &status, 0);
    if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
      fprintf(stderr,
              "\nCommand exited with error "
              "(status %d).\n",
              WEXITSTATUS(status));
      sleep(2);
    }
    iface_restore();
  }
}


x86 assembly (clang, -O0):
<run_external_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x1,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    $0x0,%al
call   1090 <iface_temporary_exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    123e <run_external_cmd+0x5e>
lea    0xdcc(%rip),%rdi
call   1070 <error@plt>
jmp    131c <run_external_cmd+0x13c>
cmpl   $0x0,-0x10(%rbp)
jne    12b3 <run_external_cmd+0xd3>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10a0 <execvp@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1315 <run_external_cmd+0x135>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1315 <run_external_cmd+0x135>
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    $0x0,%al
call   1100 <iface_restore@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_external_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
call   1040 <assert@plt>
xor    %eax,%eax
call   1090 <iface_temporary_exit@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
je     127f <run_external_cmd+0x9f>
test   %eax,%eax
je     1291 <run_external_cmd+0xb1>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     1271 <run_external_cmd+0x91>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     1271 <run_external_cmd+0x91>
mov    0x2d96(%rip),%rax
mov    (%rax),%ebx
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xdcb(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
xor    %eax,%eax
call   1100 <iface_restore@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd7a(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <error@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10a0 <execvp@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rbx
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd47(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   10e0 <sleep@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fork() failed\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"\0AError executing %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"\0ACommand exited with error (status %d).\0A\00"
define void @run_external_cmd(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%7 = load i8**, i8*** %3
%8 = icmp ne i8** %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %4
%12 = icmp sge i32 %11, 1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32 (...) @iface_temporary_exit()
%16 = call i32 (...) @fork()
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %58
21: ; preds = %2
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = call i32 @putchar(i8 signext 10)
%26 = load i8**, i8*** %3
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %3
%30 = call i32 @execvp(i8* %28, i8** %29)
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %3
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %36)
%38 = call i32 @sleep(i32 2)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %21
%41 = load i32, i32* %5
%42 = call i32 @waitpid(i32 %41, i32* %6, i32 0)
%43 = load i32, i32* %6
%44 = call i64 @WIFEXITED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %40
%47 = load i32, i32* %6
%48 = call i32 @WEXITSTATUS(i32 %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = load i32, i32* %6
%53 = call i32 @WEXITSTATUS(i32 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = call i32 @sleep(i32 2)
br label %56
56: ; preds = %50, %46, %40
%57 = call i32 (...) @iface_restore()
br label %58
58: ; preds = %56, %19
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"fork() failed\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"\0AError executing %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"\0ACommand exited with error (status %d).\0A\00"
define void @run_external_cmd(i8** %0, i32 %1) {
%3 = alloca i32
%4 = icmp ne i8** %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp sgt i32 %1, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i32 (...) @iface_temporary_exit()
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
15: ; preds = %2
%16 = bitcast i32* %3 to i8*
%17 = icmp eq i32 %11, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = tail call i32 @putchar(i8 signext 10)
%20 = load i8*, i8** %0
%21 = tail call i32 @execvp(i8* %20, i8** %0)
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %0
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %25)
%27 = tail call i32 @sleep(i32 2)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %15
%30 = call i32 @waitpid(i32 %11, i32* %3, i32 0)
%31 = load i32, i32* %3
%32 = call i64 @WIFEXITED(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %44, label %34
34: ; preds = %29
%35 = load i32, i32* %3
%36 = call i32 @WEXITSTATUS(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i32, i32* %3
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = call i32 @sleep(i32 2)
br label %44
44: ; preds = %38, %34, %29
%45 = call i32 (...) @iface_restore()
br label %46
46: ; preds = %44, %13
ret void
}