#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 */
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int dup2(int, int);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipexec_redir(const char *path, int fd, int flags, mode_t mode) {
  int o, d;
  close(fd);
  o = open(path, flags, mode);
  if (o == -1) {
    perror("open");
    return -1;
  } else if (o == fd)
    return 0;
  d = dup2(o, fd);
  close(o);
  if (d < 0) {
    perror("dup2");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipexec_redir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1191 <pipexec_redir+0x51>
lea    0xe80(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <pipexec_redir+0xad>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11a9 <pipexec_redir+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11ed <pipexec_redir+0xad>
jmp    11ae <pipexec_redir+0x6e>
mov    -0x20(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <dup2@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x24(%rbp)
jge    11e6 <pipexec_redir+0xa6>
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <pipexec_redir+0xad>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipexec_redir>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1199 <pipexec_redir+0x59>
mov    %eax,%ebx
xor    %r14d,%r14d
cmp    %ebp,%eax
je     118b <pipexec_redir+0x4b>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
js     11a2 <pipexec_redir+0x62>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe60(%rip),%rdi
jmp    11a9 <pipexec_redir+0x69>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
jmp    118b <pipexec_redir+0x4b>

LLVM IR (-O0):
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"dup2\00"
define i32 @pipexec_redir(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %7
%13 = call i32 @close(i32 %12)
%14 = load i8*, i8** %6
%15 = load i32, i32* %8
%16 = load i32, i32* %9
%17 = call i32 @open(i8* %14, i32 %15, i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %39
22: ; preds = %4
%23 = load i32, i32* %10
%24 = load i32, i32* %7
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* %5
br label %39
27: ; preds = %22
br label %28
28: ; preds = %27
%29 = load i32, i32* %10
%30 = load i32, i32* %7
%31 = call i32 @dup2(i32 %29, i32 %30)
store i32 %31, i32* %11
%32 = load i32, i32* %10
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %11
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %39
38: ; preds = %28
store i32 0, i32* %5
br label %39
39: ; preds = %38, %36, %26, %20
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@.str = [5 x i8] c"open\00"
@.str.1 = [5 x i8] c"dup2\00"
define i32 @pipexec_redir(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @close(i32 %1)
%6 = tail call i32 @open(i8* %0, i32 %2, i32 %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = icmp eq i32 %6, %1
br i1 %9, label %17, label %10
10: ; preds = %8
%11 = tail call i32 @dup2(i32 %6, i32 %1)
%12 = tail call i32 @close(i32 %6)
%13 = icmp slt i32 %11, 0
br i1 %13, label %14, label %17
14: ; preds = %10, %4
%15 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %10 ]
%16 = tail call i32 @perror(i8* %15)
br label %17
17: ; preds = %14, %10, %8
%18 = phi i32 [ 0, %8 ], [ 0, %10 ], [ -1, %14 ]
ret i32 %18
}