#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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int main(void) {
  printf("Public Domain Readline \n");
  printf("\nUse the up and down arrow keys then press Enter\n");
  int menunum = 0;
  while (1) {
    int key = getch();
    if (key == 27) {
      key = getch();
      if (key == 91)
        key = getch();
      if (key == 65) {
        puts("You pressed up arrow! \n");
        menunum -= 1;
        printf("Menunum is now %d \n", menunum);
      } else if (key == 66) {
        puts("You pressed down arrow! \n");
        menunum += 1;
        printf("Menunum is now %d \n", menunum);
      } else if (key == 67) {
        puts("You pressed right arrow! \n");
      } else if (key == 68) {
        puts("You pressed left arrow! \n");
      }
    } else if (key == 10) {
      printf("You pressed ENTER! You chose item %d \n", menunum);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1b,-0xc(%rbp)
jne    1243 <main+0x113>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x5b,-0xc(%rbp)
jne    1194 <main+0x64>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x41,-0xc(%rbp)
jne    11c9 <main+0x99>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123e <main+0x10e>
cmpl   $0x42,-0xc(%rbp)
jne    11fe <main+0xce>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1239 <main+0x109>
cmpl   $0x43,-0xc(%rbp)
jne    1219 <main+0xe9>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
jmp    1234 <main+0x104>
cmpl   $0x44,-0xc(%rbp)
jne    122f <main+0xff>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
jmp    1234 <main+0x104>
jmp    1239 <main+0x109>
jmp    123e <main+0x10e>
jmp    1268 <main+0x138>
cmpl   $0xa,-0xc(%rbp)
jne    1263 <main+0x133>
mov    -0x8(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126d <main+0x13d>
jmp    1268 <main+0x138>
jmp    1162 <main+0x32>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xf70(%rip),%rdi
call   1030 <puts@plt>
lea    0xf7c(%rip),%rdi
call   1030 <puts@plt>
lea    0xea6(%rip),%r14
lea    0xec7(%rip),%r15
lea    0xed4(%rip),%r12
lea    0xee7(%rip),%rbx
lea    0xefb(%rip),%rbp
xor    %r13d,%r13d
jmp    1185 <main+0x55>
nopl   0x0(%rax,%rax,1)
cmp    $0xa,%eax
je     11f6 <main+0xc6>
xor    %eax,%eax
call   1050 <getch@plt>
cmp    $0x1b,%eax
jne    1180 <main+0x50>
xor    %eax,%eax
call   1050 <getch@plt>
cmp    $0x5b,%eax
jne    11a4 <main+0x74>
xor    %eax,%eax
call   1050 <getch@plt>
add    $0xffffffbf,%eax
cmp    $0x3,%eax
ja     1185 <main+0x55>
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
mov    %rbx,%rdi
call   1030 <puts@plt>
jmp    1185 <main+0x55>
mov    %rbp,%rdi
call   1030 <puts@plt>
jmp    1185 <main+0x55>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
add    $0xffffffff,%r13d
jmp    11e7 <main+0xb7>
mov    %r12,%rdi
call   1030 <puts@plt>
add    $0x1,%r13d
mov    %r15,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1185 <main+0x55>
lea    0xe8e(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Public Domain Readline \0A\00"
@.str.1 = [50 x i8] c"\0AUse the up and down arrow keys then press Enter\0A\00"
@.str.2 = [24 x i8] c"You pressed up arrow! \0A\00"
@.str.3 = [20 x i8] c"Menunum is now %d \0A\00"
@.str.4 = [26 x i8] c"You pressed down arrow! \0A\00"
@.str.5 = [27 x i8] c"You pressed right arrow! \0A\00"
@.str.6 = [26 x i8] c"You pressed left arrow! \0A\00"
@.str.7 = [39 x i8] c"You pressed ENTER! You chose item %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %6
6: ; preds = %0, %55
%7 = call i32 (...) @getch()
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 27
br i1 %9, label %10, label %48
10: ; preds = %6
%11 = call i32 (...) @getch()
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 91
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 (...) @getch()
store i32 %15, i32* %3
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 65
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %47
25: ; preds = %16
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 66
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
store i32 %31, i32* %2
%32 = load i32, i32* %2
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %46
34: ; preds = %25
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, 67
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %45
39: ; preds = %34
%40 = load i32, i32* %3
%41 = icmp eq i32 %40, 68
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
br label %45
45: ; preds = %44, %37
br label %46
46: ; preds = %45, %28
br label %47
47: ; preds = %46, %19
br label %55
48: ; preds = %6
%49 = load i32, i32* %3
%50 = icmp eq i32 %49, 10
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %2
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i32 %52)
br label %56
54: ; preds = %48
br label %55
55: ; preds = %54, %47
br label %6
56: ; preds = %51
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [24 x i8] c"You pressed up arrow! \0A\00"
@.str.3 = [20 x i8] c"Menunum is now %d \0A\00"
@.str.4 = [26 x i8] c"You pressed down arrow! \0A\00"
@.str.5 = [27 x i8] c"You pressed right arrow! \0A\00"
@.str.6 = [26 x i8] c"You pressed left arrow! \0A\00"
@.str.7 = [39 x i8] c"You pressed ENTER! You chose item %d \0A\00"
@str = [24 x i8] c"Public Domain Readline \00"
@str.8 = [49 x i8] c"\0AUse the up and down arrow keys then press Enter\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.8, i64 0, i64 0))
br label %3
3: ; preds = %18, %0
%4 = phi i32 [ 0, %0 ], [ %19, %18 ]
br label %5
5: ; preds = %28, %3
%6 = tail call i32 (...) @getch()
switch i32 %6, label %28 [
i32 27, label %7
i32 10, label %29
]
7: ; preds = %5
%8 = tail call i32 (...) @getch()
%9 = icmp eq i32 %8, 91
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 (...) @getch()
br label %12
12: ; preds = %10, %7
%13 = phi i32 [ %11, %10 ], [ %8, %7 ]
switch i32 %13, label %28 [
i32 65, label %14
i32 66, label %20
i32 67, label %24
i32 68, label %26
]
14: ; preds = %12
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%16 = add nsw i32 %4, -1
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %20
%19 = phi i32 [ %22, %20 ], [ %16, %14 ]
br label %3
20: ; preds = %12
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%22 = add nsw i32 %4, 1
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %18
24: ; preds = %12
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %28
26: ; preds = %12
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %28
28: ; preds = %26, %24, %12, %5
br label %5
29: ; preds = %5
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i32 %4)
ret i32 0
}