#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_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int);
void opendevconsole() {
  int fd;
  if ((fd = open("/dev/console", O_RDWR | O_NOCTTY)) >= 0) {
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    if (fd > 2)
      close(fd);
  }
}


x86 assembly (clang, -O0):
<opendevconsole>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jl     119d <opendevconsole+0x6d>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x4(%rbp)
jle    1198 <opendevconsole+0x68>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
jmp    119d <opendevconsole+0x6d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opendevconsole>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1183 <opendevconsole+0x53>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmp    $0x3,%ebx
jb     1183 <opendevconsole+0x53>
mov    %ebx,%edi
pop    %rbx
jmp    1040 <close@plt>
pop    %rbx
ret

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

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