#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*/ WINDOW;

/* Variables and functions */
int /*<<< orphan*/ default_keys();
int /*<<< orphan*/ draw_arrow(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ draw_keys(int /*<<< orphan*/ *);
int /*<<< orphan*/ erase_arrow(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ erase_window(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ getch();
int /*<<< orphan*/ key_dialog(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ mvwaddstr(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                             char *);
int /*<<< orphan*/ *newwin(int, int, int, int);
scalar_t__ pressed(int, int);
int /*<<< orphan*/ save_keys(char *);
int tolower(int /*<<< orphan*/);
int /*<<< orphan*/ wrefresh(int /*<<< orphan*/ *);
void keys_menu() {
  int active = 0;
  int bottom = 13 - 1;
  int input = 0;
  WINDOW *win = newwin(15, 37, 5, 43);
  mvwaddstr(win, 14, 0, "Q: Save & Return. D: Restore Defaults");
  draw_arrow(win, active + 1, 0);
  while (true) {
    draw_keys(win);
    wrefresh(win);
    input = tolower(getch());
    if (pressed(0, input)) {
      erase_arrow(win, active + 1, 0);
      if (active > 0) {
        active--;
      } else if (active <= 0) {
        active = bottom;
      }
      draw_arrow(win, active + 1, 0);
    } else if (pressed(1, input)) {
      erase_arrow(win, active + 1, 0);
      if (active < bottom) {
        active++;
      } else if (active >= bottom) {
        active = 0;
      }
      draw_arrow(win, active + 1, 0);
    } else if (input == '\n' || input == ' ') {
      key_dialog(win, active, -1);
      mvwaddstr(win, 0, 0, "                                     ");
    } else if (input == 'd') {
      default_keys();
    } else if (pressed(10, input) || input == 27) {
      save_keys("controls.conf");
      erase_window(win, 15, 37);
      return;
    }
  }
  return;
}


x86 assembly (clang, -O0):
<keys_menu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0xf,%edi
mov    $0x25,%esi
mov    $0x5,%edx
mov    $0x2b,%ecx
call   1060 <newwin@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0xe,%esi
xor    %edx,%edx
lea    0xde4(%rip),%rcx
call   10e0 <mvwaddstr@plt>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
add    $0x1,%esi
xor    %edx,%edx
call   1070 <draw_arrow@plt>
mov    -0x18(%rbp),%rdi
call   1030 <draw_keys@plt>
mov    -0x18(%rbp),%rdi
call   1050 <wrefresh@plt>
mov    $0x0,%al
call   10a0 <getch@plt>
mov    %eax,%edi
call   1090 <tolower@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
xor    %edi,%edi
call   10f0 <pressed@plt>
cmp    $0x0,%rax
je     12bd <keys_menu+0xed>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
add    $0x1,%esi
xor    %edx,%edx
call   10b0 <erase_arrow@plt>
cmpl   $0x0,-0x4(%rbp)
jle    1292 <keys_menu+0xc2>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    12a7 <keys_menu+0xd7>
cmpl   $0x0,-0x4(%rbp)
jg     12a2 <keys_menu+0xd2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a7 <keys_menu+0xd7>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
add    $0x1,%esi
xor    %edx,%edx
call   1070 <draw_arrow@plt>
jmp    13db <keys_menu+0x20b>
mov    -0xc(%rbp),%esi
mov    $0x1,%edi
call   10f0 <pressed@plt>
cmp    $0x0,%rax
je     132d <keys_menu+0x15d>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
add    $0x1,%esi
xor    %edx,%edx
call   10b0 <erase_arrow@plt>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12ff <keys_menu+0x12f>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1317 <keys_menu+0x147>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1312 <keys_menu+0x142>
movl   $0x0,-0x4(%rbp)
jmp    1317 <keys_menu+0x147>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
add    $0x1,%esi
xor    %edx,%edx
call   1070 <draw_arrow@plt>
jmp    13d6 <keys_menu+0x206>
cmpl   $0xa,-0xc(%rbp)
je     1341 <keys_menu+0x171>
cmpl   $0x20,-0xc(%rbp)
jne    136b <keys_menu+0x19b>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    $0xffffffff,%edx
call   10d0 <key_dialog@plt>
mov    -0x18(%rbp),%rdi
xor    %edx,%edx
lea    0xcc7(%rip),%rcx
mov    %edx,%esi
call   10e0 <mvwaddstr@plt>
jmp    13d1 <keys_menu+0x201>
cmpl   $0x64,-0xc(%rbp)
jne    1381 <keys_menu+0x1b1>
mov    $0x0,%al
call   1080 <default_keys@plt>
jmp    13cc <keys_menu+0x1fc>
mov    -0xc(%rbp),%esi
mov    $0xa,%edi
call   10f0 <pressed@plt>
cmp    $0x0,%rax
jne    13a2 <keys_menu+0x1d2>
cmpl   $0x1b,-0xc(%rbp)
jne    13c7 <keys_menu+0x1f7>
lea    0xca3(%rip),%rdi
call   1040 <save_keys@plt>
mov    -0x18(%rbp),%rdi
mov    $0xf,%esi
mov    $0x25,%edx
call   10c0 <erase_window@plt>
add    $0x20,%rsp
pop    %rbp
ret
jmp    13cc <keys_menu+0x1fc>
jmp    13d1 <keys_menu+0x201>
jmp    13d6 <keys_menu+0x206>
jmp    13db <keys_menu+0x20b>
jmp    1232 <keys_menu+0x62>

x86 assembly (clang, -O2):
<keys_menu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xf,%edi
mov    $0x25,%esi
mov    $0x5,%edx
mov    $0x2b,%ecx
call   1060 <newwin@plt>
mov    %rax,%rbx
lea    0xe02(%rip),%rcx
xor    %r12d,%r12d
mov    %rax,%rdi
mov    $0xe,%esi
xor    %edx,%edx
call   10e0 <mvwaddstr@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <draw_arrow@plt>
lea    0xe00(%rip),%r14
mov    $0xc,%r13d
xor    %r15d,%r15d
jmp    1247 <keys_menu+0x77>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <default_keys@plt>
mov    %rbx,%rdi
call   1030 <draw_keys@plt>
mov    %rbx,%rdi
call   1050 <wrefresh@plt>
xor    %eax,%eax
call   10a0 <getch@plt>
mov    %eax,%edi
call   1090 <tolower@plt>
mov    %eax,%ebp
xor    %edi,%edi
mov    %eax,%esi
call   10f0 <pressed@plt>
test   %rax,%rax
jne    12dc <keys_menu+0x10c>
mov    $0x1,%edi
mov    %ebp,%esi
call   10f0 <pressed@plt>
test   %rax,%rax
jne    12f7 <keys_menu+0x127>
cmp    $0xa,%ebp
je     1295 <keys_menu+0xc5>
cmp    $0x64,%ebp
je     1240 <keys_menu+0x70>
cmp    $0x20,%ebp
jne    12c0 <keys_menu+0xf0>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0xffffffff,%edx
call   10d0 <key_dialog@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   10e0 <mvwaddstr@plt>
jmp    1247 <keys_menu+0x77>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %ebp,%esi
call   10f0 <pressed@plt>
cmp    $0x1b,%ebp
je     1324 <keys_menu+0x154>
test   %rax,%rax
je     1247 <keys_menu+0x77>
jmp    1324 <keys_menu+0x154>
lea    0x1(%r15),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10b0 <erase_arrow@plt>
lea    -0x1(%r15),%ebp
test   %r15d,%r15d
cmovle %r13d,%ebp
jmp    130f <keys_menu+0x13f>
lea    0x1(%r15),%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   10b0 <erase_arrow@plt>
cmp    $0xc,%r15d
cmovge %r12d,%ebp
lea    0x1(%rbp),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <draw_arrow@plt>
mov    %ebp,%r15d
jmp    1247 <keys_menu+0x77>
lea    0xd21(%rip),%rdi
call   1040 <save_keys@plt>
mov    %rbx,%rdi
mov    $0xf,%esi
mov    $0x25,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <erase_window@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"Q: Save & Return. D: Restore Defaults\00"
@.str.1 = [38 x i8] c" \00"
@.str.2 = [14 x i8] c"controls.conf\00"
define void @keys_menu() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 12, i32* %2
store i32 0, i32* %3
%5 = call i32* @newwin(i32 15, i32 37, i32 5, i32 43)
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = call i32 @mvwaddstr(i32* %6, i32 14, i32 0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%8 = load i32*, i32** %4
%9 = load i32, i32* %1
%10 = add nsw i32 %9, 1
%11 = call i32 @draw_arrow(i32* %8, i32 %10, i32 0)
br label %12
12: ; preds = %0, %101
%13 = load i32*, i32** %4
%14 = call i32 @draw_keys(i32* %13)
%15 = load i32*, i32** %4
%16 = call i32 @wrefresh(i32* %15)
%17 = call i32 (...) @getch()
%18 = call i32 @tolower(i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = call i64 @pressed(i32 0, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %43
22: ; preds = %12
%23 = load i32*, i32** %4
%24 = load i32, i32* %1
%25 = add nsw i32 %24, 1
%26 = call i32 @erase_arrow(i32* %23, i32 %25, i32 0)
%27 = load i32, i32* %1
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %1
%31 = add nsw i32 %30, -1
store i32 %31, i32* %1
br label %38
32: ; preds = %22
%33 = load i32, i32* %1
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i32, i32* %2
store i32 %36, i32* %1
br label %37
37: ; preds = %35, %32
br label %38
38: ; preds = %37, %29
%39 = load i32*, i32** %4
%40 = load i32, i32* %1
%41 = add nsw i32 %40, 1
%42 = call i32 @draw_arrow(i32* %39, i32 %41, i32 0)
br label %101
43: ; preds = %12
%44 = load i32, i32* %3
%45 = call i64 @pressed(i32 1, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %69
47: ; preds = %43
%48 = load i32*, i32** %4
%49 = load i32, i32* %1
%50 = add nsw i32 %49, 1
%51 = call i32 @erase_arrow(i32* %48, i32 %50, i32 0)
%52 = load i32, i32* %1
%53 = load i32, i32* %2
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %47
%56 = load i32, i32* %1
%57 = add nsw i32 %56, 1
store i32 %57, i32* %1
br label %64
58: ; preds = %47
%59 = load i32, i32* %1
%60 = load i32, i32* %2
%61 = icmp sge i32 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 0, i32* %1
br label %63
63: ; preds = %62, %58
br label %64
64: ; preds = %63, %55
%65 = load i32*, i32** %4
%66 = load i32, i32* %1
%67 = add nsw i32 %66, 1
%68 = call i32 @draw_arrow(i32* %65, i32 %67, i32 0)
br label %100
69: ; preds = %43
%70 = load i32, i32* %3
%71 = icmp eq i32 %70, 10
br i1 %71, label %75, label %72
72: ; preds = %69
%73 = load i32, i32* %3
%74 = icmp eq i32 %73, 32
br i1 %74, label %75, label %81
75: ; preds = %72, %69
%76 = load i32*, i32** %4
%77 = load i32, i32* %1
%78 = call i32 @key_dialog(i32* %76, i32 %77, i32 -1)
%79 = load i32*, i32** %4
%80 = call i32 @mvwaddstr(i32* %79, i32 0, i32 0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %99
81: ; preds = %72
%82 = load i32, i32* %3
%83 = icmp eq i32 %82, 100
br i1 %83, label %84, label %86
84: ; preds = %81
%85 = call i32 (...) @default_keys()
br label %98
86: ; preds = %81
%87 = load i32, i32* %3
%88 = call i64 @pressed(i32 10, i32 %87)
%89 = icmp ne i64 %88, 0
br i1 %89, label %93, label %90
90: ; preds = %86
%91 = load i32, i32* %3
%92 = icmp eq i32 %91, 27
br i1 %92, label %93, label %97
93: ; preds = %90, %86
%94 = call i32 @save_keys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%95 = load i32*, i32** %4
%96 = call i32 @erase_window(i32* %95, i32 15, i32 37)
ret void
97: ; preds = %90
br label %98
98: ; preds = %97, %84
br label %99
99: ; preds = %98, %75
br label %100
100: ; preds = %99, %64
br label %101
101: ; preds = %100, %38
br label %12
}

LLVM IR (-O2):
@.str = [38 x i8] c"Q: Save & Return. D: Restore Defaults\00"
@.str.1 = [38 x i8] c" \00"
@.str.2 = [14 x i8] c"controls.conf\00"
define void @keys_menu() {
%1 = tail call i32* @newwin(i32 15, i32 37, i32 5, i32 43)
%2 = tail call i32 @mvwaddstr(i32* %1, i32 14, i32 0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @draw_arrow(i32* %1, i32 1, i32 0)
br label %4
4: ; preds = %21, %0
%5 = phi i32 [ 0, %0 ], [ %22, %21 ]
br label %6
6: ; preds = %39, %4
%7 = tail call i32 @draw_keys(i32* %1)
%8 = tail call i32 @wrefresh(i32* %1)
%9 = tail call i32 (...) @getch()
%10 = tail call i32 @tolower(i32 %9)
%11 = tail call i64 @pressed(i32 0, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %23, label %13
13: ; preds = %6
%14 = add nsw i32 %5, 1
%15 = tail call i32 @erase_arrow(i32* %1, i32 %14, i32 0)
%16 = icmp sgt i32 %5, 0
%17 = add nsw i32 %5, -1
%18 = select i1 %16, i32 %17, i32 12
%19 = add nuw nsw i32 %18, 1
%20 = tail call i32 @draw_arrow(i32* %1, i32 %19, i32 0)
br label %21
21: ; preds = %13, %26
%22 = phi i32 [ %30, %26 ], [ %18, %13 ]
br label %4
23: ; preds = %6
%24 = tail call i64 @pressed(i32 1, i32 %10)
%25 = icmp eq i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %23
%27 = add nsw i32 %5, 1
%28 = tail call i32 @erase_arrow(i32* %1, i32 %27, i32 0)
%29 = icmp slt i32 %5, 12
%30 = select i1 %29, i32 %27, i32 0
%31 = add nsw i32 %30, 1
%32 = tail call i32 @draw_arrow(i32* %1, i32 %31, i32 0)
br label %21
33: ; preds = %23
switch i32 %10, label %40 [
i32 32, label %34
i32 10, label %34
i32 100, label %37
]
34: ; preds = %33, %33
%35 = tail call i32 @key_dialog(i32* %1, i32 %5, i32 -1)
%36 = tail call i32 @mvwaddstr(i32* %1, i32 0, i32 0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %39
37: ; preds = %33
%38 = tail call i32 (...) @default_keys()
br label %39
39: ; preds = %37, %40, %34
br label %6
40: ; preds = %33
%41 = tail call i64 @pressed(i32 10, i32 %10)
%42 = icmp ne i64 %41, 0
%43 = icmp eq i32 %10, 27
%44 = or i1 %43, %42
br i1 %44, label %45, label %39
45: ; preds = %40
%46 = tail call i32 @save_keys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%47 = tail call i32 @erase_window(i32* %1, i32 15, i32 37)
ret void
}