// File: /workspace/github_cpp_makefile_star2_O3/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 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x750(%rip), %rsi       # 0x3076
leaq 0x750(%rip), %rdx       # 0x307d
leaq 0x18(%rsp), %r13
movq %r13, %rdi
callq 0x2866
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r13, %rdi
callq 0x2050
leaq 0x72c(%rip), %rsi       # 0x307e
leaq 0x18(%rsp), %rdi
callq 0x22c0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
leaq 0x18(%rsp), %rdi
callq 0x2050
movq (%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2290
movl %eax, %ebp
testl %eax, %eax
je 0x29c0
movq 0x2669(%rip), %rdi      # 0x4ff0
leaq 0x6f7(%rip), %rsi       # 0x3085
movl $0x12, %edx
callq 0x21c0
movq 0x2651(%rip), %rdi      # 0x4ff0
movl %ebp, %esi
callq 0x2280
leaq 0x38(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
jmp 0x2ba4
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x2170
testl %eax, %eax
je 0x2a5a
movq 0x2609(%rip), %r14      # 0x4ff0
leaq 0x6aa(%rip), %rsi       # 0x3098
movl $0x18, %edx
movq %r14, %rdi
callq 0x21c0
movq 0x10(%rsp), %rdi
callq 0x2180
movq %rax, %r15
testq %rax, %rax
je 0x2a29
movq %r15, %rdi
callq 0x20b0
movq 0x25d4(%rip), %rdi      # 0x4ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x21c0
jmp 0x2a41
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2260
leaq 0x38(%rsp), %rsi
movb $0xa, (%rsi)
movq 0x25a0(%rip), %rdi      # 0x4ff0
movl $0x1, %edx
callq 0x21c0
movq 0x8(%rsp), %rdi
callq 0x2200
leaq 0x58(%rsp), %r14
leaq 0x68(%rsp), %rbp
leaq 0x38(%rsp), %r15
leaq 0x7(%rsp), %r12
leaq 0x6(%rsp), %r13
cmpl $0x64, %eax
jne 0x2b0a
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x2150
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x2796
movq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x2150
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x2796
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2c66
movq 0x58(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2adf
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2afb
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x8(%rsp), %rdi
callq 0x2200
jmp 0x2a7d
movq 0x8(%rsp), %rdi
callq 0x2250
movl %eax, %ebx
testl %eax, %eax
je 0x2b55
movq 0x24cf(%rip), %rdi      # 0x4ff0
leaq 0x589(%rip), %rsi       # 0x30b1
movl $0x16, %edx
callq 0x21c0
movq 0x24b7(%rip), %rdi      # 0x4ff0
movl %ebx, %esi
callq 0x2280
leaq 0x38(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
movq 0x10(%rsp), %rdi
callq 0x20c0
movl %eax, %ebp
testl %eax, %eax
je 0x2ba2
movq 0x2484(%rip), %rdi      # 0x4ff0
leaq 0x555(%rip), %rsi       # 0x30c8
movl $0x13, %edx
callq 0x21c0
movq 0x246c(%rip), %rdi      # 0x4ff0
movl %ebp, %esi
callq 0x2280
leaq 0x38(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
jmp 0x2ba4
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2bc0
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c09
jmp 0x2c09
jmp 0x2c09
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x2c30
jmp 0x2c0c
jmp 0x2be8
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2c0c
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2c0c
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2c28
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22a0