// File: /workspace/github_cpp_makefile_star2_O1/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 0x734(%rip), %rsi       # 0x3076
leaq 0x734(%rip), %rdx       # 0x307d
leaq 0x18(%rsp), %r13
movq %r13, %rdi
callq 0x2888
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r13, %rdi
callq 0x2050
leaq 0x710(%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 0x29dc
movq 0x264d(%rip), %rdi      # 0x4ff0
leaq 0x6db(%rip), %rsi       # 0x3085
movl $0x12, %edx
callq 0x21c0
movq 0x2635(%rip), %rdi      # 0x4ff0
movl %ebp, %esi
callq 0x2280
leaq 0x38(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
jmp 0x2bbc
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 0x2a76
movq 0x25ed(%rip), %r14      # 0x4ff0
leaq 0x68e(%rip), %rsi       # 0x3098
movl $0x18, %edx
movq %r14, %rdi
callq 0x21c0
movq 0x10(%rsp), %rdi
callq 0x2180
movq %rax, %r15
testq %rax, %rax
je 0x2a45
movq %r15, %rdi
callq 0x20b0
movq 0x25b8(%rip), %rdi      # 0x4ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x21c0
jmp 0x2a5d
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 0x2584(%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 0x2b26
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x2150
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x27b8
movq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x2150
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x27b8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2c86
movq 0x58(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2afb
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2b17
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
movq 0x8(%rsp), %rdi
callq 0x2200
jmp 0x2a99
movq 0x8(%rsp), %rdi
callq 0x2250
movl %eax, %ebx
testl %eax, %eax
je 0x2b71
movq 0x24b3(%rip), %rdi      # 0x4ff0
leaq 0x56d(%rip), %rsi       # 0x30b1
movl $0x16, %edx
callq 0x21c0
movq 0x249b(%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 0x2bbc
movq 0x2468(%rip), %rdi      # 0x4ff0
leaq 0x539(%rip), %rsi       # 0x30c8
movl $0x13, %edx
callq 0x21c0
movq 0x2450(%rip), %rdi      # 0x4ff0
movl %ebp, %esi
callq 0x2280
leaq 0x38(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x21c0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2bd8
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 0x2c29
jmp 0x2c29
jmp 0x2c29
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x2c50
xorl %eax, %eax
jmp 0x2c07
jmp 0x2c02
movq %rax, %rbx
movb $0x1, %al
testb %al, %al
je 0x2c2c
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x2c2c
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x2c2c
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x2c48
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22a0