// File: /workspace/github_cpp_makefile_star2_O3/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 %r8d, 0x8(%rsp)
movl %ecx, %ebx
movq %rsi, %r14
movq %rdi, (%rsp)
testl %r9d, %r9d
je 0x15aff
movq %rdx, %r12
movl 0x60(%rsp), %ebp
movl 0x8(%rsp), %eax
imull %eax, %eax
movq %rax, 0x10(%rsp)
movq (%rsp), %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movl %ebx, 0xc(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rsi
movq %rsi, %rax
subq %rcx, %rax
sarq $0x3, %rax
xorl %edx, %edx
divq 0x10(%rsp)
cmpq $0x1, %rax
adcq $0x0, %rax
movq %rax, 0x18(%rsp)
cmpq %rcx, %rsi
je 0x15ad4
movl 0xc(%rsp), %r13d
subl %ebp, %r13d
leal (,%rbp,2), %r15d
xorl %ebx, %ebx
movl (%rcx,%rbx,8), %edx
movl 0x4(%rcx,%rbx,8), %eax
movq (%rsp), %rsi
leaq (%rsi,%rdx,8), %rdi
cmpl %eax, %edx
ja 0x15aa8
leaq (%rsi,%rax,8), %rax
movq %rdi, %rdx
movl (%rdx), %esi
leal (%rsi,%rbp), %r9d
movl (%r14,%r9,4), %r8d
shll $0x10, %r8d
movl %r8d, 0x4(%rdx)
cmpl %r9d, %r13d
jbe 0x15a9b
addl %r15d, %esi
addl (%r14,%rsi,4), %r8d
movl %r8d, 0x4(%rdx)
addq $0x8, %rdx
cmpq %rax, %rdx
jbe 0x15a79
movl 0x4(%rcx,%rbx,8), %eax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
leaq 0xad8f(%rip), %rdx      # 0x20847
callq 0x16338
incl %ebx
movq (%r12), %rcx
movq 0x8(%r12), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rbx, %rax
ja 0x15a5f
movq (%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
movq %r12, %rcx
movl 0x8(%rsp), %r8d
movq 0x18(%rsp), %r9
callq 0x16143
leal (%rbp,%rbp,2), %ebp
testl %eax, %eax
jne 0x15a1d
testl %ebx, %ebx
je 0x15b1d
movl %ebx, %eax
xorl %ecx, %ecx
movq (%rsp), %rsi
movl (%r14,%rcx,4), %edx
movl %edx, 0x4(%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x15b0b
jmp 0x15b26
testq %r14, %r14
movq (%rsp), %rax
je 0x15b32
movq %r14, %rdi
callq 0x3300
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq