#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_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ is_a_console(int);
int open(char const *const, int /*<<< orphan*/);
int open_a_console(const char *const fnam) {
  int fd;
  fd = open(fnam, O_RDONLY);
  if (fd < 0 && errno == EACCES)
    fd = open(fnam, O_WRONLY);
  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    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <open_a_console+0x5a>
mov    -0x10(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a0 <open_a_console+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <open_a_console+0x9b>
mov    -0x14(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%eax
jne    11c5 <open_a_console+0x95>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <open_a_console+0x9b>
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   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     115b <open_a_console+0x2b>
mov    %ebp,%edi
call   1030 <is_a_console@plt>
test   %eax,%eax
je     1190 <open_a_console+0x60>
mov    %ebp,%r14d
jmp    119d <open_a_console+0x6d>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
jne    119d <open_a_console+0x6d>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    114b <open_a_console+0x1b>
jmp    119d <open_a_console+0x6d>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@O_WRONLY = 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_RDONLY
%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 %22
21: ; preds = %18
store i32 -1, i32* %2
br label %31
22: ; preds = %18
%23 = load i32, i32* %4
%24 = call i32 @is_a_console(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %2
br label %31
29: ; preds = %22
%30 = load i32, i32* %4
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %26, %21
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@O_WRONLY = global i32 0
define i32 @open_a_console(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %13
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 %19
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 %19, label %13
13: ; preds = %1, %9
%14 = phi i32 [ %11, %9 ], [ %3, %1 ]
%15 = tail call i32 @is_a_console(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @close(i32 %14)
br label %19
19: ; preds = %5, %13, %9, %17
%20 = phi i32 [ -1, %17 ], [ -1, %9 ], [ %14, %13 ], [ -1, %5 ]
ret i32 %20
}