// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/rlcsa.cpp
// Function: CSA::RLCSA::directLocate(unsigned int, bool) const
usint
RLCSA::directLocate(usint index, bool steps) const
{
  // Note that index is in BWT coordinates initially.
  
  // This keeps track of how far along the sequence we had to go to find an SA
  // sample.
  usint offset = 0;
  while(true)
  {
    // First try in BWT space, so we can account for the text end characters.
    if(this->hasImplicitSample(index))
    {
      // If we took an implicit sample at this index in the BWT (due to us being
      // in the range occupied by sequence start characters), we know where it
      // falls in the original sequences: at the endpoint of the appropriate
      // text.
      if(steps) { return offset; }
      else      { return this->getImplicitSample(index) - offset; }
    }
    // Pop index into SA space, where the SA samples live
    index -= this->number_of_sequences;
    if(this->sa_samples->isSampled(index))
    {
      // If we took a real SA sample here, we know where it falls in the
      // original sequences.
      return (steps ? offset : this->sa_samples->getSampleAt(index) - offset);
    }
    
    // If we get here, we couldn't map this position. Proceed forwards (towards
    // the end of the sequence), in hopes of hitting either a sample or the
    // sequence end character. Note that psi maps from SA position to the *BWT*
    // position of the subsequent character, popping index back into BWT space.
    index = this->psi(index);
    offset++;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, %r14
movl 0x820(%rdi), %eax
xorl %ebx, %ebx
cmpl %esi, %eax
ja 0x78fa
xorl %ebx, %ebx
movq %rsp, %r12
subl %eax, %ebp
movq 0x810(%r14), %rax
movq 0x10(%rax), %rsi
movq %r12, %rdi
callq 0x12ac0
movq %r12, %rdi
movl %ebp, %esi
callq 0x13386
movl %eax, %r13d
movq %r12, %rdi
callq 0x12ac6
testb %r13b, %r13b
jne 0x7932
movl 0x4(%r14), %ecx
cmpl %ebp, %ecx
jbe 0x78e5
movq 0x808(%r14), %rcx
movl %ebp, %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), %r13d
addq $0x4, %rax
cmpl %ebp, 0x4(%rcx,%r13,8)
jb 0x78a2
movq 0x8(%r14,%r13,8), %rsi
movq %r12, %rdi
callq 0x1375c
movq 0x808(%r14), %rax
subl (%rax,%r13,8), %ebp
movq %r12, %rdi
movl %ebp, %esi
callq 0x13a48
movl %eax, %ebp
movq %r12, %rdi
callq 0x13762
movl 0x820(%r14), %eax
jmp 0x78f0
movl 0x820(%r14), %eax
addl %eax, %ecx
movl %ecx, %ebp
incl %ebx
cmpl %ebp, %eax
jbe 0x7845
testb %r15b, %r15b
jne 0x79e8
movq 0x828(%r14), %rsi
movq %rsp, %r14
movq %r14, %rdi
callq 0x12ac0
movq %r14, %rdi
movl %ebp, %esi
callq 0x12c6e
movl %eax, %ebp
movq %rsp, %rdi
callq 0x12ac6
subl %ebx, %ebp
incl %ebp
jmp 0x79e6
testb %r15b, %r15b
jne 0x79e8
movq 0x810(%r14), %r15
movq 0x10(%r15), %rsi
movq %rsp, %r14
movq %r14, %rdi
callq 0x12ac0
xorl %r12d, %r12d
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x12acc
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 0x7996
movl %r9d, %edx
movl %r8d, %eax
jmp 0x79ad
movq (%rsi), %r8
movl %edi, %r9d
movl (%r8,%r9,4), %r12d
shll %cl, %r12d
shrl %cl, %r12d
movl %eax, %ecx
shll %cl, %r12d
incl %edi
testl %eax, %eax
je 0x79ca
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, %r12d
imull 0x4(%r15), %r12d
movl 0x8(%r15), %ebp
decl %ebp
cmpl %r12d, %ebp
cmovael %r12d, %ebp
movq %rsp, %rdi
callq 0x12ac6
subl %ebx, %ebp
movl %ebp, %ebx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7a0a
jmp 0x7a0a
movq %rax, %rbx
movq %rsp, %rdi
callq 0x13762
jmp 0x7a15
movq %rax, %rbx
movq %rsp, %rdi
callq 0x12ac6
movq %rbx, %rdi
callq 0x3460
nop