#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 /*<<< orphan*/ close(int const);
int dup(int);
int /*<<< orphan*/ dup2(int const, int);
int errno;
int execvp(char const *const, char **);
int /*<<< orphan*/ pm_error(char *, ...);
int /*<<< orphan*/ strerror(int);
void execProgram(const char *const progName, const char **const argArray,
                 int const stdinFd, int const stdoutFd) {
  int stdinSaveFd, stdoutSaveFd;
  int rc;
  int execErrno;
  if (stdinFd != 0) {
    stdinSaveFd = dup(0);
    close(0);
    dup2(stdinFd, 0);
    close(stdinFd);
  }
  if (stdoutFd != 1) {
    stdoutSaveFd = dup(1);
    close(1);
    dup2(stdoutFd, 1);
    close(stdoutFd);
  }
  rc = execvp(progName, (char **)argArray);
  execErrno = errno;
  if (stdinFd != 0) {
    close(0);
    dup2(stdinSaveFd, 0);
    close(stdinSaveFd);
  }
  if (stdoutFd != 1) {
    close(1);
    dup2(stdoutSaveFd, 1);
    close(stdoutSaveFd);
  }
  if (rc < 0)
    pm_error("Unable to exec '%s' "
             "(i.e. the program did not run at all).  "
             "execvp() errno=%d (%s)",
             progName, execErrno, strerror(execErrno));
  else
    pm_error("INTERNAL ERROR.  execvp() returns, but does not fail.");
}


x86 assembly (clang, -O0):
<execProgram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a3 <execProgram+0x43>
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x1c(%rbp)
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x1,-0x18(%rbp)
je     11d9 <execProgram+0x79>
mov    $0x1,%edi
call   1040 <dup@plt>
mov    %eax,-0x20(%rbp)
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
mov    %eax,-0x24(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1218 <execProgram+0xb8>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x1,-0x18(%rbp)
je     1241 <execProgram+0xe1>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jge    127d <execProgram+0x11d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1080 <pm_error@plt>
jmp    128b <execProgram+0x12b>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1080 <pm_error@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execProgram>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,%r13
mov    %rdi,%r14
test   %edx,%edx
je     124a <execProgram+0xea>
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%r15d
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0x1,%ebp
je     11ce <execProgram+0x6e>
mov    $0x1,%edi
call   1040 <dup@plt>
mov    %eax,0x4(%rsp)
mov    $0x1,%edi
call   1050 <close@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1060 <execvp@plt>
mov    %eax,%r12d
mov    0x2dfd(%rip),%rax
mov    (%rax),%r13d
test   %ebx,%ebx
je     1203 <execProgram+0xa3>
xor    %edi,%edi
call   1050 <close@plt>
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %r15d,%edi
call   1050 <close@plt>
cmp    $0x1,%ebp
je     1229 <execProgram+0xc9>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%ebx
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %r12d,%r12d
js     1258 <execProgram+0xf8>
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <pm_error@plt>
cmp    $0x1,%ebp
je     11ce <execProgram+0x6e>
jmp    11a3 <execProgram+0x43>
mov    %r13d,%edi
call   1070 <strerror@plt>
lea    0xd99(%rip),%rdi
mov    %r14,%rsi
mov    %r13d,%edx
mov    %eax,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <pm_error@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [83 x i8] c"Unable to exec '%s' (i.e. the program did not run at all). execvp() errno=%d (%s)\00"
@.str.1 = [54 x i8] c"INTERNAL ERROR. execvp() returns, but does not fail.\00"
define void @execProgram(i8* %0, i8** %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = call i32 @dup(i32 0)
store i32 %16, i32* %9
%17 = call i32 @close(i32 0)
%18 = load i32, i32* %7
%19 = call i32 @dup2(i32 %18, i32 0)
%20 = load i32, i32* %7
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %15, %4
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = call i32 @dup(i32 1)
store i32 %26, i32* %10
%27 = call i32 @close(i32 1)
%28 = load i32, i32* %8
%29 = call i32 @dup2(i32 %28, i32 1)
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %25, %22
%33 = load i8*, i8** %5
%34 = load i8**, i8*** %6
%35 = call i32 @execvp(i8* %33, i8** %34)
store i32 %35, i32* %11
%36 = load i32, i32* @errno
store i32 %36, i32* %12
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %32
%40 = call i32 @close(i32 0)
%41 = load i32, i32* %9
%42 = call i32 @dup2(i32 %41, i32 0)
%43 = load i32, i32* %9
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %39, %32
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 1
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = call i32 @close(i32 1)
%50 = load i32, i32* %10
%51 = call i32 @dup2(i32 %50, i32 1)
%52 = load i32, i32* %10
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %48, %45
%55 = load i32, i32* %11
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i8*, i8** %5
%59 = load i32, i32* %12
%60 = load i32, i32* %12
%61 = call i32 @strerror(i32 %60)
%62 = call i32 (i8*, ...) @pm_error(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0), i8* %58, i32 %59, i32 %61)
br label %65
63: ; preds = %54
%64 = call i32 (i8*, ...) @pm_error(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
br label %65
65: ; preds = %63, %57
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [83 x i8] c"Unable to exec '%s' (i.e. the program did not run at all). execvp() errno=%d (%s)\00"
@.str.1 = [54 x i8] c"INTERNAL ERROR. execvp() returns, but does not fail.\00"
define void @execProgram(i8* %0, i8** %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %2, 0
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = tail call i32 @dup(i32 0)
%8 = tail call i32 @close(i32 0)
%9 = tail call i32 @dup2(i32 %2, i32 0)
%10 = tail call i32 @close(i32 %2)
br label %11
11: ; preds = %6, %4
%12 = phi i32 [ %7, %6 ], [ undef, %4 ]
%13 = icmp eq i32 %3, 1
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i32 @dup(i32 1)
%16 = tail call i32 @close(i32 1)
%17 = tail call i32 @dup2(i32 %3, i32 1)
%18 = tail call i32 @close(i32 %3)
br label %19
19: ; preds = %14, %11
%20 = phi i32 [ %15, %14 ], [ undef, %11 ]
%21 = tail call i32 @execvp(i8* %0, i8** %1)
%22 = load i32, i32* @errno
br i1 %5, label %27, label %23
23: ; preds = %19
%24 = tail call i32 @close(i32 0)
%25 = tail call i32 @dup2(i32 %12, i32 0)
%26 = tail call i32 @close(i32 %12)
br label %27
27: ; preds = %23, %19
br i1 %13, label %32, label %28
28: ; preds = %27
%29 = tail call i32 @close(i32 1)
%30 = tail call i32 @dup2(i32 %20, i32 1)
%31 = tail call i32 @close(i32 %20)
br label %32
32: ; preds = %28, %27
%33 = icmp slt i32 %21, 0
br i1 %33, label %34, label %37
34: ; preds = %32
%35 = tail call i32 @strerror(i32 %22)
%36 = tail call i32 (i8*, ...) @pm_error(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %22, i32 %35)
br label %39
37: ; preds = %32
%38 = tail call i32 (i8*, ...) @pm_error(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
ret void
}