#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 */
typedef int /*<<< orphan*/ code;
typedef int /*<<< orphan*/ c;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ device_free();
int /*<<< orphan*/ device_kbhit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int read(int /*<<< orphan*/, unsigned char *, int);
int device_getch(void) {
  int r;
  unsigned char c;
  if ((r = read(STDIN_FILENO, &c, sizeof(c))) < 0) {
    return r;
  } else if (c == '\e') {
    if (!device_kbhit(0))
      return c;
    unsigned char code[2];
    read(STDIN_FILENO, code, sizeof(code));
    return code[1] + 256;
  } else {
    if (c == 3) {
      device_free();
      exit(EXIT_FAILURE);
    }
    return c;
  }
}


x86 assembly (clang, -O0):
<device_getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1176 <device_getch+0x36>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <device_getch+0xb0>
movzbl -0x9(%rbp),%eax
cmp    $0x1b,%eax
jne    11c7 <device_getch+0x87>
xor    %edi,%edi
call   1050 <device_kbhit@plt>
cmp    $0x0,%eax
jne    119f <device_getch+0x5f>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <device_getch+0xb0>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    -0xb(%rbp),%rsi
mov    $0x2,%edx
call   1030 <read@plt>
movzbl -0xa(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <device_getch+0xb0>
movzbl -0x9(%rbp),%eax
cmp    $0x3,%eax
jne    11e9 <device_getch+0xa9>
mov    $0x0,%al
call   1040 <device_free@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movzbl -0x9(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_getch>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rbx
mov    (%rbx),%edi
lea    0xd(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     11b1 <device_getch+0x71>
movzbl 0xd(%rsp),%eax
cmp    $0x1b,%eax
je     1184 <device_getch+0x44>
cmp    $0x3,%al
jne    11b1 <device_getch+0x71>
xor    %eax,%eax
call   1040 <device_free@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1050 <device_kbhit@plt>
test   %eax,%eax
je     11ac <device_getch+0x6c>
mov    (%rbx),%edi
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1030 <read@plt>
movzbl 0xf(%rsp),%eax
or     $0x100,%eax
jmp    11b1 <device_getch+0x71>
mov    $0x1b,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @device_getch() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca [2 x i8]
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @read(i32 %5, i8* %3, i32 1)
store i32 %6, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* %2
store i32 %9, i32* %1
br label %39
10: ; preds = %0
%11 = load i8, i8* %3
%12 = zext i8 %11 to i32
%13 = icmp eq i32 %12, 27
br i1 %13, label %14, label %28
14: ; preds = %10
%15 = call i32 @device_kbhit(i32 0)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i8, i8* %3
%19 = zext i8 %18 to i32
store i32 %19, i32* %1
br label %39
20: ; preds = %14
%21 = load i32, i32* @STDIN_FILENO
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 2)
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = add nsw i32 %26, 256
store i32 %27, i32* %1
br label %39
28: ; preds = %10
%29 = load i8, i8* %3
%30 = zext i8 %29 to i32
%31 = icmp eq i32 %30, 3
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 (...) @device_free()
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %28
%37 = load i8, i8* %3
%38 = zext i8 %37 to i32
store i32 %38, i32* %1
br label %39
39: ; preds = %36, %20, %17, %8
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @device_getch() {
%1 = alloca i8
%2 = alloca [2 x i8]
%3 = load i32, i32* @STDIN_FILENO
%4 = call i32 @read(i32 %3, i8* %1, i32 1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %24, label %6
6: ; preds = %0
%7 = load i8, i8* %1
%8 = zext i8 %7 to i32
switch i8 %7, label %24 [
i8 27, label %9
i8 3, label %20
]
9: ; preds = %6
%10 = tail call i32 @device_kbhit(i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %9
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%14 = load i32, i32* @STDIN_FILENO
%15 = call i32 @read(i32 %14, i8* %13, i32 2)
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = or i32 %18, 256
br label %24
20: ; preds = %6
%21 = tail call i32 (...) @device_free()
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %9, %6, %0, %12
%25 = phi i32 [ %19, %12 ], [ %4, %0 ], [ %8, %6 ], [ 27, %9 ]
ret i32 %25
}