// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/database.cpp
// Function: ash_sleep()
void ash_sleep() {
  Config & config = Config::instance();

  int retries = config.get_int("DB_MAX_RETRIES", -1);
  if (retries < 0) retries = 5;

  int fail_ms = config.get_int("DB_FAIL_TIMEOUT", -1);
  if (fail_ms < 0) fail_ms = 0;

  int random_ms = config.get_int("DB_FAIL_RANDOM_TIMEOUT", -1);
  if (random_ms < 0) random_ms = 0;

  // Sleep a number amount of ms within the parameters.
  unsigned long int ms = fail_ms;
  if (random_ms > 0) {
    // Randomize with both the time and the PID.  Since the database being
    // locked is likely a collission with another _ash_log - time along will
    // randomize both processes to generate the same random numbers.
    srand(time(0) ^ getpid());
    ms += rand() % random_ms;
  }
  LOG(INFO) << "Sleeping " << ms << " milliseconds.";
  if (ms == 0) return;

  // Measure a timestamp to count how long we actually slept.
  struct timespec before_ts, after_ts;
  if (clock_gettime(CLOCK_MONOTONIC, &before_ts)) {
    perror("clock_gettime failed");
  }

  unsigned int sleep_attempts = retries * 2;
  struct timespec to_sleep, remaining;
  to_sleep.tv_sec = ms / 1000L;
  to_sleep.tv_nsec = (ms % 1000L) * 1000000L;

  while (to_sleep.tv_sec || to_sleep.tv_nsec) {
    // Sanity check to make sure we aren't looping infinitely.
    if (sleep_attempts == 0) {
      LOG(WARNING) << "Sleep break triggered.  CLOCK_MONOTONIC may not work as "
                   << "expected on your system.  Or the database may be "
                   << "extremely busy with concurrent requests.";
      break;  // safety
    }
    --sleep_attempts;

    // Sleep and verify the return code.
    int rval = clock_nanosleep(CLOCK_MONOTONIC, 0, &to_sleep, &remaining);
    switch (rval) {
      case -1: 
        LOG(ERROR) << "Failed to clock_nanosleep: " << strerror(errno);
        continue;
      case 0: break;
      case EFAULT:
        LOG(ERROR) << "clock_nanosleep: EFAULT sleeping " << ms << " ms.";
        break;
      case EINTR:
        LOG(ERROR) << "clock_nanosleep: EINTR sleeping " << ms << " ms.";
        break;
      case EINVAL:
        LOG(ERROR) << "clock_nanosleep: EINVAL sleeping (tv_sec="
                   << to_sleep.tv_sec << ", tv_nsec="
                   << to_sleep.tv_nsec << ")";
        break;
      default:
        LOG(ERROR) << "Unexpected rval from clock_nanosleep: " << rval;
    }

    // If there is any time remaining, prepare to sleep again.
    if (remaining.tv_sec == 0
        && remaining.tv_nsec > 0
        && remaining.tv_nsec <= 999999999
        && remaining.tv_nsec < to_sleep.tv_nsec)
    {
      LOG(DEBUG) << "slept (to_sleep.tv_nsec - remaining.tv_nsec = "
                 << to_sleep.tv_nsec << " - " << remaining.tv_nsec << " = "
                 << (to_sleep.tv_nsec - remaining.tv_nsec) << ")";
      to_sleep.tv_sec = remaining.tv_sec;
      to_sleep.tv_nsec = remaining.tv_nsec;
    }
  }

  // Check to see how long has passed since ash_sleep began.
  if (clock_gettime(CLOCK_MONOTONIC, &after_ts)) {
    perror("clock_gettime failed");
  }
  
  unsigned long int slept =
    (after_ts.tv_sec - before_ts.tv_sec) * 1000 +
    (after_ts.tv_nsec - before_ts.tv_nsec) / 1000000L;

  LOG(INFO) << "Slept " << slept << " milliseconds.";

  if (ms > slept) {
    // This should never happen because of the while loop above.
    LOG(ERROR) << "Failed to sleep " << ms << " ms between failures.";
  }
  
  if (slept > (ms + 1) * 100) {
    LOG(ERROR) << "Major clock problem detected.  Requested sleep of: " << ms
               << " ms took " << slept << " ms to complete.";
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
callq 0x69cd0
movq %rax, %rbx
leaq 0x50(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x12a93(%rip), %rsi     # 0x7daf8
leaq 0x12a9a(%rip), %rdx     # 0x7db06
leaq 0x40(%rsp), %rdi
callq 0x68bce
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69bfc
movl %eax, %r14d
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b09a
callq 0x8390
leaq 0x40(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x12a5e(%rip), %rsi     # 0x7db07
leaq 0x12a66(%rip), %rdx     # 0x7db16
callq 0x68bce
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69bfc
movl %eax, %ebp
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b0d8
callq 0x8390
xorl %r12d, %r12d
testl %ebp, %ebp
cmovgl %ebp, %r12d
leaq 0x40(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x12a27(%rip), %rsi     # 0x7db17
leaq 0x12a36(%rip), %rdx     # 0x7db2d
callq 0x68bce
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69bfc
movl %eax, %ebp
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b11f
callq 0x8390
movl %r12d, %ebx
testl %ebp, %ebp
jle 0x6b14e
xorl %edi, %edi
callq 0x8370
movq %rax, %r15
callq 0x8590
xorl %eax, %r15d
movl %r15d, %edi
callq 0x83b0
callq 0x80d0
cltd
idivl %ebp
movslq %edx, %rax
addq %rax, %rbx
leaq 0x40(%rsp), %rdi
movl $0x1, %esi
callq 0x6fef2
leaq 0x48(%rsp), %r15
leaq 0x129c5(%rip), %rsi     # 0x7db2e
movl $0x9, %edx
movq %r15, %rdi
callq 0x8470
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8240
leaq 0x129b0(%rip), %rsi     # 0x7db38
movl $0xe, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
testq %rbx, %rbx
je 0x6b727
leaq 0x30(%rsp), %rsi
movl $0x1, %edi
callq 0x8360
testl %eax, %eax
jne 0x6b739
movq %rbx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
shrq $0x4, %rdx
movq %rdx, (%rsp)
imulq $0x3e8, %rdx, %rax      # imm = 0x3E8
movq %rbx, %rcx
subq %rax, %rcx
imulq $0xf4240, %rcx, %rax    # imm = 0xF4240
movq %rax, 0x8(%rsp)
cmpq $0x3e8, %rbx            # imm = 0x3E8
setb %al
testq %rcx, %rcx
sete %cl
testb %cl, %al
jne 0x6b5ad
leal (%r14,%r14), %eax
testl %r14d, %r14d
movl $0xa, %ebp
cmovnsl %eax, %ebp
leaq 0x48(%rsp), %r14
incl %ebp
movq %rsp, %r13
leaq 0x10(%rsp), %r15
decl %ebp
je 0x6b553
movl $0x1, %edi
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %rcx
callq 0x87b0
movl %eax, %r12d
cmpl $0x3, %eax
jle 0x6b2de
cmpl $0x4, %r12d
je 0x6b34a
cmpl $0xe, %r12d
je 0x6b392
cmpl $0x16, %r12d
jne 0x6b2f1
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
movl $0x29, %edx
leaq 0x48(%rsp), %rdi
leaq 0x129c6(%rip), %rsi     # 0x7dc54
callq 0x8470
movq (%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x83a0
movq %rax, %r12
movl $0xa, %edx
movq %rax, %rdi
leaq 0x129cb(%rip), %rsi     # 0x7dc7e
callq 0x8470
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x83a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x10081(%rip), %rsi     # 0x7b355
callq 0x8470
jmp 0x6b3d8
cmpl $-0x1, %r12d
je 0x6b4c7
testl %r12d, %r12d
je 0x6b3e2
movq %r13, %r15
movq %rbx, %r13
movq %r14, %rbx
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
movl $0x26, %edx
leaq 0x48(%rsp), %r14
movq %r14, %rdi
leaq 0x1296c(%rip), %rsi     # 0x7dc89
callq 0x8470
movq %r14, %rdi
movl %r12d, %esi
callq 0x86d0
leaq 0x40(%rsp), %rdi
callq 0x70392
movq %rbx, %r14
movq %r13, %rbx
movq %r15, %r13
leaq 0x10(%rsp), %r15
jmp 0x6b3e2
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
movl $0x20, %edx
leaq 0x48(%rsp), %rdi
leaq 0x128c9(%rip), %rsi     # 0x7dc33
callq 0x8470
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x8240
movl $0x4, %edx
movq %rax, %rdi
leaq 0x128a3(%rip), %rsi     # 0x7dc2e
callq 0x8470
jmp 0x6b3d8
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
movl $0x21, %edx
leaq 0x48(%rsp), %rdi
leaq 0x1285a(%rip), %rsi     # 0x7dc0c
callq 0x8470
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x8240
movl $0x4, %edx
movq %rax, %rdi
leaq 0x1285b(%rip), %rsi     # 0x7dc2e
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
cmpq $0x0, 0x10(%rsp)
sete %cl
movq 0x18(%rsp), %rdx
leaq -0x1(%rdx), %rax
cmpq $0x3b9ac9ff, %rax       # imm = 0x3B9AC9FF
setb %sil
movq 0x8(%rsp), %rax
cmpq %rax, %rdx
setl %dl
andb %cl, %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0x6b547
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x6fef2
movl $0x2e, %edx
movq %r14, %rdi
leaq 0x1287e(%rip), %rsi     # 0x7dcb0
callq 0x8470
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x83a0
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x12889(%rip), %rsi     # 0x7dcdf
callq 0x8470
movq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x83a0
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x12861(%rip), %rsi     # 0x7dcdb
callq 0x8470
movq 0x8(%rsp), %rsi
subq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x83a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xfeb5(%rip), %rsi      # 0x7b355
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
jmp 0x6b547
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
movl $0x1b, %edx
leaq 0x48(%rsp), %rdi
leaq 0x12709(%rip), %rsi     # 0x7dbf0
callq 0x8470
callq 0x8060
movl (%rax), %edi
callq 0x8150
testq %rax, %rax
je 0x6b51a
movq %rax, %r12
movq %rax, %rdi
callq 0x8190
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6b538
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x48(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl 0x68(%rsp,%rax), %esi
orl $0x1, %esi
callq 0x86b0
leaq 0x40(%rsp), %rdi
callq 0x70392
movq 0x8(%rsp), %rax
orq (%rsp), %rax
jne 0x6b22a
jmp 0x6b5ad
leaq 0x40(%rsp), %rdi
movl $0x2, %esi
callq 0x6fef2
leaq 0x48(%rsp), %r14
leaq 0x125ee(%rip), %rsi     # 0x7db5c
movl $0x38, %edx
movq %r14, %rdi
callq 0x8470
leaq 0x12613(%rip), %rsi     # 0x7db95
movl $0x31, %edx
movq %r14, %rdi
callq 0x8470
leaq 0x12631(%rip), %rsi     # 0x7dbc7
movl $0x28, %edx
movq %r14, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
leaq 0x20(%rsp), %rsi
movl $0x1, %edi
callq 0x8360
testl %eax, %eax
jne 0x6b74a
movq 0x20(%rsp), %r12
subq 0x30(%rsp), %r12
movq 0x28(%rsp), %rax
subq 0x38(%rsp), %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %r14
leaq 0x40(%rsp), %rdi
movl $0x1, %esi
callq 0x6fef2
leaq 0x48(%rsp), %r15
leaq 0x126e0(%rip), %rsi     # 0x7dce3
movl $0x6, %edx
movq %r15, %rdi
callq 0x8470
imulq $0x3e8, %r12, %rax      # imm = 0x3E8
movq %r14, %rcx
shrq $0x3f, %rcx
sarq $0x12, %r14
addq %rcx, %r14
addq %rax, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x8240
leaq 0x124fe(%rip), %rsi     # 0x7db38
movl $0xe, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
cmpq %r14, %rbx
jbe 0x6b6a7
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
leaq 0x48(%rsp), %r15
leaq 0x12679(%rip), %rsi     # 0x7dcea
movl $0x10, %edx
movq %r15, %rdi
callq 0x8470
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8240
leaq 0x1266b(%rip), %rsi     # 0x7dcfb
movl $0x15, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
imulq $0x64, %rbx, %rax
addq $0x64, %rax
cmpq %rax, %r14
jbe 0x6b727
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x6fef2
leaq 0x48(%rsp), %r15
leaq 0x12642(%rip), %rsi     # 0x7dd11
movl $0x33, %edx
movq %r15, %rdi
callq 0x8470
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8240
movq %rax, %rbx
leaq 0x12654(%rip), %rsi     # 0x7dd45
movl $0x9, %edx
movq %rax, %rdi
callq 0x8470
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8240
leaq 0x1263f(%rip), %rsi     # 0x7dd4f
movl $0x10, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70392
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12407(%rip), %rdi     # 0x7db47
callq 0x8300
jmp 0x6b1bf
leaq 0x123f6(%rip), %rdi     # 0x7db47
callq 0x8300
jmp 0x6b5c4
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
jmp 0x6b787
jmp 0x6b7a2
jmp 0x6b787
jmp 0x6b7a2
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b82f
callq 0x8390
jmp 0x6b82f
movq %rax, %rbx
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
jmp 0x6b82f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70392
movq %rbx, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68a31
nop