#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*/ F_GETFD;
int O_NOCTTY;
int O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
scalar_t__ fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ grantpt(int);
int open(char *, int);
int posix_openpt(int);
int /*<<< orphan*/ unlockpt(int);
int getconsole(void) {
  int master, null;
  if ((null = open("/dev/null", O_RDWR)) < 0)
    error(1, 0, "Failed to open /dev/null");
  if (fcntl(STDIN_FILENO, F_GETFD) < 0)
    dup2(null, STDIN_FILENO);
  if (fcntl(STDOUT_FILENO, F_GETFD) < 0)
    dup2(null, STDOUT_FILENO);
  if (fcntl(STDERR_FILENO, F_GETFD) < 0)
    dup2(null, STDERR_FILENO);
  if (null != STDIN_FILENO)
    if (null != STDOUT_FILENO)
      if (null != STDERR_FILENO)
        close(null);
  if ((master = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
    error(1, 0, "Failed to allocate a console pseudo-terminal");
  grantpt(master);
  unlockpt(master);
  return master;
}


x86 assembly (clang, -O0):
<getconsole>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11bc <getconsole+0x3c>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe49(%rip),%rdx
call   1070 <error@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11ee <getconsole+0x6e>
mov    -0x8(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1220 <getconsole+0xa0>
mov    -0x8(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1252 <getconsole+0xd2>
mov    -0x8(%rbp),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%eax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%eax
je     129a <getconsole+0x11a>
mov    -0x8(%rbp),%eax
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%eax
je     1295 <getconsole+0x115>
mov    -0x8(%rbp),%eax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%eax
je     1290 <getconsole+0x110>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    1295 <getconsole+0x115>
jmp    129a <getconsole+0x11a>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    0x2d26(%rip),%rax
or     (%rax),%edi
call   10a0 <posix_openpt@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    12d0 <getconsole+0x150>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd4e(%rip),%rdx
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
call   1090 <grantpt@plt>
mov    -0x4(%rbp),%edi
call   1060 <unlockpt@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getconsole>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4e(%rip),%r14
mov    (%r14),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ba <getconsole+0x3a>
lea    0xe52(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <error@plt>
mov    0x2df7(%rip),%r15
mov    (%r15),%edi
mov    0x2dfd(%rip),%r13
mov    0x0(%r13),%esi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    11e3 <getconsole+0x63>
mov    (%r15),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2e06(%rip),%r12
mov    (%r12),%edi
mov    0x0(%r13),%esi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1207 <getconsole+0x87>
mov    (%r12),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2db2(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x0(%r13),%esi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1229 <getconsole+0xa9>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    (%r15),%ebx
je     1240 <getconsole+0xc0>
cmp    (%r12),%ebx
je     1240 <getconsole+0xc0>
cmp    0x0(%rbp),%ebx
je     1240 <getconsole+0xc0>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
or     (%r14),%edi
call   10a0 <posix_openpt@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    126a <getconsole+0xea>
lea    0xdbb(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <error@plt>
mov    %ebx,%edi
call   1090 <grantpt@plt>
mov    %ebx,%edi
call   1060 <unlockpt@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"Failed to open /dev/null\00"
@STDIN_FILENO = global i32 0
@F_GETFD = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [45 x i8] c"Failed to allocate a console pseudo-terminal\00"
define i32 @getconsole() {
%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 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @STDIN_FILENO
%10 = load i32, i32* @F_GETFD
%11 = call i64 @fcntl(i32 %9, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i32, i32* %2
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
br label %17
17: ; preds = %13, %8
%18 = load i32, i32* @STDOUT_FILENO
%19 = load i32, i32* @F_GETFD
%20 = call i64 @fcntl(i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* %2
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
br label %26
26: ; preds = %22, %17
%27 = load i32, i32* @STDERR_FILENO
%28 = load i32, i32* @F_GETFD
%29 = call i64 @fcntl(i32 %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* %2
%33 = load i32, i32* @STDERR_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
br label %35
35: ; preds = %31, %26
%36 = load i32, i32* %2
%37 = load i32, i32* @STDIN_FILENO
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %52
39: ; preds = %35
%40 = load i32, i32* %2
%41 = load i32, i32* @STDOUT_FILENO
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i32, i32* %2
%45 = load i32, i32* @STDERR_FILENO
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %43
br label %51
51: ; preds = %50, %39
br label %52
52: ; preds = %51, %35
%53 = load i32, i32* @O_RDWR
%54 = load i32, i32* @O_NOCTTY
%55 = or i32 %53, %54
%56 = call i32 @posix_openpt(i32 %55)
store i32 %56, i32* %1
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %60
60: ; preds = %58, %52
%61 = load i32, i32* %1
%62 = call i32 @grantpt(i32 %61)
%63 = load i32, i32* %1
%64 = call i32 @unlockpt(i32 %63)
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"Failed to open /dev/null\00"
@STDIN_FILENO = global i32 0
@F_GETFD = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [45 x i8] c"Failed to allocate a console pseudo-terminal\00"
define i32 @getconsole() {
%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 slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_GETFD
%9 = tail call i64 @fcntl(i32 %7, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32, i32* @STDIN_FILENO
%13 = tail call i32 @dup2(i32 %2, i32 %12)
br label %14
14: ; preds = %11, %6
%15 = load i32, i32* @STDOUT_FILENO
%16 = load i32, i32* @F_GETFD
%17 = tail call i64 @fcntl(i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @STDOUT_FILENO
%21 = tail call i32 @dup2(i32 %2, i32 %20)
br label %22
22: ; preds = %19, %14
%23 = load i32, i32* @STDERR_FILENO
%24 = load i32, i32* @F_GETFD
%25 = tail call i64 @fcntl(i32 %23, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @STDERR_FILENO
%29 = tail call i32 @dup2(i32 %2, i32 %28)
br label %30
30: ; preds = %27, %22
%31 = load i32, i32* @STDIN_FILENO
%32 = icmp eq i32 %2, %31
%33 = load i32, i32* @STDOUT_FILENO
%34 = icmp eq i32 %2, %33
%35 = select i1 %32, i1 true, i1 %34
%36 = load i32, i32* @STDERR_FILENO
%37 = icmp eq i32 %2, %36
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %41, label %39
39: ; preds = %30
%40 = tail call i32 @close(i32 %2)
br label %41
41: ; preds = %39, %30
%42 = load i32, i32* @O_RDWR
%43 = load i32, i32* @O_NOCTTY
%44 = or i32 %43, %42
%45 = tail call i32 @posix_openpt(i32 %44)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %41
%50 = tail call i32 @grantpt(i32 %45)
%51 = tail call i32 @unlockpt(i32 %45)
ret i32 %45
}