// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xd38, %rsp            # imm = 0xD38
movq %rdi, 0xd28(%rsp)
movq %rsi, 0xd20(%rsp)
movq 0xd28(%rsp), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0xd18(%rsp)
callq 0x90ab0
movq %rax, 0xd10(%rsp)
movq 0xd10(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0xce7(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x87f0
movq 0x60(%rsp), %rdx
leaq 0x1eb05(%rip), %rsi     # 0xb256d
leaq 0xce8(%rsp), %rdi
callq 0x85b0
jmp 0x93a77
movq 0x58(%rsp), %rdi
leaq 0xce8(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x90840
movl %eax, 0x4c(%rsp)
jmp 0x93a94
leaq 0xce8(%rsp), %rdi
callq 0x88f8
jmp 0x93aa3
leaq 0xce7(%rsp), %rdi
callq 0x84a0
movl 0x4c(%rsp), %eax
movl %eax, 0xd0c(%rsp)
cmpl $0x0, 0xd0c(%rsp)
jg 0x93b1f
movl $0x5, 0xd0c(%rsp)
jmp 0x93b1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
jmp 0x93b0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0xce8(%rsp), %rdi
callq 0x88f8
jmp 0x93b0b
jmp 0x93b0d
leaq 0xce7(%rsp), %rdi
callq 0x84a0
jmp 0x93dd5
movl 0xd0c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcd0(%rsp)
movq 0x50(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0xd20(%rsp), %rdi
callq 0x8190
movq %rax, 0x40(%rsp)
movq 0xd20(%rsp), %rdi
callq 0x8670
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl %eax, %edx
leaq 0xd18(%rsp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x15400
cmpq $0x0, 0xd18(%rsp)
je 0x93b9b
movq 0xd18(%rsp), %rax
movq %rax, 0xd30(%rsp)
jmp 0x93dc5
movq 0x50(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x15450
movl %eax, 0xccc(%rsp)
movl 0xccc(%rsp), %eax
addl $-0x5, %eax
subl $0x1, %eax
ja 0x93d4e
jmp 0x93bc5
leaq 0x9b0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
xorl %esi, %esi
callq 0xa1800
movq 0x30(%rsp), %rdi
leaq 0x1ebf0(%rip), %rsi     # 0xb27d5
callq 0x945b0
jmp 0x93bec
leaq 0x9b0(%rsp), %rdi
callq 0xa1f50
movq 0xd18(%rsp), %rdi
callq 0xfcf0
movl 0xcd0(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xcd0(%rsp)
cmpl $0x0, %eax
jle 0x93cae
leaq 0x698(%rsp), %rdi
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
callq 0xa1800
movq 0x28(%rsp), %rdi
leaq 0x1ebc3(%rip), %rsi     # 0xb2803
callq 0x945b0
jmp 0x93c47
leaq 0x698(%rsp), %rdi
callq 0xa1f50
callq 0x92c00
jmp 0x93b30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x9b0(%rsp), %rdi
callq 0xa1f50
jmp 0x93c81
jmp 0x93dd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x698(%rsp), %rdi
callq 0xa1f50
jmp 0x93ca9
jmp 0x93dd5
leaq 0x380(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movl $0x4, %esi
callq 0xa1800
movq 0x18(%rsp), %rdi
leaq 0x1eb62(%rip), %rsi     # 0xb2833
callq 0x945b0
movq %rax, 0x20(%rsp)
jmp 0x93cdd
movq 0x20(%rsp), %rdi
movl 0xd0c(%rsp), %esi
callq 0x8780
movq %rax, 0x10(%rsp)
jmp 0x93cf5
movq 0x10(%rsp), %rdi
leaq 0x1eb55(%rip), %rsi     # 0xb2856
callq 0x8400
jmp 0x93d08
leaq 0x380(%rsp), %rdi
callq 0xa1f50
movq $0x0, 0xd30(%rsp)
jmp 0x93dc5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x380(%rsp), %rdi
callq 0xa1f50
jmp 0x93d49
jmp 0x93dd5
leaq 0x68(%rsp), %rdi
movq %rdi, (%rsp)
movl $0x4, %esi
callq 0xa1800
movq (%rsp), %rdi
leaq 0x1eafc(%rip), %rsi     # 0xb2868
callq 0x945b0
movq %rax, 0x8(%rsp)
jmp 0x93d78
movq 0x8(%rsp), %rdi
movl 0xccc(%rsp), %esi
callq 0x8780
jmp 0x93d8b
leaq 0x68(%rsp), %rdi
callq 0xa1f50
movq $0x0, 0xd30(%rsp)
jmp 0x93dc5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xcd8(%rsp)
movl %eax, 0xcd4(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xa1f50
jmp 0x93dc3
jmp 0x93dd5
movq 0xd30(%rsp), %rax
addq $0xd38, %rsp            # imm = 0xD38
retq
movq 0xcd8(%rsp), %rdi
callq 0x87d0
movq %rax, %rdi
callq 0x8cf60
nopw (%rax,%rax)