// File: 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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movl -0x14(%rbp), %eax
cmpq $0x8, %rax
jb 0xbd83
movl -0x14(%rbp), %eax
andq $0x3, %rax
cmpq $0x0, %rax
je 0xbdce
movq 0x2c266(%rip), %rdi     # 0x37ff0
leaq 0x1d891(%rip), %rsi     # 0x29622
callq 0x32a0
movq %rax, %rdi
movl $0x4, %esi
callq 0x30d0
movq %rax, %rdi
leaq 0x1d69c(%rip), %rsi     # 0x29449
callq 0x32a0
movq %rax, %rdi
movq 0x2c1f4(%rip), %rsi     # 0x37fb0
callq 0x3300
movq $0x0, -0x8(%rbp)
jmp 0xc46e
movq -0x150(%rbp), %rdi
callq 0x9860
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %esi
leaq -0x98(%rbp), %rdi
movl $0x100000, %edx         # imm = 0x100000
callq 0x21340
leaq -0xb0(%rbp), %rdi
callq 0xe5d0
movq 0x1d90f(%rip), %rax     # 0x29714
movq %rax, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movq -0x150(%rbp), %rcx
movl -0xbc(%rbp), %eax
cmpl 0x820(%rcx), %eax
jae 0xc39f
movq -0x150(%rbp), %rdi
movl -0xbc(%rbp), %esi
callq 0xa540
movq %rax, -0x158(%rbp)
jmp 0xbe4a
movq -0x158(%rbp), %rax
movq %rax, -0xc4(%rbp)
cmpl $0x0, -0xbc(%rbp)
jbe 0xbeba
movl -0xb4(%rbp), %eax
addl $0x1, %eax
cmpl -0xc4(%rbp), %eax
jae 0xbeba
movq -0x150(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %edx
incl %edx
movl -0xc4(%rbp), %ecx
movl %ecx, %r8d
subl %eax, %r8d
addl $-0x2, %r8d
leaq -0x98(%rbp), %rsi
callq 0xe5f0
jmp 0xbea1
jmp 0xbeba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0xc454
leaq -0xb8(%rbp), %rdi
leaq -0xc4(%rbp), %rsi
callq 0xe630
jmp 0xbecf
movq -0x150(%rbp), %rax
movl -0xc4(%rbp), %ecx
movl %ecx, -0xd8(%rbp)
movq 0x810(%rax), %rdi
movl -0xc4(%rbp), %esi
callq 0x14b50
movq %rax, -0x160(%rbp)
jmp 0xbefd
movq -0x160(%rbp), %rax
movq %rax, -0xe4(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0xec(%rbp)
movl -0xec(%rbp), %eax
cmpl -0xc0(%rbp), %eax
ja 0xc30d
movq -0x150(%rbp), %rdi
movl -0xdc(%rbp), %esi
callq 0xe0c0
movl %eax, -0x164(%rbp)
jmp 0xbf4f
movq -0x150(%rbp), %rax
movl -0x164(%rbp), %ecx
movl %ecx, -0xf0(%rbp)
movl -0xdc(%rbp), %ecx
movl %ecx, -0x16c(%rbp)
movq 0x808(%rax), %rdi
movl -0xf0(%rbp), %esi
callq 0xdc20
movl %eax, -0x168(%rbp)
jmp 0xbf88
movl -0x16c(%rbp), %eax
movl -0x168(%rbp), %ecx
cmpl %ecx, %eax
jne 0xc061
leaq -0xb0(%rbp), %rdi
callq 0xe660
testb $0x1, %al
jne 0xbfdf
leaq -0xb0(%rbp), %rdi
callq 0xe680
movq %rax, -0xf8(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xe6b0
movl (%rax), %eax
movl %eax, -0xd8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xe6d0
movq -0x150(%rbp), %rdi
movl -0xd8(%rbp), %edx
movl -0xec(%rbp), %r8d
movl %r8d, %ecx
incl %ecx
subl %edx, %r8d
leaq -0x98(%rbp), %rsi
callq 0xe5f0
jmp 0xc009
movq -0x150(%rbp), %rdi
movl -0xec(%rbp), %eax
incl %eax
movl %eax, -0xd8(%rbp)
movl -0xdc(%rbp), %esi
movl -0xf0(%rbp), %eax
movl %eax, %ecx
movl %ecx, %edx
movq -0x20(%rbp), %rax
movq (%rax,%rcx,8), %rcx
callq 0xe210
movl %eax, -0x170(%rbp)
jmp 0xc043
movq -0x150(%rbp), %rcx
movl -0x170(%rbp), %eax
subl 0x820(%rcx), %eax
movl %eax, -0xe8(%rbp)
jmp 0xc2ed
movq -0x150(%rbp), %rax
movq 0x808(%rax), %rdi
movl -0xf0(%rbp), %esi
callq 0xdc20
movl %eax, -0x174(%rbp)
jmp 0xc082
movq -0x150(%rbp), %rcx
movl -0x174(%rbp), %eax
addl 0x820(%rcx), %eax
movl %eax, -0xfc(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0x104(%rbp)
movl %eax, -0x100(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xe680
movq %rax, -0x110(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xe730
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0xe700
testb $0x1, %al
jne 0xc0ef
jmp 0xc1de
leaq -0x110(%rbp), %rdi
callq 0xe6b0
movq (%rax), %rax
movq %rax, -0x120(%rbp)
movl -0x11c(%rbp), %eax
cmpl -0xfc(%rbp), %eax
jae 0xc16a
movl -0x120(%rbp), %eax
cmpl -0x100(%rbp), %eax
jae 0xc12d
movl -0x120(%rbp), %eax
movl %eax, -0x100(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0xe7b0
movq -0x130(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0xe760
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x110(%rbp)
jmp 0xc1d9
movl -0x120(%rbp), %eax
cmpl -0x104(%rbp), %eax
jae 0xc184
movl -0x120(%rbp), %eax
movl %eax, -0x104(%rbp)
movq -0x150(%rbp), %rdi
movl -0x11c(%rbp), %esi
movl 0x820(%rdi), %eax
subl %eax, %esi
movl -0xf0(%rbp), %eax
movl %eax, %ecx
movl %ecx, %edx
movq -0x20(%rbp), %rax
movq (%rax,%rcx,8), %rcx
callq 0xe210
movl %eax, -0x178(%rbp)
jmp 0xc1b8
leaq -0x110(%rbp), %rdi
callq 0xe6b0
movl -0x178(%rbp), %ecx
movl %ecx, 0x4(%rax)
leaq -0x110(%rbp), %rdi
callq 0xe7d0
jmp 0xc0c0
movl -0x100(%rbp), %eax
cmpl -0x104(%rbp), %eax
jae 0xc219
movq -0x150(%rbp), %rdi
movl -0x100(%rbp), %edx
movl -0x104(%rbp), %ecx
movl -0xec(%rbp), %r8d
subl %edx, %r8d
leaq -0x98(%rbp), %rsi
callq 0xe5f0
jmp 0xc217
jmp 0xc219
movq -0x150(%rbp), %rdi
movl -0xdc(%rbp), %esi
decl %esi
movl -0xf0(%rbp), %eax
movl %eax, %ecx
movl %ecx, %edx
movq -0x20(%rbp), %rax
movq (%rax,%rcx,8), %rcx
callq 0xe210
movl %eax, -0x17c(%rbp)
jmp 0xc247
movq -0x150(%rbp), %rdi
movl -0x17c(%rbp), %eax
movl %eax, -0x134(%rbp)
movl -0xf0(%rbp), %eax
movl %eax, %ecx
movl %ecx, %esi
movq -0x20(%rbp), %rax
movq (%rax,%rcx,8), %rdx
callq 0xe7f0
movl %eax, -0x180(%rbp)
jmp 0xc279
movq -0x150(%rbp), %rdx
movl -0x180(%rbp), %eax
subl 0x820(%rdx), %eax
movl %eax, -0xe8(%rbp)
movl -0x134(%rbp), %eax
movl -0xe8(%rbp), %ecx
addl 0x820(%rdx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0xc2eb
leaq -0x13c(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0x134(%rbp), %rdx
callq 0xdec0
jmp 0xc2c7
leaq -0xb0(%rbp), %rdi
leaq -0x13c(%rbp), %rsi
callq 0xe820
jmp 0xc2dc
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0xc2ed
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
movl -0xe8(%rbp), %eax
movl %eax, -0xdc(%rbp)
jmp 0xbf23
leaq -0xb0(%rbp), %rdi
callq 0xe660
testb $0x1, %al
jne 0xc350
leaq -0xb0(%rbp), %rdi
callq 0xe680
movq %rax, -0x148(%rbp)
leaq -0x148(%rbp), %rdi
callq 0xe6b0
movl (%rax), %eax
movl %eax, -0xd8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xe6d0
movl -0xd8(%rbp), %eax
cmpl -0xc0(%rbp), %eax
ja 0xc389
movq -0x150(%rbp), %rdi
movl -0xd8(%rbp), %edx
movl -0xc0(%rbp), %ecx
incl %ecx
movl %ecx, %r8d
subl %edx, %r8d
leaq -0x98(%rbp), %rsi
callq 0xe5f0
jmp 0xc387
jmp 0xc389
jmp 0xc38b
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xbe16
movq -0x150(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x9930
leaq -0x98(%rbp), %rdi
callq 0x21630
jmp 0xc3bd
movl $0x48, %edi
callq 0x32c0
movq %rax, -0x188(%rbp)
jmp 0xc3d0
movq -0x150(%rbp), %rax
movq 0x828(%rax), %rdi
callq 0xe860
movl %eax, -0x18c(%rbp)
jmp 0xc3eb
movq -0x188(%rbp), %rdi
movl -0x18c(%rbp), %ecx
movl %ecx, %eax
leal 0x2(%rax,%rax), %edx
leaq -0x98(%rbp), %rsi
callq 0x20a10
jmp 0xc40c
movq -0x188(%rbp), %rax
movq %rax, -0x8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xe870
leaq -0x98(%rbp), %rdi
callq 0x21380
jmp 0xc46e
movq -0x188(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
movl $0x48, %esi
callq 0x32d0
leaq -0xb0(%rbp), %rdi
callq 0xe870
leaq -0x98(%rbp), %rdi
callq 0x21380
jmp 0xc47b
movq -0x8(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x3530
nopw (%rax,%rax)