// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/database.cpp
// Function: ash::Database::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int) const
ResultSet * Database::exec(const string & query, const int limit) const {
  // Load the relevant configured values.
  Config & config = Config::instance();

  int max_retries = config.get_int("DB_MAX_RETRIES", -1);
  if (max_retries <= 0) max_retries = 5;
  int tries = max_retries + 1, fetched = 0;

  ResultSet::HeadersType headers;
  ResultSet::DataType results;
  stringstream ss;
  sqlite3_stmt * ps = prepare_stmt(query);
  unsigned int rows, columns = sqlite3_column_count(ps);

  // YES, this is a GOTO target.  This is used to implement the retry logic.
  // If a query fails because of a lock, it may goto this block to retry.
try_sql:
  fetched = 0;
  for (rows = 0; fetched < limit || limit <= 0; ++rows) {
    int result = sqlite3_step(ps);
    switch (result) {
      // TODO(cpa): add more cases to handle errors.
      case SQLITE_ROW:
        // build the list of header names, if this is the first row fetched.
        if (headers.empty()) {
          for (size_t c = 0; c < columns; ++c) {
            ss.str("");
            ss << sqlite3_column_name(ps, c);
            headers.push_back(ss.str());
          }
        }
        // Add the row data.
        results.push_back(ResultSet::RowType());
        for (size_t c = 0; c < columns; ++c) {
          ss.str("");
          if (sqlite3_column_text(ps, c)) {
            ss << sqlite3_column_text(ps, c);
          }
          results.back().push_back(ss.str());
        }
        ++fetched;
        continue;  // for loop
      case SQLITE_CONSTRAINT:
        // Note: there is no point retrying this type of error.
        LOG(DEBUG) << "constraint violation executing: '" << query << "'";
        goto finalize;
      case SQLITE_DONE:
        goto finalize;
      case SQLITE_LOCKED:  // Fallthrough.
      case SQLITE_BUSY: {
        // Abort if we are out of attempts.
        if (tries <= 0) {
          LOG(FATAL) << "Failed to unlock db: " << sqlite3_errmsg(db)
                     << "\nGave up after " << max_retries << " failures."
                     << "\nExecuting: '" << query << "'\n";
        }

        // Sleep some random number of milliseconds.
        ash_sleep();

        // Reset the prepared statement.
        sqlite3_finalize(ps);
        ps = prepare_stmt(query);

        // Decrement the try count and jump.
        --tries;
        LOG(WARNING) << "Database was locked, tries remaining: " << tries;
        goto try_sql;
      }
      default:
        sqlite3_finalize(ps);
        // TODO(cpa): remove this cerr line once the FATAL errors are redirected to stderr by default.
        cerr << "unknown sqlite3_step code: " << result << " executing '"
             << query << "'\nError:\n" << sqlite3_errmsg(db) << endl;
        LOG(FATAL) << "unknown sqlite3_step code: " << result
                   << " executing '" << query << "'\nError:\n"
                   << sqlite3_errmsg(db);
    }  // switch
  }  // for loop

  // Yes, another GOTO target.  This is used because there is a switch statement
  // within a for loop and it's convenient to break the loop within the switch.
finalize:
  sqlite3_finalize(ps);

  return rows ? new ResultSet(headers, results) : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x518, %rsp            # imm = 0x518
movl %edx, 0x2c(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x69ef2
movq %rax, %r14
leaq 0x80(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x132de(%rip), %rsi     # 0x7daa8
leaq 0x132e5(%rip), %rdx     # 0x7dab6
leaq 0x70(%rsp), %rdi
callq 0x68de4
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69e16
movl %eax, %ebp
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6a7fe
callq 0x8390
testl %ebp, %ebp
movl $0x5, %eax
cmovgl %ebp, %eax
movq %rax, 0x30(%rsp)
leaq 0x58(%rsp), %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
movq %rax, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
leaq 0x390(%rsp), %rdi
callq 0x8290
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x6bb02
movq %rax, %rbp
movq %rax, %rdi
callq 0x104a3
movl %eax, %ecx
movq 0x30(%rsp), %rax
incl %eax
movl %eax, (%rsp)
movl %ecx, 0x1c(%rsp)
movl %ecx, %eax
movq %rax, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x70(%rsp), %r14
movl $0x0, 0x4(%rsp)
movq %rbp, %rdi
callq 0xf4d2
movl %eax, %r12d
movl $0xd, %r15d
cmpl $0x63, %eax
jg 0x6a904
leal -0x5(%r12), %eax
cmpl $0x2, %eax
jae 0x6a919
cmpl $0x0, (%rsp)
jg 0x6ade1
movq %r14, %rdi
movl $0x4, %esi
callq 0x70186
movl $0x15, %edx
leaq 0x78(%rsp), %rdi
leaq 0x1352f(%rip), %rsi     # 0x7ddf7
callq 0x8470
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x12bfd
movq %rax, %r12
testq %rax, %rax
je 0x6ad40
movq %r12, %rdi
callq 0x8190
leaq 0x78(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6ad61
cmpl $0x64, %r12d
je 0x6a97a
cmpl $0x65, %r12d
je 0x6ae44
jmp 0x6ab93
cmpl $0x13, %r12d
jne 0x6ab93
movq %r14, %rdi
xorl %esi, %esi
callq 0x70186
movl $0x21, %edx
leaq 0x78(%rsp), %rdi
leaq 0x13497(%rip), %rsi     # 0x7ddd5
callq 0x8470
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
leaq 0x78(%rsp), %rdi
callq 0x8470
movl $0x1, %edx
movq %rax, %rdi
leaq 0x139db(%rip), %rsi     # 0x7e343
callq 0x8470
movq %r14, %rdi
callq 0x70626
jmp 0x6ae44
cmpl $0x0, 0x1c(%rsp)
sete %al
leaq 0x58(%rsp), %r15
cmpq %r15, 0x58(%rsp)
setne %cl
orb %al, %cl
jne 0x6aa6f
xorl %r12d, %r12d
movq %rbx, 0x70(%rsp)
movq %r14, %rdi
leaq 0x139b7(%rip), %rdx     # 0x7e360
movq %rdx, %rsi
callq 0x68de4
leaq 0x3a8(%rsp), %rdi
movq %r14, %rsi
callq 0x85c0
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6a9d0
callq 0x8390
movq %rbp, %rdi
movl %r12d, %esi
callq 0x10790
movq %rax, %r13
testq %rax, %rax
je 0x6aa00
movq %r13, %rdi
callq 0x8190
leaq 0x3a0(%rsp), %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6aa27
movq 0x3a0(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x3a0(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0x3c0(%rsp,%rax), %esi
orl $0x1, %esi
callq 0x86b0
movq %r14, %rdi
leaq 0x3a8(%rsp), %rsi
callq 0x8630
movq %r15, %rdi
movq %r14, %rsi
callq 0x6c3c0
movq %rax, %rdi
movq %r15, %rsi
callq 0x8070
incq 0x68(%rsp)
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6aa61
callq 0x8390
incq %r12
cmpq %r12, 0x38(%rsp)
jne 0x6a99a
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x6c128
movq %r14, %rdi
callq 0x68d64
cmpl $0x0, 0x1c(%rsp)
je 0x6ab84
xorl %r12d, %r12d
movq %rbx, 0x70(%rsp)
movq %r14, %rdi
leaq 0x138ab(%rip), %rdx     # 0x7e360
movq %rdx, %rsi
callq 0x68de4
leaq 0x3a8(%rsp), %rdi
movq %r14, %rsi
callq 0x85c0
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6aadc
callq 0x8390
movq %rbp, %rdi
movl %r12d, %esi
callq 0x10690
testq %rax, %rax
je 0x6ab43
movq %rbp, %rdi
movl %r12d, %esi
callq 0x10690
movq %rax, %r13
testq %rax, %rax
je 0x6ab1c
movq %r13, %rdi
callq 0x8190
leaq 0x3a0(%rsp), %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6ab43
movq 0x3a0(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x3a0(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0x3c0(%rsp,%rax), %esi
orl $0x1, %esi
callq 0x86b0
movq 0x48(%rsp), %r15
movq %r14, %rdi
leaq 0x3a8(%rsp), %rsi
callq 0x8630
addq $-0x18, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x6c154
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6ab76
callq 0x8390
incq %r12
cmpq %r12, 0x38(%rsp)
jne 0x6aaa6
incl 0x4(%rsp)
movl $0x5, %r15d
jmp 0x6ae44
movq %rbp, %rdi
callq 0xe974
movl $0x1b, %edx
movq 0x29441(%rip), %rdi     # 0x93fe8
leaq 0x132a7(%rip), %rsi     # 0x7de55
callq 0x8470
movq 0x2942e(%rip), %rdi     # 0x93fe8
movl %r12d, %esi
callq 0x86d0
movq %rax, %r13
movl $0xc, %edx
movq %rax, %rdi
leaq 0x1329d(%rip), %rsi     # 0x7de71
callq 0x8470
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r13, %rdi
callq 0x8470
movq %rax, %r13
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1327f(%rip), %rsi     # 0x7de7e
callq 0x8470
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x12bfd
movq %rax, %r15
testq %rax, %rax
je 0x6ac32
movq %r15, %rdi
callq 0x8190
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6ac4d
movq (%r13), %rax
movq -0x18(%rax), %rax
movq %r13, %rdi
addq %rax, %rdi
movl 0x20(%r13,%rax), %esi
orl $0x1, %esi
callq 0x86b0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x83c0
movsbl %al, %esi
movq %r13, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8250
movq %r14, %rdi
movl $0x4, %esi
callq 0x70186
movl $0x1b, %edx
leaq 0x78(%rsp), %rdi
leaq 0x131c2(%rip), %rsi     # 0x7de55
callq 0x8470
leaq 0x78(%rsp), %rdi
movl %r12d, %esi
callq 0x86d0
movq %rax, %r12
movl $0xc, %edx
movq %rax, %rdi
leaq 0x131ba(%rip), %rsi     # 0x7de71
callq 0x8470
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r12, %rdi
callq 0x8470
movq %rax, %r12
movl $0x9, %edx
movq %rax, %rdi
leaq 0x1319c(%rip), %rsi     # 0x7de7e
callq 0x8470
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x12bfd
movq %rax, %r15
testq %rax, %rax
je 0x6ad15
movq %r15, %rdi
callq 0x8190
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6ad30
movq (%r12), %rax
movq -0x18(%rax), %rax
movq %r12, %rdi
addq %rax, %rdi
movl 0x20(%r12,%rax), %esi
orl $0x1, %esi
callq 0x86b0
movq %r14, %rdi
callq 0x70626
xorl %r15d, %r15d
jmp 0x6ae44
movq 0x78(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x78(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0x98(%rsp,%rax), %esi
orl $0x1, %esi
callq 0x86b0
movl $0xf, %edx
leaq 0x78(%rsp), %rdi
leaq 0x1309b(%rip), %rsi     # 0x7de0d
callq 0x8470
leaq 0x78(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x86d0
movq %rax, %r12
movl $0xa, %edx
movq %rax, %rdi
leaq 0x12f1e(%rip), %rsi     # 0x7dcb6
callq 0x8470
movl $0xd, %edx
movq %r12, %rdi
leaq 0x13071(%rip), %rsi     # 0x7de1d
callq 0x8470
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r12, %rdi
callq 0x8470
movl $0x2, %edx
movq %rax, %rdi
leaq 0x13057(%rip), %rsi     # 0x7de2b
callq 0x8470
movq %r14, %rdi
callq 0x70626
callq 0x6b2a4
movq %rbp, %rdi
callq 0xe974
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x6bb02
movq %rax, %rbp
movq %r14, %rdi
movl $0x2, %esi
callq 0x70186
movl $0x26, %edx
leaq 0x78(%rsp), %r15
movq %r15, %rdi
leaq 0x1300d(%rip), %rsi     # 0x7de2e
callq 0x8470
movl (%rsp), %esi
decl %esi
movq %r15, %rdi
movl %esi, (%rsp)
callq 0x86d0
movq %r14, %rdi
callq 0x70626
movl $0x2, %r15d
cmpl $0x4, %r15d
jg 0x6ae6d
testl %r15d, %r15d
je 0x6ae73
movl $0x0, %eax
movq %rax, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x2, %r15d
je 0x6a880
jmp 0x6aed4
cmpl $0x5, %r15d
jne 0x6ae99
movl 0x2c(%rsp), %ecx
testl %ecx, %ecx
setle %al
movq 0x20(%rsp), %rdx
incl %edx
movq %rdx, 0x20(%rsp)
cmpl %ecx, 0x4(%rsp)
setl %cl
orb %al, %cl
jne 0x6a880
jmp 0x6ae9f
cmpl $0xd, %r15d
jne 0x6aed4
movq %rbp, %rdi
callq 0xe974
cmpl $0x0, 0x20(%rsp)
je 0x6aecf
movl $0x40, %edi
callq 0x83f0
movq %rax, %r14
leaq 0x58(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %rax, %rdi
callq 0x6a138
jmp 0x6aed4
xorl %r14d, %r14d
jmp 0x6aed4
leaq 0x390(%rsp), %rdi
callq 0x87e8
leaq 0x40(%rsp), %rdi
callq 0x68d12
leaq 0x58(%rsp), %rdi
callq 0x68da8
movq %r14, %rax
addq $0x518, %rsp            # imm = 0x518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movq %r14, %rdi
jmp 0x6afbf
movq %rax, %r15
jmp 0x6afe4
jmp 0x6afca
movq %rax, %r15
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6afee
callq 0x8390
jmp 0x6afee
movq %rax, %r15
jmp 0x6afee
movq %rax, %r15
jmp 0x6afda
jmp 0x6afca
jmp 0x6afca
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x68d64
jmp 0x6afcd
jmp 0x6afca
jmp 0x6afca
jmp 0x6afca
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x70626
jmp 0x6afcd
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x70626
jmp 0x6afcd
jmp 0x6afca
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x70626
jmp 0x6afcd
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x70626
jmp 0x6afcd
jmp 0x6afb2
jmp 0x6afca
jmp 0x6afb2
jmp 0x6afca
jmp 0x6afca
jmp 0x6afb2
jmp 0x6afca
movq %rax, %r15
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6afcd
callq 0x8390
jmp 0x6afcd
jmp 0x6afca
jmp 0x6afca
movq %rax, %r15
leaq 0x390(%rsp), %rdi
callq 0x87e8
leaq 0x40(%rsp), %rdi
callq 0x68d12
leaq 0x58(%rsp), %rdi
callq 0x68da8
movq %r15, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68c53