#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 */
scalar_t__ EACCES;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ is_a_console(int);
int open(char const *, int /*<<< orphan*/);
int open_a_console(const char *fnam) {
  int fd;
  fd = open(fnam, O_RDWR);
  if (fd < 0 && errno == EACCES)
    fd = open(fnam, O_WRONLY);
  if (fd < 0 && errno == EACCES)
    fd = open(fnam, O_RDONLY);
  if (fd < 0)
    return -1;
  if (!is_a_console(fd)) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_a_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118a <open_a_console+0x5a>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <open_a_console+0x5a>
mov    -0x10(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c3 <open_a_console+0x93>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jne    11c3 <open_a_console+0x93>
mov    -0x10(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d9 <open_a_console+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <open_a_console+0xd4>
mov    -0x14(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%eax
jne    11fe <open_a_console+0xce>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <open_a_console+0xd4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_a_console>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     115d <open_a_console+0x2d>
mov    %ebp,%edi
call   1030 <is_a_console@plt>
test   %eax,%eax
je     11b6 <open_a_console+0x86>
mov    %ebp,%ebx
jmp    11c2 <open_a_console+0x92>
mov    0x2e64(%rip),%r15
mov    (%r15),%rax
mov    0x2e6a(%rip),%rbx
mov    (%rbx),%rcx
cmp    %rcx,%rax
jne    1193 <open_a_console+0x63>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    114e <open_a_console+0x1e>
mov    (%r15),%rax
mov    (%rbx),%rcx
mov    $0xffffffff,%ebx
cmp    %rcx,%rax
jne    11c2 <open_a_console+0x92>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    114e <open_a_console+0x1e>
jmp    11c2 <open_a_console+0x92>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
define i32 @open_a_console(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EACCES
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %4
br label %18
18: ; preds = %14, %10, %1
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EACCES
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %4
br label %29
29: ; preds = %25, %21, %18
%30 = load i32, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %2
br label %42
33: ; preds = %29
%34 = load i32, i32* %4
%35 = call i32 @is_a_console(i32 %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %42
40: ; preds = %33
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %37, %32
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
define i32 @open_a_console(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %24
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @EACCES
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %16
9: ; preds = %5
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %9
%14 = load i64, i64* @errno
%15 = load i64, i64* @EACCES
br label %16
16: ; preds = %13, %5
%17 = phi i64 [ %15, %13 ], [ %7, %5 ]
%18 = phi i64 [ %14, %13 ], [ %6, %5 ]
%19 = icmp eq i64 %18, %17
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 @open(i8* %0, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %1, %9, %20
%25 = phi i32 [ %22, %20 ], [ %11, %9 ], [ %3, %1 ]
%26 = tail call i32 @is_a_console(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 @close(i32 %25)
br label %30
30: ; preds = %16, %24, %20, %28
%31 = phi i32 [ -1, %28 ], [ -1, %20 ], [ %25, %24 ], [ -1, %16 ]
ret i32 %31
}