// File: /workspace/github_cpp_makefile_star2_O3/adamnovak[P]rlcsa/rlcsa.cpp
// Function: CSA::RLCSA::buildRLCSA(unsigned char*, unsigned int*, unsigned int, unsigned int, unsigned int, CSA::Sampler*, bool, bool)
void
RLCSA::buildRLCSA(uchar* data, usint* ranks, usint bytes, usint block_size, usint threads, Sampler* sampler, bool multiple_sequences, bool delete_data)
{
  threads = std::max(threads, (usint)1);
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif


  // Do we store SA samples?
  bool sample_sa = true;
  if(this->sample_rate == 0)
  {
    sample_sa = false; this->sample_rate = 1;
  }


  // Determine the number of sequences and mark their end points.
  DeltaEncoder endings(RLCSA::ENDPOINT_BLOCK_SIZE);
  if(multiple_sequences)
  {
    this->number_of_sequences = 0;
    usint marker = 0;
    usint padding = 0, chars_encountered = 0;

    for(usint i = 0; i < bytes; i++)
    {
      if(data[i] == 0)
      {
        if(i == marker) { break; }  // Empty sequence.
        this->number_of_sequences++;
        marker = i + 1;
        usint pos = chars_encountered + padding - 1;
        endings.setBit(pos);
        padding = ((pos + this->sample_rate) / this->sample_rate) * this->sample_rate - chars_encountered;
      }
      else { chars_encountered++; }
    }

    if(this->number_of_sequences == 0 || marker != bytes)
    {
      std::cerr << "RLCSA: Collection must consist of 0-terminated nonempty sequences!" << std::endl;
      if(delete_data) { delete[] data; }
      return;
    }
    this->end_points = new DeltaVector(endings, chars_encountered + padding);
  }
  else
  {
    this->number_of_sequences = 1;
    DeltaEncoder endings(RLCSA::ENDPOINT_BLOCK_SIZE, RLCSA::ENDPOINT_BLOCK_SIZE);
    endings.setBit(bytes - 1);
    this->end_points = new DeltaVector(endings, bytes);
  }


  // Build character tables etc.
  usint distribution[CHARS];
  for(usint c = 0; c < CHARS; c++) { distribution[c] = 0; }
  for(usint i = 0; i < bytes; i++) { distribution[(usint)data[i]]++; }
  if(multiple_sequences) { distribution[0] = 0; } // \0 is an end marker
  this->alphabet = new Alphabet(distribution); this->data_size = this->alphabet->getDataSize();


  // Build suffix array.
  short_pair* sa = 0;
  if(multiple_sequences)
  {
    if(ranks == 0) { sa = simpleSuffixSort(data, bytes, this->number_of_sequences, threads); }
    else           { sa = simpleSuffixSort(ranks, bytes, threads); }
  }
  else
  {
    bytes++;
    sa = new short_pair[bytes];
    #pragma omp parallel for schedule(static)
    for(usint i = 0; i < bytes - 1; i++) { sa[i].second = data[i] + 1; }
    sa[bytes - 1].second = 0;
    simpleSuffixSort(sa, bytes, threads);
  }
  if(delete_data) { delete[] data; }


  // Sample SA.
  if(sample_sa)
  {
    if(sampler != 0) { this->sa_samples = new SASamples(sa, sampler, threads); }
    else             { this->sa_samples = new SASamples(sa, this->end_points, this->data_size, this->sample_rate, threads); }
    this->support_locate = this->sa_samples->supportsLocate();
    this->support_display = this->sa_samples->supportsDisplay();
  }


  // Build Psi.
  #pragma omp parallel for schedule(static)
  for(usint i = 0; i < bytes; i++) { sa[i].first = sa[(sa[i].first + 1) % bytes].second; }


  // Build RLCSA.
  #pragma omp parallel for schedule(dynamic, 1)
  for(usint c = 0; c < CHARS; c++)
  {
    if(!(this->alphabet->hasChar(c))) { this->array[c] = 0; continue; }

    short_pair* curr = sa + this->alphabet->cumulative(c) + this->number_of_sequences;
    short_pair* limit = curr + this->alphabet->countOf(c);
    PsiVector::Encoder encoder(block_size);
    pair_type run((*curr).first, 1); ++curr;

    for(; curr < limit; ++curr)
    {
      if((*curr).first == run.first + run.second) { run.second++; }
      else
      {
        encoder.addRun(run.first, run.second);
        run = pair_type((*curr).first, 1);
      }
    }
    encoder.addRun(run.first, run.second);
    encoder.flush();

    this->array[c] = new PsiVector(encoder, this->data_size + this->number_of_sequences);
  }
  delete[] sa;


  this->ok = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x518, %rsp            # imm = 0x518
movl %r8d, 0x14(%rsp)
movl %ecx, %r15d
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x1, %r9d
adcl $0x0, %r9d
movl %r9d, 0x4(%rsp)
movl 0x81c(%rdi), %eax
movl %eax, 0x10(%rsp)
testl %eax, %eax
jne 0x5d33
movl $0x1, 0x81c(%rbx)
movb 0x560(%rsp), %bpl
leaq 0x28(%rsp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x134f2
cmpb $0x0, 0x558(%rsp)
je 0x5e28
movl $0x0, 0x820(%rbx)
testl %r15d, %r15d
je 0x5f31
movq %r15, 0x8(%rsp)
movl %r15d, %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %eax, %eax
cmpb $0x0, (%r14,%r15)
je 0x5d92
incl %r13d
incq %r15
jmp 0x5dd6
movl %eax, %edx
movl 0x820(%rbx), %ecx
cmpq %rdx, %r15
je 0x5de1
incl %ecx
movl %ecx, 0x820(%rbx)
addl %r13d, %r12d
decl %r12d
leaq 0x28(%rsp), %rdi
movl %r12d, %esi
callq 0x13502
incq %r15
movl 0x81c(%rbx), %ecx
addl %ecx, %r12d
movl %r12d, %eax
xorl %edx, %edx
divl %ecx
addl %r13d, %edx
subl %edx, %r12d
movl %r15d, %eax
cmpq %rbp, %r15
jne 0x5d83
movl 0x820(%rbx), %ecx
testl %ecx, %ecx
movb 0x560(%rsp), %bpl
movq 0x8(%rsp), %r15
je 0x5f31
cmpl %r15d, %eax
jne 0x5f31
movl $0x48, %edi
callq 0x3260
movq %rax, %rbp
addl %r12d, %r13d
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
movl %r13d, %edx
callq 0x12aa8
movq %rbp, 0x828(%rbx)
jmp 0x5e8e
movl $0x1, 0x820(%rbx)
leaq 0x110(%rsp), %rdi
movl $0x10, %esi
movl $0x10, %edx
callq 0x134f2
leal -0x1(%r15), %esi
leaq 0x110(%rsp), %rdi
callq 0x13502
movl $0x48, %edi
callq 0x3260
movq %rax, %r12
leaq 0x110(%rsp), %rsi
movq %rax, %rdi
movl %r15d, %edx
callq 0x12aa8
movq %r12, 0x828(%rbx)
leaq 0x110(%rsp), %rdi
callq 0x134fc
leaq 0x110(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x3150
movl %r15d, %ebp
testl %r15d, %r15d
je 0x5ec0
xorl %eax, %eax
movzbl (%r14,%rax), %ecx
incl 0x110(%rsp,%rcx,4)
incq %rax
cmpq %rax, %rbp
jne 0x5eac
cmpb $0x0, 0x558(%rsp)
je 0x5ed5
movl $0x0, 0x110(%rsp)
movl $0x1010, %edi           # imm = 0x1010
callq 0x3260
movq %rax, %r12
leaq 0x110(%rsp), %rsi
movq %rax, %rdi
callq 0xcdd0
movq %r12, 0x808(%rbx)
movl 0x800(%r12), %eax
movl %eax, 0x4(%rbx)
cmpb $0x0, 0x558(%rsp)
je 0x5f97
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5fe4
movl %r15d, %esi
movl 0x4(%rsp), %edx
callq 0x15b41
jmp 0x5ff9
movq 0x1a0b8(%rip), %rbx     # 0x1fff0
leaq 0x1271e(%rip), %rsi     # 0x1865d
movl $0x42, %edx
movq %rbx, %rdi
callq 0x32b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq 0x1a086(%rip), %rdi     # 0x1fff0
callq 0x3040
movq %rax, %rdi
callq 0x3190
testq %r14, %r14
sete %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x61fb
movq %r14, %rdi
callq 0x3300
jmp 0x61fb
leal 0x1(%r15), %r13d
movq %r15, 0x8(%rsp)
leaq (,%r13,8), %r15
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
testl %r13d, %r13d
je 0x5fcc
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3150
cmpl $0x0, 0x8(%rsp)
je 0x6001
xorl %eax, %eax
movzbl (%r14,%rax), %ecx
incl %ecx
movl %ecx, 0x4(%r12,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x5fce
jmp 0x6003
movl 0x820(%rbx), %edx
movq %r14, %rdi
movl %r15d, %esi
movl 0x4(%rsp), %ecx
callq 0x15dfb
movq %rax, %r12
movl %r15d, %r13d
jmp 0x601b
xorl %ebp, %ebp
movl $0x0, 0x4(%r12,%rbp,8)
movq %r12, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x15c9a
testq %r14, %r14
sete %al
movb 0x560(%rsp), %cl
xorb $0x1, %cl
orb %al, %cl
jne 0x6037
movq %r14, %rdi
callq 0x3300
cmpl $0x0, 0x10(%rsp)
je 0x60b9
movq 0x550(%rsp), %r15
testq %r15, %r15
je 0x606c
movl $0x30, %edi
callq 0x3260
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl 0x4(%rsp), %ecx
callq 0xb8aa
jmp 0x609a
movl $0x30, %edi
callq 0x3260
movq %rax, %r14
movq 0x828(%rbx), %rdx
movl 0x4(%rbx), %ecx
movl 0x81c(%rbx), %r8d
movq %rax, %rdi
movq %r12, %rsi
movl 0x4(%rsp), %r9d
callq 0xb5de
movq %r14, 0x810(%rbx)
cmpq $0x0, 0x18(%r14)
setne 0x818(%rbx)
cmpq $0x0, 0x28(%r14)
setne 0x819(%rbx)
testl %r13d, %r13d
je 0x60df
movl %r13d, %ecx
xorl %esi, %esi
movl (%r12,%rsi,8), %eax
incl %eax
xorl %edx, %edx
divl %r13d
movl 0x4(%r12,%rdx,8), %eax
movl %eax, (%r12,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x60c3
xorl %r13d, %r13d
leaq 0x98(%rsp), %r14
movq %rbx, 0x18(%rsp)
movq 0x808(%rbx), %rax
movq %rbx, %rcx
movq (%rax,%r13,8), %rbx
movq %rbx, %rbp
shrq $0x20, %rbp
cmpl %ebp, %ebx
jbe 0x6119
movq $0x0, 0x8(%rcx,%r13,8)
movq %rcx, %rbx
jmp 0x61db
movq %r14, %rdi
movl 0x820(%rcx), %r15d
movl 0x14(%rsp), %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x145e0
movl %ebx, %eax
leaq (%r12,%rax,8), %rax
leaq (%rax,%r15,8), %r15
subl %ebx, %ebp
leaq 0x8(,%rbp,8), %rbp
movabsq $0x7fffffff8, %rax      # imm = 0x7FFFFFFF8
andq %rax, %rbp
movl (%r15), %esi
movl $0x1, %edx
cmpq $0x9, %rbp
jb 0x6198
addq %r15, %rbp
addq $0x8, %r15
movl $0x1, %edx
movq 0x18(%rsp), %rbx
leal (%rdx,%rsi), %eax
cmpl %eax, (%r15)
jne 0x617d
incl %edx
jmp 0x618d
movq %r14, %rdi
callq 0x147fe
movl (%r15), %esi
movl $0x1, %edx
addq $0x8, %r15
cmpq %rbp, %r15
jb 0x6171
jmp 0x619d
movq 0x18(%rsp), %rbx
movq %r14, %rdi
callq 0x147fe
movq %r14, %rdi
callq 0x14836
movl $0x48, %edi
callq 0x3260
movq %rax, %r15
movl 0x820(%rbx), %edx
addl 0x4(%rbx), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x13744
movq %r15, 0x8(%rbx,%r13,8)
movq %r14, %rdi
callq 0x145f8
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x60ef
testq %r12, %r12
je 0x61f8
movq %r12, %rdi
callq 0x3300
movb $0x1, (%rbx)
leaq 0x28(%rsp), %rdi
callq 0x134fc
addq $0x518, %rsp            # imm = 0x518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6226
movq %rax, %rbx
movl $0x48, %esi
movq %rbp, %rdi
jmp 0x6252
movq %rax, %rbx
movl $0x30, %esi
movq %r14, %rdi
jmp 0x6252
movq %rax, %rbx
movl $0x48, %esi
movq %r12, %rdi
callq 0x3270
jmp 0x625e
jmp 0x6273
movq %rax, %rbx
movl $0x1010, %esi           # imm = 0x1010
movq %r12, %rdi
callq 0x3270
jmp 0x629c
jmp 0x6273
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x134fc
jmp 0x629c
jmp 0x6273
jmp 0x6273
jmp 0x6273
movq %rax, %rbx
jmp 0x629c
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0x3270
jmp 0x628f
jmp 0x628c
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x145f8
leaq 0x28(%rsp), %rdi
callq 0x134fc
movq %rbx, %rdi
callq 0x3460