// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/database.cpp
// Function: ash::Database::Database(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Database::Database(const string & filename)
  : db_filename(filename), db(0)
{
  struct stat file;
  // Test that the history file exists, if not, create it.
  if (stat(db_filename.c_str(), &file)) {
    FILE * created_file = fopen(db_filename.c_str(), "w+e");
    if (!created_file) {
      LOG(FATAL) << "failed to create new DB file: " << db_filename << endl;
    }
    fclose(created_file);
  }

  // Open the DB, if failure, abort.
  if (sqlite3_open(db_filename.c_str(), &db)) {
    LOG(FATAL) << "Failed to open " << db_filename << "\nError: "
        << sqlite3_errmsg(db) << endl;
  }

  // Init the DB if it is missing the main tables.
  size_t registered = DBObject::table_names.size();

  stringstream ss;
  ss << "select count(*) as table_count "
     << "from sqlite_master "
     << "where type = 'table' and tbl_name in (";

  // List the table names registered by the code.
  if (registered > 0) ss << DBObject::quote(DBObject::table_names[0]);
  for (size_t i = 1; i < registered; ++i)
    ss << ", " << DBObject::quote(DBObject::table_names[i]);

  ss << ");";
  string query = ss.str();

  ResultSet * rs = exec(query.c_str());
  size_t defined_tables =
    rs && rs -> rows == 1 ? atoi(rs -> data[0][0].c_str()) : 0;
  if (rs) delete rs;
  if (defined_tables == registered) return;  // Normal case.

  // Initialize the DB if it's not already set up.
  init_db();

  // Log a warning if there was an unexpected number of tables.
  if (defined_tables > registered) {
    LOG(WARNING) << "Expected " << registered
        << " tables to be defined, found " << defined_tables << " instead.";
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x578, %rsp            # imm = 0x578
movq %rdi, %r15
leaq 0x297af(%rip), %rax     # 0x93968
movq %rax, (%rdi)
leaq 0x8(%rdi), %rbx
leaq 0x18(%rdi), %r14
movq %r14, 0x8(%rdi)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
addq %rax, %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x68e74
movq $0x0, 0x28(%r15)
movq %r15, 0x8(%rsp)
movq 0x8(%r15), %rdi
leaq 0x4e8(%rsp), %rsi
callq 0x8350
testl %eax, %eax
movq %rbx, 0x20(%rsp)
movq %r14, 0x18(%rsp)
je 0x6a29d
movq (%rbx), %rdi
leaq 0x137b8(%rip), %rsi     # 0x7d9cf
callq 0x84a0
movq %rax, %r15
testq %rax, %rax
jne 0x6a295
leaq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x70186
leaq 0x50(%rsp), %r12
leaq 0x13794(%rip), %rsi     # 0x7d9d3
movl $0x1e, %edx
movq %r12, %rdi
callq 0x8470
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r12, %rdi
callq 0x8470
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x83c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8250
leaq 0x48(%rsp), %rdi
callq 0x70626
movq %r15, %rdi
callq 0x83e0
movq 0x8(%rsp), %rax
leaq 0x28(%rax), %r12
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x15c90
testl %eax, %eax
je 0x6a381
leaq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x70186
leaq 0x50(%rsp), %r15
leaq 0x1371e(%rip), %rsi     # 0x7d9f2
movl $0xf, %edx
movq %r15, %rdi
callq 0x8470
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r15, %rdi
callq 0x8470
movq %rax, %r15
leaq 0x13702(%rip), %rsi     # 0x7da02
movl $0x8, %edx
movq %rax, %rdi
callq 0x8470
movq (%r12), %rdi
callq 0x12bfd
movq %rax, %r12
testq %rax, %rax
je 0x6a336
movq %r12, %rdi
callq 0x8190
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6a350
movq (%r15), %rax
movq -0x18(%rax), %rax
movq %r15, %rdi
addq %rax, %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x86b0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x83c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8250
leaq 0x48(%rsp), %rdi
callq 0x70626
movq 0x2c020(%rip), %rbx     # 0x963a8
movq 0x2c011(%rip), %r13     # 0x963a0
leaq 0x360(%rsp), %rdi
callq 0x8290
leaq 0x370(%rsp), %r12
leaq 0x13660(%rip), %rsi     # 0x7da0b
movl $0x1f, %edx
movq %r12, %rdi
callq 0x8470
leaq 0x1366c(%rip), %rsi     # 0x7da2b
movl $0x13, %edx
movq %r12, %rdi
callq 0x8470
leaq 0x1366c(%rip), %rsi     # 0x7da3f
movl $0x26, %edx
movq %r12, %rdi
callq 0x8470
cmpq %r13, %rbx
je 0x6a41b
movq 0x2bfb4(%rip), %rsi     # 0x963a0
leaq 0x48(%rsp), %rdi
callq 0x6a6be
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %r12, %rdi
callq 0x8470
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a41b
callq 0x8390
subq %r13, %rbx
sarq $0x5, %rbx
movq %rbx, 0x10(%rsp)
cmpq $0x2, %rbx
jb 0x6a499
leaq 0x58(%rsp), %rbx
movq 0x10(%rsp), %rax
leaq -0x1(%rax), %r14
movl $0x20, %r15d
leaq 0x11a09(%rip), %r13     # 0x7be51
leaq 0x48(%rsp), %rbp
movl $0x2, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x8470
movq 0x2bf3c(%rip), %rsi     # 0x963a0
addq %r15, %rsi
movq %rbp, %rdi
callq 0x6a6be
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %r12, %rdi
callq 0x8470
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6a490
callq 0x8390
addq $0x20, %r15
decq %r14
jne 0x6a44d
leaq 0x125f0(%rip), %rsi     # 0x7ca90
movl $0x2, %edx
movq %r12, %rdi
callq 0x8470
movq 0x10(%rsp), %rbx
leaq 0x378(%rsp), %rsi
leaq 0x28(%rsp), %rdi
callq 0x8630
movq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x8170
leaq 0x48(%rsp), %rsi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x6a790
movq %rax, %r13
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a4ff
callq 0x8390
testq %r13, %r13
je 0x6a51f
cmpq $0x1, 0x30(%r13)
jne 0x6a51f
movq 0x18(%r13), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x8600
movslq %eax, %r12
jmp 0x6a522
xorl %r12d, %r12d
testq %r13, %r13
je 0x6a537
movq %r13, %rdi
callq 0x68c5e
movq %r13, %rdi
callq 0x8390
cmpq %rbx, %r12
je 0x6a5c2
movq 0x8(%rsp), %rdi
callq 0x6affe
cmpq %rbx, %r12
jbe 0x6a5c2
leaq 0x48(%rsp), %rdi
movl $0x2, %esi
callq 0x70186
leaq 0x50(%rsp), %r14
leaq 0x14f53(%rip), %rsi     # 0x7f4bd
movl $0x9, %edx
movq %r14, %rdi
callq 0x8470
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8240
movq %rax, %r14
leaq 0x134da(%rip), %rsi     # 0x7da66
movl $0x1d, %edx
movq %rax, %rdi
callq 0x8470
movq %r14, %rdi
movq %r12, %rsi
callq 0x8240
leaq 0x134d9(%rip), %rsi     # 0x7da84
movl $0x9, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x48(%rsp), %rdi
callq 0x70626
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a5d5
callq 0x8390
leaq 0x360(%rsp), %rdi
callq 0x87e8
addq $0x578, %rsp            # imm = 0x578
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6a65d
jmp 0x6a626
jmp 0x6a626
movq %rax, %r14
movq %r13, %rdi
jmp 0x6a61f
movq %rax, %r14
leaq 0x58(%rsp), %rax
jmp 0x6a650
jmp 0x6a675
movq %rax, %r14
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a64b
callq 0x8390
jmp 0x6a64b
movq %rax, %r14
jmp 0x6a64b
jmp 0x6a675
jmp 0x6a65d
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x70626
jmp 0x6a699
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x70626
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x6a687
jmp 0x6a68c
jmp 0x6a65d
movq %rax, %r14
jmp 0x6a699
jmp 0x6a675
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x70626
jmp 0x6a699
jmp 0x6a675
movq %rax, %r14
jmp 0x6a68c
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6a68c
callq 0x8390
leaq 0x360(%rsp), %rdi
callq 0x87e8
movq 0x20(%rsp), %rax
movq (%rax), %rdi
cmpq 0x18(%rsp), %rdi
je 0x6a6ad
callq 0x8390
movq %r14, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68c53
nop