// File: /workspace/github_cpp_makefile_star2_O2/adamnovak[P]rlcsa/misc/utils.cpp
// Function: CSA::prefixTripling(std::pair<unsigned int, unsigned int>*, unsigned int*, std::vector<std::pair<unsigned int, unsigned int>, std::allocator<std::pair<unsigned int, unsigned int>>>&, unsigned int, unsigned int, unsigned int, unsigned int)
short_pair*
prefixTripling(skew_pair* pairs, uint* keys, std::vector<ss_range>& unsorted, uint n, uint threads, uint total, uint h)
{
  const usint PACK_FACTOR = sizeof(usint) * CHAR_BIT / 2;

  // Triple prefix length until sorted.
  while(total > 0)
  {
    uint chunk = std::max((size_t)1, unsorted.size() / (threads * threads));
    #pragma omp parallel for schedule(dynamic, chunk)
    for(uint i = 0; i < unsorted.size(); i++)
    {
      // Set sort keys for the current range.
      skew_pair* limit = pairs + unsorted[i].second;
      for(skew_pair* curr = pairs + unsorted[i].first; curr <= limit; ++curr)
      {
        curr->second = (usint)(keys[curr->first + h]) << PACK_FACTOR;
        if(n - h > curr->first + h) { curr->second += keys[curr->first + 2 * h]; }
      }
      sequentialSort(pairs + unsorted[i].first, pairs + unsorted[i].second + 1, skew_comparator);
    }
    total = setRanks(pairs, keys, n, unsorted, threads, chunk);
    h *= 3;
//    std::cout << "Sorted with h = " << h << ", unsorted total = " << total << " (" << unsorted.size() << " ranges)" << std::endl;
  }

  #pragma omp parallel for schedule(static)
  for(uint i = 0; i < n; i++) { pairs[i].second = keys[i]; }
  delete[] keys; keys = 0;
  return packPairs(pairs, n);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %eax
movl %ecx, %r13d
movq %rdx, %r12
movq %rsi, %r14
movl 0x60(%rsp), %r15d
movl %r8d, 0x8(%rsp)
movl %r8d, %ecx
imull %ecx, %ecx
movq %rcx, 0x10(%rsp)
movq %rdi, (%rsp)
leaq 0x8(%rdi), %rcx
movq %rcx, 0x20(%rsp)
movl %r13d, 0xc(%rsp)
testl %eax, %eax
je 0x112d9
movq (%r12), %rcx
movq 0x8(%r12), %rsi
movq %rsi, %rax
subq %rcx, %rax
sarq $0x3, %rax
xorl %edi, %edi
xorl %edx, %edx
divq 0x10(%rsp)
cmpq $0x1, %rax
adcq $0x0, %rax
movq %rax, 0x18(%rsp)
subl %r15d, %r13d
leal (%r15,%r15), %ebp
movl %edi, %ebx
subq %rcx, %rsi
sarq $0x3, %rsi
cmpq %rbx, %rsi
jbe 0x112af
movl (%rcx,%rbx,8), %edx
movl 0x4(%rcx,%rbx,8), %eax
movq (%rsp), %rsi
leaq (%rsi,%rax,8), %rax
leaq (%rsi,%rdx,8), %rdi
movq %rdi, %rdx
cmpq %rax, %rdx
ja 0x1128f
movl (%rdx), %esi
leal (%rsi,%r15), %r9d
movl (%r14,%r9,4), %r8d
shll $0x10, %r8d
movl %r8d, 0x4(%rdx)
cmpl %r9d, %r13d
jbe 0x11289
addl %ebp, %esi
addl (%r14,%rsi,4), %r8d
movl %r8d, 0x4(%rdx)
addq $0x8, %rdx
jmp 0x11263
movl 0x4(%rcx,%rbx,8), %eax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
callq 0x12209
leal 0x1(%rbx), %edi
movq (%r12), %rcx
movq 0x8(%r12), %rsi
jmp 0x1123f
movq (%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %r13d
movl %r13d, %edx
movq %r12, %rcx
movl 0x8(%rsp), %r8d
movq 0x18(%rsp), %r9
callq 0x11790
leal (%r15,%r15,2), %r15d
jmp 0x11207
movl %r13d, %eax
xorl %ecx, %ecx
movq (%rsp), %rsi
cmpq %rcx, %rax
je 0x112f4
movl (%r14,%rcx,4), %edx
movl %edx, 0x4(%rsi,%rcx,8)
incq %rcx
jmp 0x112e2
testq %r14, %r14
je 0x11301
movq %r14, %rdi
callq 0x32b0
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq