// File: /workspace/github_c_makefile_star2_O2/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 0x5d6d2
movq %rax, %rbx
leaq 0x11422(%rip), %rsi     # 0x6fa9e
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x8150
leaq 0x40(%rsp), %rsi
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x5d632
movl %eax, %r14d
leaq 0x40(%rsp), %rdi
callq 0x84b0
leaq 0x113fe(%rip), %rsi     # 0x6faad
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x8150
leaq 0x40(%rsp), %rsi
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x5d632
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
callq 0x84b0
xorl %r15d, %r15d
testl %ebp, %ebp
cmovgl %ebp, %r15d
leaq 0x113d3(%rip), %rsi     # 0x6fabd
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x8150
leaq 0x40(%rsp), %rsi
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x5d632
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
callq 0x84b0
movl %r15d, %ebx
testl %ebp, %ebp
jle 0x5e744
xorl %edi, %edi
callq 0x8350
movq %rax, %r15
callq 0x8580
xorl %eax, %r15d
movl %r15d, %edi
callq 0x8390
callq 0x80c0
cltd
idivl %ebp
movslq %edx, %rax
addq %rax, %rbx
leaq 0x40(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x11377(%rip), %rsi     # 0x6fad4
callq 0x83a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8220
leaq 0x1136a(%rip), %rsi     # 0x6fade
movq %rax, %rdi
callq 0x83a0
leaq 0x40(%rsp), %rdi
callq 0x62fc8
testq %rbx, %rbx
je 0x5eb9e
leal (%r14,%r14), %r13d
pushq $0x1
popq %rdi
leaq 0x20(%rsp), %rsi
callq 0x8340
testl %eax, %eax
jne 0x5ebb0
testl %r14d, %r14d
pushq $0xa
popq %rax
cmovsl %eax, %r13d
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
movq %rsp, %r14
movq %rax, (%r14)
imulq $0xf4240, %rdx, %rcx    # imm = 0xF4240
movq %rcx, 0x8(%r14)
leaq 0x10(%rsp), %rbp
leaq 0x40(%rsp), %r15
orq %rax, %rcx
je 0x5ea6c
subl $0x1, %r13d
jb 0x5ea26
pushq $0x1
popq %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x8780
cmpl $0x16, %eax
je 0x5e8fd
movl %eax, %r12d
testl %eax, %eax
je 0x5e958
movq %r15, %rdi
pushq $0x3
popq %rsi
cmpl $0x4, %r12d
je 0x5e8a8
cmpl $0xe, %r12d
je 0x5e873
cmpl $-0x1, %r12d
jne 0x5e8da
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x11354(%rip), %rsi     # 0x6fb96
callq 0x83a0
movq %rax, %r12
callq 0x8050
movl (%rax), %edi
callq 0x8120
movq %r12, %rdi
movq %rax, %rsi
callq 0x83a0
movq %r15, %rdi
callq 0x62fc8
movq 0x8(%rsp), %rcx
jmp 0x5ea1d
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x1132e(%rip), %rsi     # 0x6fbb2
callq 0x83a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8220
movq %rax, %rdi
leaq 0x11336(%rip), %rsi     # 0x6fbd4
callq 0x83a0
jmp 0x5e950
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x11320(%rip), %rsi     # 0x6fbd9
callq 0x83a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8220
movq %rax, %rdi
leaq 0x11301(%rip), %rsi     # 0x6fbd4
callq 0x83a0
jmp 0x5e950
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x11344(%rip), %rsi     # 0x6fc2f
callq 0x83a0
movq %rax, %rdi
movl %r12d, %esi
callq 0x86a0
jmp 0x5e950
movq %r15, %rdi
pushq $0x3
popq %rsi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x112e6(%rip), %rsi     # 0x6fbfa
callq 0x83a0
movq (%rsp), %rsi
movq %rax, %rdi
callq 0x8380
movq %rax, %rdi
leaq 0x112f5(%rip), %rsi     # 0x6fc24
callq 0x83a0
movq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x8380
movq %rax, %rdi
leaq 0xea2a(%rip), %rsi      # 0x6d375
callq 0x83a0
movq %r15, %rdi
callq 0x62fc8
cmpq $0x0, 0x10(%rsp)
sete %al
movq 0x18(%rsp), %rdx
leaq -0x1(%rdx), %rcx
cmpq $0x3b9ac9ff, %rcx       # imm = 0x3B9AC9FF
setb %sil
movq 0x8(%rsp), %rcx
cmpq %rcx, %rdx
setl %dl
andb %al, %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0x5ea1d
movq %r15, %rdi
xorl %esi, %esi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x112b2(%rip), %rsi     # 0x6fc56
callq 0x83a0
movq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x8380
movq %rax, %rdi
leaq 0x112c5(%rip), %rsi     # 0x6fc85
callq 0x83a0
movq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x8380
movq %rax, %rdi
leaq 0x112a5(%rip), %rsi     # 0x6fc81
callq 0x83a0
movq 0x8(%rsp), %rsi
subq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x8380
movq %rax, %rdi
leaq 0xe978(%rip), %rsi      # 0x6d375
callq 0x83a0
movq %r15, %rdi
callq 0x62fc8
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rsp)
movq %rcx, 0x8(%rsp)
movq (%rsp), %rax
jmp 0x5e7da
leaq 0x40(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x110c3(%rip), %rsi     # 0x6fb02
callq 0x83a0
leaq 0x110f0(%rip), %rsi     # 0x6fb3b
movq %rax, %rdi
callq 0x83a0
leaq 0x11113(%rip), %rsi     # 0x6fb6d
movq %rax, %rdi
callq 0x83a0
leaq 0x40(%rsp), %rdi
callq 0x62fc8
pushq $0x1
popq %rdi
leaq 0x30(%rsp), %rsi
callq 0x8340
testl %eax, %eax
jne 0x5ebc1
movq 0x30(%rsp), %r15
subq 0x20(%rsp), %r15
movq 0x38(%rsp), %rax
subq 0x28(%rsp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, %r14
leaq 0x40(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x111ce(%rip), %rsi     # 0x6fc89
callq 0x83a0
imulq $0x3e8, %r15, %rcx      # imm = 0x3E8
addq %rcx, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x8220
leaq 0x11002(%rip), %rsi     # 0x6fade
movq %rax, %rdi
callq 0x83a0
leaq 0x40(%rsp), %rdi
callq 0x62fc8
cmpq %r14, %rbx
jbe 0x5eb35
leaq 0x40(%rsp), %rdi
pushq $0x3
popq %rsi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x11184(%rip), %rsi     # 0x6fc90
callq 0x83a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8220
leaq 0x1117e(%rip), %rsi     # 0x6fca1
movq %rax, %rdi
callq 0x83a0
leaq 0x40(%rsp), %rdi
callq 0x62fc8
imulq $0x64, %rbx, %rax
addq $0x64, %rax
cmpq %rax, %r14
jbe 0x5eb9e
leaq 0x40(%rsp), %rdi
pushq $0x3
popq %rsi
callq 0x62c92
leaq 0x48(%rsp), %rdi
leaq 0x1115c(%rip), %rsi     # 0x6fcb7
callq 0x83a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8220
leaq 0x11179(%rip), %rsi     # 0x6fceb
movq %rax, %rdi
callq 0x83a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x8220
leaq 0x11169(%rip), %rsi     # 0x6fcf5
movq %rax, %rdi
callq 0x83a0
leaq 0x40(%rsp), %rdi
callq 0x62fc8
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10f36(%rip), %rdi     # 0x6faed
callq 0x82d0
jmp 0x5e7a8
leaq 0x10f25(%rip), %rdi     # 0x6faed
callq 0x82d0
jmp 0x5ea81
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x84b0
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x84b0
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x84b0
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
jmp 0x5ec4e
jmp 0x5ec4e
movq %rax, %rbx
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
jmp 0x5ecba
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x62fc8
movq %rbx, %rdi
callq 0x86f0
movq %rax, %rdi
callq 0x5c505