#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*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int redirect_stdio_to_devnull(void) {
  int fd;
  fd = open("/dev/null", O_RDWR);
  if (fd == -1)
    return -1;
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  if (fd > 2)
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<redirect_stdio_to_devnull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1166 <redirect_stdio_to_devnull+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <redirect_stdio_to_devnull+0x82>
mov    -0x8(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x8(%rbp)
jle    11ab <redirect_stdio_to_devnull+0x7b>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_stdio_to_devnull>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
lea    0xebd(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     118f <redirect_stdio_to_devnull+0x5f>
mov    %eax,%ebx
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
xor    %ebp,%ebp
cmp    $0x3,%ebx
jl     1194 <redirect_stdio_to_devnull+0x64>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1194 <redirect_stdio_to_devnull+0x64>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @redirect_stdio_to_devnull() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %24
8: ; preds = %0
%9 = load i32, i32* %2
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @dup2(i32 %9, i32 %10)
%12 = load i32, i32* %2
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i32 @dup2(i32 %12, i32 %13)
%15 = load i32, i32* %2
%16 = load i32, i32* @STDERR_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i32, i32* %2
%19 = icmp sgt i32 %18, 2
br i1 %19, label %20, label %23
20: ; preds = %8
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %20, %8
store i32 0, i32* %1
br label %24
24: ; preds = %23, %7
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @redirect_stdio_to_devnull() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %14, label %4
4: ; preds = %0
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i32 @dup2(i32 %2, i32 %5)
%7 = load i32, i32* @STDOUT_FILENO
%8 = tail call i32 @dup2(i32 %2, i32 %7)
%9 = load i32, i32* @STDERR_FILENO
%10 = tail call i32 @dup2(i32 %2, i32 %9)
%11 = icmp sgt i32 %2, 2
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @close(i32 %2)
br label %14
14: ; preds = %4, %12, %0
%15 = phi i32 [ -1, %0 ], [ 0, %12 ], [ 0, %4 ]
ret i32 %15
}