#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcmp(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
void execcmd(char *cmd, int out, int mode) {
  int eStat = EXIT_SUCCESS;
  char *args[4];
  args[0] = "sh";
  args[1] = "-c";
  args[3] = NULL;
  if (!memcmp(cmd, "quit", 4)) {
    int nbytes = write(out, "quit", strlen("quit"));
    if (nbytes != strlen("quit")) {
      fprintf(stderr, "Error: Did not write properly, %d\n", nbytes);
      fprintf(stderr, "%s\n", strerror(errno));
      eStat = EXIT_FAILURE;
    }
    close(out);
    exit(eStat);
  } else {
    close(out);
    args[2] = cmd;
    execvp(*args, args);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<execcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe5b(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%eax
jne    1283 <execcmd+0x103>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    0xe1f(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    0xe0e(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
lea    0xdf9(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cmp    %ecx,%eax
je     1273 <execcmd+0xf3>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%edx
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<execcmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%r15d
lea    0xe5f(%rip),%rax
mov    %rax,(%rsp)
lea    0xe57(%rip),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xe45(%rip),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %eax,%eax
jne    1255 <execcmd+0xd5>
lea    0xe2c(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebx
je     1245 <execcmd+0xc5>
mov    0x2ddc(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xdfd(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x0(%rbp),%ebx
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdff(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%r15d
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1090 <exit@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %rbx,0x10(%rsp)
lea    0xd97(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execvp@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [5 x i8] c"quit\00"
@stderr = global i32 0
@.str.3 = [35 x i8] c"Error: Did not write properly, %d\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define void @execcmd(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i8*]
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* @EXIT_SUCCESS
store i32 %10, i32* %7
%11 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = 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** %12
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 3
store i8* null, i8** %13
%14 = load i8*, i8** %4
%15 = call i32 @memcmp(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%16 = icmp ne i32 %15, 0
br i1 %16, label %38, label %17
17: ; preds = %3
%18 = load i32, i32* %5
%19 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @write(i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i32, i32* %9
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @EXIT_FAILURE
store i32 %32, i32* %7
br label %33
33: ; preds = %24, %17
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %7
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %3
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %4
%42 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 2
store i8* %41, i8** %42
%43 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%44 = load i8*, i8** %43
%45 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%46 = call i32 @execvp(i8* %44, i8** %45)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [5 x i8] c"quit\00"
@stderr = global i32 0
@.str.3 = [35 x i8] c"Error: Did not write properly, %d\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define void @execcmd(i8* %0, i32 %1, i32 %2) {
%4 = alloca [4 x i8*]
%5 = load i32, i32* @EXIT_SUCCESS
%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 ([3 x i8], [3 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 3
store i8* null, i8** %9
%10 = tail call i32 @memcmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %3
%13 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @write(i32 %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32 %14, %15
br i1 %16, label %25, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
br label %25
25: ; preds = %17, %12
%26 = phi i32 [ %24, %17 ], [ %5, %12 ]
%27 = tail call i32 @close(i32 %1)
%28 = tail call i32 @exit(i32 %26)
unreachable
29: ; preds = %3
%30 = tail call i32 @close(i32 %1)
%31 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 2
store i8* %0, i8** %31
%32 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %7)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
}