#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*/ _PATH_DEVNULL;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int open_devnull(int fd) {
  FILE *f = 0;
  if (!fd) {
    f = freopen(_PATH_DEVNULL, "rb", stdin);
  } else if (fd == 1) {
    f = freopen(_PATH_DEVNULL, "wb", stdout);
  } else if (fd == 2) {
    f = freopen(_PATH_DEVNULL, "wb", stderr);
  }
  return (f && fileno(f) == fd);
}


x86 assembly (clang, -O0):
<open_devnull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1164 <open_devnull+0x44>
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
lea    0xeaa(%rip),%rsi
call   1030 <freopen@plt>
mov    %rax,-0x10(%rbp)
jmp    11cb <open_devnull+0xab>
cmpl   $0x1,-0x4(%rbp)
jne    1195 <open_devnull+0x75>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
lea    0xe7c(%rip),%rsi
call   1030 <freopen@plt>
mov    %rax,-0x10(%rbp)
jmp    11c6 <open_devnull+0xa6>
cmpl   $0x2,-0x4(%rbp)
jne    11c1 <open_devnull+0xa1>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
lea    0xe4b(%rip),%rsi
call   1030 <freopen@plt>
mov    %rax,-0x10(%rbp)
jmp    11c6 <open_devnull+0xa6>
jmp    11cb <open_devnull+0xab>
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
je     11ed <open_devnull+0xcd>
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
cmp    -0x4(%rbp),%eax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_devnull>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
cmp    $0x2,%edi
ja     116c <open_devnull+0x4c>
mov    %edi,%ebp
movslq %edi,%rax
lea    0x2cca(%rip),%rcx
mov    (%rcx,%rax,8),%rcx
lea    0xec7(%rip),%rdx
movslq (%rdx,%rax,4),%rsi
add    %rdx,%rsi
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    (%rcx),%edx
call   1030 <freopen@plt>
test   %rax,%rax
je     116c <open_devnull+0x4c>
mov    %rax,%rdi
call   1040 <fileno@plt>
xor    %ebx,%ebx
cmp    %ebp,%eax
sete   %bl
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@_PATH_DEVNULL = global i32 0
@.str = [3 x i8] c"rb\00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"wb\00"
@stdout = global i32 0
@stderr = global i32 0
define i32 @open_devnull(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
store i32 %0, i32* %2
store i32* null, i32** %3
%4 = load i32, i32* %2
%5 = icmp ne i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = load i32, i32* @_PATH_DEVNULL
%8 = load i32, i32* @stdin
%9 = call i32* @freopen(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32* %9, i32** %3
br label %26
10: ; preds = %1
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @_PATH_DEVNULL
%15 = load i32, i32* @stdout
%16 = call i32* @freopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32* %16, i32** %3
br label %25
17: ; preds = %10
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @_PATH_DEVNULL
%22 = load i32, i32* @stderr
%23 = call i32* @freopen(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32* %23, i32** %3
br label %24
24: ; preds = %20, %17
br label %25
25: ; preds = %24, %13
br label %26
26: ; preds = %25, %6
%27 = load i32*, i32** %3
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32*, i32** %3
%31 = call i32 @fileno(i32* %30)
%32 = load i32, i32* %2
%33 = icmp eq i32 %31, %32
br label %34
34: ; preds = %29, %26
%35 = phi i1 [ false, %26 ], [ %33, %29 ]
%36 = zext i1 %35 to i32
ret i32 %36
}

LLVM IR (-O2):
@_PATH_DEVNULL = global i32 0
@.str = [3 x i8] c"rb\00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"wb\00"
@stdout = global i32 0
@stderr = global i32 0
@switch.table.open_devnull = [3 x i32*] [i32* @stdin, i32* @stdout, i32* @stderr]
@reltable.open_devnull = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.open_devnull to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.open_devnull to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.open_devnull to i64)) to i32)]
define i32 @open_devnull(i32 %0) {
%2 = icmp ult i32 %0, 3
br i1 %2, label %3, label %18
3: ; preds = %1
%4 = sext i32 %0 to i64
%5 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.open_devnull, i64 0, i64 %4
%6 = load i32*, i32** %5
%7 = sext i32 %0 to i64
%8 = shl i64 %7, 2
%9 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.open_devnull to i8*), i64 %8)
%10 = load i32, i32* @_PATH_DEVNULL
%11 = load i32, i32* %6
%12 = tail call i32* @freopen(i32 %10, i8* %9, i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = tail call i32 @fileno(i32* %12)
%16 = icmp eq i32 %15, %0
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %1, %14, %3
%19 = phi i32 [ 0, %3 ], [ %17, %14 ], [ 0, %1 ]
ret i32 %19
}