#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 tty_getc();
int tty_getch() {
  int ch;
  ch = tty_getc();
  if (ch == 27) {
    ch = tty_getc();
    if (ch == '\n') {
      return 266;
    }
    if (ch == '[' || ch == 'O') {
      ch = tty_getc();
      if (ch == 'A') {
        return 259;
      }
      if (ch == 'B') {
        return 258;
      }
      if (ch == 'C') {
        return 261;
      }
      if (ch == 'D') {
        return 260;
      }
    }
  }
  return ch;
}


x86 assembly (clang, -O0):
<tty_getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <tty_getc@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1b,-0x8(%rbp)
jne    11cc <tty_getch+0xbc>
mov    $0x0,%al
call   1030 <tty_getc@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jne    114c <tty_getch+0x3c>
movl   $0x10a,-0x4(%rbp)
jmp    11d2 <tty_getch+0xc2>
cmpl   $0x5b,-0x8(%rbp)
je     1160 <tty_getch+0x50>
cmpl   $0x4f,-0x8(%rbp)
jne    11c7 <tty_getch+0xb7>
mov    $0x0,%al
call   1030 <tty_getc@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x41,-0x8(%rbp)
jne    1180 <tty_getch+0x70>
movl   $0x103,-0x4(%rbp)
jmp    11d2 <tty_getch+0xc2>
cmpl   $0x42,-0x8(%rbp)
jne    1196 <tty_getch+0x86>
movl   $0x102,-0x4(%rbp)
jmp    11d2 <tty_getch+0xc2>
cmpl   $0x43,-0x8(%rbp)
jne    11ac <tty_getch+0x9c>
movl   $0x105,-0x4(%rbp)
jmp    11d2 <tty_getch+0xc2>
cmpl   $0x44,-0x8(%rbp)
jne    11c2 <tty_getch+0xb2>
movl   $0x104,-0x4(%rbp)
jmp    11d2 <tty_getch+0xc2>
jmp    11c7 <tty_getch+0xb7>
jmp    11cc <tty_getch+0xbc>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_getch>:
push   %rax
xor    %eax,%eax
call   1030 <tty_getc@plt>
cmp    $0x1b,%eax
jne    1151 <tty_getch+0x41>
xor    %eax,%eax
call   1030 <tty_getc@plt>
cmp    $0xa,%eax
je     1153 <tty_getch+0x43>
cmp    $0x5b,%eax
je     1133 <tty_getch+0x23>
cmp    $0x4f,%eax
jne    1151 <tty_getch+0x41>
xor    %eax,%eax
call   1030 <tty_getc@plt>
mov    %eax,%ecx
add    $0xffffffbf,%ecx
cmp    $0x4,%ecx
jae    1151 <tty_getch+0x41>
movslq %ecx,%rax
lea    0xeb2(%rip),%rcx
mov    (%rcx,%rax,4),%eax
pop    %rcx
ret
mov    $0x10a,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @tty_getch() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @tty_getc()
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, 27
br i1 %5, label %6, label %36
6: ; preds = %0
%7 = call i32 (...) @tty_getc()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 10
br i1 %9, label %10, label %11
10: ; preds = %6
store i32 266, i32* %1
br label %38
11: ; preds = %6
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 91
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 79
br i1 %16, label %17, label %35
17: ; preds = %14, %11
%18 = call i32 (...) @tty_getc()
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, 65
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 259, i32* %1
br label %38
22: ; preds = %17
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, 66
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 258, i32* %1
br label %38
26: ; preds = %22
%27 = load i32, i32* %2
%28 = icmp eq i32 %27, 67
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 261, i32* %1
br label %38
30: ; preds = %26
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, 68
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 260, i32* %1
br label %38
34: ; preds = %30
br label %35
35: ; preds = %34, %14
br label %36
36: ; preds = %35, %0
%37 = load i32, i32* %2
store i32 %37, i32* %1
br label %38
38: ; preds = %36, %33, %29, %25, %21, %10
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@switch.table.tty_getch = [4 x i32] [i32 259, i32 258, i32 261, i32 260]
define i32 @tty_getch() {
%1 = tail call i32 (...) @tty_getc()
%2 = icmp eq i32 %1, 27
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = tail call i32 (...) @tty_getc()
switch i32 %4, label %9 [
i32 10, label %15
i32 91, label %5
i32 79, label %5
]
5: ; preds = %3, %3
%6 = tail call i32 (...) @tty_getc()
%7 = add i32 %6, -65
%8 = icmp ult i32 %7, 4
br i1 %8, label %11, label %9
9: ; preds = %5, %3, %0
%10 = phi i32 [ %1, %0 ], [ %6, %5 ], [ %4, %3 ]
br label %15
11: ; preds = %5
%12 = sext i32 %7 to i64
%13 = getelementptr inbounds [4 x i32], [4 x i32]* @switch.table.tty_getch, i64 0, i64 %12
%14 = load i32, i32* %13
br label %15
15: ; preds = %11, %3, %9
%16 = phi i32 [ %10, %9 ], [ 266, %3 ], [ %14, %11 ]
ret i32 %16
}