#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 /*<<< orphan*/ EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ iface_restore();
int /*<<< orphan*/ iface_temporary_exit();
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ xsleep(int, int);
void run_external_cmd(char **args, const int arg_num) {
  pid_t child;
  assert(args != NULL);
  assert(arg_num >= 1);
  assert(args[0] != NULL);
  assert(args[arg_num] == NULL);
  iface_temporary_exit();
  child = fork();
  if (child == -1)
    error("fork() failed: %s", strerror(errno));
  else {
    int status;
    if (child == 0) {
      putchar('\n');
      execvp(args[0], args);
      fprintf(stderr, "\nError executing %s: %s\n", args[0], strerror(errno));
      xsleep(2, 1);
      exit(EXIT_FAILURE);
    }
    waitpid(child, &status, 0);
    if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
      fprintf(stderr, "\nCommand exited with error (status %d).\n",
              WEXITSTATUS(status));
      xsleep(2, 1);
    }
    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    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    $0x0,%al
call   10a0 <iface_temporary_exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    127e <run_external_cmd+0x9e>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd8c(%rip),%rdi
call   1080 <error@plt>
jmp    136a <run_external_cmd+0x18a>
cmpl   $0x0,-0x10(%rbp)
jne    12fc <run_external_cmd+0x11c>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <execvp@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xd3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <xsleep@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1363 <run_external_cmd+0x183>
mov    -0x14(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1363 <run_external_cmd+0x183>
mov    0x2cac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xcde(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <xsleep@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    %edi,%edi
cmpq   $0x0,(%rbx)
setne  %dil
call   1040 <assert@plt>
movslq %ebp,%rax
xor    %edi,%edi
cmpq   $0x0,(%rbx,%rax,8)
sete   %dil
call   1040 <assert@plt>
xor    %eax,%eax
call   10a0 <iface_temporary_exit@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
cmp    $0xffffffff,%eax
je     12aa <run_external_cmd+0xca>
test   %eax,%eax
je     12cd <run_external_cmd+0xed>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     129c <run_external_cmd+0xbc>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     129c <run_external_cmd+0xbc>
mov    0x2d70(%rip),%rax
mov    (%rax),%ebx
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xda9(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <xsleep@plt>
xor    %eax,%eax
call   1100 <iface_restore@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd41(%rip),%rdi
mov    %rax,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <error@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10b0 <execvp@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%rbx
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd0f(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
mov    $0x1,%esi
call   1060 <xsleep@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"fork() failed: %s\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"\0AError executing %s: %s\0A\00"
@EXIT_FAILURE = 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 = load i8**, i8*** %3
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = icmp ne i8* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i8**, i8*** %3
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = call i32 (...) @iface_temporary_exit()
%30 = call i32 (...) @fork()
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %2
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %35)
br label %75
37: ; preds = %2
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %37
%41 = call i32 @putchar(i8 signext 10)
%42 = load i8**, i8*** %3
%43 = getelementptr inbounds i8*, i8** %42, i64 0
%44 = load i8*, i8** %43
%45 = load i8**, i8*** %3
%46 = call i32 @execvp(i8* %44, i8** %45)
%47 = load i32, i32* @stderr
%48 = load i8**, i8*** %3
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %50, i8* %52)
%54 = call i32 @xsleep(i32 2, i32 1)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %37
%58 = load i32, i32* %5
%59 = call i32 @waitpid(i32 %58, i32* %6, i32 0)
%60 = load i32, i32* %6
%61 = call i64 @WIFEXITED(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %73
63: ; preds = %57
%64 = load i32, i32* %6
%65 = call i32 @WEXITSTATUS(i32 %64)
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %63
%68 = load i32, i32* @stderr
%69 = load i32, i32* %6
%70 = call i32 @WEXITSTATUS(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %70)
%72 = call i32 @xsleep(i32 2, i32 1)
br label %73
73: ; preds = %67, %63, %57
%74 = call i32 (...) @iface_restore()
br label %75
75: ; preds = %73, %33
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"fork() failed: %s\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"\0AError executing %s: %s\0A\00"
@EXIT_FAILURE = 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 = load i8*, i8** %0
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = sext i32 %1 to i64
%15 = getelementptr inbounds i8*, i8** %0, i64 %14
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i32 (...) @iface_temporary_exit()
%21 = tail call i32 (...) @fork()
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %2
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %59
27: ; preds = %2
%28 = bitcast i32* %3 to i8*
%29 = icmp eq i32 %21, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = tail call i32 @putchar(i8 signext 10)
%32 = load i8*, i8** %0
%33 = tail call i32 @execvp(i8* %32, i8** %0)
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %0
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %37)
%39 = tail call i32 @xsleep(i32 2, i32 1)
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %27
%43 = call i32 @waitpid(i32 %21, i32* %3, i32 0)
%44 = load i32, i32* %3
%45 = call i64 @WIFEXITED(i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %57, label %47
47: ; preds = %42
%48 = load i32, i32* %3
%49 = call i32 @WEXITSTATUS(i32 %48)
%50 = icmp eq i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = load i32, i32* %3
%54 = call i32 @WEXITSTATUS(i32 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = call i32 @xsleep(i32 2, i32 1)
br label %57
57: ; preds = %51, %47, %42
%58 = call i32 (...) @iface_restore()
br label %59
59: ; preds = %57, %23
ret void
}