#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 const *,
                            int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int reopen_devnull(int fd) {
  FILE *f = NULL;
  FILE *f0 = NULL;
  const char *mode = "wb";
  switch (fd) {
  case 0:
    f0 = stdin;
    mode = "rb";
    break;
  case 1:
    f0 = stdout;
    break;
  case 2:
    f0 = stderr;
    break;
  };
  if (f0)
    f = freopen(_PATH_DEVNULL, mode, f0);
  return (f && fileno(f) == fd);
}


x86 assembly (clang, -O0):
<reopen_devnull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xebe(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     117b <reopen_devnull+0x5b>
jmp    1159 <reopen_devnull+0x39>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     1199 <reopen_devnull+0x79>
jmp    116a <reopen_devnull+0x4a>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     11ac <reopen_devnull+0x8c>
jmp    11ba <reopen_devnull+0x9a>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11ba <reopen_devnull+0x9a>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11ba <reopen_devnull+0x9a>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11df <reopen_devnull+0xbf>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <freopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x25(%rbp)
je     1201 <reopen_devnull+0xe1>
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
cmp    -0x4(%rbp),%eax
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reopen_devnull>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
cmp    $0x2,%edi
ja     1172 <reopen_devnull+0x52>
mov    %edi,%ebp
movslq %edi,%rax
lea    0x2cca(%rip),%rcx
mov    (%rcx,%rax,8),%rcx
mov    (%rcx),%rdx
test   %rdx,%rdx
je     1172 <reopen_devnull+0x52>
lea    0xebf(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1030 <freopen@plt>
test   %rax,%rax
je     1172 <reopen_devnull+0x52>
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):
@.str = [3 x i8] c"wb\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@stdout = global i32* null
@stderr = global i32* null
@_PATH_DEVNULL = global i32 0
define i32 @reopen_devnull(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
store i32 %0, i32* %2
store i32* null, i32** %3
store i32* null, i32** %4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i32, i32* %2
switch i32 %6, label %13 [
i32 0, label %7
i32 1, label %9
i32 2, label %11
]
7: ; preds = %1
%8 = load i32*, i32** @stdin
store i32* %8, i32** %4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %5
br label %13
9: ; preds = %1
%10 = load i32*, i32** @stdout
store i32* %10, i32** %4
br label %13
11: ; preds = %1
%12 = load i32*, i32** @stderr
store i32* %12, i32** %4
br label %13
13: ; preds = %1, %11, %9, %7
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @_PATH_DEVNULL
%18 = load i8*, i8** %5
%19 = load i32*, i32** %4
%20 = call i32* @freopen(i32 %17, i8* %18, i32* %19)
store i32* %20, i32** %3
br label %21
21: ; preds = %16, %13
%22 = load i32*, i32** %3
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32*, i32** %3
%26 = call i32 @fileno(i32* %25)
%27 = load i32, i32* %2
%28 = icmp eq i32 %26, %27
br label %29
29: ; preds = %24, %21
%30 = phi i1 [ false, %21 ], [ %28, %24 ]
%31 = zext i1 %30 to i32
ret i32 %31
}

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