#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int execve(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void execute(int input, int output, int toclose, char **args) {
  int pid = fork();
  if (pid == 0) {
    int res;
    if (toclose != -1)
      close(toclose);
    if (input != -1) {
      if (dup2(input, STDIN_FILENO) == -1) {
        fprintf(stderr, "Error duplicating input.\n");
        exit(EXIT_FAILURE);
      }
      close(input);
    }
    if (output != -1) {
      if (dup2(output, STDOUT_FILENO) == -1) {
        fprintf(stderr, "Error duplicating output.\n");
        exit(EXIT_FAILURE);
      }
      close(output);
    }
    res = execve(args[0], args, NULL);
    fprintf(stderr, "Error: %s\n", strerror(errno));
    exit(res);
  } else if (pid > 0) {
    if (input != -1)
      close(input);
    if (output != -1)
      close(output);
  } else {
    if (input != -1)
      close(input);
    if (output != -1)
      close(output);
  }
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    129a <execute+0x12a>
cmpl   $0xffffffff,-0xc(%rbp)
je     11ab <execute+0x3b>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x4(%rbp)
je     11fc <execute+0x8c>
mov    -0x4(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11f4 <execute+0x84>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     124d <execute+0xdd>
mov    -0x8(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1245 <execute+0xd5>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execve@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    12cd <execute+0x15d>
cmpl   $0xffffffff,-0x4(%rbp)
je     12b6 <execute+0x146>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     12c8 <execute+0x158>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    12f6 <execute+0x186>
cmpl   $0xffffffff,-0x4(%rbp)
je     12df <execute+0x16f>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     12f1 <execute+0x181>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    12f6 <execute+0x186>
jmp    12fb <execute+0x18b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11b9 <execute+0x49>
cmp    $0xffffffff,%ebp
je     1198 <execute+0x28>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     11ae <execute+0x3e>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0xffffffff,%r15d
je     11c7 <execute+0x57>
mov    %r15d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebp
je     11fa <execute+0x8a>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11f3 <execute+0x83>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
jmp    1224 <execute+0xb4>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     1240 <execute+0xd0>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1239 <execute+0xc9>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <execve@plt>
mov    %eax,%ebx
mov    0x2d82(%rip),%rax
mov    (%rax),%ebp
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Error duplicating input.\0A\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [27 x i8] c"Error duplicating output.\0A\00"
@.str.2 = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
define void @execute(i32 %0, i32 %1, i32 %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8** %3, i8*** %8
%11 = call i32 (...) @fork()
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %64
14: ; preds = %4
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %7
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %36
23: ; preds = %20
%24 = load i32, i32* %5
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %23
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %20
%37 = load i32, i32* %6
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %52
39: ; preds = %36
%40 = load i32, i32* %6
%41 = load i32, i32* @STDOUT_FILENO
%42 = call i32 @dup2(i32 %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %39
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %36
%53 = load i8**, i8*** %8
%54 = getelementptr inbounds i8*, i8** %53, i64 0
%55 = load i8*, i8** %54
%56 = load i8**, i8*** %8
%57 = call i32 @execve(i8* %55, i8** %56, i32* null)
store i32 %57, i32* %10
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %10
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %4
%65 = load i32, i32* %9
%66 = icmp sgt i32 %65, 0
br i1 %66, label %67, label %80
67: ; preds = %64
%68 = load i32, i32* %5
%69 = icmp ne i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32, i32* %5
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %67
%74 = load i32, i32* %6
%75 = icmp ne i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %76, %73
br label %93
80: ; preds = %64
%81 = load i32, i32* %5
%82 = icmp ne i32 %81, -1
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* %5
%85 = call i32 @close(i32 %84)
br label %86
86: ; preds = %83, %80
%87 = load i32, i32* %6
%88 = icmp ne i32 %87, -1
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
br label %92
92: ; preds = %89, %86
br label %93
93: ; preds = %92, %79
br label %94
94: ; preds = %93
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Error duplicating input.\0A\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [27 x i8] c"Error duplicating output.\0A\00"
@.str.2 = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
define void @execute(i32 %0, i32 %1, i32 %2, i8** %3) {
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %45
7: ; preds = %4
%8 = icmp eq i32 %2, -1
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = tail call i32 @close(i32 %2)
br label %11
11: ; preds = %9, %7
%12 = icmp eq i32 %0, -1
br i1 %12, label %24, label %13
13: ; preds = %11
%14 = load i32, i32* @STDIN_FILENO
%15 = tail call i32 @dup2(i32 %0, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %13
%23 = tail call i32 @close(i32 %0)
br label %24
24: ; preds = %22, %11
%25 = icmp eq i32 %1, -1
br i1 %25, label %37, label %26
26: ; preds = %24
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @dup2(i32 %1, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %26
%36 = tail call i32 @close(i32 %1)
br label %37
37: ; preds = %35, %24
%38 = load i8*, i8** %3
%39 = tail call i32 @execve(i8* %38, i8** %3, i32* null)
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = tail call i32 @exit(i32 %39)
unreachable
45: ; preds = %4
%46 = icmp sgt i32 %5, 0
%47 = icmp eq i32 %0, -1
br i1 %46, label %48, label %53
48: ; preds = %45
br i1 %47, label %51, label %49
49: ; preds = %48
%50 = tail call i32 @close(i32 %0)
br label %51
51: ; preds = %49, %48
%52 = icmp eq i32 %1, -1
br i1 %52, label %60, label %58
53: ; preds = %45
br i1 %47, label %56, label %54
54: ; preds = %53
%55 = tail call i32 @close(i32 %0)
br label %56
56: ; preds = %54, %53
%57 = icmp eq i32 %1, -1
br i1 %57, label %60, label %58
58: ; preds = %56, %51
%59 = tail call i32 @close(i32 %1)
br label %60
60: ; preds = %58, %51, %56
ret void
}