#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 /*<<< orphan*/ FALSE;
int KEY_DOWN;
int KEY_ENTER;
int KEY_UP;
int /*<<< orphan*/ TRUE;
int /*<<< orphan*/ cbreak();
int /*<<< orphan*/ clear_all_screen();
int /*<<< orphan*/ draw_menu(char **, int, int, int);
int /*<<< orphan*/ echo();
int getch();
int /*<<< orphan*/ keypad(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mvprintw(int, int, char *);
int /*<<< orphan*/ nocbreak();
int /*<<< orphan*/ noecho();
int /*<<< orphan*/ stdscr;
int getchoice(char *greet, char *choices[]) {
  static int selected_row = 0;
  int max_row = 0;
  int start_screenrow = 6, start_screencol = 10;
  char **option;
  int selected;
  int key = 0;
  option = choices;
  while (*option) {
    max_row++;
    option++;
  }
  if (selected_row >= max_row)
    selected_row = 0;
  clear_all_screen();
  mvprintw(start_screenrow - 2, start_screencol, greet);
  keypad(stdscr, TRUE);
  cbreak();
  noecho();
  key = 0;
  while (key != 'q' && key != KEY_ENTER && key != '\n') {
    if (key == KEY_UP) {
      if (selected_row == 0)
        selected_row = max_row - 1;
      else
        selected_row--;
    }
    if (key == KEY_DOWN) {
      if (selected_row == (max_row - 1))
        selected_row = 0;
      else
        selected_row++;
    }
    selected = *choices[selected_row];
    draw_menu(choices, selected_row, start_screenrow, start_screencol);
    key = getch();
  }
  keypad(stdscr, FALSE);
  nocbreak();
  echo();
  if (key == 'q')
    selected = 'q';
  return (selected);
}


x86 assembly (clang, -O0):
<getchoice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x6,-0x18(%rbp)
movl   $0xa,-0x1c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     11ec <getchoice+0x5c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    11c4 <getchoice+0x34>
mov    0x2e7a(%rip),%eax
cmp    -0x14(%rbp),%eax
jl     1205 <getchoice+0x75>
movl   $0x0,0x2e67(%rip)
mov    $0x0,%al
call   1090 <clear_all_screen@plt>
mov    -0x18(%rbp),%edi
sub    $0x2,%edi
mov    -0x1c(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   10b0 <mvprintw@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
call   1050 <keypad@plt>
mov    $0x0,%al
call   1040 <cbreak@plt>
mov    $0x0,%al
call   1080 <noecho@plt>
movl   $0x0,-0x30(%rbp)
xor    %eax,%eax
cmpl   $0x71,-0x30(%rbp)
mov    %al,-0x31(%rbp)
je     127a <getchoice+0xea>
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
mov    0x2d4b(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x31(%rbp)
je     127a <getchoice+0xea>
cmpl   $0xa,-0x30(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    128a <getchoice+0xfa>
jmp    1350 <getchoice+0x1c0>
mov    -0x30(%rbp),%eax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%eax
jne    12ce <getchoice+0x13e>
cmpl   $0x0,0x2dc9(%rip)
jne    12ba <getchoice+0x12a>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,0x2db7(%rip)
jmp    12c9 <getchoice+0x139>
mov    0x2dac(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2da3(%rip)
jmp    12ce <getchoice+0x13e>
mov    -0x30(%rbp),%eax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%eax
jne    1317 <getchoice+0x187>
mov    0x2d86(%rip),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1303 <getchoice+0x173>
movl   $0x0,0x2d6e(%rip)
jmp    1312 <getchoice+0x182>
mov    0x2d63(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d5a(%rip)
jmp    1317 <getchoice+0x187>
mov    -0x10(%rbp),%rax
movslq 0x2d4a(%rip),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d36(%rip),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   10a0 <draw_menu@plt>
mov    $0x0,%al
call   1060 <getch@plt>
mov    %eax,-0x30(%rbp)
jmp    124a <getchoice+0xba>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
mov    0x2c60(%rip),%rax
mov    (%rax),%esi
call   1050 <keypad@plt>
mov    $0x0,%al
call   1070 <nocbreak@plt>
mov    $0x0,%al
call   1030 <echo@plt>
cmpl   $0x71,-0x30(%rbp)
jne    1386 <getchoice+0x1f6>
movl   $0x71,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getchoice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
cmpq   $0x0,(%rsi)
je     12ad <getchoice+0x11d>
xor    %ebx,%ebx
nopl   (%rax)
cmpq   $0x0,0x8(%r14,%rbx,8)
lea    0x1(%rbx),%rbx
jne    11b0 <getchoice+0x20>
cmp    %ebx,0x2eaa(%rip)
jl     11ce <getchoice+0x3e>
movl   $0x0,0x2e9e(%rip)
xor    %eax,%eax
call   1090 <clear_all_screen@plt>
mov    $0x4,%edi
mov    $0xa,%esi
mov    %rbp,%rdx
call   10b0 <mvprintw@plt>
mov    0x2dea(%rip),%r15
mov    (%r15),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1050 <keypad@plt>
xor    %eax,%eax
call   1040 <cbreak@plt>
xor    %eax,%eax
call   1080 <noecho@plt>
mov    0x2d9c(%rip),%r13
cmpl   $0x0,0x0(%r13)
je     12c0 <getchoice+0x130>
add    $0xffffffff,%ebx
mov    0x2db7(%rip),%r12
mov    0x2db8(%rip),%r15
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    (%r12),%eax
jne    1258 <getchoice+0xc8>
mov    0x2e20(%rip),%ecx
sub    $0x1,%ecx
cmovb  %ebx,%ecx
mov    %ecx,0x2e14(%rip)
mov    0x2e0e(%rip),%ecx
cmp    (%r15),%eax
jne    1278 <getchoice+0xe8>
cmp    %ebx,%ecx
lea    0x1(%rcx),%eax
mov    $0x0,%ecx
cmove  %ecx,%eax
mov    %eax,0x2df6(%rip)
mov    %eax,%ecx
movslq %ecx,%rsi
mov    (%r14,%rsi,8),%rax
movsbl (%rax),%ebp
mov    %r14,%rdi
mov    $0x6,%edx
mov    $0xa,%ecx
call   10a0 <draw_menu@plt>
xor    %eax,%eax
call   1060 <getch@plt>
cmp    $0x71,%eax
je     12c2 <getchoice+0x132>
cmp    $0xa,%eax
je     12c7 <getchoice+0x137>
cmp    0x0(%r13),%eax
jne    1240 <getchoice+0xb0>
jmp    12c7 <getchoice+0x137>
xor    %ebx,%ebx
cmp    %ebx,0x2db7(%rip)
jge    11c4 <getchoice+0x34>
jmp    11ce <getchoice+0x3e>
jmp    12ce <getchoice+0x13e>
mov    $0x71,%ebp
mov    0x2d0a(%rip),%r15
mov    (%r15),%edi
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
call   1050 <keypad@plt>
xor    %eax,%eax
call   1070 <nocbreak@plt>
xor    %eax,%eax
call   1030 <echo@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getchoice.selected_row = internal global i32 0
@stdscr = global i32 0
@TRUE = global i32 0
@KEY_ENTER = global i32 0
@KEY_UP = global i32 0
@KEY_DOWN = global i32 0
@FALSE = global i32 0
define i32 @getchoice(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %5
store i32 6, i32* %6
store i32 10, i32* %7
store i32 0, i32* %10
%11 = load i8**, i8*** %4
store i8** %11, i8*** %8
br label %12
12: ; preds = %16, %2
%13 = load i8**, i8*** %8
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
%19 = load i8**, i8*** %8
%20 = getelementptr inbounds i8*, i8** %19, i32 1
store i8** %20, i8*** %8
br label %12
21: ; preds = %12
%22 = load i32, i32* @getchoice.selected_row
%23 = load i32, i32* %5
%24 = icmp sge i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* @getchoice.selected_row
br label %26
26: ; preds = %25, %21
%27 = call i32 (...) @clear_all_screen()
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 2
%30 = load i32, i32* %7
%31 = load i8*, i8** %3
%32 = call i32 @mvprintw(i32 %29, i32 %30, i8* %31)
%33 = load i32, i32* @stdscr
%34 = load i32, i32* @TRUE
%35 = call i32 @keypad(i32 %33, i32 %34)
%36 = call i32 (...) @cbreak()
%37 = call i32 (...) @noecho()
store i32 0, i32* %10
br label %38
38: ; preds = %78, %26
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 113
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i32, i32* %10
%43 = load i32, i32* @KEY_ENTER
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* %10
%47 = icmp ne i32 %46, 10
br label %48
48: ; preds = %45, %41, %38
%49 = phi i1 [ false, %41 ], [ false, %38 ], [ %47, %45 ]
br i1 %49, label %50, label %92
50: ; preds = %48
%51 = load i32, i32* %10
%52 = load i32, i32* @KEY_UP
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %64
54: ; preds = %50
%55 = load i32, i32* @getchoice.selected_row
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i32, i32* %5
%59 = sub nsw i32 %58, 1
store i32 %59, i32* @getchoice.selected_row
br label %63
60: ; preds = %54
%61 = load i32, i32* @getchoice.selected_row
%62 = add nsw i32 %61, -1
store i32 %62, i32* @getchoice.selected_row
br label %63
63: ; preds = %60, %57
br label %64
64: ; preds = %63, %50
%65 = load i32, i32* %10
%66 = load i32, i32* @KEY_DOWN
%67 = icmp eq i32 %65, %66
br i1 %67, label %68, label %78
68: ; preds = %64
%69 = load i32, i32* @getchoice.selected_row
%70 = load i32, i32* %5
%71 = sub nsw i32 %70, 1
%72 = icmp eq i32 %69, %71
br i1 %72, label %73, label %74
73: ; preds = %68
store i32 0, i32* @getchoice.selected_row
br label %77
74: ; preds = %68
%75 = load i32, i32* @getchoice.selected_row
%76 = add nsw i32 %75, 1
store i32 %76, i32* @getchoice.selected_row
br label %77
77: ; preds = %74, %73
br label %78
78: ; preds = %77, %64
%79 = load i8**, i8*** %4
%80 = load i32, i32* @getchoice.selected_row
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8*, i8** %79, i64 %81
%83 = load i8*, i8** %82
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
store i32 %85, i32* %9
%86 = load i8**, i8*** %4
%87 = load i32, i32* @getchoice.selected_row
%88 = load i32, i32* %6
%89 = load i32, i32* %7
%90 = call i32 @draw_menu(i8** %86, i32 %87, i32 %88, i32 %89)
%91 = call i32 (...) @getch()
store i32 %91, i32* %10
br label %38
92: ; preds = %48
%93 = load i32, i32* @stdscr
%94 = load i32, i32* @FALSE
%95 = call i32 @keypad(i32 %93, i32 %94)
%96 = call i32 (...) @nocbreak()
%97 = call i32 (...) @echo()
%98 = load i32, i32* %10
%99 = icmp eq i32 %98, 113
br i1 %99, label %100, label %101
100: ; preds = %92
store i32 113, i32* %9
br label %101
101: ; preds = %100, %92
%102 = load i32, i32* %9
ret i32 %102
}

LLVM IR (-O2):
@getchoice.selected_row = internal unnamed_addr global i32 0
@stdscr = global i32 0
@TRUE = global i32 0
@KEY_ENTER = global i32 0
@KEY_UP = global i32 0
@KEY_DOWN = global i32 0
@FALSE = global i32 0
define i32 @getchoice(i8* %0, i8** %1) {
%3 = load i8*, i8** %1
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %2, %5
%6 = phi i8** [ %9, %5 ], [ %1, %2 ]
%7 = phi i32 [ %8, %5 ], [ 0, %2 ]
%8 = add nuw nsw i32 %7, 1
%9 = getelementptr inbounds i8*, i8** %6, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %5
12: ; preds = %5, %2
%13 = phi i32 [ 0, %2 ], [ %8, %5 ]
%14 = load i32, i32* @getchoice.selected_row
%15 = icmp slt i32 %14, %13
br i1 %15, label %17, label %16
16: ; preds = %12
store i32 0, i32* @getchoice.selected_row
br label %17
17: ; preds = %16, %12
%18 = tail call i32 (...) @clear_all_screen()
%19 = tail call i32 @mvprintw(i32 4, i32 10, i8* %0)
%20 = load i32, i32* @stdscr
%21 = load i32, i32* @TRUE
%22 = tail call i32 @keypad(i32 %20, i32 %21)
%23 = tail call i32 (...) @cbreak()
%24 = tail call i32 (...) @noecho()
%25 = add nsw i32 %13, -1
%26 = load i32, i32* @KEY_ENTER
%27 = icmp eq i32 %26, 0
br i1 %27, label %61, label %33
28: ; preds = %50
%29 = load i32, i32* @KEY_ENTER
%30 = icmp ne i32 %57, %29
%31 = icmp ne i32 %57, 10
%32 = and i1 %31, %30
br i1 %32, label %33, label %59
33: ; preds = %17, %28
%34 = phi i32 [ %57, %28 ], [ 0, %17 ]
%35 = load i32, i32* @KEY_UP
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @getchoice.selected_row
%39 = icmp eq i32 %38, 0
%40 = add nsw i32 %38, -1
%41 = select i1 %39, i32 %25, i32 %40
store i32 %41, i32* @getchoice.selected_row
br label %42
42: ; preds = %37, %33
%43 = load i32, i32* @KEY_DOWN
%44 = icmp eq i32 %34, %43
%45 = load i32, i32* @getchoice.selected_row
br i1 %44, label %46, label %50
46: ; preds = %42
%47 = icmp eq i32 %45, %25
%48 = add nsw i32 %45, 1
%49 = select i1 %47, i32 0, i32 %48
store i32 %49, i32* @getchoice.selected_row
br label %50
50: ; preds = %46, %42
%51 = phi i32 [ %45, %42 ], [ %49, %46 ]
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %1, i64 %52
%54 = load i8*, i8** %53
%55 = load i8, i8* %54
%56 = tail call i32 @draw_menu(i8** %1, i32 %51, i32 6, i32 10)
%57 = tail call i32 (...) @getch()
%58 = icmp eq i32 %57, 113
br i1 %58, label %61, label %28
59: ; preds = %28
%60 = sext i8 %55 to i32
br label %61
61: ; preds = %50, %59, %17
%62 = phi i32 [ %60, %59 ], [ undef, %17 ], [ 113, %50 ]
%63 = load i32, i32* @stdscr
%64 = load i32, i32* @FALSE
%65 = tail call i32 @keypad(i32 %63, i32 %64)
%66 = tail call i32 (...) @nocbreak()
%67 = tail call i32 (...) @echo()
ret i32 %62
}