// File: dmarsh19[P]sqlite_ncurses/src/sqlite_query.cc
// Function: sqlite_query(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<Record, std::allocator<Record>>&)
int sqlite_query(const std::string &db_filepath,
  const std::string &table_name,
  const std::string &cols,
  std::vector<Record> &records)
{
  int rc = 0;
  int step;
  std::string sql_statement = "SELECT ";
  sqlite3 *pDb;
  sqlite3_stmt *pStmt;

  sql_statement += cols;
  sql_statement += " FROM ";
  sql_statement += table_name;

  rc = sqlite3_open(db_filepath.c_str(), &pDb);
  if (rc != SQLITE_OK) {
    std::cerr << "sqlite3_open err: " << rc << '\n';
    return rc;
  }

  rc = sqlite3_prepare_v2(pDb, sql_statement.c_str(), -1, &pStmt, 0);
  if (rc != SQLITE_OK)
    std::cerr << "sqlite3_prepare_v2 err: " << sqlite3_errmsg(pDb) << '\n';

  step = sqlite3_step(pStmt);
  while (step == SQLITE_ROW) {
    // The cast changes the type of the return value to match the type needed 
    // by the string constructor. Then construct a string using that pointer. 
    records.push_back({
      std::string(reinterpret_cast<const char *>(sqlite3_column_text(pStmt, 0))),
      std::string(reinterpret_cast<const char *>(sqlite3_column_text(pStmt, 1)))
    });
    step = sqlite3_step(pStmt);
  }

  rc = sqlite3_finalize(pStmt);
  if (rc != SQLITE_OK)
    std::cerr << "sqlite3_finalize err: " << rc << '\n';

  rc = sqlite3_close(pDb);
  if (rc != SQLITE_OK)
     std::cerr << "sqlite3_close err: " << rc << '\n';

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x2390
movq -0xd0(%rbp), %rdx
leaq 0xe76(%rip), %rsi       # 0x4076
leaq -0x50(%rbp), %rdi
callq 0x2af0
jmp 0x320b
leaq -0x51(%rbp), %rdi
callq 0x2240
movq -0x20(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x22c0
jmp 0x3223
leaq 0xe54(%rip), %rsi       # 0x407e
leaq -0x50(%rbp), %rdi
callq 0x21d0
jmp 0x3235
movq -0x18(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x22c0
jmp 0x3244
movq -0x10(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
leaq -0x70(%rbp), %rsi
callq 0x2370
movl %eax, -0xd4(%rbp)
jmp 0x3261
movl -0xd4(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x32f8
movq 0x3d75(%rip), %rdi      # 0x6ff0
leaq 0xe03(%rip), %rsi       # 0x4085
callq 0x21f0
movq %rax, -0xe0(%rbp)
jmp 0x3290
movq -0xe0(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2360
movq %rax, -0xe8(%rbp)
jmp 0x32a8
movq -0xe8(%rbp), %rdi
movl $0xa, %esi
callq 0x2280
jmp 0x32bb
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x7c(%rbp)
jmp 0x3630
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x2240
jmp 0x364e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3645
movq -0x70(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x20b0
movq -0xf8(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %r8d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x78(%rbp), %rcx
callq 0x21c0
movl %eax, -0xec(%rbp)
jmp 0x3331
movl -0xec(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x339f
movq 0x3ca9(%rip), %rdi      # 0x6ff0
leaq 0xd4a(%rip), %rsi       # 0x4098
callq 0x21f0
movq %rax, -0x100(%rbp)
jmp 0x335c
movq -0x70(%rbp), %rdi
callq 0x21e0
movq %rax, -0x108(%rbp)
jmp 0x336e
movq -0x108(%rbp), %rsi
movq -0x100(%rbp), %rdi
callq 0x21f0
movq %rax, -0x110(%rbp)
jmp 0x338a
movq -0x110(%rbp), %rdi
movl $0xa, %esi
callq 0x2280
jmp 0x339d
jmp 0x339f
movq -0x78(%rbp), %rdi
callq 0x22d0
movl %eax, -0x114(%rbp)
jmp 0x33b0
movl -0x114(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $0x64, -0x30(%rbp)
jne 0x3551
movq -0x28(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x21a0
movq %rax, -0x120(%rbp)
jmp 0x33f0
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x2390
movq -0x128(%rbp), %rdi
movq -0x120(%rbp), %rsi
movq -0x138(%rbp), %rdx
callq 0x2af0
jmp 0x341f
movb $0x1, -0xc3(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0x21a0
movq %rax, -0x140(%rbp)
jmp 0x344b
leaq -0xc2(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x2390
movq -0x148(%rbp), %rdi
movq -0x140(%rbp), %rsi
movq -0x150(%rbp), %rdx
callq 0x2af0
jmp 0x347a
movq -0x130(%rbp), %rdi
movb $0x0, -0xc3(%rbp)
leaq -0xc0(%rbp), %rsi
callq 0x3660
jmp 0x3496
leaq -0xc0(%rbp), %rdi
callq 0x2e10
leaq -0xc2(%rbp), %rdi
callq 0x2240
leaq -0xc1(%rbp), %rdi
callq 0x2240
movq -0x78(%rbp), %rdi
callq 0x22d0
movl %eax, -0x154(%rbp)
jmp 0x34cb
movl -0x154(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x33b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3540
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3527
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x351b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x2e10
leaq -0xc2(%rbp), %rdi
callq 0x2240
testb $0x1, -0xc3(%rbp)
jne 0x3532
jmp 0x353e
movq -0x128(%rbp), %rdi
callq 0x2110
jmp 0x3540
leaq -0xc1(%rbp), %rdi
callq 0x2240
jmp 0x3645
movq -0x78(%rbp), %rdi
callq 0x2340
movl %eax, -0x158(%rbp)
jmp 0x3562
movl -0x158(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x35ba
movq 0x3a78(%rip), %rdi      # 0x6ff0
leaq 0xb32(%rip), %rsi       # 0x40b1
callq 0x21f0
movq %rax, -0x160(%rbp)
jmp 0x358d
movq -0x160(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2360
movq %rax, -0x168(%rbp)
jmp 0x35a5
movq -0x168(%rbp), %rdi
movl $0xa, %esi
callq 0x2280
jmp 0x35b8
jmp 0x35ba
movq -0x70(%rbp), %rdi
callq 0x20d0
movl %eax, -0x16c(%rbp)
jmp 0x35cb
movl -0x16c(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3623
movq 0x3a0f(%rip), %rdi      # 0x6ff0
leaq 0xae0(%rip), %rsi       # 0x40c8
callq 0x21f0
movq %rax, -0x178(%rbp)
jmp 0x35f6
movq -0x178(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2360
movq %rax, -0x180(%rbp)
jmp 0x360e
movq -0x180(%rbp), %rdi
movl $0xa, %esi
callq 0x2280
jmp 0x3621
jmp 0x3623
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x7c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x2110
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0x2110
movq -0x60(%rbp), %rdi
callq 0x2380
nopw (%rax,%rax)