// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xd48, %rsp            # imm = 0xD48
movq %rdi, 0xd40(%rsp)
movq %rsi, 0xd38(%rsp)
movq 0xd40(%rsp), %rdi
movq %rdi, 0xf8(%rsp)
leaq 0x3e8d2(%rip), %rax     # 0xcf990
movq %rax, (%rdi)
addq $0x8, %rdi
movq %rdi, 0x100(%rsp)
movq 0xd38(%rsp), %rsi
callq 0x81a0
movq 0xf8(%rsp), %rax
movq 0x100(%rsp), %rdi
movq $0x0, 0x28(%rax)
callq 0x8190
movq %rax, 0x108(%rsp)
jmp 0x91101
movq 0x108(%rsp), %rdi
leaq 0xca8(%rsp), %rsi
callq 0x8360
cmpl $0x0, %eax
je 0x91245
movq 0xf8(%rsp), %rdi
addq $0x8, %rdi
callq 0x8190
movq %rax, 0xf0(%rsp)
jmp 0x9113a
movq 0xf0(%rsp), %rdi
leaq 0x2134b(%rip), %rsi     # 0xb2494
callq 0x8510
movq %rax, 0xe8(%rsp)
jmp 0x91158
movq 0xe8(%rsp), %rax
movq %rax, 0xc90(%rsp)
cmpq $0x0, 0xc90(%rsp)
jne 0x91234
leaq 0x978(%rsp), %rdi
movl $0x4, %esi
callq 0xa1800
jmp 0x9118b
leaq 0x21306(%rip), %rsi     # 0xb2498
leaq 0x978(%rsp), %rdi
callq 0x945b0
movq %rax, 0xe0(%rsp)
jmp 0x911a9
movq 0xe0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
addq $0x8, %rsi
callq 0x8350
movq %rax, 0xd8(%rsp)
jmp 0x911cc
movq 0xd8(%rsp), %rdi
movq 0x3edad(%rip), %rsi     # 0xcff88
callq 0x8470
jmp 0x911e2
leaq 0x978(%rsp), %rdi
callq 0xa1f50
jmp 0x911f1
jmp 0x91234
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
jmp 0x918f1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
leaq 0x978(%rsp), %rdi
callq 0xa1f50
jmp 0x9122f
jmp 0x918f1
movq 0xc90(%rsp), %rdi
callq 0x8410
jmp 0x91243
jmp 0x91245
movq 0xf8(%rsp), %rdi
addq $0x8, %rdi
callq 0x8190
movq %rax, 0xd0(%rsp)
jmp 0x91260
movq 0xd0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
addq $0x28, %rsi
callq 0x197d0
movl %eax, 0xcc(%rsp)
jmp 0x91282
movl 0xcc(%rsp), %eax
cmpl $0x0, %eax
je 0x9138e
leaq 0x660(%rsp), %rdi
movl $0x4, %esi
callq 0xa1800
jmp 0x912a6
leaq 0x2120a(%rip), %rsi     # 0xb24b7
leaq 0x660(%rsp), %rdi
callq 0x945b0
movq %rax, 0xc0(%rsp)
jmp 0x912c4
movq 0xc0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
addq $0x8, %rsi
callq 0x8350
movq %rax, 0xb8(%rsp)
jmp 0x912e7
movq 0xb8(%rsp), %rdi
leaq 0x211d1(%rip), %rsi     # 0xb24c7
callq 0x8400
movq %rax, 0xb0(%rsp)
jmp 0x91305
movq 0xf8(%rsp), %rax
movq 0x28(%rax), %rdi
callq 0x154c0
movq %rax, 0xa8(%rsp)
jmp 0x91320
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rdi
callq 0x8400
movq %rax, 0xa0(%rsp)
jmp 0x9133f
movq 0xa0(%rsp), %rdi
movq 0x3ec3a(%rip), %rsi     # 0xcff88
callq 0x8470
jmp 0x91355
leaq 0x660(%rsp), %rdi
callq 0xa1f50
jmp 0x91364
jmp 0x9138e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
leaq 0x660(%rsp), %rdi
callq 0xa1f50
jmp 0x91389
jmp 0x918f1
leaq 0x4112b(%rip), %rdi     # 0xd24c0
callq 0x945e0
movq %rax, 0x98(%rsp)
jmp 0x913a4
movq 0x98(%rsp), %rax
movq %rax, 0x658(%rsp)
leaq 0x4d0(%rsp), %rdi
callq 0x82a0
jmp 0x913c3
leaq 0x4e0(%rsp), %rdi
leaq 0x210fe(%rip), %rsi     # 0xb24d0
callq 0x8400
movq %rax, 0x90(%rsp)
jmp 0x913e1
movq 0x90(%rsp), %rdi
leaq 0x21100(%rip), %rsi     # 0xb24f0
callq 0x8400
movq %rax, 0x88(%rsp)
jmp 0x913ff
movq 0x88(%rsp), %rdi
leaq 0x210f6(%rip), %rsi     # 0xb2504
callq 0x8400
jmp 0x91415
cmpq $0x0, 0x658(%rsp)
jbe 0x914c8
leaq 0x4e0(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x41088(%rip), %rdi     # 0xd24c0
xorl %eax, %eax
movl %eax, %esi
callq 0x94600
movq %rax, 0x80(%rsp)
jmp 0x9144b
movq 0x80(%rsp), %rsi
leaq 0x4b0(%rsp), %rdi
callq 0x91920
jmp 0x91462
movq 0x78(%rsp), %rdi
leaq 0x4b0(%rsp), %rsi
callq 0x8350
jmp 0x91476
leaq 0x4b0(%rsp), %rdi
callq 0x88f8
jmp 0x91485
jmp 0x914c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
jmp 0x918e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
leaq 0x4b0(%rsp), %rdi
callq 0x88f8
jmp 0x914c3
jmp 0x918e0
movq $0x1, 0x4a8(%rsp)
movq 0x4a8(%rsp), %rax
cmpq 0x658(%rsp), %rax
jae 0x9159a
leaq 0x4e0(%rsp), %rdi
leaq 0x1f454(%rip), %rsi     # 0xb094d
callq 0x8400
movq %rax, 0x70(%rsp)
jmp 0x91505
movq 0x4a8(%rsp), %rsi
leaq 0x40fac(%rip), %rdi     # 0xd24c0
callq 0x94600
movq %rax, 0x68(%rsp)
jmp 0x91520
movq 0x68(%rsp), %rsi
leaq 0x488(%rsp), %rdi
callq 0x91920
jmp 0x91534
movq 0x70(%rsp), %rdi
leaq 0x488(%rsp), %rsi
callq 0x8350
jmp 0x91548
leaq 0x488(%rsp), %rdi
callq 0x88f8
jmp 0x91557
jmp 0x91559
movq 0x4a8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x4a8(%rsp)
jmp 0x914d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
leaq 0x488(%rsp), %rdi
callq 0x88f8
jmp 0x91595
jmp 0x918e0
leaq 0x4e0(%rsp), %rdi
leaq 0x1ffe3(%rip), %rsi     # 0xb158c
callq 0x8400
jmp 0x915b0
leaq 0x468(%rsp), %rdi
leaq 0x4d0(%rsp), %rsi
callq 0x83e0
jmp 0x915c7
leaq 0x468(%rsp), %rdi
callq 0x8190
movq %rax, 0x60(%rsp)
jmp 0x915db
leaq 0x43f(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x87f0
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x440(%rsp), %rdi
callq 0x85b0
jmp 0x91606
movq 0xf8(%rsp), %rdi
leaq 0x440(%rsp), %rsi
xorl %edx, %edx
callq 0x91b50
movq %rax, 0x50(%rsp)
jmp 0x91624
leaq 0x440(%rsp), %rdi
callq 0x88f8
jmp 0x91633
leaq 0x43f(%rsp), %rdi
callq 0x84a0
movq 0x50(%rsp), %rax
movq %rax, 0x460(%rsp)
cmpq $0x0, 0x460(%rsp)
je 0x916b9
movq 0x460(%rsp), %rax
cmpq $0x1, 0x30(%rax)
jne 0x916b9
movq 0x460(%rsp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x94620
movq %rax, 0x48(%rsp)
jmp 0x91683
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x94640
movq %rax, 0x40(%rsp)
jmp 0x91698
movq 0x40(%rsp), %rdi
callq 0x8190
movq %rax, 0x38(%rsp)
jmp 0x916a9
movq 0x38(%rsp), %rdi
callq 0x86a0
movl %eax, 0x34(%rsp)
jmp 0x916c1
xorl %eax, %eax
movl %eax, 0x34(%rsp)
jmp 0x916c1
movl 0x34(%rsp), %eax
cltq
movq %rax, 0x430(%rsp)
cmpq $0x0, 0x460(%rsp)
je 0x91795
movq 0x460(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x91707
movq 0x28(%rsp), %rdi
callq 0x8d100
jmp 0x916fd
movq 0x28(%rsp), %rdi
callq 0x83a0
jmp 0x91795
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
jmp 0x918cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
jmp 0x91760
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
leaq 0x440(%rsp), %rdi
callq 0x88f8
jmp 0x9175e
jmp 0x91760
leaq 0x43f(%rsp), %rdi
callq 0x84a0
jmp 0x918cf
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
callq 0x83a0
jmp 0x918cf
movq 0x430(%rsp), %rax
cmpq 0x658(%rsp), %rax
jne 0x917b7
movl $0x1, 0x42c(%rsp)
jmp 0x918a9
movq 0xf8(%rsp), %rdi
callq 0x92830
jmp 0x917c6
movq 0x430(%rsp), %rax
cmpq 0x658(%rsp), %rax
jbe 0x9189e
leaq 0x110(%rsp), %rdi
movl $0x2, %esi
callq 0xa1800
jmp 0x917f0
leaq 0x2280f(%rip), %rsi     # 0xb4006
leaq 0x110(%rsp), %rdi
callq 0x945b0
movq %rax, 0x20(%rsp)
jmp 0x9180b
movq 0x20(%rsp), %rdi
movq 0x658(%rsp), %rsi
callq 0x80c0
movq %rax, 0x18(%rsp)
jmp 0x91824
movq 0x18(%rsp), %rdi
leaq 0x20cfb(%rip), %rsi     # 0xb252b
callq 0x8400
movq %rax, 0x10(%rsp)
jmp 0x9183c
movq 0x10(%rsp), %rdi
movq 0x430(%rsp), %rsi
callq 0x80c0
movq %rax, 0x8(%rsp)
jmp 0x91855
movq 0x8(%rsp), %rdi
leaq 0x20ce8(%rip), %rsi     # 0xb2549
callq 0x8400
jmp 0x91868
leaq 0x110(%rsp), %rdi
callq 0xa1f50
jmp 0x91877
jmp 0x9189e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xca0(%rsp)
movl %eax, 0xc9c(%rsp)
leaq 0x110(%rsp), %rdi
callq 0xa1f50
jmp 0x9189c
jmp 0x918cf
movl $0x0, 0x42c(%rsp)
leaq 0x468(%rsp), %rdi
callq 0x88f8
jmp 0x918b8
leaq 0x4d0(%rsp), %rdi
callq 0x8900
jmp 0x918c7
addq $0xd48, %rsp            # imm = 0xD48
retq
leaq 0x468(%rsp), %rdi
callq 0x88f8
jmp 0x918de
jmp 0x918e0
leaq 0x4d0(%rsp), %rdi
callq 0x8900
jmp 0x918ef
jmp 0x918f1
movq 0x100(%rsp), %rdi
callq 0x88f8
jmp 0x91900
jmp 0x91902
movq 0xca0(%rsp), %rdi
callq 0x87d0
movq %rax, %rdi
callq 0x8cf60
nopw (%rax,%rax)