// File: /workspace/github_cpp_makefile_star2_O1/adamnovak[P]rlcsa/adaptive_samples.cpp
// Function: CSA::AdaptiveSamples::locate(unsigned int, bool)
usint
AdaptiveSamples::locate(usint i, bool steps)
{
  if(i >= this->index.getSize()) { return (steps ? 0 : this->index.getSize()); }

  usint offset = 0;
  sample_type sample(i, 0);
  this->index.convertToBWTIndex(i);

  while(true)
  {
    // Priority 1: Implicit samples.
    if(this->index.hasImplicitSample(i))
    {
      sample.second = this->index.getImplicitSample(i) - offset;
      break;
    }
    this->index.convertToSAIndex(i);

    // Priority 2: Regular samples.
    if(this->half_greedy && this->regular_samples->isSampled(i))
    {
      sample.second = this->regular_samples->getSampleAt(i) - offset;
      break;
    }

    // Priority 3: Primary samples.
    key_type j = this->hash(i);
    if(this->samples[j].first == i)
    {
      sample.second = this->samples[j].second - offset;
      break;
    }

    // Priority 4: Candidate samples.
    if(this->use_candidates)
    {
      j = this->secondaryHash(i);
      if(this->candidate_samples[j].first == i)
      {
        sample.second = this->candidate_samples[j].second - offset;
        this->addSample(this->candidate_samples[j]);
        break;
      }
    }

    i = this->index.psi(i); offset++;
  }

  if(offset > 0) { this->trySample(sample, offset); }

  // Update the average number of steps taken.
  if(this->promote_probability > 0.0)
  {
    this->sum = this->sum + offset - this->window[this->pos];
    this->window[this->pos] = offset; this->pos = (this->pos + 1) % this->window_size;
  }

  return (steps ? offset : sample.second);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq (%rdi), %rax
movl 0x4(%rax), %r12d
cmpl %esi, %r12d
jbe 0x14679
movq %rdi, %r14
movl %edx, 0x8(%rsp)
movl 0x820(%rax), %r15d
movl %esi, 0x4(%rsp)
addl %esi, %r15d
xorl %r12d, %r12d
leaq 0x10(%rsp), %r13
xorl %ebp, %ebp
movq (%r14), %rax
movl %r15d, %ebx
subl 0x820(%rax), %ebx
jb 0x14686
cmpb $0x1, 0x35(%r14)
jne 0x14533
movq 0x28(%r14), %rax
movq 0x10(%rax), %rsi
movq %r13, %rdi
callq 0x1c544
movq %r13, %rdi
movl %ebx, %esi
callq 0x1ccba
movl %eax, %r15d
movq %r13, %rdi
callq 0x1c54a
testb %r15b, %r15b
jne 0x146bd
movl %ebx, %eax
notl %eax
movl %ebx, %ecx
shll $0xf, %ecx
addl %eax, %ecx
movl %ecx, %eax
shrl $0xc, %eax
xorl %ecx, %eax
leal (%rax,%rax,4), %eax
movl %eax, %ecx
shrl $0x4, %ecx
xorl %eax, %ecx
imull $0x809, %ecx, %ecx      # imm = 0x809
movl %ecx, %eax
shrl $0x10, %eax
xorl %ecx, %eax
movl 0x10(%r14), %ecx
xorl %edx, %edx
divl %ecx
movq 0x8(%r14), %rax
cmpl %ebx, (%rax,%rdx,8)
jne 0x1457c
movl 0x4(%rax,%rdx,8), %r12d
subl %ebp, %r12d
xorl %eax, %eax
jmp 0x14669
cmpb $0x1, 0x34(%r14)
jne 0x145ee
movl %ebx, %eax
shll $0xc, %eax
addl %ebx, %eax
addl $0x7ed55d16, %eax       # imm = 0x7ED55D16
movl %eax, %edx
shrl $0x13, %edx
xorl %eax, %edx
xorl $0xc761c23c, %edx       # imm = 0xC761C23C
movl %edx, %eax
shll $0x5, %eax
leal (%rdx,%rax), %esi
shll $0x9, %esi
addl %edx, %eax
addl $0xe9f8cc1d, %eax       # imm = 0xE9F8CC1D
addl $0xaccf6200, %esi       # imm = 0xACCF6200
xorl %eax, %esi
leal (%rsi,%rsi,8), %edx
addl $0xfd7046c5, %edx       # imm = 0xFD7046C5
movl %edx, %eax
shrl $0x10, %eax
xorl %edx, %eax
xorl $0xb55a4f09, %eax       # imm = 0xB55A4F09
xorl %edx, %edx
divl %ecx
movq 0x18(%r14), %rax
cmpl %ebx, (%rax,%rdx,8)
jne 0x145ee
movl 0x4(%rax,%rdx,8), %r12d
subl %ebp, %r12d
movq (%rax,%rdx,8), %rsi
movq %r14, %rdi
callq 0x14040
jmp 0x14575
movq (%r14), %r15
movl 0x4(%r15), %eax
cmpl %ebx, %eax
jbe 0x1465c
movl %ebp, 0xc(%rsp)
movq 0x808(%r15), %rcx
movl %ebx, %eax
xorl %edx, %edx
divl 0x1008(%rcx)
movl 0xc08(%rcx,%rax,4), %eax
leaq (%rcx,%rax,4), %rax
addq $0x804, %rax            # imm = 0x804
movl (%rax), %ebp
addq $0x4, %rax
cmpl %ebx, 0x4(%rcx,%rbp,8)
jb 0x1461f
movq 0x8(%r15,%rbp,8), %rsi
movq %r13, %rdi
callq 0x1d194
movq 0x808(%r15), %rax
subl (%rax,%rbp,8), %ebx
movq %r13, %rdi
movl %ebx, %esi
callq 0x1d48a
movl %eax, %ebx
movq %r13, %rdi
callq 0x1d19a
movl 0xc(%rsp), %ebp
jmp 0x14665
addl 0x820(%r15), %eax
movl %eax, %ebx
incl %ebp
movb $0x1, %al
movl %ebx, %r15d
testb %al, %al
jne 0x144ec
jmp 0x1476f
xorl %eax, %eax
testb %dl, %dl
cmovnel %eax, %r12d
jmp 0x147ce
movq 0x828(%rax), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c544
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1c6f8
movl %eax, %r12d
leaq 0x10(%rsp), %rdi
callq 0x1c54a
subl %ebp, %r12d
incl %r12d
jmp 0x1476f
movq 0x28(%r14), %r12
movq 0x10(%r12), %rsi
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x1c544
xorl %r13d, %r13d
movq %r15, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x1c550
movl %eax, %ecx
decl %ecx
movq 0x18(%r12), %rsi
movl 0xc(%rsi), %r8d
imull %r8d, %ecx
movl %ecx, %edi
shrl $0x5, %edi
andl $0x1f, %ecx
movl $0x20, %edx
movl $0x20, %r9d
subl %ecx, %r9d
movl %r8d, %eax
subl %r9d, %eax
jae 0x14719
movl %r9d, %edx
movl %r8d, %eax
jmp 0x14730
movq (%rsi), %r8
movl %edi, %r9d
movl (%r8,%r9,4), %r13d
shll %cl, %r13d
shrl %cl, %r13d
movl %eax, %ecx
shll %cl, %r13d
incl %edi
testl %eax, %eax
je 0x1474d
subl %eax, %edx
movq (%rsi), %rcx
movl %edi, %esi
movl (%rcx,%rsi,4), %esi
movl %edx, %ecx
shrl %cl, %esi
negb %al
movl %eax, %ecx
shll %cl, %esi
shrl %cl, %esi
orl %esi, %r13d
imull 0x4(%r12), %r13d
movl 0x8(%r12), %r12d
decl %r12d
cmpl %r13d, %r12d
cmovael %r13d, %r12d
leaq 0x10(%rsp), %rdi
callq 0x1c54a
subl %ebp, %r12d
testl %ebp, %ebp
je 0x1478b
movq %r12, %rax
shlq $0x20, %rax
movl 0x4(%rsp), %esi
orq %rax, %rsi
movq %r14, %rdi
movl %ebp, %edx
callq 0x1488c
movsd 0x38(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl 0x8(%rsp), %esi
jbe 0x147c7
movl 0x48(%r14), %eax
addl %ebp, %eax
movq 0x50(%r14), %rcx
movl 0x44(%r14), %edx
subl (%rcx,%rdx,4), %eax
movl %eax, 0x48(%r14)
movl %ebp, (%rcx,%rdx,4)
movl 0x44(%r14), %eax
incl %eax
xorl %edx, %edx
divl 0x40(%r14)
movl %edx, 0x44(%r14)
testb %sil, %sil
cmovnel %ebp, %r12d
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x147f6
jmp 0x147f6
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1d19a
jmp 0x14803
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1c54a
movq %rbx, %rdi
callq 0x34b0
nop