// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
leaq 0x815(%rip), %rsi       # 0x3076
leaq 0x18(%rsp), %r13
leaq 0x38(%rsp), %rdx
movq %r13, %rdi
callq 0x26d0
movq %r13, %rdi
movq %r12, %rsi
callq 0x22e0
leaq 0x7f9(%rip), %rsi       # 0x307e
leaq 0x18(%rsp), %rdi
callq 0x22d0
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x22e0
movq (%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0x22a0
movl %eax, %ebp
testl %eax, %eax
je 0x28dc
movq 0x273a(%rip), %rdi      # 0x4ff0
leaq 0x7c8(%rip), %rsi       # 0x3085
callq 0x2190
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21e0
jmp 0x2a1d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
pushq $-0x1
popq %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x2170
testl %eax, %eax
je 0x2930
movq 0x26ef(%rip), %rdi      # 0x4ff0
leaq 0x790(%rip), %rsi       # 0x3098
callq 0x2190
movq %rax, %r14
movq 0x10(%rsp), %rdi
callq 0x2180
movq %r14, %rdi
movq %rax, %rsi
callq 0x2190
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21e0
movq 0x8(%rsp), %rdi
callq 0x2220
leaq 0x58(%rsp), %r14
leaq 0x38(%rsp), %r15
leaq 0x7(%rsp), %r12
pushq $0x1
popq %rbp
leaq 0x6(%rsp), %r13
cmpl $0x64, %eax
jne 0x29a9
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x2150
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x26d0
movq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x2150
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x26d0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a74
movq %r15, %rdi
callq 0x277c
movq 0x8(%rsp), %rdi
callq 0x2220
jmp 0x2951
movq 0x8(%rsp), %rdi
callq 0x2270
movl %eax, %ebx
testl %eax, %eax
je 0x29e1
movq 0x2630(%rip), %rdi      # 0x4ff0
leaq 0x6ea(%rip), %rsi       # 0x30b1
callq 0x2190
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21e0
movq 0x10(%rsp), %rdi
callq 0x20c0
movl %eax, %ebp
testl %eax, %eax
je 0x2a1b
movq 0x25f8(%rip), %rdi      # 0x4ff0
leaq 0x6c9(%rip), %rsi       # 0x30c8
callq 0x2190
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21e0
jmp 0x2a1d
xorl %ebp, %ebp
leaq 0x18(%rsp), %rdi
callq 0x20b0
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2a5e
jmp 0x2a5e
jmp 0x2a5e
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x277c
jmp 0x2a61
jmp 0x2a4f
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20b0
jmp 0x2a61
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x22b0
nop