// File: /workspace/github_c_makefile_star2_O1/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
movq $0x0, 0x10(%rsp)
callq 0x69ef2
movq %rax, %r15
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x11f6d(%rip), %rsi     # 0x7daa8
leaq 0x11f74(%rip), %rdx     # 0x7dab6
leaq 0x20(%rsp), %rdi
callq 0x68de4
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69e16
movl %eax, %ebp
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x6bb6f
callq 0x8390
testl %ebp, %ebp
movl $0x5, %eax
cmovgl %ebp, %eax
movq %rax, 0x18(%rsp)
leal 0x1(%rax), %r12d
leaq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %r13
movq %rbx, 0x8(%rsp)
movq 0x8(%rsp), %rbp
movq 0x28(%r14), %rdi
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rbx
movq %rcx, %r14
xorl %r8d, %r8d
callq 0x12b82
movq 0x10(%rsp), %rax
testq %rax, %rax
jne 0x6bd02
movq 0x28(%rbx), %rdi
callq 0x12ba4
movl %eax, %r15d
leal -0x5(%r15), %eax
cmpl $0x1, %eax
ja 0x6bc46
movq %r13, %rdi
xorl %esi, %esi
callq 0x70186
movl $0x2d, %edx
leaq 0x28(%rsp), %rdi
leaq 0x12124(%rip), %rsi     # 0x7dd10
callq 0x8470
movq %r13, %rdi
callq 0x70626
movq 0x10(%rsp), %rdi
callq 0xe974
leal -0x1(%r12), %r15d
cmpl $0x2, %r12d
jl 0x6bc94
movq %r13, %rdi
xorl %esi, %esi
callq 0x70186
movl $0x2f, %edx
leaq 0x28(%rsp), %rdi
leaq 0x12111(%rip), %rsi     # 0x7dd3e
callq 0x8470
movq %r13, %rdi
callq 0x70626
callq 0x6b2a4
movb $0x1, %al
jmp 0x6bcea
movq %r13, %rdi
movl $0x4, %esi
callq 0x70186
movl $0x31, %edx
leaq 0x28(%rsp), %r13
movq %r13, %rdi
leaq 0x1213c(%rip), %rsi     # 0x7dda3
callq 0x8470
movq %r13, %rdi
movl %r15d, %esi
callq 0x86d0
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x70626
xorl %eax, %eax
movl %r12d, %r15d
movq %r14, %rcx
movq %rbx, %r14
movq %rbp, %rbx
jmp 0x6bcf5
movq %r13, %rdi
movl $0x4, %esi
callq 0x70186
movl $0x22, %edx
leaq 0x28(%rsp), %r12
movq %r12, %rdi
leaq 0x120b9(%rip), %rsi     # 0x7dd6e
callq 0x8470
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x86d0
movl $0x11, %edx
movq %rax, %rdi
leaq 0x120bb(%rip), %rsi     # 0x7dd91
callq 0x8470
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x70626
xorl %eax, %eax
movq %r14, %rcx
movq %rbx, %r14
movq 0x8(%rsp), %rbx
movl %r15d, %r12d
testb %al, %al
jne 0x6bb96
xorl %eax, %eax
addq $0x338, %rsp            # imm = 0x338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x6bd67
callq 0x8390
jmp 0x6bd67
movq %rax, %rbx
jmp 0x6bd67
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x70626
jmp 0x6bd67
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x70626
jmp 0x6bd67
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x70626
jmp 0x6bd67
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x70626
movq %rbx, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68c53
nop