// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movl $0x10, -0x28(%rbp)
cmpl $0x0, -0x24(%rbp)
jbe 0x24058
movq $0x1, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0x157f0
movl -0x20(%rbp), %ecx
imull -0x20(%rbp), %ecx
movl %ecx, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x11410
movq (%rax), %rax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
callq 0x157f0
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jae 0x2402d
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0x78(%rbp), %rax
movl 0x4(%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0x70(%rbp), %rax
movl (%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
ja 0x23fb1
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
addl 0x10(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %ecx
shll $0x10, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x1c(%rbp), %eax
subl 0x10(%rbp), %eax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
addl 0x10(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x23fa1
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
movl 0x10(%rbp), %edx
shll %edx
addl %edx, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %ecx
movq -0x58(%rbp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
jmp 0x23fa3
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
jmp 0x23f48
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0x90(%rbp), %rax
movl (%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdi
movl 0x4(%rax), %eax
shlq $0x3, %rax
addq %rax, %rsi
addq $0x8, %rsi
leaq 0x148bd(%rip), %rdx     # 0x388d7
callq 0x25010
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x23ed3
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x20(%rbp), %r8d
movl -0x2c(%rbp), %r9d
callq 0x24cd0
movl %eax, -0x24(%rbp)
imull $0x3, 0x10(%rbp), %eax
movl %eax, 0x10(%rbp)
jmp 0x23e8c
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x24087
movq -0x10(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x8(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %edx, 0x4(%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2405f
movq -0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x240a4
movq -0x98(%rbp), %rdi
callq 0x33e0
movq $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x23be0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)