// File: /workspace/github_cpp_makefile_star2_O1/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, %ebx
movq %rdx, %r15
movq %rsi, %r14
movb 0x558(%rsp), %r13b
cmpl $0x1, %r9d
adcl $0x0, %r9d
movl %r9d, 0xc(%rsp)
movl 0x81c(%rdi), %eax
movl %eax, 0x10(%rsp)
testl %eax, %eax
jne 0x5df9
movl $0x1, 0x81c(%rdi)
movq %rdi, (%rsp)
leaq 0x28(%rsp), %rdi
movl $0x10, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x13026
movl %ebx, %ebp
testb %r13b, %r13b
je 0x5e9f
movq %r15, 0x18(%rsp)
movq (%rsp), %rax
movl $0x0, 0x820(%rax)
movq %rbx, 0x20(%rsp)
testl %ebx, %ebx
je 0x5f0d
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %eax, %eax
movq (%rsp), %rbx
cmpb $0x0, (%r14,%r15)
je 0x5e57
incl %r13d
jmp 0x5e95
movl %eax, %ecx
cmpq %rcx, %r15
je 0x5f15
incl 0x820(%rbx)
addl %r13d, %r12d
decl %r12d
leaq 0x28(%rsp), %rdi
movl %r12d, %esi
callq 0x13036
movl 0x81c(%rbx), %ecx
addl %ecx, %r12d
movl %r12d, %eax
xorl %edx, %edx
divl %ecx
addl %r13d, %edx
subl %edx, %r12d
leal 0x1(%r15), %eax
incq %r15
cmpq %r15, %rbp
jne 0x5e4b
jmp 0x5f15
movq (%rsp), %rax
movl $0x1, 0x820(%rax)
leaq 0x110(%rsp), %rdi
movl $0x10, %esi
movl $0x10, %edx
callq 0x13026
leal -0x1(%rbx), %esi
leaq 0x110(%rsp), %rdi
callq 0x13036
movl $0x48, %edi
callq 0x3260
movq %rax, %r12
leaq 0x110(%rsp), %rsi
movq %rax, %rdi
movl %ebx, %edx
callq 0x1272c
movq (%rsp), %rax
movq %r12, 0x828(%rax)
leaq 0x110(%rsp), %rdi
callq 0x13030
jmp 0x5f6b
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%rsp), %rcx
cmpl $0x0, 0x820(%rcx)
movq 0x20(%rsp), %rbx
je 0x605d
cmpl %ebx, %eax
jne 0x605d
movl $0x48, %edi
callq 0x3260
movq %rax, %r15
addl %r12d, %r13d
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
movl %r13d, %edx
callq 0x1272c
movq (%rsp), %rax
movq %r15, 0x828(%rax)
movq 0x18(%rsp), %r15
movb 0x558(%rsp), %r13b
leaq 0x110(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x3150
testl %ebx, %ebx
je 0x5f99
xorl %eax, %eax
movzbl (%r14,%rax), %ecx
incl 0x110(%rsp,%rcx,4)
incq %rax
cmpq %rax, %rbp
jne 0x5f85
testb %r13b, %r13b
je 0x5fa9
movl $0x0, 0x110(%rsp)
movl $0x1010, %edi           # imm = 0x1010
callq 0x3260
movq %rax, %r12
leaq 0x110(%rsp), %rsi
movq %rax, %rdi
callq 0xcca8
movq (%rsp), %rcx
movq %r12, 0x808(%rcx)
movl 0x800(%r12), %eax
movl %eax, 0x4(%rcx)
testb %r13b, %r13b
je 0x5ffd
testq %r15, %r15
je 0x60c8
movq %r15, %rdi
movl %ebx, %esi
movl 0xc(%rsp), %edx
callq 0x155b5
jmp 0x60e0
leal 0x1(%rbx), %r13d
leaq (,%r13,8), %r15
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
testl %r13d, %r13d
je 0x6026
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x3150
testl %ebx, %ebx
je 0x6040
xorl %eax, %eax
movzbl (%r14,%rax), %ecx
incl %ecx
movl %ecx, 0x4(%r12,%rax,8)
incq %rax
cmpq %rax, %rbp
jne 0x602c
movl $0x0, 0x4(%r12,%rbp,8)
movq %r12, %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
callq 0x1570e
jmp 0x60e6
movq 0x18f8c(%rip), %rbx     # 0x1eff0
leaq 0x115a5(%rip), %rsi     # 0x17610
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 0x18f5a(%rip), %rdi     # 0x1eff0
callq 0x3040
movq %rax, %rdi
callq 0x3190
testq %r14, %r14
sete %al
movb 0x560(%rsp), %cl
xorb $0x1, %cl
orb %al, %cl
jne 0x62d1
movq %r14, %rdi
callq 0x3300
jmp 0x62d1
movq (%rsp), %rax
movl 0x820(%rax), %edx
movq %r14, %rdi
movl %ebx, %esi
movl 0xc(%rsp), %ecx
callq 0x157ee
movq %rax, %r12
movl %ebx, %r13d
testq %r14, %r14
sete %al
movb 0x560(%rsp), %cl
xorb $0x1, %cl
orb %al, %cl
jne 0x6102
movq %r14, %rdi
callq 0x3300
cmpl $0x0, 0x10(%rsp)
movq (%rsp), %rbp
je 0x6194
movq 0x550(%rsp), %r15
testq %r15, %r15
je 0x6143
movl $0x30, %edi
callq 0x3260
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl 0xc(%rsp), %ecx
callq 0xb790
movq (%rsp), %rbp
jmp 0x6175
movl $0x30, %edi
callq 0x3260
movq %rax, %r14
movq (%rsp), %rbp
movq 0x828(%rbp), %rdx
movl 0x4(%rbp), %ecx
movl 0x81c(%rbp), %r8d
movq %rax, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %r9d
callq 0xb4f0
movq %r14, 0x810(%rbp)
cmpq $0x0, 0x18(%r14)
setne 0x818(%rbp)
cmpq $0x0, 0x28(%r14)
setne 0x819(%rbp)
testl %r13d, %r13d
je 0x61ba
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 0x619e
xorl %r13d, %r13d
leaq 0x98(%rsp), %r14
movq 0x808(%rbp), %rax
movq (%rax,%r13,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
cmpl %edx, %ecx
jbe 0x61e9
movq $0x0, 0x8(%rbp,%r13,8)
jmp 0x62b0
movl (%rax,%r13,8), %ebx
movl 0x820(%rbp), %r15d
movq (%rax,%r13,8), %rbp
movq %r14, %rdi
movl 0x14(%rsp), %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x14082
leaq (%r12,%rbx,8), %rax
leaq (%rax,%r15,8), %r15
movq %rbp, %rax
shrq $0x20, %rax
subl %ebp, %eax
leaq 0x8(,%rax,8), %rbp
movabsq $0x7fffffff8, %rax      # imm = 0x7FFFFFFF8
andq %rax, %rbp
movl (%r15), %esi
movl $0x1, %edx
cmpq $0x9, %rbp
jb 0x626e
addq %r15, %rbp
addq $0x8, %r15
movl $0x1, %edx
leal (%rdx,%rsi), %eax
cmpl %eax, (%r15)
jne 0x6255
incl %edx
jmp 0x6265
movq %r14, %rdi
callq 0x1425a
movl (%r15), %esi
movl $0x1, %edx
addq $0x8, %r15
cmpq %rbp, %r15
jb 0x6249
movq %r14, %rdi
callq 0x1425a
movq %r14, %rdi
callq 0x14294
movl $0x48, %edi
callq 0x3260
movq %rax, %r15
movq (%rsp), %rbp
movl 0x820(%rbp), %edx
addl 0x4(%rbp), %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1337c
movq %r15, 0x8(%rbp,%r13,8)
movq %r14, %rdi
callq 0x1409a
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x61c5
testq %r12, %r12
je 0x62cd
movq %r12, %rdi
callq 0x3300
movb $0x1, (%rbp)
leaq 0x28(%rsp), %rdi
callq 0x13030
addq $0x518, %rsp            # imm = 0x518
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x62fc
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
jmp 0x6328
movq %rax, %rbx
movl $0x30, %esi
movq %r14, %rdi
jmp 0x6328
movq %rax, %rbx
movl $0x48, %esi
movq %r12, %rdi
callq 0x3270
jmp 0x6334
jmp 0x6349
movq %rax, %rbx
movl $0x1010, %esi           # imm = 0x1010
movq %r12, %rdi
callq 0x3270
jmp 0x6372
jmp 0x6349
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x13030
jmp 0x6372
jmp 0x6349
jmp 0x6349
jmp 0x6349
movq %rax, %rbx
jmp 0x6372
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0x3270
jmp 0x6365
jmp 0x6362
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x1409a
leaq 0x28(%rsp), %rdi
callq 0x13030
movq %rbx, %rdi
callq 0x3460