// File: /workspace/github_cpp_makefile_star2_O2/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 0x247c
movq 0x2b67(%rip), %rdi      # 0x4fd0
leaq 0xb94(%rip), %rsi       # 0x3004
callq 0x2190
xorl %ebx, %ebx
jmp 0x2674
movq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x8(%rsi), %rsi
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x26d0
movq 0x10(%rbx), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x26d0
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x26d0
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x2840
movl %eax, %ebx
testl %eax, %eax
je 0x2512
movq 0x2b04(%rip), %rdi      # 0x4ff0
leaq 0xb46(%rip), %rsi       # 0x3039
callq 0x2190
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21e0
jmp 0x264c
callq 0x2240
callq 0x2040
callq 0x21d0
movq 0x2aa0(%rip), %r15      # 0x4fc8
movq (%r15), %rdi
pushq $0x1
popq %rsi
callq 0x2080
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
subq %r12, %rdi
sarq $0x6, %rdi
incq %rdi
pushq $0x8
popq %rsi
callq 0x20f0
movq %rax, %rbx
pushq $0x20
popq %r14
xorl %r13d, %r13d
subq %r12, %rbp
sarq $0x6, %rbp
cmpq %r13, %rbp
jbe 0x258d
movq -0x20(%r12,%r14), %rdi
movq (%r12,%r14), %rsi
callq 0x21c0
movq %rax, (%rbx,%r13,8)
incq %r13
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbp
addq $0x40, %r14
jmp 0x255c
andq $0x0, (%rbx,%rbp,8)
movq %rbx, %rdi
callq 0x20e0
movq %rax, %r14
movq %rax, %rdi
callq 0x2230
callq 0x2280
movq (%r15), %rdi
callq 0x2070
cmpl $0x102, %eax            # imm = 0x102
je 0x25cc
cmpl $0x103, %eax            # imm = 0x103
je 0x25d3
cmpl $0x1b, %eax
je 0x25f7
cmpl $0xa, %eax
jne 0x25aa
jmp 0x25e2
movl $0x203, %esi            # imm = 0x203
jmp 0x25d8
movl $0x202, %esi            # imm = 0x202
movq %r14, %rdi
callq 0x2130
jmp 0x25aa
movq %r14, %rdi
callq 0x2260
movq %rax, %rdi
callq 0x22f0
movq %rax, %r15
jmp 0x25f7
movq %r14, %rdi
callq 0x2110
movq %r14, %rdi
callq 0x2050
xorl %r14d, %r14d
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x6, %rax
cmpq %r14, %rax
jbe 0x262b
movq (%rbx,%r14,8), %rdi
callq 0x2200
incq %r14
jmp 0x260a
callq 0x21f0
movq 0x2999(%rip), %rdi      # 0x4fd0
movq %r15, %rsi
callq 0x2190
xorl %ebx, %ebx
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x28(%rsp), %rdi
callq 0x20b0
leaq 0x48(%rsp), %rdi
callq 0x20b0
leaq 0x68(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x270c
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x26aa
movq %rax, %rbx
jmp 0x26b4
movq %rax, %rbx
jmp 0x26be
jmp 0x269d
jmp 0x269d
jmp 0x269d
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20b0
leaq 0x48(%rsp), %rdi
callq 0x20b0
leaq 0x68(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x270c
movq %rbx, %rdi
callq 0x22b0