// File: /workspace/github_c_makefile_star2_O1/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 0x69ef2
movq %rax, %rbx
leaq 0x50(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x127db(%rip), %rsi     # 0x7daa8
leaq 0x127e2(%rip), %rdx     # 0x7dab6
leaq 0x40(%rsp), %rdi
callq 0x68de4
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69e16
movl %eax, %r14d
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b302
callq 0x8390
leaq 0x40(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x127a6(%rip), %rsi     # 0x7dab7
leaq 0x127ae(%rip), %rdx     # 0x7dac6
callq 0x68de4
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69e16
movl %eax, %ebp
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b340
callq 0x8390
xorl %r12d, %r12d
testl %ebp, %ebp
cmovgl %ebp, %r12d
leaq 0x40(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x1276f(%rip), %rsi     # 0x7dac7
leaq 0x1277e(%rip), %rdx     # 0x7dadd
callq 0x68de4
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x69e16
movl %eax, %ebp
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6b387
callq 0x8390
movl %r12d, %ebx
testl %ebp, %ebp
jle 0x6b3b6
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 0x70186
leaq 0x48(%rsp), %r15
leaq 0x1270d(%rip), %rsi     # 0x7dade
movl $0x9, %edx
movq %r15, %rdi
callq 0x8470
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8240
leaq 0x126f8(%rip), %rsi     # 0x7dae8
movl $0xe, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
testq %rbx, %rbx
je 0x6b97a
leaq 0x30(%rsp), %rsi
movl $0x1, %edi
callq 0x8360
testl %eax, %eax
jne 0x6b98c
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 0x6b800
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 0x6b7a6
movl $0x1, %edi
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %rcx
callq 0x87b0
movl %eax, %r12d
cmpl $0x3, %eax
jle 0x6b546
cmpl $0x4, %r12d
je 0x6b5b2
cmpl $0xe, %r12d
je 0x6b5fa
cmpl $0x16, %r12d
jne 0x6b559
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
movl $0x29, %edx
leaq 0x48(%rsp), %rdi
leaq 0x1270e(%rip), %rsi     # 0x7dc04
callq 0x8470
movq (%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x83a0
movq %rax, %r12
movl $0xa, %edx
movq %rax, %rdi
leaq 0x12713(%rip), %rsi     # 0x7dc2e
callq 0x8470
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x83a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xfe19(%rip), %rsi      # 0x7b355
callq 0x8470
jmp 0x6b640
cmpl $-0x1, %r12d
je 0x6b71a
testl %r12d, %r12d
je 0x6b64a
movq %r13, %r15
movq %rbx, %r13
movq %r14, %rbx
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
movl $0x26, %edx
leaq 0x48(%rsp), %r14
movq %r14, %rdi
leaq 0x126b4(%rip), %rsi     # 0x7dc39
callq 0x8470
movq %r14, %rdi
movl %r12d, %esi
callq 0x86d0
leaq 0x40(%rsp), %rdi
callq 0x70626
movq %rbx, %r14
movq %r13, %rbx
movq %r15, %r13
leaq 0x10(%rsp), %r15
jmp 0x6b64a
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
movl $0x20, %edx
leaq 0x48(%rsp), %rdi
leaq 0x12611(%rip), %rsi     # 0x7dbe3
callq 0x8470
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x8240
movl $0x4, %edx
movq %rax, %rdi
leaq 0x125eb(%rip), %rsi     # 0x7dbde
callq 0x8470
jmp 0x6b640
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
movl $0x21, %edx
leaq 0x48(%rsp), %rdi
leaq 0x125a2(%rip), %rsi     # 0x7dbbc
callq 0x8470
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x8240
movl $0x4, %edx
movq %rax, %rdi
leaq 0x125a3(%rip), %rsi     # 0x7dbde
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
cmpq $0x0, 0x10(%rsp)
jne 0x6b795
movq 0x18(%rsp), %rax
leaq -0x1(%rax), %rcx
cmpq $0x3b9ac9fe, %rcx       # imm = 0x3B9AC9FE
ja 0x6b795
cmpq 0x8(%rsp), %rax
jge 0x6b795
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x70186
movl $0x2e, %edx
movq %r14, %rdi
leaq 0x125ce(%rip), %rsi     # 0x7dc60
callq 0x8470
movq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x83a0
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x125d9(%rip), %rsi     # 0x7dc8f
callq 0x8470
movq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x83a0
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x125b1(%rip), %rsi     # 0x7dc8b
callq 0x8470
movq 0x8(%rsp), %rsi
subq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x83a0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xfc55(%rip), %rsi      # 0x7b355
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
movaps 0x10(%rsp), %xmm0
movaps %xmm0, (%rsp)
jmp 0x6b795
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
movl $0x1b, %edx
leaq 0x48(%rsp), %rdi
leaq 0x12466(%rip), %rsi     # 0x7dba0
callq 0x8470
callq 0x8060
movl (%rax), %edi
callq 0x8150
testq %rax, %rax
je 0x6b76d
movq %rax, %r12
movq %rax, %rdi
callq 0x8190
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x8470
jmp 0x6b78b
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 0x70626
movq 0x8(%rsp), %rax
orq (%rsp), %rax
jne 0x6b492
jmp 0x6b800
leaq 0x40(%rsp), %rdi
movl $0x2, %esi
callq 0x70186
leaq 0x48(%rsp), %r14
leaq 0x1234b(%rip), %rsi     # 0x7db0c
movl $0x38, %edx
movq %r14, %rdi
callq 0x8470
leaq 0x12370(%rip), %rsi     # 0x7db45
movl $0x31, %edx
movq %r14, %rdi
callq 0x8470
leaq 0x1238e(%rip), %rsi     # 0x7db77
movl $0x28, %edx
movq %r14, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
leaq 0x20(%rsp), %rsi
movl $0x1, %edi
callq 0x8360
testl %eax, %eax
jne 0x6b99d
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 0x70186
leaq 0x48(%rsp), %r15
leaq 0x1243d(%rip), %rsi     # 0x7dc93
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 0x1225b(%rip), %rsi     # 0x7dae8
movl $0xe, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
cmpq %r14, %rbx
jbe 0x6b8fa
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
leaq 0x48(%rsp), %r15
leaq 0x123d6(%rip), %rsi     # 0x7dc9a
movl $0x10, %edx
movq %r15, %rdi
callq 0x8470
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8240
leaq 0x123c8(%rip), %rsi     # 0x7dcab
movl $0x15, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
imulq $0x64, %rbx, %rax
addq $0x64, %rax
cmpq %rax, %r14
jbe 0x6b97a
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x70186
leaq 0x48(%rsp), %r15
leaq 0x1239f(%rip), %rsi     # 0x7dcc1
movl $0x33, %edx
movq %r15, %rdi
callq 0x8470
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8240
movq %rax, %rbx
leaq 0x123b1(%rip), %rsi     # 0x7dcf5
movl $0x9, %edx
movq %rax, %rdi
callq 0x8470
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8240
leaq 0x1239c(%rip), %rsi     # 0x7dcff
movl $0x10, %edx
movq %rax, %rdi
callq 0x8470
leaq 0x40(%rsp), %rdi
callq 0x70626
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12164(%rip), %rdi     # 0x7daf7
callq 0x8300
jmp 0x6b427
leaq 0x12153(%rip), %rdi     # 0x7daf7
callq 0x8300
jmp 0x6b817
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
jmp 0x6b9da
jmp 0x6b9f5
jmp 0x6b9da
jmp 0x6b9f5
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x6ba82
callq 0x8390
jmp 0x6ba82
movq %rax, %rbx
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
jmp 0x6ba82
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x70626
movq %rbx, %rdi
callq 0x8720
movq %rax, %rdi
callq 0x68c53