// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
cmpl $0x8, %esi
setae %al
testb $0x3, %bpl
sete %cl
testb %cl, %al
je 0x7b61
movq %rdi, %rbx
callq 0x63ea
movq %rax, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
movl %ebp, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x10178
leaq 0x28(%rsp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq $0x0, 0x10(%rsp)
xorl %esi, %esi
cmpl 0x820(%rbx), %esi
jae 0x7b9b
movq %rbx, %rdi
movl %esi, 0x1c(%rsp)
callq 0x6b60
movq %rax, %r15
cmpl $0x0, 0x1c(%rsp)
je 0x7941
movq 0x10(%rsp), %rax
leal 0x1(%rax), %edx
cmpl %r15d, %edx
jae 0x7941
movl %r15d, %r8d
subl 0x10(%rsp), %r8d
addl $-0x2, %r8d
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movl %r15d, %ecx
callq 0x8a54
movq 0x810(%rbx), %rdi
movl %r15d, %esi
callq 0xaa2e
movq %rax, %r12
movq %r15, %rax
shrq $0x20, %rax
movq %rax, 0x10(%rsp)
shrq $0x20, %r12
movl %r15d, %r13d
movq %r15, 0x40(%rsp)
cmpl 0x10(%rsp), %r13d
ja 0x7b19
movq %rbx, %rdi
movl %r12d, %esi
callq 0x8958
movq 0x808(%rbx), %rcx
movl %eax, 0xc(%rsp)
movl %eax, %ebp
movl (%rcx,%rbp,8), %r14d
cmpl %r14d, %r12d
je 0x7a95
movq %r13, 0x58(%rsp)
movq %r12, 0x50(%rsp)
movq %r15, %rcx
movq %rbx, %r15
addl 0x820(%rbx), %r14d
movq 0x28(%rsp), %r13
movl %ecx, 0x8(%rsp)
movl %ecx, %r12d
leaq 0x28(%rsp), %rbx
cmpq %rbx, %r13
je 0x7a24
movl 0x10(%r13), %ebx
movl 0x14(%r13), %esi
cmpl %r14d, %esi
jae 0x79f7
movl 0x8(%rsp), %eax
cmpl %eax, %ebx
cmovbl %ebx, %eax
movl %eax, 0x8(%rsp)
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8a7c
movq %rax, %r13
jmp 0x79c3
movq %r15, %rdi
subl 0x820(%r15), %esi
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rcx
movl 0xc(%rsp), %edx
callq 0x89ec
cmpl %r12d, %ebx
cmovbl %ebx, %r12d
movl %eax, 0x14(%r13)
movq (%r13), %r13
jmp 0x79be
movl 0x8(%rsp), %edx
cmpl %r12d, %edx
movq %r15, %rbx
movq 0x58(%rsp), %r13
jae 0x7a4b
movl %r13d, %r8d
subl %edx, %r8d
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movl %r12d, %ecx
callq 0x8a54
movq 0x50(%rsp), %rsi
decl %esi
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %r12
movq %rbx, %rdi
movl 0xc(%rsp), %edx
movq %r12, %rcx
callq 0x89ec
movl %eax, %r15d
movq %r12, %rdi
callq 0xfe84
movl 0x820(%rbx), %ecx
movl %eax, %r12d
subl %ecx, %r12d
decl %eax
cmpl %eax, %r15d
jne 0x7af1
incl %r13d
movq 0x40(%rsp), %r15
jmp 0x796b
movq 0x28(%rsp), %rax
leaq 0x28(%rsp), %rdi
cmpq %rdi, %rax
je 0x7aad
movl 0x10(%rax), %r15d
callq 0x8a64
movl %r15d, %edx
leal 0x1(%r13), %r15d
subl %edx, %r13d
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movl %r15d, %ecx
movl %r13d, %r8d
callq 0x8a54
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rcx
movq %rbx, %rdi
movl %r12d, %esi
movl 0xc(%rsp), %edx
callq 0x89ec
movl %eax, %r12d
subl 0x820(%rbx), %r12d
jmp 0x7963
movq 0x40(%rsp), %rax
movl %eax, 0x48(%rsp)
movl %r15d, 0x4c(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x8a8a
incl %r13d
movl %r13d, %r15d
jmp 0x7963
movq 0x28(%rsp), %rax
leaq 0x28(%rsp), %rdi
cmpq %rdi, %rax
je 0x7b31
movl 0x10(%rax), %r15d
callq 0x8a64
cmpl 0x10(%rsp), %r15d
ja 0x7b56
movq 0x10(%rsp), %rax
leal 0x1(%rax), %ecx
movl %ecx, %r8d
subl %r15d, %r8d
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movl %r15d, %edx
callq 0x8a54
movl 0x1c(%rsp), %esi
incl %esi
jmp 0x78f6
movq 0x15488(%rip), %rdi     # 0x1cff0
leaq 0xba2b(%rip), %rsi      # 0x1359a
callq 0x3200
pushq $0x4
popq %rsi
movq %rax, %rdi
callq 0x3160
leaq 0xb83b(%rip), %rsi      # 0x133c1
movq %rax, %rdi
callq 0x3200
movq %rax, %rdi
callq 0x3100
xorl %r14d, %r14d
jmp 0x7beb
movq 0x20(%rsp), %rsi
callq 0x646a
leaq 0x60(%rsp), %rdi
callq 0x102cc
pushq $0x48
popq %rdi
callq 0x3220
movq %rax, %r14
movq 0x828(%rbx), %rax
movl (%rax), %eax
leal 0x2(,%rax,2), %edx
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0xfcec
leaq 0x28(%rsp), %rdi
callq 0x8c2e
leaq 0x60(%rsp), %rdi
callq 0x1018e
movq %r14, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r14, %rdi
callq 0x3230
jmp 0x7c25
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
jmp 0x7c22
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x8c2e
leaq 0x60(%rsp), %rdi
callq 0x1018e
movq %rbx, %rdi
callq 0x3410
nop