// File: /workspace/github_cpp_makefile_star2_O3/dmarsh19[P]sqlite_ncurses/src/main.cc
// Function: main
int main(int argc, char *argv[])
{
  if (argc < 2) {
    std::cout << "Usage: ./run [db_filepath] [table_name] [col1,col2]\n";
    return 0;
  }

  int rc = 0;
  int i = 0;
  int exit_flag = 0;
  int input_key;

  std::vector<Record> records;

  MENU *pCursesMenu = NULL;
  ITEM **ppCursesItems;
  ITEM *pCurrItem = NULL;
  const char *currItemDescription;

  const std::string db_filename = argv[1];
  const std::string table_name = argv[2];
  const std::string cols = argv[3];

  rc = sqlite_query(db_filename, table_name, cols, records);
  if (rc != 0) {
    std::cerr << "sqlite_query err: " << rc << '\n';
    return rc;
  }

  // initialize curses
  initscr();
  cbreak();
  noecho();
  keypad(stdscr, TRUE);

  ppCursesItems = (ITEM **)calloc(records.size() + 1, sizeof(ITEM *));

  for (i = 0; i < records.size(); i++)
    ppCursesItems[i] = new_item(records[i].key.c_str(), records[i].description.c_str());
  ppCursesItems[records.size()] = NULL;

  pCursesMenu = new_menu(ppCursesItems);
  post_menu(pCursesMenu);
  refresh();

  while (1) {
    input_key = getch();
    switch(input_key) {
      case 033: // ESCAPE
        exit_flag = 1;
        break;
      case 10: // ENTER
        pCurrItem = current_item(pCursesMenu);
        currItemDescription = item_description(pCurrItem);
        exit_flag = 1;
        break;
      case KEY_DOWN:
        menu_driver(pCursesMenu, REQ_DOWN_ITEM);
        break;
      case KEY_UP:
        menu_driver(pCursesMenu, REQ_UP_ITEM);
        break;
    }
    if (exit_flag != 0) {
      break;
    }
  }

  unpost_menu(pCursesMenu);
  free_menu(pCursesMenu);
  for (i = 0; i < records.size(); i++)
    free_item(ppCursesItems[i]);
  endwin();

  std::cout << currItemDescription << '\n';

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
cmpl $0x1, %edi
jg 0x2471
movq 0x2b77(%rip), %rdi      # 0x4fd0
leaq 0xba4(%rip), %rsi       # 0x3004
movl $0x34, %edx
callq 0x21c0
xorl %ebx, %ebx
jmp 0x2707
movq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x8(%rsi), %rsi
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x2796
movq 0x10(%rbx), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x2796
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x2796
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x28fc
movl %eax, %ebx
testl %eax, %eax
je 0x251d
movq 0x2b0c(%rip), %rdi      # 0x4ff0
leaq 0xb4e(%rip), %rsi       # 0x3039
movl $0x12, %edx
callq 0x21c0
movq 0x2af4(%rip), %rdi      # 0x4ff0
movl %ebx, %esi
callq 0x2280
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
jmp 0x26ac
callq 0x2220
callq 0x2040
callq 0x21d0
movq 0x2a95(%rip), %r15      # 0x4fc8
movq (%r15), %rdi
movl $0x1, %esi
callq 0x2090
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r12
movq %r12, %rbp
subq %r14, %rbp
sarq $0x6, %rbp
leaq 0x1(%rbp), %rdi
movl $0x8, %esi
callq 0x20f0
movq %rax, %rbx
cmpq %r14, %r12
je 0x25a2
movl $0x20, %r12d
xorl %r13d, %r13d
movq -0x20(%r14,%r12), %rdi
movq (%r14,%r12), %rsi
callq 0x21b0
movq %rax, (%rbx,%r13,8)
incq %r13
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rbp
subq %r14, %rbp
sarq $0x6, %rbp
addq $0x40, %r12
cmpq %r13, %rbp
ja 0x2573
movq $0x0, (%rbx,%rbp,8)
movq %rbx, %rdi
callq 0x20e0
movq %rax, %r14
movq %rax, %rdi
callq 0x2210
callq 0x2270
movq (%r15), %rdi
callq 0x2080
cmpl $0x102, %eax            # imm = 0x102
jg 0x25e4
movl $0x203, %esi            # imm = 0x203
je 0x25f0
cmpl $0xa, %eax
je 0x25fc
cmpl $0x1b, %eax
jne 0x25c2
jmp 0x25fa
cmpl $0x103, %eax            # imm = 0x103
jne 0x25c2
movl $0x202, %esi            # imm = 0x202
movq %r14, %rdi
callq 0x2130
jmp 0x25c2
jmp 0x260f
movq %r14, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x22d0
movq %rax, %r15
movq %r14, %rdi
callq 0x2110
movq %r14, %rdi
callq 0x2060
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x264d
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
callq 0x21f0
incq %r14
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x6, %rax
cmpq %r14, %rax
ja 0x262e
callq 0x21e0
testq %r15, %r15
je 0x2673
movq %r15, %rdi
callq 0x20b0
movq 0x296a(%rip), %rdi      # 0x4fd0
movq %r15, %rsi
movq %rax, %rdx
callq 0x21c0
jmp 0x2691
movq 0x2956(%rip), %rax      # 0x4fd0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x2260
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movq 0x2930(%rip), %rdi      # 0x4fd0
movl $0x1, %edx
callq 0x21c0
xorl %ebx, %ebx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26c7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26e2
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26fd
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x10(%rsp), %rdi
callq 0x27d4
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x274e
movq %rax, %rbx
jmp 0x2769
movq %rax, %rbx
jmp 0x2784
jmp 0x2730
jmp 0x2730
jmp 0x2730
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x274e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2769
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2784
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x10(%rsp), %rdi
callq 0x27d4
movq %rbx, %rdi
callq 0x22a0