// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movl %esi, %ebx
cmpl $0x8, %esi
setae %al
testb $0x3, %bl
sete %cl
testb %cl, %al
je 0x92f8
movq %rdi, %rbp
callq 0x73ca
movq %rax, %r14
leaq 0x50(%rsp), %rdi
movl %ebx, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x14082
leaq 0x18(%rsp), %r13
movq %r13, 0x8(%r13)
movq %r13, (%r13)
movq $0x0, 0x10(%r13)
cmpl $0x0, 0x820(%rbp)
je 0x9291
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movq %rbp, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %rbp, %rdi
movl %r15d, %esi
callq 0x7e36
movq %rax, %rbx
testl %r15d, %r15d
je 0x8fc7
movq 0x10(%rsp), %rax
incl %eax
movl %ebx, %edx
subl %eax, %edx
jbe 0x8fc7
addl %eax, %eax
subl 0x10(%rsp), %eax
leal (%rax,%rbx), %esi
addl $-0x2, %esi
leaq 0x50(%rsp), %rdi
callq 0x1425a
movl %r15d, 0x4c(%rsp)
movq 0x810(%rbp), %rdi
movl %ebx, %esi
callq 0xc444
movq %rax, %r12
movq %rbx, %rax
shrq $0x20, %rax
movq %rax, 0x10(%rsp)
cmpl %eax, %ebx
jbe 0x8ff6
movq %rbx, (%rsp)
jmp 0x921f
shrq $0x20, %r12
movl %ebx, %eax
movq %rax, (%rsp)
movq %rbx, 0x30(%rsp)
movq 0x808(%rbp), %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), %ebx
addq $0x4, %rax
cmpl %r12d, 0x4(%rcx,%rbx,8)
jb 0x9028
movl (%rcx,%rbx,8), %eax
movl %eax, 0xc(%rsp)
cmpl %eax, %r12d
jne 0x90ca
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
je 0x907e
movl 0x10(%rdi), %eax
movq %rax, (%rsp)
movq (%rdi), %r14
movl $0x18, %esi
callq 0x3270
movq %r14, %rdi
cmpq %r13, %r14
jne 0x9056
movq %r13, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movq (%rsp), %rax
movq 0x30(%rsp), %rcx
leal 0x1(%rcx), %edx
subl %eax, %ecx
leal (%rcx,%rax,2), %esi
movq %rdx, (%rsp)
subl %eax, %edx
leaq 0x50(%rsp), %rdi
callq 0x1425a
movq 0x40(%rsp), %r14
movq (%r14,%rbx,8), %rdi
movq 0x808(%rbp), %rax
subl (%rax,%rbx,8), %r12d
movl %r12d, %esi
callq 0x1368a
movl %eax, %r12d
subl 0x820(%rbp), %r12d
jmp 0x91d8
movq 0x18(%rsp), %r15
movq %r13, %rax
movq (%rsp), %rcx
movl %ecx, %r13d
movl %ecx, %ebp
cmpq %rax, %r15
je 0x9172
movq 0x38(%rsp), %rax
movl 0xc(%rsp), %ecx
addl 0x820(%rax), %ecx
movl %ecx, 0xc(%rsp)
movq (%rsp), %r13
movl %r13d, %ebp
movl 0x10(%r15), %r14d
movl 0x14(%r15), %esi
cmpl 0xc(%rsp), %esi
jae 0x9135
cmpl %r13d, %r14d
cmovbl %r14d, %r13d
movq (%r15), %r14
decq 0x28(%rsp)
movq %r15, %rdi
callq 0x33e0
movl $0x18, %esi
movq %r15, %rdi
callq 0x3270
movq %r14, %r15
jmp 0x9168
movq 0x38(%rsp), %rcx
movl 0x820(%rcx), %eax
movq 0x40(%rsp), %rdx
movq (%rdx,%rbx,8), %rdi
movq 0x808(%rcx), %rcx
addl (%rcx,%rbx,8), %eax
subl %eax, %esi
callq 0x1368a
cmpl %ebp, %r14d
cmovbl %r14d, %ebp
movl %eax, 0x14(%r15)
movq (%r15), %r15
leaq 0x18(%rsp), %rax
cmpq %rax, %r15
jne 0x90fe
subl %r13d, %ebp
jbe 0x918f
movq 0x30(%rsp), %rax
subl %r13d, %eax
leal (%rax,%r13,2), %esi
leaq 0x50(%rsp), %rdi
movl %ebp, %edx
callq 0x1425a
movq 0x40(%rsp), %r14
movq (%r14,%rbx,8), %rdi
movq 0x38(%rsp), %rbp
movq 0x808(%rbp), %rax
movl (%rax,%rbx,8), %eax
notl %eax
addl %eax, %r12d
movl %r12d, %esi
callq 0x1368a
movl %eax, %r15d
movq (%r14,%rbx,8), %rdi
callq 0x13900
movl 0x820(%rbp), %ecx
movl %eax, %r12d
subl %ecx, %r12d
decl %eax
cmpl %eax, %r15d
leaq 0x18(%rsp), %r13
jne 0x91df
movq 0x30(%rsp), %rbx
jmp 0x9213
movl $0x18, %edi
callq 0x3260
movq 0x30(%rsp), %rbx
shlq $0x20, %r15
movl (%rsp), %ecx
orq %r15, %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x3060
incq 0x28(%rsp)
leal 0x1(%rbx), %eax
movq %rax, (%rsp)
incl %ebx
cmpl 0x10(%rsp), %ebx
jbe 0x9000
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
movl 0x4c(%rsp), %r15d
movq (%rsp), %r12
je 0x925e
movl 0x10(%rdi), %r12d
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x3270
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x9236
movq %r13, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
cmpl 0x10(%rsp), %r12d
ja 0x9281
movq 0x10(%rsp), %rax
subl %r12d, %eax
leal (%rax,%r12,2), %esi
incl %esi
movl %eax, %edx
incl %edx
leaq 0x50(%rsp), %rdi
callq 0x1425a
incl %r15d
cmpl 0x820(%rbp), %r15d
jb 0x8f91
movq %r14, %rsi
callq 0x7442
leaq 0x50(%rsp), %rdi
callq 0x14294
movl $0x48, %edi
callq 0x3260
movq %rax, %r15
movq 0x828(%rbp), %rax
movl (%rax), %eax
leal 0x2(,%rax,2), %edx
leaq 0x50(%rsp), %rsi
movq %r15, %rdi
callq 0x1337c
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
je 0x92ec
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x3270
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x92d7
leaq 0x50(%rsp), %rdi
callq 0x1409a
jmp 0x9361
movq 0x15cf1(%rip), %rbx     # 0x1eff0
leaq 0xe28f(%rip), %rsi      # 0x17595
movl $0x27, %edx
movq %rbx, %rdi
callq 0x32b0
movl $0x4, %esi
movq %rbx, %rdi
callq 0x3180
movq %rax, %rbx
leaq 0xe092(%rip), %rsi      # 0x173bc
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 $0xc8, %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 0x93a3
jmp 0x9394
jmp 0x9394
jmp 0x9394
jmp 0x9394
jmp 0x9394
jmp 0x939b
movq %rax, %r14
jmp 0x93a3
jmp 0x939b
movq %rax, %r14
leaq 0x18(%rsp), %r13
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
je 0x93c2
movq (%rdi), %rbx
movl $0x18, %esi
callq 0x3270
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x93ad
leaq 0x50(%rsp), %rdi
callq 0x1409a
movq %r14, %rdi
callq 0x3460