// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/rlcsa.cpp
// Function: CSA::RLCSA::buildPLCP(unsigned int) const
PLCPVector*
RLCSA::buildPLCP(usint block_size) const
{
  if(block_size < 2 * sizeof(usint) || block_size % sizeof(usint) != 0)
  {
    std::cerr << "PLCP: Block size must be a multiple of " << sizeof(usint) << " bytes!" << std::endl;
    return 0;
  }

  PsiVector::Iterator** iters = this->getIterators();

  PLCPVector::Encoder plcp(block_size);
  std::list<pair_type> matches;
  pair_type prev_range = EMPTY_PAIR;
  for(usint j = 0; j < this->number_of_sequences; j++)
  {
    // Encode the padding as a single run.
    pair_type seq_range = this->getSequenceRange(j);
    if(j > 0 && prev_range.second + 1 < seq_range.first)
    {
      this->encodePLCPRun(plcp, prev_range.second + 1, seq_range.first, seq_range.first - prev_range.second - 2);
    }
    prev_range = seq_range;

    usint maximal = seq_range.first;
    usint x = this->sa_samples->inverseSA(seq_range.first).second, next_x;

    // Invariant: x == inverseSA(i)
    for(usint i = seq_range.first; i <= seq_range.second; i++, x = next_x)
    {
      usint c = this->getCharacter(x);

      // T[i,n] is lexicographically the first suffix beginning with c.
      if(x == this->alphabet->cumulative(c))
      {
        if(!matches.empty())
        {
          maximal = (*(matches.begin())).first;
          matches.clear();
        }
        this->encodePLCPRun(plcp, maximal, i + 1, i - maximal);
        maximal = i + 1;
        next_x = this->psiUnsafe(x, c, *(iters[c])) - this->number_of_sequences;
        continue;
      }

      // Process the previous left matches we are still following.
      usint low = this->alphabet->cumulative(c) + this->number_of_sequences;
      usint start, stop; start = stop = maximal;
      for(std::list<pair_type>::iterator iter = matches.begin(); iter != matches.end();)
      {
        pair_type match = *iter;
        if(match.second < low)  // These no longer match the current character.
        {
          if(match.first < start) { start = match.first; }  // Form a single run from then.
          iter = matches.erase(iter);
        }
        else
        {
          if(match.first < stop) { stop = match.first; }  // End of the run to be encoded.
          (*iter).second = this->psiUnsafe(match.second - this->number_of_sequences, c, *(iters[c]));
          ++iter;
        }
      }
      if(start < stop) { this->encodePLCPRun(plcp, start, stop, i - start); }

      // If PLCP[i] is minimal, we add the left match to the list.
      // We add pairs of type (j, inverseSA(j) + n_of_s) as inverseSA is < 0 for end markers.
      usint next_y = this->psiUnsafe(x - 1, c, *(iters[c]));
      next_x = this->psiUnsafeNext(c, *(iters[c])) - this->number_of_sequences;
      if(next_y != next_x + this->number_of_sequences - 1)
      {
        matches.push_back(pair_type(maximal, next_y));
        maximal = i + 1;
      }
    }

    if(!matches.empty())
    {
      maximal = (*(matches.begin())).first;
      matches.clear();
    }
    if(maximal <= seq_range.second)
    {
      this->encodePLCPRun(plcp, maximal, seq_range.second + 1, seq_range.second + 1 - maximal);
    }
  }

  this->deleteIterators(iters);

  plcp.flush();
  return new PLCPVector(plcp, 2 * (this->end_points->getSize() + 1));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %esi, %ebx
cmpl $0x8, %esi
setae %al
testb $0x3, %bl
sete %cl
testb %cl, %al
je 0x9405
movq %rdi, %r12
callq 0x732a
movq %rax, 0x28(%rsp)
leaq 0x60(%rsp), %rdi
movl %ebx, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x145e0
leaq 0x30(%rsp), %r13
movq %r13, 0x8(%r13)
movq %r13, (%r13)
movq $0x0, 0x10(%r13)
cmpl $0x0, 0x820(%r12)
je 0x939b
xorl %esi, %esi
movq $0x0, 0x18(%rsp)
movq %r12, 0x50(%rsp)
movq %r12, %rdi
movl %esi, 0x24(%rsp)
callq 0x7f00
movq %rax, %r14
cmpl $0x0, 0x24(%rsp)
je 0x90b8
movq 0x18(%rsp), %rax
incl %eax
movl %r14d, %edx
subl %eax, %edx
jbe 0x90b8
addl %eax, %eax
subl 0x18(%rsp), %eax
leal (%rax,%r14), %esi
addl $-0x2, %esi
leaq 0x60(%rsp), %rdi
callq 0x147fe
movq 0x810(%r12), %rdi
movl %r14d, %esi
callq 0xc578
movq %rax, %r15
movq %r14, %rax
shrq $0x20, %rax
movq %rax, 0x18(%rsp)
cmpl %eax, %r14d
jbe 0x90e6
movq %r14, 0x8(%rsp)
jmp 0x9329
shrq $0x20, %r15
movl %r14d, %eax
movq %rax, 0x8(%rsp)
movq 0x808(%r12), %rcx
movl %r15d, %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), %ebx
addq $0x4, %rax
cmpl %r15d, 0x4(%rcx,%rbx,8)
jb 0x9116
movl (%rcx,%rbx,8), %eax
movl %eax, 0x14(%rsp)
cmpl %eax, %r15d
jne 0x91cb
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x9179
movq %r15, %rbp
movq %r14, %r15
movl 0x10(%rdi), %eax
movq %rax, 0x8(%rsp)
movq (%rdi), %r14
movl $0x18, %esi
callq 0x3270
movq %r14, %rdi
cmpq %r13, %r14
jne 0x914b
movq %r13, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, %r14
movq %rbp, %r15
leal 0x1(%r14), %ebp
movq 0x8(%rsp), %rax
subl %eax, %r14d
leal (%r14,%rax,2), %esi
movl %ebp, %edx
subl %eax, %edx
leaq 0x60(%rsp), %rdi
callq 0x147fe
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rdi
movq 0x808(%r12), %rax
subl (%rax,%rbx,8), %r15d
movl %r15d, %esi
callq 0x13a48
movl %eax, %r15d
subl 0x820(%r12), %r15d
movl %ebp, %eax
movq %rax, 0x8(%rsp)
jmp 0x931c
movq %r15, 0x58(%rsp)
movq %r14, 0x48(%rsp)
movq 0x30(%rsp), %r15
cmpq %r13, %r15
je 0x9292
movl 0x14(%rsp), %eax
addl 0x820(%r12), %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %r14
movl %r14d, %ebp
movq %r13, %r12
movl 0x10(%r15), %r13d
movl 0x14(%r15), %esi
cmpl 0x14(%rsp), %esi
jae 0x9235
cmpl %r14d, %r13d
cmovbl %r13d, %r14d
movq (%r15), %r13
decq 0x40(%rsp)
movq %r15, %rdi
callq 0x33e0
movl $0x18, %esi
movq %r15, %rdi
callq 0x3270
movq %r13, %r15
jmp 0x9268
movq 0x50(%rsp), %rcx
movl 0x820(%rcx), %eax
movq 0x28(%rsp), %rdx
movq (%rdx,%rbx,8), %rdi
movq 0x808(%rcx), %rcx
addl (%rcx,%rbx,8), %eax
subl %eax, %esi
callq 0x13a48
cmpl %ebp, %r13d
cmovbl %r13d, %ebp
movl %eax, 0x14(%r15)
movq (%r15), %r15
movq %r12, %r13
cmpq %r12, %r15
jne 0x91fb
subl %r14d, %ebp
movq 0x50(%rsp), %r12
jbe 0x9292
movq 0x48(%rsp), %rax
subl %r14d, %eax
leal (%rax,%r14,2), %esi
leaq 0x60(%rsp), %rdi
movl %ebp, %edx
callq 0x147fe
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rbp
movq 0x808(%r12), %rax
movl (%rax,%rbx,8), %eax
notl %eax
movq 0x58(%rsp), %rsi
addl %eax, %esi
movq %rbp, %rdi
callq 0x13a48
movl %eax, %r15d
movq %rbp, %rdi
callq 0x13cb8
movl 0x820(%r12), %ecx
movl %eax, %r14d
subl %ecx, %r14d
decl %eax
cmpl %eax, %r15d
jne 0x92e2
movq 0x48(%rsp), %rax
incl %eax
movl %eax, %ebp
jmp 0x9319
movl $0x18, %edi
callq 0x3260
movq 0x48(%rsp), %rbx
shlq $0x20, %r15
movl 0x8(%rsp), %ecx
orq %r15, %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x3060
incq 0x40(%rsp)
incl %ebx
movl %ebx, %ebp
movq %rbx, 0x8(%rsp)
movq %r14, %r15
movl %ebp, %r14d
cmpl 0x18(%rsp), %ebp
jbe 0x90f2
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
movq 0x8(%rsp), %r14
je 0x9364
movl 0x10(%rdi), %r14d
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x3270
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x933c
movq %r13, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpl 0x18(%rsp), %r14d
ja 0x9387
movq 0x18(%rsp), %rax
subl %r14d, %eax
leal (%rax,%r14,2), %esi
incl %esi
movl %eax, %edx
incl %edx
leaq 0x60(%rsp), %rdi
callq 0x147fe
movl 0x24(%rsp), %esi
incl %esi
cmpl 0x820(%r12), %esi
jb 0x907d
movq 0x28(%rsp), %rsi
callq 0x73a2
leaq 0x60(%rsp), %rdi
callq 0x14836
movl $0x48, %edi
callq 0x3260
movq %rax, %r15
movq 0x828(%r12), %rax
movl (%rax), %eax
leal 0x2(,%rax,2), %edx
leaq 0x60(%rsp), %rsi
movq %r15, %rdi
callq 0x13744
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x93f9
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x3270
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x93e4
leaq 0x60(%rsp), %rdi
callq 0x145f8
jmp 0x946e
movq 0x16be4(%rip), %rbx     # 0x1fff0
leaq 0xf1cf(%rip), %rsi      # 0x185e2
movl $0x27, %edx
movq %rbx, %rdi
callq 0x32b0
movl $0x4, %esi
movq %rbx, %rdi
callq 0x3180
movq %rax, %rbx
leaq 0xefd2(%rip), %rsi      # 0x18409
movl $0x7, %edx
movq %rax, %rdi
callq 0x32b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3190
xorl %r15d, %r15d
movq %r15, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x48, %esi
movq %r15, %rdi
callq 0x3270
jmp 0x94ae
jmp 0x94a3
jmp 0x94a3
jmp 0x94a3
jmp 0x94a3
jmp 0x94a3
jmp 0x94a3
jmp 0x94a3
movq %rax, %r14
jmp 0x94ae
movq %rax, %r14
movq %r12, %r13
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x94cd
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x3270
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x94b8
leaq 0x60(%rsp), %rdi
callq 0x145f8
movq %r14, %rdi
callq 0x3460
nop