#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free(char *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printError();
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void do_execute(char **shArgs, int do_redir, char *file) {
  int childPid;
  if (shArgs[0] == NULL)
    return;
  childPid = fork();
  if (childPid == 0) {
    int fd;
    if (do_redir) {
      close(STDOUT_FILENO);
      fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
      free(file);
      if (fd == -1) {
        printError();
        exit(1);
      }
    }
    execvp(shArgs[0], shArgs);
    printError();
    exit(1);
  } else if (childPid > 0) {
    if (do_redir) {
      free(file);
    }
    waitpid(childPid, NULL, 0);
  }
}


x86 assembly (clang, -O0):
<do_execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a6 <do_execute+0x26>
jmp    127c <do_execute+0xfc>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    124c <do_execute+0xcc>
cmpl   $0x0,-0xc(%rbp)
je     122b <do_execute+0xab>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1226 <do_execute+0xa6>
mov    $0x0,%al
call   1070 <printError@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    122b <do_execute+0xab>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
mov    $0x0,%al
call   1070 <printError@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    1277 <do_execute+0xf7>
cmpl   $0x0,-0xc(%rbp)
je     1269 <do_execute+0xe9>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    127c <do_execute+0xfc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_execute>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpq   $0x0,(%rdi)
je     11c5 <do_execute+0x45>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11d0 <do_execute+0x50>
jle    11c5 <do_execute+0x45>
mov    %eax,%ebx
test   %ebp,%ebp
je     11b0 <do_execute+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <waitpid@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %ebp,%ebp
je     121d <do_execute+0x9d>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e0f(%rip),%rax
mov    0x2dc8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
je     1228 <do_execute+0xa8>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1080 <execvp@plt>
xor    %eax,%eax
call   1070 <printError@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
define void @do_execute(i8** %0, i32 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8**, i8*** %4
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
br label %61
14: ; preds = %3
%15 = call i32 (...) @fork()
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %48
18: ; preds = %14
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %40
21: ; preds = %18
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @close(i32 %22)
%24 = load i8*, i8** %6
%25 = load i32, i32* @O_CREAT
%26 = load i32, i32* @O_WRONLY
%27 = or i32 %25, %26
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRWXU
%31 = call i32 @open(i8* %24, i32 %29, i32 %30)
store i32 %31, i32* %8
%32 = load i8*, i8** %6
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %21
%37 = call i32 (...) @printError()
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %21
br label %40
40: ; preds = %39, %18
%41 = load i8**, i8*** %4
%42 = getelementptr inbounds i8*, i8** %41, i64 0
%43 = load i8*, i8** %42
%44 = load i8**, i8*** %4
%45 = call i32 @execvp(i8* %43, i8** %44)
%46 = call i32 (...) @printError()
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %14
%49 = load i32, i32* %7
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = load i32, i32* %5
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %51
%58 = load i32, i32* %7
%59 = call i32 @waitpid(i32 %58, i32* null, i32 0)
br label %60
60: ; preds = %57, %48
br label %61
61: ; preds = %13, %60
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
define void @do_execute(i8** %0, i32 %1, i8* nocapture %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %39, label %6
6: ; preds = %3
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %31
9: ; preds = %6
%10 = icmp eq i32 %1, 0
br i1 %10, label %26, label %11
11: ; preds = %9
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @close(i32 %12)
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %15, %14
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRWXU
%20 = tail call i32 @open(i8* %2, i32 %18, i32 %19)
%21 = tail call i32 @free(i8* %2)
%22 = icmp eq i32 %20, -1
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = tail call i32 (...) @printError()
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %11, %9
%27 = load i8*, i8** %0
%28 = tail call i32 @execvp(i8* %27, i8** %0)
%29 = tail call i32 (...) @printError()
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %6
%32 = icmp sgt i32 %7, 0
br i1 %32, label %33, label %39
33: ; preds = %31
%34 = icmp eq i32 %1, 0
br i1 %34, label %37, label %35
35: ; preds = %33
%36 = tail call i32 @free(i8* %2)
br label %37
37: ; preds = %35, %33
%38 = tail call i32 @waitpid(i32 %7, i32* null, i32 0)
br label %39
39: ; preds = %37, %31, %3
ret void
}