// File: /workspace/github_c_makefile_star2_O3/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 $0x328, %rsp            # imm = 0x328
movq %rsi, %rbx
movq %rdi, %r14
movq $0x0, 0x8(%rsp)
callq 0x69cd0
movq %rax, %r15
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1220f(%rip), %rsi     # 0x7daf8
leaq 0x12216(%rip), %rdx     # 0x7db06
leaq 0x10(%rsp), %rdi
callq 0x68bce
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69bfc
movl %eax, %ebp
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x6b91d
callq 0x8390
testl %ebp, %ebp
movl $0x5, %r15d
cmovgl %ebp, %r15d
movq 0x28(%r14), %rdi
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
leaq 0x8(%rsp), %r12
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x12c27
movq (%r12), %rax
testq %rax, %rax
jne 0x6ba90
leal 0x2(%r15), %r12d
leaq 0x10(%rsp), %rbp
movq 0x28(%r14), %rdi
callq 0x12c49
movl %eax, %r13d
leal -0x5(%r13), %eax
cmpl $0x1, %eax
ja 0x6ba08
movq %rbp, %rdi
xorl %esi, %esi
callq 0x6fef2
movl $0x2d, %edx
leaq 0x18(%rsp), %rdi
leaq 0x123d3(%rip), %rsi     # 0x7dd60
callq 0x8470
movq %rbp, %rdi
callq 0x70392
movq 0x8(%rsp), %rdi
callq 0xea51
decl %r12d
cmpl $0x2, %r12d
jl 0x6ba3d
movq %rbp, %rdi
xorl %esi, %esi
callq 0x6fef2
movl $0x2f, %edx
leaq 0x18(%rsp), %rdi
leaq 0x123c2(%rip), %rsi     # 0x7dd8e
callq 0x8470
movq %rbp, %rdi
callq 0x70392
callq 0x6b03c
movq 0x28(%r14), %rdi
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x12c27
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x6b959
jmp 0x6ba90
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
callq 0x6fef2
leaq 0x18(%rsp), %rbx
leaq 0x123d0(%rip), %rsi     # 0x7ddf3
movl $0x31, %edx
movq %rbx, %rdi
callq 0x8470
movq %rbx, %rdi
movl %r13d, %esi
callq 0x86d0
jmp 0x6ba84
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
callq 0x6fef2
leaq 0x18(%rsp), %rbx
leaq 0x12366(%rip), %rsi     # 0x7ddbe
movl $0x22, %edx
movq %rbx, %rdi
callq 0x8470
movq %rbx, %rdi
movl %r15d, %esi
callq 0x86d0
leaq 0x1236a(%rip), %rsi     # 0x7dde1
movl $0x11, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x10(%rsp), %rdi
callq 0x70392
xorl %eax, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70392
jmp 0x6baf5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70392
jmp 0x6baf5
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x6baf5
callq 0x8390
jmp 0x6baf5
movq %rax, %rbx
jmp 0x6baf5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70392
jmp 0x6baf5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x70392
movq %rbx, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68a31
nop