// File: 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
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x258e
movq 0x4a5a(%rip), %rdi      # 0x6fd0
leaq 0x1a87(%rip), %rsi      # 0x4004
callq 0x21f0
movl $0x0, -0x4(%rbp)
jmp 0x2ab8
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2ad0
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xf0(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x2390
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x2af0
jmp 0x25f4
leaq -0x79(%rbp), %rdi
callq 0x2240
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x100(%rbp)
leaq -0xb1(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x2390
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
callq 0x2af0
jmp 0x263b
leaq -0xb1(%rbp), %rdi
callq 0x2240
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x110(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x2390
movq -0x110(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0xd8(%rbp), %rdi
callq 0x2af0
jmp 0x2685
leaq -0xd9(%rbp), %rdi
callq 0x2240
leaq -0x78(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
leaq -0x38(%rbp), %rcx
callq 0x31c0
movl %eax, -0x114(%rbp)
jmp 0x26b4
movl -0x114(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x27ac
movq 0x4922(%rip), %rdi      # 0x6ff0
leaq 0x1964(%rip), %rsi      # 0x4039
callq 0x21f0
movq %rax, -0x120(%rbp)
jmp 0x26e3
movq -0x120(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x2360
movq %rax, -0x128(%rbp)
jmp 0x26fb
movq -0x128(%rbp), %rdi
movl $0xa, %esi
callq 0x2280
jmp 0x270e
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xe0(%rbp)
jmp 0x2a6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x2240
jmp 0x2aad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xb1(%rbp), %rdi
callq 0x2240
jmp 0x2aa4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x2240
jmp 0x2a98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2110
jmp 0x2a98
callq 0x2300
jmp 0x27b3
callq 0x2050
jmp 0x27ba
callq 0x2270
jmp 0x27c1
movq 0x4800(%rip), %rax      # 0x6fc8
movq (%rax), %rdi
movl $0x1, %esi
callq 0x2090
jmp 0x27d7
leaq -0x38(%rbp), %rdi
callq 0x2ba0
movq %rax, %rdi
addq $0x1, %rdi
movl $0x8, %esi
callq 0x2120
movq %rax, -0x48(%rbp)
movl $0x0, -0x18(%rbp)
movslq -0x18(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2ba0
movq %rax, %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
jae 0x2897
movslq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x2bc0
movq %rax, %rdi
callq 0x20b0
movq -0x148(%rbp), %rdi
movq %rax, -0x140(%rbp)
movslq -0x18(%rbp), %rsi
callq 0x2bc0
movq %rax, %rdi
addq $0x20, %rdi
callq 0x20b0
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x2220
movq %rax, -0x138(%rbp)
jmp 0x2876
movq -0x138(%rbp), %rdx
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x27fc
movq -0x48(%rbp), %rax
movq %rax, -0x158(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2ba0
movq %rax, %rcx
movq -0x158(%rbp), %rax
movq $0x0, (%rax,%rcx,8)
movq -0x48(%rbp), %rdi
callq 0x20f0
movq %rax, -0x150(%rbp)
jmp 0x28cf
movq -0x150(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x22e0
jmp 0x28e5
callq 0x2350
jmp 0x28ec
jmp 0x28ee
jmp 0x28f0
movq 0x46d1(%rip), %rax      # 0x6fc8
movq (%rax), %rdi
callq 0x2080
movl %eax, -0x15c(%rbp)
jmp 0x2907
movl -0x15c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x160(%rbp)
subl $0xa, %eax
je 0x2954
jmp 0x2920
movl -0x160(%rbp), %eax
subl $0x1b, %eax
je 0x294b
jmp 0x292d
movl -0x160(%rbp), %eax
subl $0x102, %eax            # imm = 0x102
je 0x2997
jmp 0x293c
movl -0x160(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x29a9
jmp 0x29bb
movl $0x1, -0x1c(%rbp)
jmp 0x29bb
movq -0x40(%rbp), %rdi
callq 0x2320
movq %rax, -0x168(%rbp)
jmp 0x2966
movq -0x168(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x23b0
movq %rax, -0x170(%rbp)
jmp 0x2983
movq -0x170(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x1, -0x1c(%rbp)
jmp 0x29bb
movq -0x40(%rbp), %rdi
movl $0x203, %esi            # imm = 0x203
callq 0x2170
jmp 0x29a7
jmp 0x29bb
movq -0x40(%rbp), %rdi
movl $0x202, %esi            # imm = 0x202
callq 0x2170
jmp 0x29b9
jmp 0x29bb
cmpl $0x0, -0x1c(%rbp)
je 0x29c3
jmp 0x29c8
jmp 0x28ee
movq -0x40(%rbp), %rdi
callq 0x2150
jmp 0x29d3
movq -0x40(%rbp), %rdi
callq 0x2060
jmp 0x29de
movl $0x0, -0x18(%rbp)
movslq -0x18(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x2ba0
movq %rax, %rcx
movq -0x178(%rbp), %rax
cmpq %rcx, %rax
jae 0x2a28
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x22a0
jmp 0x2a1b
jmp 0x2a1d
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x29e5
callq 0x2290
jmp 0x2a2f
movq -0x58(%rbp), %rsi
movq 0x4596(%rip), %rdi      # 0x6fd0
callq 0x21f0
movq %rax, -0x180(%rbp)
jmp 0x2a48
movq -0x180(%rbp), %rdi
movl $0xa, %esi
callq 0x2280
jmp 0x2a5b
movl $0x0, -0x4(%rbp)
movl $0x1, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2110
leaq -0xb0(%rbp), %rdi
callq 0x2110
leaq -0x78(%rbp), %rdi
callq 0x2110
leaq -0x38(%rbp), %rdi
callq 0x2be0
jmp 0x2ab8
leaq -0xb0(%rbp), %rdi
callq 0x2110
leaq -0x78(%rbp), %rdi
callq 0x2110
leaq -0x38(%rbp), %rdi
callq 0x2be0
jmp 0x2ac4
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x2380