// File: /workspace/github_c_makefile_star2_O3/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 0x299d3(%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 0x68c64
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 0x6a079
movq (%rbx), %rdi
leaq 0x13a2c(%rip), %rsi     # 0x7da1f
callq 0x84a0
movq %rax, %r15
testq %rax, %rax
jne 0x6a071
leaq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x6fef2
leaq 0x50(%rsp), %r12
leaq 0x13a08(%rip), %rsi     # 0x7da23
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 0x70392
movq %r15, %rdi
callq 0x83e0
movq 0x8(%rsp), %rax
leaq 0x28(%rax), %r12
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x15cab
testl %eax, %eax
je 0x6a15d
leaq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x6fef2
leaq 0x50(%rsp), %r15
leaq 0x13992(%rip), %rsi     # 0x7da42
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 0x13976(%rip), %rsi     # 0x7da52
movl $0x8, %edx
movq %rax, %rdi
callq 0x8470
movq (%r12), %rdi
callq 0x12cc6
movq %rax, %r12
testq %rax, %rax
je 0x6a112
movq %r12, %rdi
callq 0x8190
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6a12c
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 0x70392
movq 0x2c244(%rip), %rbx     # 0x963a8
movq 0x2c235(%rip), %r13     # 0x963a0
leaq 0x360(%rsp), %rdi
callq 0x8290
leaq 0x370(%rsp), %r12
leaq 0x138d4(%rip), %rsi     # 0x7da5b
movl $0x1f, %edx
movq %r12, %rdi
callq 0x8470
leaq 0x138e0(%rip), %rsi     # 0x7da7b
movl $0x13, %edx
movq %r12, %rdi
callq 0x8470
leaq 0x138e0(%rip), %rsi     # 0x7da8f
movl $0x26, %edx
movq %r12, %rdi
callq 0x8470
cmpq %r13, %rbx
je 0x6a1f7
movq 0x2c1d8(%rip), %rsi     # 0x963a0
leaq 0x48(%rsp), %rdi
callq 0x6a49a
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 0x6a1f7
callq 0x8390
subq %r13, %rbx
sarq $0x5, %rbx
movq %rbx, 0x10(%rsp)
cmpq $0x2, %rbx
jb 0x6a275
leaq 0x58(%rsp), %rbx
movq 0x10(%rsp), %rax
leaq -0x1(%rax), %r14
movl $0x20, %r15d
leaq 0x11c2d(%rip), %r13     # 0x7be51
leaq 0x48(%rsp), %rbp
movl $0x2, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x8470
movq 0x2c160(%rip), %rsi     # 0x963a0
addq %r15, %rsi
movq %rbp, %rdi
callq 0x6a49a
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %r12, %rdi
callq 0x8470
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6a26c
callq 0x8390
addq $0x20, %r15
decq %r14
jne 0x6a229
leaq 0x12814(%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 0x6a56e
movq %rax, %r13
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a2db
callq 0x8390
testq %r13, %r13
je 0x6a2fb
cmpq $0x1, 0x30(%r13)
jne 0x6a300
movq 0x18(%r13), %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x8600
movslq %eax, %r12
jmp 0x6a303
xorl %r12d, %r12d
jmp 0x6a313
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x68a3c
movq %r13, %rdi
callq 0x8390
cmpq %rbx, %r12
je 0x6a39e
movq 0x8(%rsp), %rdi
callq 0x6ad9e
cmpq %rbx, %r12
jbe 0x6a39e
leaq 0x48(%rsp), %rdi
movl $0x2, %esi
callq 0x6fef2
leaq 0x50(%rsp), %r14
leaq 0x151c7(%rip), %rsi     # 0x7f50d
movl $0x9, %edx
movq %r14, %rdi
callq 0x8470
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8240
movq %rax, %r14
leaq 0x1374e(%rip), %rsi     # 0x7dab6
movl $0x1d, %edx
movq %rax, %rdi
callq 0x8470
movq %r14, %rdi
movq %r12, %rsi
callq 0x8240
leaq 0x1374d(%rip), %rsi     # 0x7dad4
movl $0x9, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x48(%rsp), %rdi
callq 0x70392
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a3b1
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 0x6a439
jmp 0x6a402
jmp 0x6a402
movq %rax, %r14
movq %r13, %rdi
jmp 0x6a3fb
movq %rax, %r14
leaq 0x58(%rsp), %rax
jmp 0x6a42c
jmp 0x6a451
movq %rax, %r14
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a427
callq 0x8390
jmp 0x6a427
movq %rax, %r14
jmp 0x6a427
jmp 0x6a451
jmp 0x6a439
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x70392
jmp 0x6a475
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x70392
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x6a463
jmp 0x6a468
jmp 0x6a439
movq %rax, %r14
jmp 0x6a475
jmp 0x6a451
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x70392
jmp 0x6a475
jmp 0x6a451
movq %rax, %r14
jmp 0x6a468
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6a468
callq 0x8390
leaq 0x360(%rsp), %rdi
callq 0x87e8
movq 0x20(%rsp), %rax
movq (%rax), %rdi
cmpq 0x18(%rsp), %rdi
je 0x6a489
callq 0x8390
movq %r14, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68a31
nop