#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 */
scalar_t__ ENOENT;
int dup2(int, int);
scalar_t__ errno;
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
void exec_child(int fd_o, int fd_e, char **argv) {
  int r = dup2(fd_o, 1);
  do {
    if (r == -1) {
      perror("redirecting stdout");
      exit(1);
    }
  } while (0);
  r = dup2(fd_e, 2);
  do {
    if (r == -1) {
      perror("redirecting stderr");
      exit(1);
    }
  } while (0);
  r = execvp(*argv, argv);
  if (r == -1) {
    perror("executing command");
    exit(errno == ENOENT ? 127 : 126);
  }
}


x86 assembly (clang, -O0):
<exec_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1182 <exec_child+0x42>
lea    0xe8d(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1187 <exec_child+0x47>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b7 <exec_child+0x77>
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11bc <exec_child+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <execvp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120e <exec_child+0xce>
lea    0xe46(%rip),%rdi
call   1040 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%rcx
mov    0x2dda(%rip),%rax
mov    (%rax),%rdx
mov    $0x7e,%edi
mov    $0x7f,%eax
cmp    %rdx,%rcx
cmove  %eax,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_child>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     117f <exec_child+0x3f>
mov    %ebp,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1188 <exec_child+0x48>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1050 <execvp@plt>
cmp    $0xffffffff,%eax
je     119e <exec_child+0x5e>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7a(%rip),%rdi
jmp    118f <exec_child+0x4f>
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe81(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
sete   %dil
or     $0x7e,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"redirecting stdout\00"
@.str.1 = [19 x i8] c"redirecting stderr\00"
@.str.2 = [18 x i8] c"executing command\00"
@errno = global i64 0
@ENOENT = global i64 0
define void @exec_child(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%8 = load i32, i32* %4
%9 = call i32 @dup2(i32 %8, i32 1)
store i32 %9, i32* %7
br label %10
10: ; preds = %3
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
br label %17
17: ; preds = %16
%18 = load i32, i32* %5
%19 = call i32 @dup2(i32 %18, i32 2)
store i32 %19, i32* %7
br label %20
20: ; preds = %17
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %20
br label %27
27: ; preds = %26
%28 = load i8**, i8*** %6
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %6
%31 = call i32 @execvp(i8* %29, i8** %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %42
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 127, i32 126
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %27
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"redirecting stdout\00"
@.str.1 = [19 x i8] c"redirecting stderr\00"
@.str.2 = [18 x i8] c"executing command\00"
@errno = global i64 0
@ENOENT = global i64 0
define void @exec_child(i32 %0, i32 %1, i8** %2) {
%4 = tail call i32 @dup2(i32 %0, i32 1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = tail call i32 @dup2(i32 %1, i32 2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = load i8*, i8** %2
%17 = tail call i32 @execvp(i8* %16, i8** %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
%24 = select i1 %23, i32 127, i32 126
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %15
ret void
}