#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 */
char getchar();
int /*<<< orphan*/ initTermios(int);
int /*<<< orphan*/ resetTermios();
char getch_(int echo) {
  char ch;
  initTermios(echo);
  ch = getchar();
  if (ch == '\x1b') {
    if (getchar() == '[') {
      switch (getchar()) {
      case 'A':
        ch = '8';
        break;
      case 'B':
        ch = '2';
        break;
      case 'C':
        ch = '6';
        break;
      case 'D':
        ch = '4';
        break;
      }
    }
  }
  resetTermios();
  return ch;
}


x86 assembly (clang, -O0):
<getch_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <initTermios@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x1b,%eax
jne    11c6 <getch_+0x96>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%eax
cmp    $0x5b,%eax
jne    11c1 <getch_+0x91>
xor    %eax,%eax
call   1040 <getchar@plt>
movsbl %al,%eax
add    $0xffffffbf,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x3,%eax
ja     11bc <getch_+0x8c>
mov    -0x10(%rbp),%rax
lea    0xe6c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movb   $0x38,-0x5(%rbp)
jmp    11bc <getch_+0x8c>
movb   $0x32,-0x5(%rbp)
jmp    11bc <getch_+0x8c>
movb   $0x36,-0x5(%rbp)
jmp    11bc <getch_+0x8c>
movb   $0x34,-0x5(%rbp)
jmp    11c1 <getch_+0x91>
jmp    11c6 <getch_+0x96>
mov    $0x0,%al
call   1030 <resetTermios@plt>
movsbl -0x5(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch_>:
push   %rbx
call   1050 <initTermios@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebx
cmp    $0x1b,%al
jne    116c <getch_+0x3c>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    $0x1b,%bl
cmp    $0x5b,%al
jne    116c <getch_+0x3c>
xor    %eax,%eax
call   1040 <getchar@plt>
movsbl %al,%ecx
add    $0xffffffbf,%ecx
cmp    $0x3,%ecx
ja     116c <getch_+0x3c>
shl    $0x3,%ecx
mov    $0x34363238,%ebx
shr    %cl,%ebx
xor    %eax,%eax
call   1030 <resetTermios@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define signext i8 @getch_(i32 %0) {
%2 = alloca i32
%3 = alloca i8
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @initTermios(i32 %4)
%6 = call signext i8 (...) @getchar()
store i8 %6, i8* %3
%7 = load i8, i8* %3
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 27
br i1 %9, label %10, label %23
10: ; preds = %1
%11 = call signext i8 (...) @getchar()
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 91
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = call signext i8 (...) @getchar()
%16 = sext i8 %15 to i32
switch i32 %16, label %21 [
i32 65, label %17
i32 66, label %18
i32 67, label %19
i32 68, label %20
]
17: ; preds = %14
store i8 56, i8* %3
br label %21
18: ; preds = %14
store i8 50, i8* %3
br label %21
19: ; preds = %14
store i8 54, i8* %3
br label %21
20: ; preds = %14
store i8 52, i8* %3
br label %21
21: ; preds = %14, %20, %19, %18, %17
br label %22
22: ; preds = %21, %10
br label %23
23: ; preds = %22, %1
%24 = call i32 (...) @resetTermios()
%25 = load i8, i8* %3
ret i8 %25
}

LLVM IR (-O2):
define signext i8 @getch_(i32 %0) {
%2 = tail call i32 @initTermios(i32 %0)
%3 = tail call signext i8 (...) @getchar()
%4 = icmp eq i8 %3, 27
br i1 %4, label %5, label %17
5: ; preds = %1
%6 = tail call signext i8 (...) @getchar()
%7 = icmp eq i8 %6, 91
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = tail call signext i8 (...) @getchar()
%10 = sext i8 %9 to i32
%11 = add nsw i32 %10, -65
%12 = icmp ult i32 %11, 4
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = shl nsw i32 %11, 3
%15 = lshr i32 875967032, %14
%16 = trunc i32 %15 to i8
br label %17
17: ; preds = %13, %8, %5, %1
%18 = phi i8 [ 27, %8 ], [ 27, %5 ], [ %3, %1 ], [ %16, %13 ]
%19 = tail call i32 (...) @resetTermios()
ret i8 %18
}