#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 const *const);
int /*<<< orphan*/ stderr;
int getfd(const char *const nm) {
  int fd;
  if (nm) {
    fd = open_a_console(nm);
    if (fd >= 0)
      return fd;
  } else {
    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    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117d <getfd+0x3d>
mov    -0x10(%rbp),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1178 <getfd+0x38>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <getfd+0x10e>
jmp    122f <getfd+0xef>
lea    0xe7c(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11a1 <getfd+0x61>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <getfd+0x10e>
lea    0xe61(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11c5 <getfd+0x85>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <getfd+0x10e>
lea    0xe47(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11e9 <getfd+0xa9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <getfd+0x10e>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    122a <getfd+0xea>
mov    -0x14(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%rax
je     1217 <getfd+0xd7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <getfd+0x10e>
jmp    121c <getfd+0xdc>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f0 <getfd+0xb0>
jmp    122f <getfd+0xef>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getfd>:
push   %rbx
test   %rdi,%rdi
je     1174 <getfd+0x34>
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e4 <getfd+0xa4>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xebb(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe85(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e4 <getfd+0xa4>
lea    0xe7c(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e4 <getfd+0xa4>
lea    0xe74(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e4 <getfd+0xa4>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    11e4 <getfd+0xa4>
mov    $0x1,%ebx
mov    $0x1,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    11e4 <getfd+0xa4>
mov    $0x2,%ebx
mov    $0x2,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
je     1155 <getfd+0x15>
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"
@stderr = global i32 0
@.str.3 = [56 x i8] c"Couldnt get a file descriptor referring to the console\0A\00"
define i32 @getfd(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 %15
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = call i32 @open_a_console(i8* %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %52
14: ; preds = %7
br label %48
15: ; preds = %1
%16 = call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* %4
store i32 %20, i32* %2
br label %52
21: ; preds = %15
%22 = call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* %4
store i32 %26, i32* %2
br label %52
27: ; preds = %21
%28 = call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %4
store i32 %32, i32* %2
br label %52
33: ; preds = %27
store i32 0, i32* %4
br label %34
34: ; preds = %44, %33
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 3
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = load i32, i32* %4
%39 = call i64 @is_a_console(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %52
43: ; preds = %37
br label %44
44: ; preds = %43
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %34
47: ; preds = %34
br label %48
48: ; preds = %47, %14
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %41, %31, %25, %19, %12
%53 = load i32, i32* %2
ret i32 %53
}

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(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = tail call i32 @open_a_console(i8* %0)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %28, label %24
6: ; preds = %1
%7 = tail call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = icmp sgt i32 %7, -1
br i1 %8, label %28, label %9
9: ; preds = %6
%10 = tail call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp sgt i32 %10, -1
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = tail call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp sgt i32 %13, -1
br i1 %14, label %28, label %15
15: ; preds = %12
%16 = tail call i64 @is_a_console(i32 0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = tail call i64 @is_a_console(i32 1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = tail call i64 @is_a_console(i32 2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21, %3
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %15, %18, %21, %12, %9, %6, %3
%29 = phi i32 [ %4, %3 ], [ %7, %6 ], [ %10, %9 ], [ %13, %12 ], [ 0, %15 ], [ 1, %18 ], [ 2, %21 ]
ret i32 %29
}