#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 */
struct termios {
  int c_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int builtin_getch() {
  struct termios oldt, newt;
  int ch;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  ch = getchar();
  if (ch == 27)
    if ((ch = getchar()) == '[')
      if (!(ch = getchar()))
        ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  return ch;
}


x86 assembly (clang, -O0):
<builtin_getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    0x2e80(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1b,-0x14(%rbp)
jne    11d4 <builtin_getch+0xa4>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x5b,%eax
jne    11cf <builtin_getch+0x9f>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    11ca <builtin_getch+0x9a>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x14(%rbp)
jmp    11cf <builtin_getch+0x9f>
jmp    11d4 <builtin_getch+0xa4>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<builtin_getch>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e90(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e76(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e78(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
call   1050 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
cmp    $0x1b,%eax
jne    11af <builtin_getch+0x7f>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
cmp    $0x5b,%eax
jne    11af <builtin_getch+0x7f>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11af <builtin_getch+0x7f>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
mov    (%r14),%edi
mov    (%r15),%esi
mov    %rsp,%rdx
call   1050 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @builtin_getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %1)
%6 = bitcast %struct.termios* %2 to i8*
%7 = bitcast %struct.termios* %1 to i8*
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %8, %9
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = call i32 (...) @getchar()
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 27
br i1 %20, label %21, label %31
21: ; preds = %0
%22 = call i32 (...) @getchar()
store i32 %22, i32* %3
%23 = icmp eq i32 %22, 91
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = call i32 (...) @getchar()
store i32 %25, i32* %3
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 (...) @getchar()
store i32 %28, i32* %3
br label %29
29: ; preds = %27, %24
br label %30
30: ; preds = %29, %21
br label %31
31: ; preds = %30, %0
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %1)
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @builtin_getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %1)
%7 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = load i32, i32* %7
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = and i32 %9, %13
store i32 %14, i32* %8
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = call i32 (...) @getchar()
%19 = icmp eq i32 %18, 27
br i1 %19, label %20, label %28
20: ; preds = %0
%21 = call i32 (...) @getchar()
%22 = icmp eq i32 %21, 91
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = call i32 (...) @getchar()
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 (...) @getchar()
br label %28
28: ; preds = %20, %26, %23, %0
%29 = phi i32 [ %24, %23 ], [ %27, %26 ], [ %21, %20 ], [ %18, %0 ]
%30 = load i32, i32* @STDIN_FILENO
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %30, i32 %31, %struct.termios* %1)
ret i32 %29
}