#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 WEXITSTATUS(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int execvp(char *, char *const *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int execute(char *cmd_line, int quiet) {
  int pid, status, rc;
  const char *new_argv[4];
  new_argv[0] = "/bin/sh";
  new_argv[1] = "-c";
  new_argv[2] = cmd_line;
  new_argv[3] = NULL;
  pid = fork();
  if (pid == 0) {
    if (quiet)
      close(2);
    if (execvp("/bin/sh", (char *const *)new_argv) == -1) {
      do {
        printf("execvp(): %s", strerror(errno));
        printf("\n");
      } while (0);
    } else {
      do {
        printf("execvp() failed");
        printf("\n");
      } while (0);
    }
    exit(21);
  }
  rc = waitpid(pid, &status, 0);
  return (WEXITSTATUS(status));
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe67(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    125d <execute+0xdd>
cmpl   $0x0,-0xc(%rbp)
je     11dd <execute+0x5d>
mov    $0x2,%edi
call   1040 <close@plt>
lea    -0x40(%rbp),%rsi
lea    0xe18(%rip),%rdi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
jne    122d <execute+0xad>
jmp    11fb <execute+0x7b>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1253 <execute+0xd3>
jmp    1232 <execute+0xb2>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1253 <execute+0xd3>
mov    $0x15,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebx
lea    0xe62(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe5e(%rip),%rax
mov    %rax,0x18(%rsp)
mov    %rdi,0x20(%rsp)
movq   $0x0,0x28(%rsp)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11e5 <execute+0x55>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
add    $0x30,%rsp
pop    %rbx
ret
test   %ebx,%ebx
je     11f3 <execute+0x63>
mov    $0x2,%edi
call   1050 <close@plt>
lea    0xe06(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <execvp@plt>
cmp    $0xffffffff,%eax
jne    122a <execute+0x9a>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xded(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1238 <execute+0xa8>
lea    0xde7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0x15,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [13 x i8] c"execvp(): %s\00"
@errno = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [16 x i8] c"execvp() failed\00"
define i32 @execute(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i8*]
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %10
%11 = load i8*, i8** %3
%12 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 2
store i8* %11, i8** %12
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 3
store i8* null, i8** %13
%14 = call i32 (...) @fork()
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @close(i32 2)
br label %22
22: ; preds = %20, %17
%23 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%24 = call i32 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %22
br label %27
27: ; preds = %26
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %27
br label %38
33: ; preds = %22
br label %34
34: ; preds = %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %34
br label %38
38: ; preds = %37, %32
%39 = call i32 @exit(i32 21)
unreachable
40: ; preds = %2
%41 = load i32, i32* %5
%42 = call i32 @waitpid(i32 %41, i32* %6, i32 0)
store i32 %42, i32* %7
%43 = load i32, i32* %6
%44 = call i32 @WEXITSTATUS(i32 %43)
ret i32 %44
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [13 x i8] c"execvp(): %s\00"
@errno = global i32 0
@.str.4 = [16 x i8] c"execvp() failed\00"
define i32 @execute(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()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %2
%14 = icmp eq i32 %1, 0
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = tail call i32 @close(i32 2)
br label %17
17: ; preds = %15, %13
%18 = call i32 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %7)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %26
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = call i32 @putchar(i32 10)
%28 = call i32 @exit(i32 21)
unreachable
29: ; preds = %2
%30 = call i32 @waitpid(i32 %11, i32* %3, i32 0)
%31 = load i32, i32* %3
%32 = call i32 @WEXITSTATUS(i32 %31)
ret i32 %32
}