// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x80, %rsp
cmpl $0x2, %edi
jne 0x1542
movq 0x8(%rsi), %rdi
leaq 0xd67(%rip), %rsi       # 0x2026
callq 0x1110
testq %rax, %rax
je 0x1557
movq %rax, %rbx
movq %rsp, %r14
leaq 0x2e16(%rip), %r15      # 0x40f0
movq %r14, %rdi
movl $0x80, %esi
movq %rbx, %rdx
callq 0x10f0
testq %rax, %rax
je 0x133b
movq %r14, %rax
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x130b
cmpl $0xd, %ecx
je 0x1308
cmpl $0xa, %ecx
je 0x1308
incq %rax
jmp 0x12f2
movb $0x0, (%rax)
movslq 0xad5e(%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, 0xad3c(%rip)      # 0xc070
cmpl $0xff, %eax
jne 0x12da
movq %rbx, %rdi
callq 0x1080
leaq 0x237(%rip), %rsi       # 0x1581
movl $0x2, %edi
callq 0x1100
callq 0x10c0
leaq 0x229(%rip), %rdi       # 0x1589
callq 0x17f0
callq 0x10a0
movq 0x2c6f(%rip), %rbx      # 0x3fe0
movq (%rbx), %rdi
movl $0x1, %ebp
movl $0x1, %esi
callq 0x10d0
callq 0x158e
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x139a
movswl 0x4(%rdi), %r14d
incl %r14d
jmp 0x13a0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
callq 0x1070
cmpl $0x151, %eax            # imm = 0x151
jle 0x13fc
cmpl $0x156, %eax            # imm = 0x156
jg 0x1421
cmpl $0x152, %eax            # imm = 0x152
je 0x1488
cmpl $0x153, %eax            # imm = 0x153
jne 0x1383
movl %r14d, %ecx
shrl $0x1f, %ecx
addl %r14d, %ecx
sarl %ecx
movl 0xaca2(%rip), %eax      # 0xc078
subl %ecx, %eax
testl %eax, %eax
jg 0x13de
xorl %eax, %eax
movl %eax, 0xac94(%rip)      # 0xc078
addl %eax, %r14d
cmpl %r14d, 0xac86(%rip)     # 0xc074
jl 0x1383
decl %r14d
movl %r14d, 0xac7a(%rip)     # 0xc074
jmp 0x1383
cmpl $0x70, %eax
jg 0x143c
cmpl $0x1a, %eax
jg 0x14d0
cmpl $0xa, %eax
je 0x1567
cmpl $0xd, %eax
jne 0x1383
jmp 0x1567
cmpl $0x19a, %eax            # imm = 0x19A
je 0x152b
cmpl $0x157, %eax            # imm = 0x157
jne 0x1383
jmp 0x1567
cmpl $0x102, %eax            # imm = 0x102
je 0x14e4
cmpl $0x103, %eax            # imm = 0x103
jne 0x1520
movl 0xac1c(%rip), %ecx      # 0xc074
xorl %eax, %eax
testl %ecx, %ecx
setg %al
subl %eax, %ecx
movl %ecx, 0xac0d(%rip)      # 0xc074
movl 0xac0b(%rip), %eax      # 0xc078
cmpl %ecx, %eax
jle 0x1383
cmpl $0x2, %eax
cmovll %ebp, %eax
decl %eax
movl %eax, 0xabf5(%rip)      # 0xc078
jmp 0x1383
movl 0xabe2(%rip), %eax      # 0xc070
movl 0xabe4(%rip), %edx      # 0xc078
addl %r14d, %edx
movl %r14d, %ecx
shrl $0x1f, %ecx
addl %r14d, %ecx
sarl %ecx
addl %edx, %ecx
cmpl %ecx, %eax
jl 0x14aa
movl %ecx, %eax
subl %r14d, %eax
testl %eax, %eax
jg 0x14b3
xorl %eax, %eax
movl %eax, 0xabbf(%rip)      # 0xc078
cmpl %eax, 0xabb5(%rip)      # 0xc074
jge 0x1383
movl %eax, 0xaba9(%rip)      # 0xc074
jmp 0x1383
cmpl $0x1b, %eax
je 0x1563
cmpl $0x51, %eax
jne 0x1383
jmp 0x1563
movl 0xab8a(%rip), %ecx      # 0xc074
leal 0x1(%rcx), %eax
cmpl 0xab7d(%rip), %eax      # 0xc070
jl 0x14f7
movl %ecx, %eax
movl %eax, 0xab77(%rip)      # 0xc074
subl %r14d, %eax
jl 0x1383
movl 0xab6c(%rip), %ecx      # 0xc078
xorl %edx, %edx
cmpl %eax, %ecx
setle %dl
addl %ecx, %edx
movl %edx, 0xab5d(%rip)      # 0xc078
jmp 0x1383
cmpl $0x71, %eax
jne 0x1383
jmp 0x1563
callq 0x1170
callq 0x158e
callq 0x1040
movq (%rbx), %rdi
jmp 0x1379
movq (%rsi), %rsi
leaq 0xab8(%rip), %rdi       # 0x2004
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x10b0
jmp 0x156f
leaq 0xaca(%rip), %rdi       # 0x2028
callq 0x1060
xorl %ebx, %ebx
jmp 0x156f
movl 0xab07(%rip), %ebx      # 0xc074
incl %ebx
movl %ebx, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq