#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);
int /*<<< orphan*/ open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
void Redirect(char *str, int rv) {
  switch (rv) {
  case 1:
    close(0);
    open(str, 00);
    break;
  case 2:
    close(1);
    open(str, 01 | 0100, 0644);
    break;
  case 3:
    close(1);
    open(str, 02000 | 01 | 0100, 0644);
    break;
  default:
    printf("Bad redirect!\n");
    break;
  }
}


x86 assembly (clang, -O0):
<Redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0x1,%eax
je     1175 <Redirect+0x45>
jmp    1153 <Redirect+0x23>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
je     118e <Redirect+0x5e>
jmp    1164 <Redirect+0x34>
mov    -0x10(%rbp),%eax
sub    $0x3,%eax
je     11b2 <Redirect+0x82>
jmp    11d6 <Redirect+0xa6>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1050 <open@plt>
jmp    11e2 <Redirect+0xb2>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
mov    $0x41,%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1050 <open@plt>
jmp    11e2 <Redirect+0xb2>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rdi
mov    $0x441,%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1050 <open@plt>
jmp    11e2 <Redirect+0xb2>
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Redirect>:
push   %rbx
mov    %rdi,%rbx
cmp    $0x3,%esi
je     1157 <Redirect+0x27>
cmp    $0x2,%esi
je     116b <Redirect+0x3b>
cmp    $0x1,%esi
jne    118a <Redirect+0x5a>
xor    %edi,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
pop    %rbx
jmp    1050 <open@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    $0x441,%esi
jmp    117d <Redirect+0x4d>
mov    $0x1,%edi
call   1040 <close@plt>
mov    %rbx,%rdi
mov    $0x41,%esi
mov    $0x1a4,%edx
xor    %eax,%eax
pop    %rbx
jmp    1050 <open@plt>
lea    0xe6f(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Bad redirect!\0A\00"
define void @Redirect(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
switch i32 %5, label %18 [
i32 1, label %6
i32 2, label %10
i32 3, label %14
]
6: ; preds = %2
%7 = call i32 @close(i32 0)
%8 = load i8*, i8** %3
%9 = call i32 (i8*, i32, ...) @open(i8* %8, i32 0)
br label %20
10: ; preds = %2
%11 = call i32 @close(i32 1)
%12 = load i8*, i8** %3
%13 = call i32 (i8*, i32, ...) @open(i8* %12, i32 65, i32 420)
br label %20
14: ; preds = %2
%15 = call i32 @close(i32 1)
%16 = load i8*, i8** %3
%17 = call i32 (i8*, i32, ...) @open(i8* %16, i32 1089, i32 420)
br label %20
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %14, %10, %6
ret void
}

LLVM IR (-O2):
@str = [14 x i8] c"Bad redirect!\00"
define void @Redirect(i8* nocapture %0, i32 %1) {
switch i32 %1, label %12 [
i32 1, label %3
i32 2, label %6
i32 3, label %9
]
3: ; preds = %2
%4 = tail call i32 @close(i32 0)
%5 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 0)
br label %14
6: ; preds = %2
%7 = tail call i32 @close(i32 1)
%8 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 65, i32 420)
br label %14
9: ; preds = %2
%10 = tail call i32 @close(i32 1)
%11 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 1089, i32 420)
br label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %12, %9, %6, %3
ret void
}