// File: /workspace/github_c_makefile_star2_O2/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 $0x568, %rsp            # imm = 0x568
movq %rdi, %r14
leaq 0x28ff1(%rip), %rax     # 0x86998
movq %rax, (%rdi)
leaq 0x8(%rdi), %rbx
movq %rbx, %rdi
callq 0x8500
andq $0x0, 0x28(%r14)
movq %r14, 0x10(%rsp)
movq 0x8(%r14), %rdi
leaq 0x4d8(%rsp), %rsi
callq 0x8330
testl %eax, %eax
movq %rbx, 0x8(%rsp)
je 0x5da3b
movq (%rbx), %rdi
leaq 0x11fe1(%rip), %rsi     # 0x6f9c5
callq 0x8470
movq %rax, %r15
testq %rax, %rax
jne 0x5da2e
leaq 0x38(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x62c92
leaq 0x40(%rsp), %rdi
leaq 0x11fbf(%rip), %rsi     # 0x6f9c9
callq 0x83a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x8320
movq %rax, %rdi
callq 0x81a0
leaq 0x38(%rsp), %rdi
callq 0x62fc8
movq %r15, %rdi
callq 0x83b0
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rax
leaq 0x28(%rax), %r15
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x140b1
testl %eax, %eax
je 0x5dab5
leaq 0x38(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x62c92
leaq 0x40(%rsp), %rdi
leaq 0x11f7c(%rip), %rsi     # 0x6f9e8
callq 0x83a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x8320
leaq 0x11f73(%rip), %rsi     # 0x6f9f8
movq %rax, %rdi
callq 0x83a0
movq %rax, %rbx
movq (%r15), %rdi
callq 0x11788
movq %rbx, %rdi
movq %rax, %rsi
callq 0x83a0
movq %rax, %rdi
callq 0x81a0
leaq 0x38(%rsp), %rdi
callq 0x62fc8
movq 0x2b8ec(%rip), %r15     # 0x893a8
movq 0x2b8dd(%rip), %rbx     # 0x893a0
leaq 0x350(%rsp), %rdi
callq 0x8260
leaq 0x360(%rsp), %rdi
leaq 0x11f22(%rip), %rsi     # 0x6fa01
callq 0x83a0
leaq 0x11f36(%rip), %rsi     # 0x6fa21
movq %rax, %rdi
callq 0x83a0
leaq 0x11f3b(%rip), %rsi     # 0x6fa35
movq %rax, %rdi
callq 0x83a0
cmpq %rbx, %r15
je 0x5db34
movq 0x2b892(%rip), %rsi     # 0x893a0
leaq 0x38(%rsp), %rdi
callq 0x5dd76
leaq 0x38(%rsp), %rsi
leaq 0x360(%rsp), %rdi
callq 0x8320
leaq 0x38(%rsp), %rdi
callq 0x84b0
subq %rbx, %r15
sarq $0x5, %r15
pushq $0x1
popq %r14
pushq $0x20
popq %r12
leaq 0x10327(%rip), %r13     # 0x6de71
leaq 0x38(%rsp), %rbp
cmpq %r15, %r14
jae 0x5db95
leaq 0x360(%rsp), %rdi
movq %r13, %rsi
callq 0x83a0
movq %rax, %rbx
movq 0x2b832(%rip), %rsi     # 0x893a0
addq %r12, %rsi
movq %rbp, %rdi
callq 0x5dd76
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x8320
movq %rbp, %rdi
callq 0x84b0
incq %r14
addq $0x20, %r12
jmp 0x5db4f
leaq 0x10f14(%rip), %rsi     # 0x6eab0
leaq 0x360(%rsp), %rdi
callq 0x83a0
leaq 0x368(%rsp), %rsi
leaq 0x18(%rsp), %rdi
callq 0x8610
movq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x8150
leaq 0x38(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x5de40
movq %rax, %r13
leaq 0x38(%rsp), %rdi
callq 0x84b0
testq %r13, %r13
je 0x5dc0d
cmpq $0x1, 0x30(%r13)
jne 0x5dc12
movq 0x18(%r13), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x85e0
movslq %eax, %r12
jmp 0x5dc15
xorl %r12d, %r12d
jmp 0x5dc25
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x5c51e
movq %r13, %rdi
callq 0x8370
cmpq %r15, %r12
je 0x5dc95
movq 0x10(%rsp), %rdi
callq 0x5e456
cmpq %r15, %r12
jbe 0x5dc95
leaq 0x38(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x62c92
leaq 0x40(%rsp), %rdi
leaq 0x1384b(%rip), %rsi     # 0x7149d
callq 0x83a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x8220
leaq 0x11df3(%rip), %rsi     # 0x6fa5c
movq %rax, %rdi
callq 0x83a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x8220
leaq 0x11df7(%rip), %rsi     # 0x6fa7a
movq %rax, %rdi
callq 0x83a0
leaq 0x38(%rsp), %rdi
callq 0x62fc8
leaq 0x18(%rsp), %rdi
callq 0x84b0
leaq 0x350(%rsp), %rdi
callq 0x87e8
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5dd24
jmp 0x5dd15
jmp 0x5dd15
movq %rax, %r14
movq %r13, %rdi
callq 0x8370
jmp 0x5dd18
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x84b0
jmp 0x5dd4e
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x62fc8
jmp 0x5dd5b
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x84b0
jmp 0x5dd18
jmp 0x5dd24
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x62fc8
jmp 0x5dd18
jmp 0x5dd4b
jmp 0x5dd24
jmp 0x5dd4b
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x84b0
jmp 0x5dd4e
movq %rax, %r14
jmp 0x5dd5b
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x62fc8
jmp 0x5dd5b
jmp 0x5dd4b
jmp 0x5dd4b
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x84b0
jmp 0x5dd4e
movq %rax, %r14
leaq 0x350(%rsp), %rdi
callq 0x87e8
movq 0x8(%rsp), %rdi
callq 0x84b0
movq %r14, %rdi
callq 0x86f0
movq %rax, %rdi
callq 0x5c505
nop