// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/_ash_log.cpp
// Function: main
int main(int argc, char ** argv) {
  if (getenv("ASH_DISABLED")) return FLAGS_exit;

  // Load the config from the environment.
  Config & config = Config::instance();

  // Log the complete command, if debugging.
  stringstream ss;
  ss << "argv = '[0]='" << argv[0] << "'";
  for (int i = 1; i < argc; ++i)
    ss << ",[" << i << "]='" << argv[i] << "'";
  LOG(DEBUG) << ss.str();

  // Show usage if executed with no args.
  if (argc == 1 && !config.sets("HIDE_USAGE_FOR_NO_ARGS")) {
    Flag::parse(&argc, &argv, true);  // Sets the prog name in help output.
    usage(cerr);
  }

  // Parse the flags.
  Flag::parse(&argc, &argv, true);

  // Display the version and stop: -V
  if (FLAGS_version) {
    cout << ASH_VERSION << endl;
    return 0;
  }

  // Display a user alert: -a 'My alert'
  if (!FLAGS_alert.empty()) {
    cerr << FLAGS_alert << endl;
  }

  // Get the filename backing the history database.
  string db_file = config.get_string("HISTORY_DB");
  if (db_file == "") {
    usage(cerr << "\nExpected ASH_CFG_HISTORY_DB to be defined.");
  }

  // Register the tables expected in the program.
  Session::register_table();
  Command::register_table();

  // Emit the current session number, inserting one if none exists: -S
  if (FLAGS_get_session_id) {
    Database db = Database(db_file);
    stringstream ss;
    char * id = getenv("ASH_SESSION_ID");
    if (id) {
      ss << "select count(*) as session_cnt from sessions where id = " << id
         << " and duration is null;";
      ResultSet * rs = db.exec(ss.str());
      if (!rs || rs -> rows != 1) {
        cerr << "ERROR: session_id(" << id << ") not found, "
             << "creating new session." << endl << ss.str() << endl;
        id = 0;
      }
      ss.str("");
    }

    if (id) {
      cout << id << endl;
    } else {
      Session session;
      cout << db.insert(&session) << endl;
    }
  }

  // Insert a command into the DB if there's a command to insert.
  const bool command_flag_used = !FLAGS_command.empty()
    || FLAGS_command_exit
    || !FLAGS_command_pipe_status.empty()
    || FLAGS_command_start
    || FLAGS_command_finish
    || FLAGS_command_number;

  if (command_flag_used) {
    Database db = Database(db_file);
    Command com(FLAGS_command, FLAGS_command_exit, FLAGS_command_start,
      FLAGS_command_finish, FLAGS_command_number, FLAGS_command_pipe_status);
    db.insert(&com);
  }

  // End the current session in the DB: -E
  if (FLAGS_end_session) {
    char * id = getenv("ASH_SESSION_ID");
    if (id == NULL) {
      LOG(ERROR) << "Can't end the current session: ASH_SESSION_ID undefined.";
    } else {
      Session session;
      Database db = Database(db_file);
      db.exec(session.get_close_session_sql());
    }
  }

  // Set the exit code to match what the previous command exited: -e 123
  return FLAGS_exit;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5a8, %rsp            # imm = 0x5A8
movl %edi, 0x14(%rsp)
movq %rsi, 0x18(%rsp)
leaq 0xff44(%rip), %rdi      # 0x6a720
callq 0x7460
testq %rax, %rax
je 0x5a7f1
movl 0x26ab4(%rip), %ebx     # 0x812a0
jmp 0x5ada7
callq 0x5bf8a
movq %rax, %rbx
leaq 0x420(%rsp), %rdi
callq 0x7240
leaq 0x430(%rsp), %r14
leaq 0xff18(%rip), %rsi      # 0x6a72d
movq %r14, %rdi
callq 0x7370
movq 0x18(%rsp), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x7370
leaq 0x10a69(%rip), %rsi     # 0x6b29d
movq %rax, %rdi
callq 0x7370
pushq $0x1
popq %r15
leaq 0xfef4(%rip), %r12      # 0x6a73b
leaq 0xfee9(%rip), %r13      # 0x6a737
leaq 0x10a48(%rip), %rbp     # 0x6b29d
movslq 0x14(%rsp), %rax
cmpq %rax, %r15
jge 0x5a8a1
movq %r14, %rdi
movq %r12, %rsi
callq 0x7370
movq %rax, %rdi
movl %r15d, %esi
callq 0x7630
movq %rax, %rdi
movq %r13, %rsi
callq 0x7370
movq 0x18(%rsp), %rcx
movq (%rcx,%r15,8), %rsi
movq %rax, %rdi
callq 0x7370
movq %rax, %rdi
movq %rbp, %rsi
callq 0x7370
incq %r15
jmp 0x5a855
leaq 0x108(%rsp), %rdi
xorl %esi, %esi
callq 0x5fe62
leaq 0x438(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x75b0
leaq 0x110(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x72f0
leaq 0xc8(%rsp), %rdi
callq 0x7470
leaq 0x108(%rsp), %rdi
callq 0x60198
cmpl $0x1, 0x14(%rsp)
jne 0x5a93e
leaq 0xfe3c(%rip), %rsi      # 0x6a73e
leaq 0x108(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x7130
leaq 0x108(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5be80
movl %eax, %ebp
leaq 0x108(%rsp), %rdi
callq 0x7470
testb %bpl, %bpl
je 0x5adbb
leaq 0x14(%rsp), %rdi
leaq 0x18(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x5e402
cmpb $0x1, 0x26981(%rip)     # 0x812d8
jne 0x5a97b
movq 0x24660(%rip), %rdi     # 0x7efc0
leaq 0xfdee(%rip), %rsi      # 0x6a755
callq 0x7370
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x7180
jmp 0x5ad9a
cmpq $0x0, 0x26725(%rip)     # 0x810a8
je 0x5a9a0
movq 0x24664(%rip), %rdi     # 0x7eff0
leaq 0x2670d(%rip), %rsi     # 0x810a0
callq 0x72f0
movq %rax, %rdi
callq 0x7180
leaq 0xfdb4(%rip), %rsi      # 0x6a75b
leaq 0x108(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x7130
leaq 0xfd35(%rip), %rsi      # 0x6a6f5
leaq 0x20(%rsp), %rdi
leaq 0x13(%rsp), %rdx
callq 0x7130
leaq 0x58(%rsp), %rdi
leaq 0x108(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %rbx, %rsi
callq 0x5bf4a
leaq 0x20(%rsp), %rdi
callq 0x7470
leaq 0x108(%rsp), %rdi
callq 0x7470
leaq 0xfcee(%rip), %rsi      # 0x6a6f5
leaq 0x58(%rsp), %rdi
callq 0x5afac
testb %al, %al
jne 0x5add9
callq 0x60294
callq 0x5afbc
cmpb $0x1, 0x268e6(%rip)     # 0x81310
jne 0x5abe3
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5c29c
leaq 0x108(%rsp), %rdi
callq 0x7240
leaq 0xfd3f(%rip), %rdi      # 0x6a792
callq 0x7460
testq %rax, %rax
je 0x5ab92
movq %rax, %r14
leaq 0x118(%rsp), %rdi
leaq 0xfd2e(%rip), %rsi      # 0x6a7a1
callq 0x7370
movq %rax, %rdi
movq %r14, %rsi
callq 0x7370
leaq 0xfd50(%rip), %rsi      # 0x6a7da
movq %rax, %rdi
callq 0x7370
leaq 0x120(%rsp), %rbx
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x75b0
leaq 0x78(%rsp), %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x5c750
movq %rax, %r15
leaq 0x20(%rsp), %rdi
callq 0x7470
testq %r15, %r15
je 0x5aad1
cmpq $0x1, 0x30(%r15)
je 0x5ab47
movq 0x24518(%rip), %rdi     # 0x7eff0
leaq 0xfd12(%rip), %rsi      # 0x6a7f1
callq 0x7370
movq %rax, %rdi
movq %r14, %rsi
callq 0x7370
leaq 0xfd0e(%rip), %rsi      # 0x6a804
movq %rax, %rdi
callq 0x7370
leaq 0xfd0d(%rip), %rsi      # 0x6a812
movq %rax, %rdi
callq 0x7370
movq %rax, %rdi
callq 0x7180
movq %rax, %r14
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x75b0
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x72f0
movq %rax, %rdi
callq 0x7180
leaq 0x20(%rsp), %rdi
callq 0x7470
xorl %r14d, %r14d
leaq 0xfba7(%rip), %rsi      # 0x6a6f5
leaq 0x20(%rsp), %rdi
leaq 0x13(%rsp), %rdx
callq 0x7130
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x7550
leaq 0x20(%rsp), %rdi
callq 0x7470
testq %r14, %r14
je 0x5ab92
movq 0x24440(%rip), %rdi     # 0x7efc0
movq %r14, %rsi
callq 0x7370
movq %rax, %rdi
callq 0x7180
jmp 0x5abcc
leaq 0x20(%rsp), %rdi
callq 0x60488
leaq 0x78(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x5d5da
movq 0x2440e(%rip), %rdi     # 0x7efc0
movq %rax, %rsi
callq 0x7350
movq %rax, %rdi
callq 0x7180
leaq 0x20(%rsp), %rdi
callq 0x60b90
leaq 0x108(%rsp), %rdi
callq 0x7738
leaq 0x78(%rsp), %rdi
callq 0x5ce18
cmpq $0x0, 0x2650d(%rip)     # 0x810f8
jne 0x5ac1f
cmpl $0x0, 0x2657c(%rip)     # 0x81170
jne 0x5ac1f
cmpq $0x0, 0x26582(%rip)     # 0x81180
jne 0x5ac1f
cmpl $0x0, 0x265f1(%rip)     # 0x811f8
jne 0x5ac1f
cmpl $0x0, 0x26620(%rip)     # 0x81230
jne 0x5ac1f
cmpl $0x0, 0x2664f(%rip)     # 0x81268
je 0x5acd9
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5c29c
leaq 0x264bb(%rip), %rsi     # 0x810f0
leaq 0xa8(%rsp), %rdi
callq 0x74c0
movl 0x26528(%rip), %ebx     # 0x81170
movl 0x265aa(%rip), %ebp     # 0x811f8
movl 0x265db(%rip), %r14d    # 0x81230
movl 0x2660c(%rip), %r15d    # 0x81268
leaq 0x26515(%rip), %rsi     # 0x81178
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
callq 0x74c0
movq %r12, (%rsp)
leaq 0x108(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
movl %ebx, %edx
movl %ebp, %ecx
movl %r14d, %r8d
movl %r15d, %r9d
callq 0x5b184
leaq 0xe8(%rsp), %rdi
callq 0x7470
leaq 0xa8(%rsp), %rdi
callq 0x7470
leaq 0x20(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x5d5da
leaq 0x108(%rsp), %rdi
callq 0x5b8a2
leaq 0x20(%rsp), %rdi
callq 0x5ce18
cmpb $0x1, 0x26668(%rip)     # 0x81348
jne 0x5ad8a
leaq 0xfaa5(%rip), %rdi      # 0x6a792
callq 0x7460
testq %rax, %rax
je 0x5ad59
leaq 0x108(%rsp), %rdi
callq 0x60488
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x5c29c
leaq 0x78(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x60be6
leaq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
xorl %edx, %edx
callq 0x5c750
leaq 0x78(%rsp), %rdi
callq 0x7470
leaq 0x20(%rsp), %rdi
callq 0x5ce18
leaq 0x108(%rsp), %rdi
callq 0x60b90
jmp 0x5ad8a
leaq 0x108(%rsp), %rdi
pushq $0x3
popq %rsi
callq 0x5fe62
leaq 0x110(%rsp), %rdi
leaq 0xfab0(%rip), %rsi      # 0x6a828
callq 0x7370
leaq 0x108(%rsp), %rdi
callq 0x60198
movl 0x26510(%rip), %ebx     # 0x812a0
leaq 0x58(%rsp), %rdi
callq 0x7470
leaq 0x420(%rsp), %rdi
callq 0x7738
movl %ebx, %eax
addq $0x5a8, %rsp            # imm = 0x5A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14(%rsp), %rdi
leaq 0x18(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x5e402
movq 0x2421c(%rip), %rdi     # 0x7eff0
callq 0x5a78c
movq 0x24210(%rip), %rdi     # 0x7eff0
leaq 0xf97f(%rip), %rsi      # 0x6a766
callq 0x7370
movq %rax, %rdi
callq 0x5a78c
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x60198
jmp 0x5af5f
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x7470
jmp 0x5ae6a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7470
jmp 0x5aef8
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7470
jmp 0x5aef8
jmp 0x5af5c
jmp 0x5aef5
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7470
jmp 0x5aef8
jmp 0x5aef5
jmp 0x5af5c
movq %rax, %rbx
jmp 0x5ae74
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5ce18
leaq 0x108(%rsp), %rdi
callq 0x60b90
jmp 0x5af5f
jmp 0x5aef5
jmp 0x5aef5
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x5b8a2
jmp 0x5af50
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x7470
jmp 0x5af16
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x60b90
jmp 0x5aef8
jmp 0x5af81
jmp 0x5af5c
movq %rax, %rbx
jmp 0x5af05
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7470
jmp 0x5af2a
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x7470
jmp 0x5af84
jmp 0x5aef5
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x7738
leaq 0x78(%rsp), %rdi
callq 0x5ce18
jmp 0x5af5f
jmp 0x5af5c
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x7470
jmp 0x5af50
jmp 0x5af81
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x7470
jmp 0x5af84
jmp 0x5af81
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x7470
jmp 0x5af70
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5ce18
jmp 0x5af5f
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x7470
jmp 0x5af84
jmp 0x5af81
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x60198
jmp 0x5af84
jmp 0x5af81
movq %rax, %rbx
leaq 0x420(%rsp), %rdi
callq 0x7738
movq %rbx, %rdi
callq 0x7670
movq %rax, %rdi
callq 0x5afa1