#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 *);
int /*<<< orphan*/ stderr;
int getfd(const char *fnam) {
  int fd;
  if (fnam) {
    fd = open_a_console(fnam);
    if (fd >= 0)
      return fd;
    fprintf(stderr, "Couldnt open %s\n", fnam);
    exit(1);
  }
  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;
  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     119d <getfd+0x5d>
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    128f <getfd+0x14f>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe6d(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11c1 <getfd+0x81>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <getfd+0x14f>
lea    0xe52(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11e5 <getfd+0xa5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <getfd+0x14f>
lea    0xe38(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1209 <getfd+0xc9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <getfd+0x14f>
lea    0xe1e(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     122d <getfd+0xed>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <getfd+0x14f>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    126e <getfd+0x12e>
mov    -0x14(%rbp),%edi
call   1030 <is_a_console@plt>
cmp    $0x0,%rax
je     125b <getfd+0x11b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128f <getfd+0x14f>
jmp    1260 <getfd+0x120>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1234 <getfd+0xf4>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
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   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     117e <getfd+0x3e>
mov    %rdi,%rbx
call   1050 <open_a_console@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fc <getfd+0xbc>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0xe96(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe8c(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fc <getfd+0xbc>
lea    0xe83(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fc <getfd+0xbc>
lea    0xe7b(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fc <getfd+0xbc>
lea    0xe73(%rip),%rdi
call   1050 <open_a_console@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fc <getfd+0xbc>
xor    %ebp,%ebp
xor    %edi,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    11fc <getfd+0xbc>
mov    $0x1,%ebp
mov    $0x1,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
jne    11fc <getfd+0xbc>
mov    $0x2,%ebp
mov    $0x2,%edi
call   1030 <is_a_console@plt>
test   %rax,%rax
je     1205 <getfd+0xc5>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"Couldnt open %s\0A\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@.str.2 = [10 x i8] c"/dev/tty0\00"
@.str.3 = [10 x i8] c"/dev/vc/0\00"
@.str.4 = [13 x i8] c"/dev/console\00"
@.str.5 = [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 %19
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 %61
14: ; preds = %7
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %61
25: ; preds = %19
%26 = call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* %4
store i32 %30, i32* %2
br label %61
31: ; preds = %25
%32 = call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp sge i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %61
37: ; preds = %31
%38 = call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp sge i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %61
43: ; preds = %37
store i32 0, i32* %4
br label %44
44: ; preds = %54, %43
%45 = load i32, i32* %4
%46 = icmp slt i32 %45, 3
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = load i32, i32* %4
%49 = call i64 @is_a_console(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = load i32, i32* %4
store i32 %52, i32* %2
br label %61
53: ; preds = %47
br label %54
54: ; preds = %53
%55 = load i32, i32* %4
%56 = add nsw i32 %55, 1
store i32 %56, i32* %4
br label %44
57: ; preds = %44
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %51, %41, %35, %29, %23, %12
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"Couldnt open %s\0A\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@.str.2 = [10 x i8] c"/dev/tty0\00"
@.str.3 = [10 x i8] c"/dev/vc/0\00"
@.str.4 = [13 x i8] c"/dev/console\00"
@.str.5 = [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 %10, label %3
3: ; preds = %1
%4 = tail call i32 @open_a_console(i8* %0)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %35, label %6
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 @open_a_console(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp sgt i32 %11, -1
br i1 %12, label %35, label %13
13: ; preds = %10
%14 = tail call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp sgt i32 %14, -1
br i1 %15, label %35, label %16
16: ; preds = %13
%17 = tail call i32 @open_a_console(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp sgt i32 %17, -1
br i1 %18, label %35, label %19
19: ; preds = %16
%20 = tail call i32 @open_a_console(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp sgt i32 %20, -1
br i1 %21, label %35, label %22
22: ; preds = %19
%23 = tail call i64 @is_a_console(i32 0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = tail call i64 @is_a_console(i32 1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = tail call i64 @is_a_console(i32 2)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %22, %25, %28, %19, %16, %13, %10, %3
%36 = phi i32 [ %4, %3 ], [ %11, %10 ], [ %14, %13 ], [ %17, %16 ], [ %20, %19 ], [ 0, %22 ], [ 1, %25 ], [ 2, %28 ]
ret i32 %36
}