#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*/ die(int /*<<< orphan*/, char *);
int /*<<< orphan*/ dup2(int, int);
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)
    die(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)
    die(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   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11b7 <getconsole+0x37>
xor    %edi,%edi
lea    0xe4e(%rip),%rsi
call   1080 <die@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11e9 <getconsole+0x69>
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    121b <getconsole+0x9b>
mov    -0x8(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    124d <getconsole+0xcd>
mov    -0x8(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%eax
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%eax
je     1295 <getconsole+0x115>
mov    -0x8(%rbp),%eax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%eax
je     1290 <getconsole+0x110>
mov    -0x8(%rbp),%eax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%eax
je     128b <getconsole+0x10b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    1290 <getconsole+0x110>
jmp    1295 <getconsole+0x115>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    0x2d2b(%rip),%rax
or     (%rax),%edi
call   10a0 <posix_openpt@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    12c6 <getconsole+0x146>
xor    %edi,%edi
lea    0xd58(%rip),%rsi
call   1080 <die@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   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11b5 <getconsole+0x35>
lea    0xe52(%rip),%rsi
xor    %edi,%edi
call   1080 <die@plt>
mov    0x2dfc(%rip),%r15
mov    (%r15),%edi
mov    0x2e02(%rip),%r13
mov    0x0(%r13),%esi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    11de <getconsole+0x5e>
mov    (%r15),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2e0b(%rip),%r12
mov    (%r12),%edi
mov    0x0(%r13),%esi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1202 <getconsole+0x82>
mov    (%r12),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    0x2db7(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x0(%r13),%esi
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1224 <getconsole+0xa4>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    (%r15),%ebx
je     123b <getconsole+0xbb>
cmp    (%r12),%ebx
je     123b <getconsole+0xbb>
cmp    0x0(%rbp),%ebx
je     123b <getconsole+0xbb>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
or     (%r14),%edi
call   10a0 <posix_openpt@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1260 <getconsole+0xe0>
lea    0xdc0(%rip),%rsi
xor    %edi,%edi
call   1080 <die@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 @die(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 @die(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 @die(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 @die(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
}