#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*/ CloseErrors();
int /*<<< orphan*/ InitErrors();
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int /*<<< orphan*/);
void DetachStdio(void) {
  int nullfd;
  close(0);
  close(1);
  close(2);
  nullfd = open("/dev/null", O_RDWR);
  if (nullfd != 0) {
    dup2(nullfd, 0);
    close(nullfd);
  }
  dup2(0, 1);
  CloseErrors();
  InitErrors();
}


x86 assembly (clang, -O0):
<DetachStdio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11a7 <DetachStdio+0x57>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    $0x0,%al
call   1050 <CloseErrors@plt>
mov    $0x0,%al
call   1030 <InitErrors@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DetachStdio>:
push   %rbx
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
je     1197 <DetachStdio+0x47>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
call   1050 <CloseErrors@plt>
xor    %eax,%eax
pop    %rbx
jmp    1030 <InitErrors@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @DetachStdio() {
%1 = alloca i32
%2 = call i32 @close(i32 0)
%3 = call i32 @close(i32 1)
%4 = call i32 @close(i32 2)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* %1
%11 = call i32 @dup2(i32 %10, i32 0)
%12 = load i32, i32* %1
%13 = call i32 @close(i32 %12)
br label %14
14: ; preds = %9, %0
%15 = call i32 @dup2(i32 0, i32 1)
%16 = call i32 (...) @CloseErrors()
%17 = call i32 (...) @InitErrors()
ret void
}

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