#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__ close(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ perror(char *);
void redirect_stream(int fd_source, int fd_target) {
  if (fd_source == fd_target)
    return;
  if (dup2(fd_source, fd_target) < 0) {
    perror("redirect_stream:dup2");
    return;
  }
  if (close(fd_source) < 0) {
    perror("redirect_stream:close");
    return;
  }
}


x86 assembly (clang, -O0):
<redirect_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    114f <redirect_stream+0x1f>
jmp    1193 <redirect_stream+0x63>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1175 <redirect_stream+0x45>
lea    0xe95(%rip),%rdi
call   1050 <perror@plt>
jmp    1193 <redirect_stream+0x63>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1193 <redirect_stream+0x63>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_stream>:
push   %rbx
cmp    %esi,%edi
je     114d <redirect_stream+0x1d>
mov    %edi,%ebx
call   1030 <dup2@plt>
test   %rax,%rax
js     114f <redirect_stream+0x1f>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     115c <redirect_stream+0x2c>
pop    %rbx
ret
lea    0xeaa(%rip),%rdi
pop    %rbx
jmp    1050 <perror@plt>
lea    0xeb2(%rip),%rdi
pop    %rbx
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"redirect_stream:dup2\00"
@.str.1 = [22 x i8] c"redirect_stream:close\00"
define void @redirect_stream(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %3
%6 = load i32, i32* %4
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %9
8: ; preds = %2
br label %22
9: ; preds = %2
%10 = load i32, i32* %3
%11 = load i32, i32* %4
%12 = call i64 @dup2(i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %22
16: ; preds = %9
%17 = load i32, i32* %3
%18 = call i64 @close(i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %8, %14, %20, %16
ret void
}

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