// File: /workspace/github_cpp_makefile_star2_O3/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), %rbx
movl 0x4(%rbx), %r12d
cmpl %esi, %r12d
jbe 0x149b1
movq %rdi, %r14
movl %edx, 0xc(%rsp)
movl 0x820(%rbx), %eax
xorl %ebp, %ebp
movl %eax, %r12d
movl %esi, 0x8(%rsp)
addl %esi, %r12d
jb 0x1491d
xorl %ebp, %ebp
leaq 0x10(%rsp), %r13
subl %eax, %r12d
cmpb $0x1, 0x35(%r14)
jne 0x147ee
movq 0x28(%r14), %rax
movq 0x10(%rax), %rsi
movq %r13, %rdi
callq 0x1c828
movq %r13, %rdi
movl %r12d, %esi
callq 0x1d0ee
movl %eax, %ebx
movq %r13, %rdi
callq 0x1c82e
testb %bl, %bl
jne 0x149d8
movl %r12d, %eax
notl %eax
movl %r12d, %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 %r12d, (%rax,%rdx,8)
je 0x149ce
cmpb $0x1, 0x34(%r14)
jne 0x14892
movl %r12d, %eax
shll $0xc, %eax
addl %r12d, %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 %r12d, (%rax,%rdx,8)
je 0x14a33
movq (%r14), %rbx
movl 0x4(%rbx), %ecx
cmpl %r12d, %ecx
jbe 0x14907
movq 0x808(%rbx), %rcx
movl %r12d, %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), %r15d
addq $0x4, %rax
cmpl %r12d, 0x4(%rcx,%r15,8)
jb 0x148c0
movq 0x8(%rbx,%r15,8), %rsi
movq %r13, %rdi
callq 0x1d4c4
movq 0x808(%rbx), %rax
subl (%rax,%r15,8), %r12d
movq %r13, %rdi
movl %r12d, %esi
callq 0x1d7b0
movl %eax, %r12d
movq %r13, %rdi
callq 0x1d4ca
movq (%r14), %rbx
movl 0x820(%rbx), %eax
jmp 0x14912
movl 0x820(%rbx), %eax
addl %eax, %ecx
movl %ecx, %r12d
incl %ebp
cmpl %r12d, %eax
jbe 0x147b7
movq 0x828(%rbx), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c828
movq %rbx, %rdi
movl %r12d, %esi
callq 0x1c9d6
movl %eax, %r12d
incl %r12d
leaq 0x10(%rsp), %rdi
callq 0x1c82e
movl 0xc(%rsp), %ebx
movl 0x8(%rsp), %ecx
subl %ebp, %r12d
testl %ebp, %ebp
je 0x14971
movq %r12, %rax
shlq $0x20, %rax
movl %ecx, %esi
orq %rax, %rsi
movq %r14, %rdi
movl %ebp, %edx
callq 0x14b3e
movsd 0x38(%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x149a9
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 %bl, %bl
cmovnel %ebp, %r12d
jmp 0x149b9
xorl %eax, %eax
testb %dl, %dl
cmovnel %eax, %r12d
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rax,%rdx,8), %r12d
jmp 0x1494c
movq 0x28(%r14), %r15
movq 0x10(%r15), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c828
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x1c834
movl %eax, %ecx
decl %ecx
movq 0x18(%r15), %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 0x14a49
movl %r9d, %edx
movl %r8d, %eax
jmp 0x14a60
movl 0x4(%rax,%rdx,8), %r12d
movq (%rax,%rdx,8), %rsi
movq %r14, %rdi
callq 0x14304
jmp 0x1494c
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 0x14a7d
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(%r15), %r13d
movl 0x8(%r15), %r12d
decl %r12d
cmpl %r13d, %r12d
cmovael %r13d, %r12d
jmp 0x14942
jmp 0x14aa8
jmp 0x14aa8
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1d4ca
jmp 0x14ab5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1c82e
movq %rbx, %rdi
callq 0x34b0
nop