#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*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int WEXITSTATUS(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ execvp(char const *, char *const *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ log_msg(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int exec_cmd(const char *cmd, int quiet) {
  pid_t pid;
  int status, rc;
  const char *cmdargs[4];
  cmdargs[0] = "/bin/sh";
  cmdargs[1] = "-c";
  cmdargs[2] = cmd;
  cmdargs[3] = NULL;
  pid = fork();
  if (pid == 0) {
    if (quiet)
      close(2);
    log_msg(LOG_DEBUG, "Executing command %s\n", cmd);
    if (execvp(cmdargs[0], (char *const *)cmdargs) < 0) {
      log_msg(LOG_ERR, "Error executing %s: %s\n", cmd, strerror(errno));
      exit(1);
    }
  } else if (pid == -1) {
    log_msg(LOG_ERR, "Error spawning: %s: %s\n", cmd, strerror(errno));
    return -1;
  } else {
    rc = waitpid(pid, &status, 0);
  }
  return (WEXITSTATUS(status));
}


x86 assembly (clang, -O0):
<exec_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe67(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1257 <exec_cmd+0xd7>
cmpl   $0x0,-0x14(%rbp)
je     11dd <exec_cmd+0x5d>
mov    $0x2,%edi
call   1030 <close@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1040 <log_msg@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <execvp@plt>
cmp    $0x0,%rax
jge    1252 <exec_cmd+0xd2>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1040 <log_msg@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12bc <exec_cmd+0x13c>
cmpl   $0xffffffff,-0x18(%rbp)
jne    12a6 <exec_cmd+0x126>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1040 <log_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <exec_cmd+0x147>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x20(%rbp)
jmp    12bc <exec_cmd+0x13c>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cmd>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe6e(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe6a(%rip),%rax
mov    %rax,0x18(%rsp)
mov    %rdi,0x20(%rsp)
movq   $0x0,0x28(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11da <exec_cmd+0x5a>
test   %eax,%eax
je     1212 <exec_cmd+0x92>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    120b <exec_cmd+0x8b>
mov    0x2de7(%rip),%rax
mov    (%rax),%ebp
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe41(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <log_msg@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
test   %ebp,%ebp
je     1220 <exec_cmd+0xa0>
mov    $0x2,%edi
call   1030 <close@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <log_msg@plt>
lea    0xdbf(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <execvp@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%ebp
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb8(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <log_msg@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@LOG_DEBUG = global i32 0
@.str.2 = [22 x i8] c"Executing command %s\0A\00"
@LOG_ERR = global i32 0
@.str.3 = [24 x i8] c"Error executing %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [24 x i8] c"Error spawning: %s: %s\0A\00"
define i32 @exec_cmd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4 x i8*]
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %11
%12 = load i8*, i8** %4
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 2
store i8* %12, i8** %13
%14 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 3
store i8* null, i8** %14
%15 = call i32 (...) @fork()
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %40
18: ; preds = %2
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @close(i32 2)
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* @LOG_DEBUG
%25 = load i8*, i8** %4
%26 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 0
%30 = call i64 @execvp(i8* %28, i8** %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %23
%33 = load i32, i32* @LOG_ERR
%34 = load i8*, i8** %4
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %34, i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %23
br label %53
40: ; preds = %2
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i32, i32* @LOG_ERR
%45 = load i8*, i8** %4
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %44, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %45, i32 %47)
store i32 -1, i32* %3
br label %56
49: ; preds = %40
%50 = load i32, i32* %6
%51 = call i32 @waitpid(i32 %50, i32* %7, i32 0)
store i32 %51, i32* %8
br label %52
52: ; preds = %49
br label %53
53: ; preds = %52, %39
%54 = load i32, i32* %7
%55 = call i32 @WEXITSTATUS(i32 %54)
store i32 %55, i32* %3
br label %56
56: ; preds = %53, %43
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@LOG_DEBUG = global i32 0
@.str.2 = [22 x i8] c"Executing command %s\0A\00"
@LOG_ERR = global i32 0
@.str.3 = [24 x i8] c"Error executing %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [24 x i8] c"Error spawning: %s: %s\0A\00"
define i32 @exec_cmd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [4 x i8*]
%5 = bitcast i32* %3 to i8*
%6 = bitcast [4 x i8*]* %4 to i8*
%7 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 2
store i8* %0, i8** %9
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 3
store i8* null, i8** %10
%11 = tail call i32 (...) @fork()
switch i32 %11, label %31 [
i32 0, label %12
i32 -1, label %26
]
12: ; preds = %2
%13 = icmp eq i32 %1, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = tail call i32 @close(i32 2)
br label %16
16: ; preds = %14, %12
%17 = load i32, i32* @LOG_DEBUG
%18 = tail call i32 (i32, i8*, i8*, ...) @log_msg(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = call i64 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %7)
%20 = icmp slt i64 %19, 0
%21 = load i32, i32* @LOG_ERR
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 (i32, i8*, i8*, ...) @log_msg(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i32, i32* @LOG_ERR
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, i8*, ...) @log_msg(i32 %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %29)
br label %35
31: ; preds = %2
%32 = call i32 @waitpid(i32 %11, i32* %3, i32 0)
%33 = load i32, i32* %3
%34 = call i32 @WEXITSTATUS(i32 %33)
br label %35
35: ; preds = %31, %26
%36 = phi i32 [ %34, %31 ], [ -1, %26 ]
ret i32 %36
}