#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*/ COLOR_PAIR(int);
int KEY_DOWN;
int KEY_UP;
int /*<<< orphan*/ doupdate();
int getch();
int /*<<< orphan*/ wbkgd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ wnoutrefresh(int /*<<< orphan*/ *);
int scroll_menu(WINDOW **items, int count, int menu_start_col, int index_num) {
  int key;
  int selected = 0;
  while (1) {
    key = getch();
    if (key == KEY_DOWN || key == KEY_UP) {
      wbkgd(items[selected + 1], COLOR_PAIR(2));
      wnoutrefresh(items[selected + 1]);
      if (key == KEY_DOWN) {
        selected = (selected + 1) % count;
      } else {
        selected = (selected + count - 1) % count;
      }
      wbkgd(items[selected + 1], COLOR_PAIR(1));
      wnoutrefresh(items[selected + 1]);
      doupdate();
    } else if (key == 27) {
      return -1;
    } else if (key == 10) {
      return selected;
    }
  }
}


x86 assembly (clang, -O0):
<scroll_menu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%eax
je     119a <scroll_menu+0x4a>
mov    -0x20(%rbp),%eax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%eax
jne    125a <scroll_menu+0x10a>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
mov    $0x2,%edi
call   1040 <COLOR_PAIR@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1030 <wbkgd@plt>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1050 <wnoutrefresh@plt>
mov    -0x20(%rbp),%eax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
jne    11fe <scroll_menu+0xae>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x24(%rbp)
jmp    120e <scroll_menu+0xbe>
mov    -0x24(%rbp),%eax
add    -0x14(%rbp),%eax
sub    $0x1,%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    $0x1,%edi
call   1040 <COLOR_PAIR@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1030 <wbkgd@plt>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1050 <wnoutrefresh@plt>
mov    $0x0,%al
call   1070 <doupdate@plt>
jmp    128f <scroll_menu+0x13f>
cmpl   $0x1b,-0x20(%rbp)
jne    1270 <scroll_menu+0x120>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <scroll_menu+0x144>
cmpl   $0xa,-0x20(%rbp)
jne    1285 <scroll_menu+0x135>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1294 <scroll_menu+0x144>
jmp    128a <scroll_menu+0x13a>
jmp    128f <scroll_menu+0x13f>
jmp    116c <scroll_menu+0x1c>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scroll_menu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x18(%rsp)
mov    %rsi,0x20(%rsp)
lea    -0x1(%rsi),%eax
mov    %eax,0xc(%rsp)
mov    0x2e72(%rip),%r14
mov    0x2e63(%rip),%r15
xor    %r12d,%r12d
mov    0x18(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1060 <getch@plt>
mov    %eax,%ebx
cmp    (%r14),%eax
je     11b3 <scroll_menu+0x63>
cmp    (%r15),%ebx
je     11b3 <scroll_menu+0x63>
cmp    $0xa,%ebx
je     1231 <scroll_menu+0xe1>
cmp    $0x1b,%ebx
jne    1190 <scroll_menu+0x40>
jmp    122b <scroll_menu+0xdb>
movslq %r12d,%rbp
add    $0x1,%r12d
mov    0x8(%r13,%rbp,8),%rax
mov    %rax,0x10(%rsp)
mov    $0x2,%edi
call   1040 <COLOR_PAIR@plt>
mov    0x10(%rsp),%rdi
mov    %eax,%esi
call   1030 <wbkgd@plt>
mov    0x8(%r13,%rbp,8),%rdi
call   1050 <wnoutrefresh@plt>
add    0xc(%rsp),%ebp
cmp    (%r14),%ebx
cmove  %r12d,%ebp
mov    %ebp,%eax
cltd
idivl  0x20(%rsp)
mov    %edx,%r12d
movslq %edx,%rbp
mov    0x8(%r13,%rbp,8),%rbx
mov    $0x1,%edi
call   1040 <COLOR_PAIR@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <wbkgd@plt>
mov    0x8(%r13,%rbp,8),%rdi
call   1050 <wnoutrefresh@plt>
xor    %eax,%eax
call   1070 <doupdate@plt>
jmp    1190 <scroll_menu+0x40>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@KEY_DOWN = global i32 0
@KEY_UP = global i32 0
define i32 @scroll_menu(i32** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32** %0, i32*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
br label %12
12: ; preds = %4, %80
%13 = call i32 (...) @getch()
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = load i32, i32* @KEY_DOWN
%16 = icmp eq i32 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = load i32, i32* %10
%19 = load i32, i32* @KEY_UP
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %69
21: ; preds = %17, %12
%22 = load i32**, i32*** %6
%23 = load i32, i32* %11
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32*, i32** %22, i64 %25
%27 = load i32*, i32** %26
%28 = call i32 @COLOR_PAIR(i32 2)
%29 = call i32 @wbkgd(i32* %27, i32 %28)
%30 = load i32**, i32*** %6
%31 = load i32, i32* %11
%32 = add nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32*, i32** %30, i64 %33
%35 = load i32*, i32** %34
%36 = call i32 @wnoutrefresh(i32* %35)
%37 = load i32, i32* %10
%38 = load i32, i32* @KEY_DOWN
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %21
%41 = load i32, i32* %11
%42 = add nsw i32 %41, 1
%43 = load i32, i32* %7
%44 = srem i32 %42, %43
store i32 %44, i32* %11
br label %52
45: ; preds = %21
%46 = load i32, i32* %11
%47 = load i32, i32* %7
%48 = add nsw i32 %46, %47
%49 = sub nsw i32 %48, 1
%50 = load i32, i32* %7
%51 = srem i32 %49, %50
store i32 %51, i32* %11
br label %52
52: ; preds = %45, %40
%53 = load i32**, i32*** %6
%54 = load i32, i32* %11
%55 = add nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32*, i32** %53, i64 %56
%58 = load i32*, i32** %57
%59 = call i32 @COLOR_PAIR(i32 1)
%60 = call i32 @wbkgd(i32* %58, i32 %59)
%61 = load i32**, i32*** %6
%62 = load i32, i32* %11
%63 = add nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32*, i32** %61, i64 %64
%66 = load i32*, i32** %65
%67 = call i32 @wnoutrefresh(i32* %66)
%68 = call i32 (...) @doupdate()
br label %80
69: ; preds = %17
%70 = load i32, i32* %10
%71 = icmp eq i32 %70, 27
br i1 %71, label %72, label %73
72: ; preds = %69
store i32 -1, i32* %5
br label %81
73: ; preds = %69
%74 = load i32, i32* %10
%75 = icmp eq i32 %74, 10
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = load i32, i32* %11
store i32 %77, i32* %5
br label %81
78: ; preds = %73
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79, %52
br label %12
81: ; preds = %76, %72
%82 = load i32, i32* %5
ret i32 %82
}

LLVM IR (-O2):
@KEY_DOWN = global i32 0
@KEY_UP = global i32 0
define i32 @scroll_menu(i32** nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = add i32 %1, -1
br label %6
6: ; preds = %15, %4
%7 = phi i32 [ %28, %15 ], [ 0, %4 ]
br label %8
8: ; preds = %6, %38
%9 = tail call i32 (...) @getch()
%10 = load i32, i32* @KEY_DOWN
%11 = icmp eq i32 %9, %10
%12 = load i32, i32* @KEY_UP
%13 = icmp eq i32 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %38
15: ; preds = %8
%16 = add nsw i32 %7, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32*, i32** %0, i64 %17
%19 = load i32*, i32** %18
%20 = tail call i32 @COLOR_PAIR(i32 2)
%21 = tail call i32 @wbkgd(i32* %19, i32 %20)
%22 = load i32*, i32** %18
%23 = tail call i32 @wnoutrefresh(i32* %22)
%24 = load i32, i32* @KEY_DOWN
%25 = icmp eq i32 %9, %24
%26 = add i32 %5, %7
%27 = select i1 %25, i32 %16, i32 %26
%28 = srem i32 %27, %1
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32*, i32** %0, i64 %30
%32 = load i32*, i32** %31
%33 = tail call i32 @COLOR_PAIR(i32 1)
%34 = tail call i32 @wbkgd(i32* %32, i32 %33)
%35 = load i32*, i32** %31
%36 = tail call i32 @wnoutrefresh(i32* %35)
%37 = tail call i32 (...) @doupdate()
br label %6
38: ; preds = %8
switch i32 %9, label %8 [
i32 27, label %39
i32 10, label %40
]
39: ; preds = %38
br label %40
40: ; preds = %38, %39
%41 = phi i32 [ -1, %39 ], [ %7, %38 ]
ret i32 %41
}