#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 getch();
int getDirection() {
  if (getch() != 27) {
    return 0;
  }
  if (getch() != 91) {
    return 0;
  }
  int ret_val;
  switch (getch()) {
  case 65:
    ret_val = 1;
    break;
  case 66:
    ret_val = 2;
    break;
  case 67:
    ret_val = 3;
    break;
  case 68:
    ret_val = 4;
    break;
  default:
    ret_val = 0;
    break;
  }
  return ret_val;
}


x86 assembly (clang, -O0):
<getDirection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getch@plt>
cmp    $0x1b,%eax
je     1134 <getDirection+0x24>
movl   $0x0,-0x4(%rbp)
jmp    11ba <getDirection+0xaa>
mov    $0x0,%al
call   1030 <getch@plt>
cmp    $0x5b,%eax
je     1150 <getDirection+0x40>
movl   $0x0,-0x4(%rbp)
jmp    11ba <getDirection+0xaa>
xor    %eax,%eax
call   1030 <getch@plt>
add    $0xffffffbf,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x3,%eax
ja     11ad <getDirection+0x9d>
mov    -0x10(%rbp),%rax
lea    0xe8c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x8(%rbp)
jmp    11b4 <getDirection+0xa4>
movl   $0x2,-0x8(%rbp)
jmp    11b4 <getDirection+0xa4>
movl   $0x3,-0x8(%rbp)
jmp    11b4 <getDirection+0xa4>
movl   $0x4,-0x8(%rbp)
jmp    11b4 <getDirection+0xa4>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getDirection>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getch@plt>
cmp    $0x1b,%eax
jne    1146 <getDirection+0x36>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getch@plt>
cmp    $0x5b,%eax
jne    1146 <getDirection+0x36>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getch@plt>
mov    %eax,%ebx
lea    -0x41(%rbx),%eax
add    $0xffffffc0,%ebx
cmp    $0x4,%eax
cmovae %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getDirection() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getch()
%4 = icmp ne i32 %3, 27
br i1 %4, label %5, label %6
5: ; preds = %0
store i32 0, i32* %1
br label %19
6: ; preds = %0
%7 = call i32 (...) @getch()
%8 = icmp ne i32 %7, 91
br i1 %8, label %9, label %10
9: ; preds = %6
store i32 0, i32* %1
br label %19
10: ; preds = %6
%11 = call i32 (...) @getch()
switch i32 %11, label %16 [
i32 65, label %12
i32 66, label %13
i32 67, label %14
i32 68, label %15
]
12: ; preds = %10
store i32 1, i32* %2
br label %17
13: ; preds = %10
store i32 2, i32* %2
br label %17
14: ; preds = %10
store i32 3, i32* %2
br label %17
15: ; preds = %10
store i32 4, i32* %2
br label %17
16: ; preds = %10
store i32 0, i32* %2
br label %17
17: ; preds = %16, %15, %14, %13, %12
%18 = load i32, i32* %2
store i32 %18, i32* %1
br label %19
19: ; preds = %17, %9, %5
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
define i32 @getDirection() {
%1 = tail call i32 (...) @getch()
%2 = icmp eq i32 %1, 27
br i1 %2, label %3, label %12
3: ; preds = %0
%4 = tail call i32 (...) @getch()
%5 = icmp eq i32 %4, 91
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = tail call i32 (...) @getch()
%8 = add i32 %7, -65
%9 = icmp ult i32 %8, 4
%10 = add i32 %7, -64
%11 = select i1 %9, i32 %10, i32 0
br label %12
12: ; preds = %6, %3, %0
%13 = phi i32 [ 0, %0 ], [ 0, %3 ], [ %11, %6 ]
ret i32 %13
}