// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x90, %rsp
cmpl $0x1, %edi
jg 0x246f
movq 0x2b79(%rip), %rdi      # 0x4fd0
leaq 0xba6(%rip), %rsi       # 0x3004
movl $0x34, %edx
callq 0x21c0
xorl %ebx, %ebx
jmp 0x2725
movq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x8(%rsi), %rsi
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x27b8
movq 0x10(%rbx), %rsi
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x27b8
movq 0x18(%rbx), %rsi
leaq 0x30(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x27b8
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0x2918
movl %eax, %ebx
testl %eax, %eax
je 0x251b
movq 0x2b0e(%rip), %rdi      # 0x4ff0
leaq 0xb50(%rip), %rsi       # 0x3039
movl $0x12, %edx
callq 0x21c0
movq 0x2af6(%rip), %rdi      # 0x4ff0
movl %ebx, %esi
callq 0x2280
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
jmp 0x26c4
callq 0x2220
callq 0x2040
callq 0x21d0
movq 0x2a97(%rip), %r12      # 0x4fc8
movq (%r12), %rdi
movl $0x1, %esi
callq 0x2090
movq 0x18(%rsp), %rdi
subq 0x10(%rsp), %rdi
sarq $0x6, %rdi
incq %rdi
movl $0x8, %esi
callq 0x20f0
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
je 0x25a6
movl $0x20, %r14d
xorl %r15d, %r15d
movq -0x20(%rax,%r14), %rdi
movq (%rax,%r14), %rsi
callq 0x21b0
movq %rax, (%rbx,%r15,8)
incq %r15
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x6, %rcx
addq $0x40, %r14
cmpq %r15, %rcx
ja 0x2575
jmp 0x25aa
sarq $0x6, %rcx
movq $0x0, (%rbx,%rcx,8)
movq %rbx, %rdi
callq 0x20e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2210
callq 0x2270
movq (%r12), %rdi
callq 0x2080
movb $0x1, %bpl
cmpl $0x101, %eax            # imm = 0x101
jg 0x25e9
cmpl $0xa, %eax
je 0x25fe
cmpl $0x1b, %eax
je 0x2611
jmp 0x2622
cmpl $0x102, %eax            # imm = 0x102
je 0x2615
cmpl $0x103, %eax            # imm = 0x103
jne 0x2622
movl $0x202, %esi            # imm = 0x202
jmp 0x261a
movq %r15, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x22d0
movq %rax, %r14
xorl %ebp, %ebp
jmp 0x2622
movl $0x203, %esi            # imm = 0x203
movq %r15, %rdi
callq 0x2130
testb %bpl, %bpl
jne 0x25ca
movq %r15, %rdi
callq 0x2110
movq %r15, %rdi
callq 0x2060
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x2665
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdi
callq 0x21f0
incq %r15
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x6, %rax
cmpq %r15, %rax
ja 0x2646
callq 0x21e0
testq %r14, %r14
je 0x268b
movq %r14, %rdi
callq 0x20b0
movq 0x2952(%rip), %rdi      # 0x4fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21c0
jmp 0x26a9
movq 0x293e(%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 0x2918(%rip), %rdi      # 0x4fd0
movl $0x1, %edx
callq 0x21c0
xorl %ebx, %ebx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26df
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26fa
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x271b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x10(%rsp), %rdi
callq 0x27f6
movl %ebx, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x276a
movq %rax, %rbx
jmp 0x2785
movq %rax, %rbx
jmp 0x27a6
jmp 0x274c
jmp 0x274c
jmp 0x274c
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x276a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2785
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x27a6
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x10(%rsp), %rdi
callq 0x27f6
movq %rbx, %rdi
callq 0x22a0