#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_NOFOLLOW;
int O_RDONLY;
int O_RDWR;
int const STDERR_FILENO;
int const STDIN_FILENO;
int const STDOUT_FILENO;
scalar_t__ close(int const);
int errno;
int const open(char *, int);
int _redirect_std_streams(void) {
  int i, num_fds, oflag;
  const int fds[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
  num_fds = (sizeof((fds)) / sizeof((fds)[0]));
  for (i = 0; i < num_fds; ++i) {
    if (close(fds[i]) < 0)
      return -errno;
    oflag = O_NOFOLLOW | (fds[i] == STDIN_FILENO) ? O_RDONLY : O_RDWR;
    if (open("/dev/null", oflag) != fds[i])
      return -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_redirect_std_streams>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x3,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1224 <_redirect_std_streams+0x104>
movslq -0x8(%rbp),%rax
mov    -0x1c(%rbp,%rax,4),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    1190 <_redirect_std_streams+0x70>
xor    %eax,%eax
mov    0x2e3a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122b <_redirect_std_streams+0x10b>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
movslq -0x8(%rbp),%rcx
mov    -0x1c(%rbp,%rcx,4),%ecx
mov    0x2e08(%rip),%rdx
cmp    (%rdx),%ecx
sete   %cl
and    $0x1,%cl
movzbl %cl,%ecx
or     %ecx,%eax
cmp    $0x0,%eax
je     11cf <_redirect_std_streams+0xaf>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11db <_redirect_std_streams+0xbb>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe21(%rip),%rdi
call   1040 <open@plt>
movslq -0x8(%rbp),%rcx
cmp    -0x1c(%rbp,%rcx,4),%eax
je     1211 <_redirect_std_streams+0xf1>
xor    %eax,%eax
mov    0x2db9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122b <_redirect_std_streams+0x10b>
jmp    1216 <_redirect_std_streams+0xf6>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115a <_redirect_std_streams+0x3a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_redirect_std_streams>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
xor    %edi,%edi
call   1030 <close@plt>
test   %rax,%rax
js     119b <_redirect_std_streams+0x7b>
mov    0x2eaf(%rip),%r14
mov    (%r14),%esi
lea    0xec9(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
je     1159 <_redirect_std_streams+0x39>
mov    0x2e85(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11a6 <_redirect_std_streams+0x86>
xor    %edi,%edi
call   1030 <close@plt>
test   %rax,%rax
js     119b <_redirect_std_streams+0x7b>
mov    (%r14),%esi
lea    0xe9d(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
jne    114c <_redirect_std_streams+0x2c>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <close@plt>
test   %rax,%rax
js     119b <_redirect_std_streams+0x7b>
mov    (%r14),%esi
lea    0xe7c(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
jne    114c <_redirect_std_streams+0x2c>
jmp    11a6 <_redirect_std_streams+0x86>
mov    0x2e36(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = constant i32 0
@STDOUT_FILENO = constant i32 0
@STDERR_FILENO = constant i32 0
@errno = global i32 0
@O_NOFOLLOW = global i32 0
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@.str = [10 x i8] c"/dev/null\00"
define i32 @_redirect_std_streams() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [3 x i32]
%6 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%7 = load i32, i32* @STDIN_FILENO
store i32 %7, i32* %6
%8 = getelementptr inbounds i32, i32* %6, i64 1
%9 = load i32, i32* @STDOUT_FILENO
store i32 %9, i32* %8
%10 = getelementptr inbounds i32, i32* %8, i64 1
%11 = load i32, i32* @STDERR_FILENO
store i32 %11, i32* %10
store i32 3, i32* %3
store i32 0, i32* %2
br label %12
12: ; preds = %54, %0
%13 = load i32, i32* %2
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %57
16: ; preds = %12
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %18
%20 = load i32, i32* %19
%21 = call i64 @close(i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %1
br label %58
26: ; preds = %16
%27 = load i32, i32* @O_NOFOLLOW
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %29
%31 = load i32, i32* %30
%32 = load i32, i32* @STDIN_FILENO
%33 = icmp eq i32 %31, %32
%34 = zext i1 %33 to i32
%35 = or i32 %27, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = load i32, i32* @O_RDONLY
br label %41
39: ; preds = %26
%40 = load i32, i32* @O_RDWR
br label %41
41: ; preds = %39, %37
%42 = phi i32 [ %38, %37 ], [ %40, %39 ]
store i32 %42, i32* %4
%43 = load i32, i32* %4
%44 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %2
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = icmp ne i32 %44, %48
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = load i32, i32* @errno
%52 = sub nsw i32 0, %51
store i32 %52, i32* %1
br label %58
53: ; preds = %41
br label %54
54: ; preds = %53
%55 = load i32, i32* %2
%56 = add nsw i32 %55, 1
store i32 %56, i32* %2
br label %12
57: ; preds = %12
store i32 0, i32* %1
br label %58
58: ; preds = %57, %50, %23
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@STDIN_FILENO = constant i32 0
@STDOUT_FILENO = constant i32 0
@STDERR_FILENO = constant i32 0
@errno = global i32 0
@O_NOFOLLOW = global i32 0
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@.str = [10 x i8] c"/dev/null\00"
define i32 @_redirect_std_streams() {
%1 = tail call i64 @close(i32 0)
%2 = icmp slt i64 %1, 0
br i1 %2, label %17, label %20
3: ; preds = %20
%4 = tail call i64 @close(i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %6
%11 = tail call i64 @close(i32 0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %24
17: ; preds = %10, %3, %0
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %27
20: ; preds = %0
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %3, label %24
24: ; preds = %13, %6, %20
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
br label %27
27: ; preds = %13, %24, %17
%28 = phi i32 [ %19, %17 ], [ %26, %24 ], [ 0, %13 ]
ret i32 %28
}