#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 */
scalar_t__ ENOTTY;
int EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getc(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(scalar_t__);
int /*<<< orphan*/ terminal_done();
scalar_t__ terminal_init();
int key(void) {
  const int UP = 0;
  const int DOWN = 1;
  const int OTHER = -1;
  int c;
  int val = 0;
  if (terminal_init()) {
    if (errno == ENOTTY)
      fprintf(stderr, "This program requires a terminal.\n");
    else
      fprintf(stderr, "Cannot initialize terminal: %s.\n", strerror(errno));
    return EXIT_FAILURE;
  }
  c = getc(stdin);
  if (c == 27) {
    c = getc(stdin);
    if (c == 91) {
      c = getc(stdin);
      if (c == 65) {
        val = UP;
      } else if (c == 66) {
        val = DOWN;
      } else
        val = OTHER;
    } else
      val = OTHER;
  } else
    val = c;
  terminal_done();
  return val;
}


x86 assembly (clang, -O0):
<key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <terminal_init@plt>
cmp    $0x0,%rax
je     11fb <key+0xab>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <key+0x6b>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11ea <key+0x9a>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <key+0x14e>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <getc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1b,-0x14(%rbp)
jne    128b <key+0x13b>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1070 <getc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x5b,-0x14(%rbp)
jne    127f <key+0x12f>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1070 <getc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x41,-0x14(%rbp)
jne    1258 <key+0x108>
movl   $0x0,-0x18(%rbp)
jmp    127a <key+0x12a>
cmpl   $0x42,-0x14(%rbp)
jne    126e <key+0x11e>
movl   $0x1,-0x18(%rbp)
jmp    1275 <key+0x125>
movl   $0xffffffff,-0x18(%rbp)
jmp    127a <key+0x12a>
jmp    1286 <key+0x136>
movl   $0xffffffff,-0x18(%rbp)
jmp    1291 <key+0x141>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <terminal_done@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<key>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <terminal_init@plt>
test   %rax,%rax
je     1191 <key+0x41>
mov    0x2e61(%rip),%rax
mov    (%rax),%rdi
mov    0x2e77(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ebx
cmp    (%rax),%rdi
jne    11df <key+0x8f>
lea    0xe7a(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11f7 <key+0xa7>
mov    0x2e48(%rip),%r14
mov    (%r14),%edi
call   1070 <getc@plt>
mov    %eax,%ebx
cmp    $0x1b,%eax
jne    11d6 <key+0x86>
mov    (%r14),%edi
call   1070 <getc@plt>
mov    $0xffffffff,%ebx
cmp    $0x5b,%eax
jne    11d6 <key+0x86>
mov    (%r14),%edi
call   1070 <getc@plt>
xor    %ecx,%ecx
cmp    $0x42,%eax
sete   %cl
add    %ecx,%ecx
add    $0xffffffff,%ecx
xor    %ebx,%ebx
cmp    $0x41,%eax
cmovne %ecx,%ebx
xor    %eax,%eax
call   1050 <terminal_done@plt>
jmp    1200 <key+0xb0>
call   1060 <strerror@plt>
lea    0xe38(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOTTY = global i64 0
@stderr = global i32 0
@.str = [35 x i8] c"This program requires a terminal.\0A\00"
@.str.1 = [33 x i8] c"Cannot initialize terminal: %s.\0A\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
define i32 @key() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %2
store i32 1, i32* %3
store i32 -1, i32* %4
store i32 0, i32* %6
%7 = call i64 (...) @terminal_init()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOTTY
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %21
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = load i64, i64* @errno
%19 = call i8* @strerror(i64 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %16, %13
%22 = load i32, i32* @EXIT_FAILURE
store i32 %22, i32* %1
br label %53
23: ; preds = %0
%24 = load i32, i32* @stdin
%25 = call i32 @getc(i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 27
br i1 %27, label %28, label %48
28: ; preds = %23
%29 = load i32, i32* @stdin
%30 = call i32 @getc(i32 %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, 91
br i1 %32, label %33, label %46
33: ; preds = %28
%34 = load i32, i32* @stdin
%35 = call i32 @getc(i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, 65
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 0, i32* %6
br label %45
39: ; preds = %33
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, 66
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 1, i32* %6
br label %44
43: ; preds = %39
store i32 -1, i32* %6
br label %44
44: ; preds = %43, %42
br label %45
45: ; preds = %44, %38
br label %47
46: ; preds = %28
store i32 -1, i32* %6
br label %47
47: ; preds = %46, %45
br label %50
48: ; preds = %23
%49 = load i32, i32* %5
store i32 %49, i32* %6
br label %50
50: ; preds = %48, %47
%51 = call i32 (...) @terminal_done()
%52 = load i32, i32* %6
store i32 %52, i32* %1
br label %53
53: ; preds = %50, %21
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@errno = global i64 0
@ENOTTY = global i64 0
@stderr = global i32 0
@.str = [35 x i8] c"This program requires a terminal.\0A\00"
@.str.1 = [33 x i8] c"Cannot initialize terminal: %s.\0A\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
define i32 @key() {
%1 = tail call i64 (...) @terminal_init()
%2 = icmp eq i64 %1, 0
br i1 %2, label %15, label %3
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @ENOTTY
%6 = icmp eq i64 %4, %5
%7 = load i32, i32* @stderr
br i1 %6, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %13
10: ; preds = %3
%11 = tail call i8* @strerror(i64 %4)
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %10, %8
%14 = load i32, i32* @EXIT_FAILURE
br label %33
15: ; preds = %0
%16 = load i32, i32* @stdin
%17 = tail call i32 @getc(i32 %16)
%18 = icmp eq i32 %17, 27
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load i32, i32* @stdin
%21 = tail call i32 @getc(i32 %20)
%22 = icmp eq i32 %21, 91
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i32, i32* @stdin
%25 = tail call i32 @getc(i32 %24)
%26 = icmp eq i32 %25, 66
%27 = select i1 %26, i32 1, i32 -1
%28 = icmp eq i32 %25, 65
%29 = select i1 %28, i32 0, i32 %27
br label %30
30: ; preds = %23, %15, %19
%31 = phi i32 [ -1, %19 ], [ %17, %15 ], [ %29, %23 ]
%32 = tail call i32 (...) @terminal_done()
br label %33
33: ; preds = %30, %13
%34 = phi i32 [ %14, %13 ], [ %31, %30 ]
ret i32 %34
}