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


x86 assembly (clang, -O0):
<close_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    117d <close_terminal+0x3d>
lea    0xe8d(%rip),%rdi
call   1060 <perror@plt>
jmp    1231 <close_terminal+0xf1>
mov    -0x4(%rbp),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    11a9 <close_terminal+0x69>
lea    0xe70(%rip),%rdi
call   1060 <perror@plt>
jmp    1231 <close_terminal+0xf1>
mov    -0x4(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    11d5 <close_terminal+0x95>
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
jmp    1231 <close_terminal+0xf1>
mov    -0x4(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1201 <close_terminal+0xc1>
lea    0xe2f(%rip),%rdi
call   1060 <perror@plt>
jmp    1231 <close_terminal+0xf1>
mov    -0x4(%rbp),%eax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%eax
jle    1231 <close_terminal+0xf1>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1231 <close_terminal+0xf1>
lea    0xe06(%rip),%rdi
call   1060 <perror@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_terminal>:
push   %r14
push   %rbx
push   %rax
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
xor    %edx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11bb <close_terminal+0x7b>
mov    %eax,%ebx
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     11c4 <close_terminal+0x84>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     11cd <close_terminal+0x8d>
mov    0x2e3d(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     11d6 <close_terminal+0x96>
cmp    (%r14),%ebx
jle    11b3 <close_terminal+0x73>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     11df <close_terminal+0x9f>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3e(%rip),%rdi
jmp    11e6 <close_terminal+0xa6>
lea    0xe44(%rip),%rdi
jmp    11e6 <close_terminal+0xa6>
lea    0xe46(%rip),%rdi
jmp    11e6 <close_terminal+0xa6>
lea    0xe49(%rip),%rdi
jmp    11e6 <close_terminal+0xa6>
lea    0xe4c(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <perror@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"open /dev/null\00"
@STDIN_FILENO = global i32 0
@.str.2 = [11 x i8] c"dup2 stdin\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"dup2 stdout\00"
@STDERR_FILENO = global i32 0
@.str.4 = [12 x i8] c"dup2 stderr\00"
@.str.5 = [6 x i8] c"close\00"
define void @close_terminal() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2, i32 0)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %39
8: ; preds = %0
%9 = load i32, i32* %1
%10 = load i32, i32* @STDIN_FILENO
%11 = call i64 @dup2(i32 %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %39
15: ; preds = %8
%16 = load i32, i32* %1
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i64 @dup2(i32 %16, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %39
22: ; preds = %15
%23 = load i32, i32* %1
%24 = load i32, i32* @STDERR_FILENO
%25 = call i64 @dup2(i32 %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %39
29: ; preds = %22
%30 = load i32, i32* %1
%31 = load i32, i32* @STDERR_FILENO
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* %1
%35 = call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %39
39: ; preds = %6, %13, %20, %27, %37, %33, %29
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"open /dev/null\00"
@STDIN_FILENO = global i32 0
@.str.2 = [11 x i8] c"dup2 stdin\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"dup2 stdout\00"
@STDERR_FILENO = global i32 0
@.str.4 = [12 x i8] c"dup2 stderr\00"
@.str.5 = [6 x i8] c"close\00"
define void @close_terminal() {
%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, i32 0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %22, label %4
4: ; preds = %0
%5 = load i32, i32* @STDIN_FILENO
%6 = tail call i64 @dup2(i32 %2, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %4
%9 = load i32, i32* @STDOUT_FILENO
%10 = tail call i64 @dup2(i32 %2, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %8
%13 = load i32, i32* @STDERR_FILENO
%14 = tail call i64 @dup2(i32 %2, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* @STDERR_FILENO
%18 = icmp sgt i32 %2, %17
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = tail call i64 @close(i32 %2)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19, %12, %8, %4, %0
%23 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %8 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), %12 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %19 ]
%24 = tail call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %16, %19
ret void
}