// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/database.cpp
// Function: ash::Database::prepare_stmt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const
sqlite3_stmt * Database::prepare_stmt(const string & query) const {
  sqlite3_stmt * ps = 0;

  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;

try_prepare:
  sqlite3_prepare_v2(db, query.c_str(), query.length(), &ps, 0);
  if (ps) return ps;
  int error_code = sqlite3_errcode(db);
  switch (error_code) {
    case SQLITE_LOCKED:  // fallthrough
    case SQLITE_BUSY:
      LOG(DEBUG) << "Database is busy while preparing a statement.";
      sqlite3_finalize(ps);
      if (--tries > 0) {
        LOG(DEBUG) << "Sleeping and trying to prepare statement again.";
        ash_sleep();
        goto try_prepare;
      }
      LOG(FATAL) << "Failed to prepare statement after " << max_retries
                 << " failed attempts.";
      return 0;  // unreachable
    default:
      LOG(FATAL) << "Unexpected error code while preparing statement: "
                 << error_code;
      return 0;  // unreachable
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x338, %rsp            # imm = 0x338
movq %rsi, %rbx
movq %rdi, %r14
andq $0x0, 0x10(%rsp)
callq 0x5d6d2
movq %rax, %r15
leaq 0x10d42(%rip), %rsi     # 0x6fa9e
leaq 0x20(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x8150
leaq 0x20(%rsp), %rsi
pushq $-0x1
popq %rdx
movq %r15, %rdi
callq 0x5d632
movl %eax, %ebp
leaq 0x20(%rsp), %rdi
callq 0x84b0
testl %ebp, %ebp
pushq $0x5
popq %rax
cmovgl %ebp, %eax
movq %rax, 0x18(%rsp)
leal 0x1(%rax), %r12d
leaq 0x20(%rsp), %rbp
movq 0x28(%r14), %rdi
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x11728
movq 0x10(%rsp), %r15
testq %r15, %r15
jne 0x5eea9
movq 0x28(%r14), %rdi
callq 0x11748
movl %eax, %r13d
leal -0x5(%r13), %eax
cmpl $0x1, %eax
ja 0x5ee3a
movq %rbp, %rdi
xorl %esi, %esi
callq 0x62c92
leaq 0x28(%rsp), %rdi
leaq 0x10f19(%rip), %rsi     # 0x6fd06
callq 0x83a0
movq %rbp, %rdi
callq 0x62fc8
movq 0x10(%rsp), %rdi
callq 0xdbaa
cmpl $0x2, %r12d
jl 0x5ee65
movq %rbp, %rdi
xorl %esi, %esi
callq 0x62c92
leaq 0x28(%rsp), %rdi
leaq 0x10f14(%rip), %rsi     # 0x6fd34
callq 0x83a0
movq %rbp, %rdi
callq 0x62fc8
callq 0x5e65c
decl %r12d
jmp 0x5ed9d
leaq 0x20(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x62c92
leaq 0x28(%rsp), %rdi
leaq 0x10f46(%rip), %rsi     # 0x6fd99
callq 0x83a0
movq %rax, %rdi
movl %r13d, %esi
callq 0x86a0
jmp 0x5ee9f
leaq 0x20(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x62c92
leaq 0x28(%rsp), %rdi
leaq 0x10ee6(%rip), %rsi     # 0x6fd64
callq 0x83a0
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x86a0
leaq 0x10ef0(%rip), %rsi     # 0x6fd87
movq %rax, %rdi
callq 0x83a0
leaq 0x20(%rsp), %rdi
callq 0x62fc8
movq %r15, %rax
addq $0x338, %rsp            # imm = 0x338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x62fc8
jmp 0x5ef0c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x62fc8
jmp 0x5ef0c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x84b0
jmp 0x5ef0c
movq %rax, %rbx
jmp 0x5ef0c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x62fc8
jmp 0x5ef0c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x62fc8
movq %rbx, %rdi
callq 0x86f0
movq %rax, %rdi
callq 0x5c505