// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x10c8, %rsp           # imm = 0x10C8
movq %rdi, 0x10c0(%rsp)
movq %rsi, 0x10b8(%rsp)
movl %edx, 0x10b4(%rsp)
movq 0x10c0(%rsp), %rax
movq %rax, 0x150(%rsp)
callq 0x90ab0
movq %rax, 0x10a8(%rsp)
movq 0x10a8(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x107f(%rsp), %rdi
movq %rdi, 0x160(%rsp)
callq 0x87f0
movq 0x160(%rsp), %rdx
leaq 0x209ae(%rip), %rsi     # 0xb256d
leaq 0x1080(%rsp), %rdi
callq 0x85b0
jmp 0x91bce
movq 0x158(%rsp), %rdi
leaq 0x1080(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x90840
movl %eax, 0x14c(%rsp)
jmp 0x91bf1
leaq 0x1080(%rsp), %rdi
callq 0x88f8
jmp 0x91c00
leaq 0x107f(%rsp), %rdi
callq 0x84a0
movl 0x14c(%rsp), %eax
movl %eax, 0x10a4(%rsp)
cmpl $0x0, 0x10a4(%rsp)
jg 0x91c7f
movl $0x5, 0x10a4(%rsp)
jmp 0x91c7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
jmp 0x91c6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0x1080(%rsp), %rdi
callq 0x88f8
jmp 0x91c6b
jmp 0x91c6d
leaq 0x107f(%rsp), %rdi
callq 0x84a0
jmp 0x92819
movl 0x10a4(%rsp), %eax
incl %eax
movl %eax, 0x1068(%rsp)
movl $0x0, 0x1064(%rsp)
leaq 0x1048(%rsp), %rdi
callq 0x943b0
leaq 0x1030(%rsp), %rdi
callq 0x94660
jmp 0x91cb6
leaq 0xea8(%rsp), %rdi
callq 0x82a0
jmp 0x91cc5
movq 0x150(%rsp), %rdi
movq 0x10b8(%rsp), %rsi
callq 0x93a00
movq %rax, 0x140(%rsp)
jmp 0x91ce4
movq 0x140(%rsp), %rax
movq %rax, 0xea0(%rsp)
movq 0xea0(%rsp), %rdi
callq 0x11d00
movl %eax, 0x13c(%rsp)
jmp 0x91d0a
movl 0x13c(%rsp), %eax
movl %eax, 0xe98(%rsp)
movl $0x0, 0x1064(%rsp)
movl $0x0, 0xe9c(%rsp)
movl 0x1064(%rsp), %ecx
movb $0x1, %al
cmpl 0x10b4(%rsp), %ecx
movb %al, 0x13b(%rsp)
jl 0x91d59
cmpl $0x0, 0x10b4(%rsp)
setle %al
movb %al, 0x13b(%rsp)
movb 0x13b(%rsp), %al
testb $0x1, %al
jne 0x91d69
jmp 0x926fe
movq 0xea0(%rsp), %rdi
callq 0x11160
movl %eax, 0x134(%rsp)
jmp 0x91d7f
movl 0x134(%rsp), %eax
movl %eax, 0xe94(%rsp)
movl 0xe94(%rsp), %eax
movl %eax, 0x130(%rsp)
addl $-0x5, %eax
subl $0x2, %eax
jb 0x922ea
jmp 0x91da9
movl 0x130(%rsp), %eax
subl $0x13, %eax
je 0x92245
jmp 0x91dbb
movl 0x130(%rsp), %eax
subl $0x64, %eax
je 0x91e29
jmp 0x91dc9
movl 0x130(%rsp), %eax
subl $0x65, %eax
je 0x922e5
jmp 0x92523
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
jmp 0x92808
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
jmp 0x927f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
jmp 0x927cc
leaq 0x1048(%rsp), %rdi
callq 0x94670
movb %al, 0x12f(%rsp)
jmp 0x91e3f
movb 0x12f(%rsp), %al
testb $0x1, %al
jne 0x91e4f
jmp 0x91fed
movq $0x0, 0xe88(%rsp)
movq 0xe88(%rsp), %rax
movl 0xe98(%rsp), %ecx
cmpq %rcx, %rax
jae 0x91feb
leaq 0xe67(%rsp), %rdi
movq %rdi, 0x120(%rsp)
callq 0x87f0
movq 0x120(%rsp), %rdx
leaq 0x20f5e(%rip), %rsi     # 0xb2df5
leaq 0xe68(%rsp), %rdi
callq 0x85b0
jmp 0x91ea6
leaq 0xea8(%rsp), %rdi
leaq 0xe68(%rsp), %rsi
callq 0x84d0
jmp 0x91ebd
leaq 0xe68(%rsp), %rdi
callq 0x88f8
jmp 0x91ecc
leaq 0xe67(%rsp), %rdi
callq 0x84a0
leaq 0xeb8(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0xea0(%rsp), %rdi
movl 0xe88(%rsp), %esi
callq 0x12110
movq %rax, 0x118(%rsp)
jmp 0x91f07
movq 0x118(%rsp), %rsi
movq 0x110(%rsp), %rdi
callq 0x8400
jmp 0x91f1e
leaq 0xe40(%rsp), %rdi
leaq 0xea8(%rsp), %rsi
callq 0x83e0
jmp 0x91f35
leaq 0x1048(%rsp), %rdi
leaq 0xe40(%rsp), %rsi
callq 0x94690
jmp 0x91f4c
leaq 0xe40(%rsp), %rdi
callq 0x88f8
jmp 0x91f5b
jmp 0x91f5d
movq 0xe88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe88(%rsp)
jmp 0x91e5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
jmp 0x91fb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0xe68(%rsp), %rdi
callq 0x88f8
jmp 0x91faf
jmp 0x91fb1
leaq 0xe67(%rsp), %rdi
callq 0x84a0
jmp 0x927cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0xe40(%rsp), %rdi
callq 0x88f8
jmp 0x91fe6
jmp 0x927cc
jmp 0x91fed
leaq 0xe28(%rsp), %rdi
callq 0x943a0
jmp 0x91ffc
leaq 0x1030(%rsp), %rdi
leaq 0xe28(%rsp), %rsi
callq 0x946d0
jmp 0x92013
leaq 0xe28(%rsp), %rdi
callq 0x8d860
jmp 0x92022
movq $0x0, 0xe20(%rsp)
movq 0xe20(%rsp), %rax
movl 0xe98(%rsp), %ecx
cmpq %rcx, %rax
jae 0x9222f
leaq 0xdff(%rsp), %rdi
movq %rdi, 0x108(%rsp)
callq 0x87f0
movq 0x108(%rsp), %rdx
leaq 0x20d8b(%rip), %rsi     # 0xb2df5
leaq 0xe00(%rsp), %rdi
callq 0x85b0
jmp 0x92079
leaq 0xea8(%rsp), %rdi
leaq 0xe00(%rsp), %rsi
callq 0x84d0
jmp 0x92090
leaq 0xe00(%rsp), %rdi
callq 0x88f8
jmp 0x9209f
leaq 0xdff(%rsp), %rdi
callq 0x84a0
movq 0xea0(%rsp), %rdi
movl 0xe20(%rsp), %esi
callq 0x11fe0
movq %rax, 0x100(%rsp)
jmp 0x920ca
movq 0x100(%rsp), %rax
cmpq $0x0, %rax
je 0x92198
leaq 0xeb8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0xea0(%rsp), %rdi
movl 0xe20(%rsp), %esi
callq 0x11fe0
movq %rax, 0xf8(%rsp)
jmp 0x9210a
movq 0xf8(%rsp), %rsi
movq 0xf0(%rsp), %rdi
callq 0x8440
jmp 0x92121
jmp 0x92198
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0xe28(%rsp), %rdi
callq 0x8d860
jmp 0x92146
jmp 0x927cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
jmp 0x92186
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0xe00(%rsp), %rdi
callq 0x88f8
jmp 0x92184
jmp 0x92186
leaq 0xdff(%rsp), %rdi
callq 0x84a0
jmp 0x927cc
leaq 0x1030(%rsp), %rdi
callq 0x94740
movq %rax, 0xe8(%rsp)
jmp 0x921af
leaq 0xdd8(%rsp), %rdi
leaq 0xea8(%rsp), %rsi
callq 0x83e0
jmp 0x921c6
movq 0xe8(%rsp), %rdi
leaq 0xdd8(%rsp), %rsi
callq 0x94780
jmp 0x921dd
leaq 0xdd8(%rsp), %rdi
callq 0x88f8
jmp 0x921ec
jmp 0x921ee
movq 0xe20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe20(%rsp)
jmp 0x9202e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0xdd8(%rsp), %rdi
callq 0x88f8
jmp 0x9222a
jmp 0x927cc
movl 0x1064(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1064(%rsp)
jmp 0x926c0
leaq 0xac0(%rsp), %rdi
xorl %esi, %esi
callq 0xa1800
jmp 0x92256
leaq 0x2063d(%rip), %rsi     # 0xb289a
leaq 0xac0(%rsp), %rdi
callq 0x945b0
movq %rax, 0xe0(%rsp)
jmp 0x92274
movq 0xe0(%rsp), %rdi
movq 0x10b8(%rsp), %rsi
callq 0x8350
movq %rax, 0xd8(%rsp)
jmp 0x92293
movq 0xd8(%rsp), %rdi
leaq 0x20b44(%rip), %rsi     # 0xb2de6
callq 0x8400
jmp 0x922a9
leaq 0xac0(%rsp), %rdi
callq 0xa1f50
jmp 0x922b8
jmp 0x92700
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0xac0(%rsp), %rdi
callq 0xa1f50
jmp 0x922e0
jmp 0x927cc
jmp 0x92700
cmpl $0x0, 0x1068(%rsp)
jg 0x9244a
leaq 0x7a8(%rsp), %rdi
movl $0x4, %esi
callq 0xa1800
jmp 0x9230c
leaq 0x205a9(%rip), %rsi     # 0xb28bc
leaq 0x7a8(%rsp), %rdi
callq 0x945b0
movq %rax, 0xd0(%rsp)
jmp 0x9232a
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x154c0
movq %rax, 0xc8(%rsp)
jmp 0x92345
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdi
callq 0x8400
movq %rax, 0xc0(%rsp)
jmp 0x92364
movq 0xc0(%rsp), %rdi
leaq 0x2055f(%rip), %rsi     # 0xb28d2
callq 0x8400
movq %rax, 0xb8(%rsp)
jmp 0x92382
movq 0xb8(%rsp), %rdi
movl 0x10a4(%rsp), %esi
callq 0x8780
movq %rax, 0xb0(%rsp)
jmp 0x923a0
movq 0xb0(%rsp), %rdi
leaq 0x203cc(%rip), %rsi     # 0xb277b
callq 0x8400
movq %rax, 0xa8(%rsp)
jmp 0x923be
movq 0xa8(%rsp), %rdi
leaq 0x20515(%rip), %rsi     # 0xb28e2
callq 0x8400
movq %rax, 0xa0(%rsp)
jmp 0x923dc
movq 0xa0(%rsp), %rdi
movq 0x10b8(%rsp), %rsi
callq 0x8350
movq %rax, 0x98(%rsp)
jmp 0x923fb
movq 0x98(%rsp), %rdi
leaq 0x204e6(%rip), %rsi     # 0xb28f0
callq 0x8400
jmp 0x92411
leaq 0x7a8(%rsp), %rdi
callq 0xa1f50
jmp 0x92420
jmp 0x9244a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0x7a8(%rsp), %rdi
callq 0xa1f50
jmp 0x92445
jmp 0x927cc
callq 0x92c00
jmp 0x92451
movq 0xea0(%rsp), %rdi
callq 0xfcf0
jmp 0x92460
movq 0x150(%rsp), %rdi
movq 0x10b8(%rsp), %rsi
callq 0x93a00
movq %rax, 0x90(%rsp)
jmp 0x9247f
movq 0x90(%rsp), %rax
movq %rax, 0xea0(%rsp)
movl 0x1068(%rsp), %eax
decl %eax
movl %eax, 0x1068(%rsp)
leaq 0x490(%rsp), %rdi
movl $0x2, %esi
callq 0xa1800
jmp 0x924b3
leaq 0x20439(%rip), %rsi     # 0xb28f3
leaq 0x490(%rsp), %rdi
callq 0x945b0
movq %rax, 0x88(%rsp)
jmp 0x924d1
movq 0x88(%rsp), %rdi
movl 0x1068(%rsp), %esi
callq 0x8780
jmp 0x924e7
leaq 0x490(%rsp), %rdi
callq 0xa1f50
jmp 0x924f6
jmp 0x91d18
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0x490(%rsp), %rdi
callq 0xa1f50
jmp 0x9251e
jmp 0x927cc
movq 0xea0(%rsp), %rdi
callq 0xfcf0
jmp 0x92532
movq 0x3dab7(%rip), %rdi     # 0xcfff0
leaq 0x203da(%rip), %rsi     # 0xb291a
callq 0x8400
movq %rax, 0x80(%rsp)
jmp 0x9254f
movq 0x80(%rsp), %rdi
movl 0xe94(%rsp), %esi
callq 0x8780
movq %rax, 0x78(%rsp)
jmp 0x9256a
movq 0x78(%rsp), %rdi
leaq 0x203c0(%rip), %rsi     # 0xb2936
callq 0x8400
movq %rax, 0x70(%rsp)
jmp 0x92582
movq 0x70(%rsp), %rdi
movq 0x10b8(%rsp), %rsi
callq 0x8350
movq %rax, 0x68(%rsp)
jmp 0x9259b
movq 0x68(%rsp), %rdi
leaq 0x2039c(%rip), %rsi     # 0xb2943
callq 0x8400
movq %rax, 0x60(%rsp)
jmp 0x925b3
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x154c0
movq %rax, 0x58(%rsp)
jmp 0x925cb
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdi
callq 0x8400
movq %rax, 0x50(%rsp)
jmp 0x925e1
movq 0x50(%rsp), %rdi
movq 0x3d99b(%rip), %rsi     # 0xcff88
callq 0x8470
jmp 0x925f4
leaq 0x178(%rsp), %rdi
movl $0x4, %esi
callq 0xa1800
jmp 0x92608
leaq 0x2030b(%rip), %rsi     # 0xb291a
leaq 0x178(%rsp), %rdi
callq 0x945b0
movq %rax, 0x48(%rsp)
jmp 0x92623
movq 0x48(%rsp), %rdi
movl 0xe94(%rsp), %esi
callq 0x8780
movq %rax, 0x40(%rsp)
jmp 0x9263b
movq 0x40(%rsp), %rdi
leaq 0x202ef(%rip), %rsi     # 0xb2936
callq 0x8400
movq %rax, 0x38(%rsp)
jmp 0x92653
movq 0x38(%rsp), %rdi
movq 0x10b8(%rsp), %rsi
callq 0x8350
movq %rax, 0x30(%rsp)
jmp 0x9266c
movq 0x30(%rsp), %rdi
leaq 0x202cb(%rip), %rsi     # 0xb2943
callq 0x8400
movq %rax, 0x28(%rsp)
jmp 0x92684
movq 0x150(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x154c0
movq %rax, 0x20(%rsp)
jmp 0x9269c
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x8400
jmp 0x926ad
leaq 0x178(%rsp), %rdi
callq 0xa1f50
jmp 0x926bc
jmp 0x926be
jmp 0x926c0
movl 0xe9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe9c(%rsp)
jmp 0x91d2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
leaq 0x178(%rsp), %rdi
callq 0xa1f50
jmp 0x926f9
jmp 0x927cc
jmp 0x92700
movq 0xea0(%rsp), %rdi
callq 0xfcf0
jmp 0x9270f
movl 0xe9c(%rsp), %eax
movb $0x0, 0x16f(%rsp)
cmpl $0x0, %eax
je 0x9276c
movl $0x40, %edi
callq 0x8420
movq %rax, 0x18(%rsp)
jmp 0x92734
movq 0x18(%rsp), %rdi
movq %rdi, 0x170(%rsp)
movb $0x1, 0x16f(%rsp)
leaq 0x1048(%rsp), %rsi
leaq 0x1030(%rsp), %rdx
callq 0x90f90
jmp 0x92760
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x92775
xorl %eax, %eax
movq %rax, 0x10(%rsp)
jmp 0x92775
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0xea8(%rsp), %rdi
callq 0x8900
jmp 0x927bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1070(%rsp)
movl %eax, 0x106c(%rsp)
testb $0x1, 0x16f(%rsp)
jne 0x927ae
jmp 0x927bb
movq 0x170(%rsp), %rdi
callq 0x83a0
jmp 0x927cc
leaq 0x1030(%rsp), %rdi
callq 0x8d6a0
jmp 0x927dd
leaq 0xea8(%rsp), %rdi
callq 0x8900
jmp 0x927db
jmp 0x927f7
leaq 0x1048(%rsp), %rdi
callq 0x8d720
movq 0x8(%rsp), %rax
addq $0x10c8, %rsp           # imm = 0x10C8
retq
leaq 0x1030(%rsp), %rdi
callq 0x8d6a0
jmp 0x92806
jmp 0x92808
leaq 0x1048(%rsp), %rdi
callq 0x8d720
jmp 0x92817
jmp 0x92819
movq 0x1070(%rsp), %rdi
callq 0x87d0
movq %rax, %rdi
callq 0x8cf60
nop