#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ is_a_console(int);
int open_a_console(char *);
int /*<<< orphan*/ stderr;
int getfd() {
  int 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;
  fprintf(stderr, ("Couldnt get a file descriptor referring to the console\n"));
  exit(1);
}


x86 assembly (clang, -O0):
<getfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     116c <getfd+0x2c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <getfd+0xd4>
lea    0xe96(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1190 <getfd+0x50>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <getfd+0xd4>
lea    0xe7c(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11b4 <getfd+0x74>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <getfd+0xd4>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f5 <getfd+0xb5>
mov    -0x8(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%rax
je     11e2 <getfd+0xa2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <getfd+0xd4>
jmp    11e7 <getfd+0xa7>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11bb <getfd+0x7b>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getfd>:
push   %rbx
lea    0xeb8(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ad <getfd+0x6d>
lea    0xeaf(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ad <getfd+0x6d>
lea    0xea7(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ad <getfd+0x6d>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    11ad <getfd+0x6d>
mov    $0x1,%ebx
mov    $0x1,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    11ad <getfd+0x6d>
mov    $0x2,%ebx
mov    $0x2,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
je     11b1 <getfd+0x71>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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"
@stderr = global i32 0
@.str.3 = [56 x i8] c"Couldnt get a file descriptor referring to the console\0A\00"
define i32 @getfd() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp sge i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i32, i32* %2
store i32 %7, i32* %1
br label %38
8: ; preds = %0
%9 = call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = load i32, i32* %2
store i32 %13, i32* %1
br label %38
14: ; preds = %8
%15 = call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %2
store i32 %19, i32* %1
br label %38
20: ; preds = %14
store i32 0, i32* %2
br label %21
21: ; preds = %31, %20
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 3
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i32, i32* %2
%26 = call i64 @is_a_console(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* %2
store i32 %29, i32* %1
br label %38
30: ; preds = %24
br label %31
31: ; preds = %30
%32 = load i32, i32* %2
%33 = add nsw i32 %32, 1
store i32 %33, i32* %2
br label %21
34: ; preds = %21
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %28, %18, %12, %6
%39 = load i32, i32* %1
ret i32 %39
}

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"
@stderr = global i32 0
@.str.3 = [56 x i8] c"Couldnt get a file descriptor referring to the console\0A\00"
define i32 @getfd() {
%1 = tail call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%2 = icmp sgt i32 %1, -1
br i1 %2, label %22, label %3
3: ; preds = %0
%4 = tail call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp sgt i32 %4, -1
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = tail call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp sgt i32 %7, -1
br i1 %8, label %22, label %9
9: ; preds = %6
%10 = tail call i64 @is_a_console(i32 0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = tail call i64 @is_a_console(i32 1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = tail call i64 @is_a_console(i32 2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9, %12, %15, %6, %3, %0
%23 = phi i32 [ %1, %0 ], [ %4, %3 ], [ %7, %6 ], [ 0, %9 ], [ 1, %12 ], [ 2, %15 ]
ret i32 %23
}