#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 _getfd(void) {
  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/vc/0");
  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):
<_getfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xed1(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     114c <_getfd+0x2c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <_getfd+0xe0>
lea    0xeb6(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1170 <_getfd+0x50>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <_getfd+0xe0>
lea    0xe9c(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1194 <_getfd+0x74>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <_getfd+0xe0>
lea    0xe82(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11b8 <_getfd+0x98>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <_getfd+0xe0>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f9 <_getfd+0xd9>
mov    -0x8(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%rax
je     11e6 <_getfd+0xc6>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <_getfd+0xe0>
jmp    11eb <_getfd+0xcb>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11bf <_getfd+0x9f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getfd>:
push   %rbx
lea    0xed8(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <_getfd+0x7e>
lea    0xecf(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <_getfd+0x7e>
lea    0xec7(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <_getfd+0x7e>
lea    0xebf(%rip),%rdi
call   1040 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119e <_getfd+0x7e>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    119e <_getfd+0x7e>
mov    $0x1,%ebx
mov    $0x1,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    119e <_getfd+0x7e>
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 = [10 x i8] c"/dev/vc/0\00"
@.str.3 = [13 x i8] c"/dev/console\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 %41
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 %41
14: ; preds = %8
%15 = call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 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 %41
20: ; preds = %14
%21 = call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* %2
store i32 %25, i32* %1
br label %41
26: ; preds = %20
store i32 0, i32* %2
br label %27
27: ; preds = %37, %26
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 3
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i32, i32* %2
%32 = call i64 @is_a_console(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = load i32, i32* %2
store i32 %35, i32* %1
br label %41
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = load i32, i32* %2
%39 = add nsw i32 %38, 1
store i32 %39, i32* %2
br label %27
40: ; preds = %27
store i32 -1, i32* %1
br label %41
41: ; preds = %40, %34, %24, %18, %12, %6
%42 = load i32, i32* %1
ret i32 %42
}

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