#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 O_APPEND;
int O_CREAT;
int O_WRONLY;
int STDERR_FILENO;
int STDOUT_FILENO;
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char const *, int, int);
scalar_t__ strcmp(char *, char const *);
void redirect(int oldfd, const char *target) {
  int targetfd;
  if (0 == strcmp("stdout", target)) {
    targetfd = STDOUT_FILENO;
  } else if (0 == strcmp("stderr", target)) {
    targetfd = STDERR_FILENO;
  } else {
    targetfd = open(target, O_WRONLY | O_APPEND | O_CREAT, 00640);
    if (-1 == targetfd) {
      exit(903);
    }
  }
  if (-1 == dup2(targetfd, oldfd)) {
    exit(904);
  }
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xea6(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    117e <redirect+0x3e>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11fb <redirect+0xbb>
mov    -0x10(%rbp),%rsi
lea    0xe7e(%rip),%rdi
call   1040 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11ad <redirect+0x6d>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11f6 <redirect+0xb6>
mov    -0x10(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
or     (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11f1 <redirect+0xb1>
mov    $0x387,%edi
call   1060 <exit@plt>
jmp    11f6 <redirect+0xb6>
jmp    11fb <redirect+0xbb>
mov    -0x14(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    121f <redirect+0xdf>
mov    $0x388,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb1(%rip),%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11a6 <redirect+0x66>
lea    0xea7(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11af <redirect+0x6f>
mov    0x2e4c(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a0,%edx
call   1050 <open@plt>
mov    %eax,%edi
cmp    $0xffffffff,%eax
jne    11b8 <redirect+0x78>
mov    $0x387,%edi
call   1060 <exit@plt>
mov    0x2e3b(%rip),%rax
jmp    11b6 <redirect+0x76>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11cb <redirect+0x8b>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x388,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"stdout\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [7 x i8] c"stderr\00"
@STDERR_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
define void @redirect(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = icmp eq i64 0, %7
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @STDOUT_FILENO
store i32 %10, i32* %5
br label %31
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* @STDERR_FILENO
store i32 %16, i32* %5
br label %30
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_APPEND
%21 = or i32 %19, %20
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %18, i32 %23, i32 416)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @exit(i32 903)
unreachable
29: ; preds = %17
br label %30
30: ; preds = %29, %15
br label %31
31: ; preds = %30, %9
%32 = load i32, i32* %5
%33 = load i32, i32* %3
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @exit(i32 904)
unreachable
38: ; preds = %31
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"stdout\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [7 x i8] c"stderr\00"
@STDERR_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
define void @redirect(i32 %0, i8* %1) {
%3 = tail call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @STDOUT_FILENO
br label %22
7: ; preds = %2
%8 = tail call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load i32, i32* @STDERR_FILENO
br label %22
12: ; preds = %7
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_APPEND
%15 = or i32 %14, %13
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %1, i32 %17, i32 416)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = tail call i32 @exit(i32 903)
unreachable
22: ; preds = %10, %12, %5
%23 = phi i32 [ %6, %5 ], [ %11, %10 ], [ %18, %12 ]
%24 = tail call i32 @dup2(i32 %23, i32 %0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @exit(i32 904)
unreachable
28: ; preds = %22
ret void
}