#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__ is_a_console(int);
int open_a_console(char *);
int get_console_fd(char *tty_name) {
  int fd;
  if (tty_name) {
    if (-1 == (fd = open_a_console(tty_name)))
      return -1;
    else
      return fd;
  }
  fd = open_a_console("/dev/tty");
  if (fd >= 0)
    return fd;
  fd = open_a_console("/dev/tty0");
  if (fd >= 0)
    return fd;
  fd = open_a_console("/dev/console");
  if (fd >= 0)
    return fd;
  for (fd = 0; fd < 3; fd++)
    if (is_a_console(fd))
      return fd;
  return -1;
}


x86 assembly (clang, -O0):
<get_console_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1169 <get_console_fd+0x49>
mov    -0x10(%rbp),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    115e <get_console_fd+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <get_console_fd+0xfd>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <get_console_fd+0xfd>
lea    0xe90(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     118d <get_console_fd+0x6d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <get_console_fd+0xfd>
lea    0xe75(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11b1 <get_console_fd+0x91>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <get_console_fd+0xfd>
lea    0xe5b(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11d5 <get_console_fd+0xb5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <get_console_fd+0xfd>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1216 <get_console_fd+0xf6>
mov    -0x14(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%rax
je     1203 <get_console_fd+0xe3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <get_console_fd+0xfd>
jmp    1208 <get_console_fd+0xe8>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11dc <get_console_fd+0xbc>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_console_fd>:
test   %rdi,%rdi
je     112a <get_console_fd+0xa>
jmp    1040 <open_a_console@plt>
push   %rbx
lea    0xece(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1196 <get_console_fd+0x76>
lea    0xec5(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1196 <get_console_fd+0x76>
lea    0xebd(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1196 <get_console_fd+0x76>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    1196 <get_console_fd+0x76>
mov    $0x1,%ebx
mov    $0x1,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    1196 <get_console_fd+0x76>
mov    $0x2,%edi
call   1030 <is_a_console@plt>
cmp    $0x1,%rax
sbb    %ebx,%ebx
or     $0x2,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [10 x i8] c"/dev/tty0\00"
@.str.2 = [13 x i8] c"/dev/console\00"
define i32 @get_console_fd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = call i32 @open_a_console(i8* %8)
store i32 %9, i32* %4
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 -1, i32* %2
br label %47
12: ; preds = %7
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %47
14: ; preds = %1
%15 = call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %47
20: ; preds = %14
%21 = call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %47
26: ; preds = %20
%27 = call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %47
32: ; preds = %26
store i32 0, i32* %4
br label %33
33: ; preds = %43, %32
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 3
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = load i32, i32* %4
%38 = call i64 @is_a_console(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %47
42: ; preds = %36
br label %43
43: ; preds = %42
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %33
46: ; preds = %33
store i32 -1, i32* %2
br label %47
47: ; preds = %46, %40, %30, %24, %18, %12, %11
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [10 x i8] c"/dev/tty0\00"
@.str.2 = [13 x i8] c"/dev/console\00"
define i32 @get_console_fd(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %5, label %3
3: ; preds = %1
%4 = tail call i32 @open_a_console(i8* %0)
br label %24
5: ; preds = %1
%6 = tail call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = icmp sgt i32 %6, -1
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = tail call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp sgt i32 %9, -1
br i1 %10, label %24, label %11
11: ; preds = %8
%12 = tail call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp sgt i32 %12, -1
br i1 %13, label %24, label %14
14: ; preds = %11
%15 = tail call i64 @is_a_console(i32 0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = tail call i64 @is_a_console(i32 1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = tail call i64 @is_a_console(i32 2)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i32 -1, i32 2
br label %24
24: ; preds = %20, %14, %17, %11, %8, %5, %3
%25 = phi i32 [ %4, %3 ], [ %6, %5 ], [ %9, %8 ], [ %12, %11 ], [ 0, %14 ], [ 1, %17 ], [ %23, %20 ]
ret i32 %25
}