#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ access2mode(int, char *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int setmode(int fd, int access) {
  char mode[30];
  FILE *f;
  switch (fd) {
  case 0:
    access |= 0x0001;
    fd = fileno(stdin);
    break;
  case 1:
    access |= 0x0002;
    fd = fileno(stdout);
    break;
  case 2:
    access |= 0x0002;
    fd = fileno(stderr);
    break;
  }
  access2mode(access, mode);
  f = freopen("", mode, (FILE *)fd);
  return (f == NULL ? -1 : fileno(f));
}


x86 assembly (clang, -O0):
<setmode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
test   %eax,%eax
je     1173 <setmode+0x43>
jmp    1151 <setmode+0x21>
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
je     1193 <setmode+0x63>
jmp    1162 <setmode+0x32>
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
je     11b3 <setmode+0x83>
jmp    11ce <setmode+0x9e>
mov    -0x8(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x4(%rbp)
jmp    11ce <setmode+0x9e>
mov    -0x8(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x4(%rbp)
jmp    11ce <setmode+0x9e>
mov    -0x8(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <access2mode@plt>
lea    -0x30(%rbp),%rsi
movslq -0x4(%rbp),%rdx
lea    0xe17(%rip),%rdi
call   1030 <freopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    120a <setmode+0xda>
mov    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
jmp    1216 <setmode+0xe6>
mov    -0x38(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setmode>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
cmp    $0x2,%edi
ja     1163 <setmode+0x33>
movslq %ebp,%rax
lea    0xeb9(%rip),%rcx
lea    0x2cb6(%rip),%rdx
mov    (%rdx,%rax,8),%rdx
or     (%rcx,%rax,4),%ebx
mov    (%rdx),%rdi
call   1040 <fileno@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <access2mode@plt>
movslq %ebp,%rdx
lea    0xe86(%rip),%rdi
mov    %r14,%rsi
call   1030 <freopen@plt>
test   %rax,%rax
je     1191 <setmode+0x61>
mov    %rax,%rdi
call   1040 <fileno@plt>
jmp    1196 <setmode+0x66>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32* null
@stdout = global i32* null
@stderr = global i32* null
@.str = [1 x i8] zeroinitializer
define i32 @setmode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [30 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
switch i32 %7, label %23 [
i32 0, label %8
i32 1, label %13
i32 2, label %18
]
8: ; preds = %2
%9 = load i32, i32* %4
%10 = or i32 %9, 1
store i32 %10, i32* %4
%11 = load i32*, i32** @stdin
%12 = call i32 @fileno(i32* %11)
store i32 %12, i32* %3
br label %23
13: ; preds = %2
%14 = load i32, i32* %4
%15 = or i32 %14, 2
store i32 %15, i32* %4
%16 = load i32*, i32** @stdout
%17 = call i32 @fileno(i32* %16)
store i32 %17, i32* %3
br label %23
18: ; preds = %2
%19 = load i32, i32* %4
%20 = or i32 %19, 2
store i32 %20, i32* %4
%21 = load i32*, i32** @stderr
%22 = call i32 @fileno(i32* %21)
store i32 %22, i32* %3
br label %23
23: ; preds = %2, %18, %13, %8
%24 = load i32, i32* %4
%25 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%26 = call i32 @access2mode(i32 %24, i8* %25)
%27 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to i32*
%31 = call i32* @freopen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %27, i32* %30)
store i32* %31, i32** %6
%32 = load i32*, i32** %6
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %23
br label %38
35: ; preds = %23
%36 = load i32*, i32** %6
%37 = call i32 @fileno(i32* %36)
br label %38
38: ; preds = %35, %34
%39 = phi i32 [ -1, %34 ], [ %37, %35 ]
ret i32 %39
}

LLVM IR (-O2):
@stdin = global i32* null
@stdout = global i32* null
@stderr = global i32* null
@.str = [1 x i8] zeroinitializer
@switch.table.setmode = [3 x i32] [i32 1, i32 2, i32 2]
@switch.table.setmode.1 = [3 x i32**] [i32** @stdin, i32** @stdout, i32** @stderr]
define i32 @setmode(i32 %0, i32 %1) {
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%5 = icmp ult i32 %0, 3
br i1 %5, label %6, label %16
6: ; preds = %2
%7 = sext i32 %0 to i64
%8 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.setmode, i64 0, i64 %7
%9 = load i32, i32* %8
%10 = sext i32 %0 to i64
%11 = getelementptr inbounds [3 x i32**], [3 x i32**]* @switch.table.setmode.1, i64 0, i64 %10
%12 = load i32**, i32*** %11
%13 = or i32 %9, %1
%14 = load i32*, i32** %12
%15 = tail call i32 @fileno(i32* %14)
br label %16
16: ; preds = %2, %6
%17 = phi i32 [ %1, %2 ], [ %13, %6 ]
%18 = phi i32 [ %0, %2 ], [ %15, %6 ]
%19 = call i32 @access2mode(i32 %17, i8* %4)
%20 = sext i32 %18 to i64
%21 = inttoptr i64 %20 to i32*
%22 = call i32* @freopen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %4, i32* %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %16
%25 = call i32 @fileno(i32* %22)
br label %26
26: ; preds = %16, %24
%27 = phi i32 [ %25, %24 ], [ -1, %16 ]
ret i32 %27
}