#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 mode_t;

/* Variables and functions */
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
void redirfd(int redirfd, char *pathname, int flags, mode_t mode) {
  int fd;
  if (mode == -1) {
    fd = open(pathname, flags);
  } else {
    fd = open(pathname, flags, mode);
  }
  if (fd < 0) {
    perror("ERROR: Failed to operate file\n");
    exit(-1);
  }
  if ((dup2(fd, redirfd) == -1)) {
    perror("ERROR: Failed to redirect file descriptor\n");
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<redirfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1175 <redirfd+0x35>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
jmp    1189 <redirfd+0x49>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a9 <redirfd+0x69>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11d3 <redirfd+0x93>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirfd>:
push   %rbx
mov    %edi,%ebx
mov    %rsi,%rdi
mov    %edx,%esi
cmp    $0xffffffff,%ecx
je     116a <redirfd+0x2a>
mov    %ecx,%edx
xor    %eax,%eax
call   1040 <open@plt>
test   %eax,%eax
js     1175 <redirfd+0x35>
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     117e <redirfd+0x3e>
pop    %rbx
ret
xor    %eax,%eax
call   1040 <open@plt>
test   %eax,%eax
jns    115a <redirfd+0x1a>
lea    0xe84(%rip),%rdi
jmp    1185 <redirfd+0x45>
lea    0xe9a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [31 x i8] c"ERROR: Failed to operate file\0A\00"
@.str.1 = [43 x i8] c"ERROR: Failed to redirect file descriptor\0A\00"
define void @redirfd(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, -1
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %2)
br label %10
8: ; preds = %4
%9 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %2, i32 %3)
br label %10
10: ; preds = %8, %6
%11 = phi i32 [ %7, %6 ], [ %9, %8 ]
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %10
%17 = tail call i32 @dup2(i32 %11, i32 %0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %16
ret void
}