// File: /workspace/github_c_makefile_star2_O2/kobolt[P]curses-collection/menu/menu.c
// Function: main
int main(int argc, char *argv[])
{
  int c, maxy, maxx;
  char line[TEXT_LIMIT], *p;
  FILE *fh;

  if (argc != 2) {
    printf("Usage: %s <file with menu lines>\n", argv[0]);
    return 0;
  }

  fh = fopen(argv[1], "r");
  if (fh == NULL) {
    printf("Error: Unable to open menu file for reading.\n");
    return 0;
  }

  while (fgets(line, TEXT_LIMIT, fh) != NULL) {
    for (p = line; *p != '\0'; p++) {
      if ((*p == '\r') || (*p == '\n')) {
        *p = '\0';
        break;
      }
    }
    strncpy(list[list_size], line, TEXT_LIMIT);
    list_size++;
    if (list_size == ENTRY_LIMIT)
      break;
  }
  fclose(fh);

  signal(SIGINT, interrupt_handler);

  initscr();
  atexit(exit_handler);
  noecho();
  keypad(stdscr, TRUE);

  while (1) {
    update_screen();
    getmaxyx(stdscr, maxy, maxx);
    c = getch();

    switch (c) {
    case KEY_RESIZE:
      /* Use this event instead of SIGWINCH for better portability. */
      winch_handler();
      break;

    case KEY_UP:
      selected_entry--;
      if (selected_entry < 0)
        selected_entry++;
      if (scroll_offset > selected_entry) {
        scroll_offset--;
        if (scroll_offset < 0)
          scroll_offset = 0;
      }
      break;

    case KEY_DOWN:
      selected_entry++;
      if (selected_entry >= list_size)
        selected_entry--;
      if (selected_entry > maxy - 1) {
        scroll_offset++;
        if (scroll_offset > selected_entry - maxy + 1)
          scroll_offset--;
      }
      break;

    case KEY_NPAGE:
      scroll_offset += maxy / 2;
      while (maxy + scroll_offset > list_size)
        scroll_offset--;
      if (scroll_offset < 0)
        scroll_offset = 0;
      if (selected_entry < scroll_offset)
        selected_entry = scroll_offset;
      break;

    case KEY_PPAGE:
      scroll_offset -= maxy / 2;
      if (scroll_offset < 0)
        scroll_offset = 0;
      if (selected_entry > maxy + scroll_offset - 1)
        selected_entry = maxy + scroll_offset - 1;
      break;

    case KEY_ENTER:
    case '\n':
    case '\r':
      /* Need to start at 1 to differentiate between a valid selection
         and other kinds of exits, that returns 0. */
      return selected_entry + 1;

    case '\e': /* Escape */
    case 'Q':
    case 'q':
      return 0;
    }

  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
cmpl $0x2, %edi
jne 0x1514
movq 0x8(%rsi), %rdi
leaq 0xd65(%rip), %rsi       # 0x2026
callq 0x1110
testq %rax, %rax
je 0x1529
movq %rax, %rbx
movq %rsp, %r14
leaq 0x2e14(%rip), %r15      # 0x40f0
movq %r14, %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x10f0
testq %rax, %rax
je 0x133d
movq %r14, %rax
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x130d
cmpl $0xd, %ecx
je 0x130a
cmpl $0xa, %ecx
je 0x130a
incq %rax
jmp 0x12f4
movb $0x0, (%rax)
movslq 0xad5c(%rip), %r12      # 0xc070
movq %r12, %rdi
shlq $0x7, %rdi
addq %r15, %rdi
movl $0x80, %edx
movq %r14, %rsi
callq 0x1050
leal 0x1(%r12), %eax
movl %eax, 0xad3a(%rip)      # 0xc070
cmpl $0xff, %eax
jne 0x12dc
movq %rbx, %rdi
callq 0x1080
leaq 0x209(%rip), %rsi       # 0x1555
pushq $0x2
popq %rbx
movl %ebx, %edi
callq 0x1100
callq 0x10c0
leaq 0x1fb(%rip), %rdi       # 0x155d
callq 0x17c0
callq 0x10a0
movq 0x2c6d(%rip), %r15      # 0x3fe0
movq (%r15), %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0x10d0
pushq $-0x1
popq %r12
xorl %r13d, %r13d
callq 0x1562
movq (%r15), %rdi
movl %r12d, %r14d
testq %rdi, %rdi
je 0x139f
movswl 0x4(%rdi), %r14d
incl %r14d
callq 0x1070
cmpl $0x102, %eax            # imm = 0x102
je 0x1479
cmpl $0x103, %eax            # imm = 0x103
je 0x140c
cmpl $0x152, %eax            # imm = 0x152
je 0x14d2
cmpl $0x153, %eax            # imm = 0x153
je 0x1442
cmpl $0x19a, %eax            # imm = 0x19A
je 0x14b4
cmpl $0xa, %eax
je 0x1539
cmpl $0xd, %eax
je 0x1539
cmpl $0x1b, %eax
je 0x1535
cmpl $0x51, %eax
je 0x1535
cmpl $0x71, %eax
je 0x1535
cmpl $0x157, %eax            # imm = 0x157
jne 0x1387
jmp 0x1539
movl 0xac62(%rip), %ecx      # 0xc074
xorl %eax, %eax
testl %ecx, %ecx
setg %al
subl %eax, %ecx
movl %ecx, 0xac53(%rip)      # 0xc074
movl 0xac51(%rip), %eax      # 0xc078
cmpl %ecx, %eax
jle 0x1387
cmpl $0x2, %eax
cmovll %ebp, %eax
decl %eax
movl %eax, 0xac3b(%rip)      # 0xc078
jmp 0x1387
movl %r14d, %eax
cltd
pushq $-0x2
popq %rcx
idivl %ecx
addl 0xac27(%rip), %eax      # 0xc078
testl %eax, %eax
cmovlel %r13d, %eax
movl %eax, 0xac1b(%rip)      # 0xc078
addl %r14d, %eax
cmpl %eax, 0xac0e(%rip)      # 0xc074
jl 0x1387
decl %eax
movl %eax, 0xac00(%rip)      # 0xc074
jmp 0x1387
movl 0xabf5(%rip), %eax      # 0xc074
leal 0x1(%rax), %ecx
cmpl 0xabe8(%rip), %ecx      # 0xc070
cmovll %ecx, %eax
movl %eax, 0xabe3(%rip)      # 0xc074
subl %r14d, %eax
jl 0x1387
movl 0xabd8(%rip), %ecx      # 0xc078
xorl %edx, %edx
cmpl %eax, %ecx
setle %dl
addl %ecx, %edx
movl %edx, 0xabc9(%rip)      # 0xc078
jmp 0x1387
callq 0x1170
callq 0x1562
callq 0x1040
movq (%r15), %rdi
movl %ebp, %esi
callq 0x10d0
jmp 0x1387
movl %r14d, %eax
cltd
idivl %ebx
movl 0xab92(%rip), %edx      # 0xc070
movl 0xab94(%rip), %ecx      # 0xc078
addl %r14d, %ecx
addl %eax, %ecx
cmpl %ecx, %edx
cmovll %edx, %ecx
subl %r14d, %ecx
testl %ecx, %ecx
cmovlel %r13d, %ecx
movl %ecx, 0xab7b(%rip)      # 0xc078
cmpl %ecx, 0xab71(%rip)      # 0xc074
jge 0x1387
movl %ecx, 0xab65(%rip)      # 0xc074
jmp 0x1387
movq (%rsi), %rsi
leaq 0xae6(%rip), %rdi       # 0x2004
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x10b0
jmp 0x1541
leaq 0xaf8(%rip), %rdi       # 0x2028
callq 0x1060
xorl %ebx, %ebx
jmp 0x1541
movl 0xab35(%rip), %ebx      # 0xc074
incl %ebx
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq