#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int dup2(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int attach_standard_file_descriptors_to_null(void) {
  int null_fd_read, null_fd_write;
  return (((null_fd_read = open("/dev/null", O_RDONLY)) != -1) &&
          (dup2(null_fd_read, STDIN_FILENO) != -1) &&
          ((null_fd_write = open("/dev/null", O_WRONLY)) != -1) &&
          (dup2(null_fd_write, STDOUT_FILENO) != -1) &&
          (dup2(null_fd_write, STDERR_FILENO) != -1));
}


x86 assembly (clang, -O0):
<attach_standard_file_descriptors_to_null>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
lea    0xec8(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x4(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x9(%rbp)
je     11d4 <attach_standard_file_descriptors_to_null+0xb4>
mov    -0x4(%rbp),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x9(%rbp)
je     11d4 <attach_standard_file_descriptors_to_null+0xb4>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x9(%rbp)
je     11d4 <attach_standard_file_descriptors_to_null+0xb4>
mov    -0x8(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x9(%rbp)
je     11d4 <attach_standard_file_descriptors_to_null+0xb4>
mov    -0x8(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_standard_file_descriptors_to_null>:
push   %rbp
push   %rbx
push   %rax
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
lea    0xecd(%rip),%rdi
call   1040 <open@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     119d <attach_standard_file_descriptors_to_null+0x7d>
mov    0x2e82(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     119d <attach_standard_file_descriptors_to_null+0x7d>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     119d <attach_standard_file_descriptors_to_null+0x7d>
mov    %eax,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     119d <attach_standard_file_descriptors_to_null+0x7d>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
setne  %bl
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@O_WRONLY = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @attach_standard_file_descriptors_to_null() {
%1 = load i32, i32* @O_RDONLY
%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 %21, label %4
4: ; preds = %0
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i32 @dup2(i32 %2, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %21, label %8
8: ; preds = %4
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = load i32, i32* @STDOUT_FILENO
%14 = tail call i32 @dup2(i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i32, i32* @STDERR_FILENO
%18 = tail call i32 @dup2(i32 %10, i32 %17)
%19 = icmp ne i32 %18, -1
%20 = zext i1 %19 to i32
br label %21
21: ; preds = %16, %12, %8, %4, %0
%22 = phi i32 [ 0, %12 ], [ 0, %8 ], [ 0, %4 ], [ 0, %0 ], [ %20, %16 ]
ret i32 %22
}