#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);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void redirect(int fd, int target_fd, const char *name) {
  if (fd == target_fd)
    return;
  if (dup2(fd, target_fd) < 0) {
    fprintf(stderr, "unable to open %s from internal pipe: %s\n", name,
            strerror(errno));
    exit(1);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1173 <redirect+0x23>
jmp    11d2 <redirect+0x82>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    11ca <redirect+0x7a>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe45(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %rbx
push   %rax
cmp    %esi,%edi
jne    115e <redirect+0xe>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %rdx,%rbx
mov    %edi,%ebp
call   1030 <dup2@plt>
test   %rax,%rax
js     117a <redirect+0x2a>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe68(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"unable to open %s from internal pipe: %s\0A\00"
@errno = global i32 0
define void @redirect(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%7 = load i32, i32* %4
%8 = load i32, i32* %5
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %3
br label %26
11: ; preds = %3
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = call i64 @dup2(i32 %12, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %6
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %11
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23, %10
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"unable to open %s from internal pipe: %s\0A\00"
@errno = global i32 0
define void @redirect(i32 %0, i32 %1, i8* %2) {
%4 = icmp eq i32 %0, %1
br i1 %4, label %16, label %5
5: ; preds = %3
%6 = tail call i64 @dup2(i32 %0, i32 %1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = tail call i32 @close(i32 %0)
br label %16
16: ; preds = %3, %14
ret void
}