#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 close(int);
int dup2(int, int);
int /*<<< orphan*/ perror(char *);
int redirect(int fromfd, int tofd) {
  if (close(tofd) == -1) {
    perror("Error performing redirection - close");
    return 6;
  }
  if (dup2(fromfd, tofd) == -1) {
    perror("Error performing redirection - dup2");
    return 1;
  }
  if (close(fromfd) == -1) {
    perror("Error performing redirection - close");
    return 6;
  }
  return 0;
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1167 <redirect+0x37>
lea    0xeaa(%rip),%rdi
call   1050 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    11c3 <redirect+0x93>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1193 <redirect+0x63>
lea    0xea3(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c3 <redirect+0x93>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11bc <redirect+0x8c>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
movl   $0x6,-0x4(%rbp)
jmp    11c3 <redirect+0x93>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    %esi,%edi
call   1040 <close@plt>
mov    $0x6,%r15d
lea    0xeb1(%rip),%r14
cmp    $0xffffffff,%eax
je     118a <redirect+0x5a>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     117d <redirect+0x4d>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     118a <redirect+0x5a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r15d
lea    0xe9b(%rip),%r14
mov    %r14,%rdi
call   1050 <perror@plt>
mov    %r15d,%eax
jmp    1172 <redirect+0x42>

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

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