// File: adamnovak[P]rlcsa/misc/utils.cpp
// Function: CSA::prefixDoubling(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*
prefixDoubling(short_pair* pairs, uint* keys, std::vector<ss_range>& unsorted, uint n, uint threads, uint total, uint h)
{
  // Double 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.
      short_pair* limit = pairs + unsorted[i].second;
      for(short_pair* curr = pairs + unsorted[i].first; curr <= limit; ++curr)
      {
        curr->second = keys[curr->first + h];
      }
      sequentialSort(pairs + unsorted[i].first, pairs + unsorted[i].second + 1, key_comparator);
    }
    total = setRanks(pairs, keys, n, unsorted, threads, chunk);
    h *= 2;
//    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 pairs;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %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)
cmpl $0x0, -0x24(%rbp)
jbe 0x23df4
movq $0x1, -0x30(%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, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x11410
movq (%rax), %rax
movl %eax, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
callq 0x157f0
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jae 0x23dc8
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0x70(%rbp), %rax
movl 0x4(%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0x68(%rbp), %rax
movl (%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
ja 0x23d52
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movl (%rcx), %ecx
addl 0x10(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
jmp 0x23d21
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0x88(%rbp), %rax
movl (%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdi
movl 0x4(%rax), %eax
shlq $0x3, %rax
addq %rax, %rsi
addq $0x8, %rsi
leaq 0x14b23(%rip), %rdx     # 0x388d8
callq 0x24fe0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x23cac
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x20(%rbp), %r8d
movl -0x28(%rbp), %r9d
callq 0x24cd0
movl %eax, -0x24(%rbp)
movl 0x10(%rbp), %eax
shll %eax
movl %eax, 0x10(%rbp)
jmp 0x23c65
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x23e23
movq -0x10(%rbp), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x8(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %edx, 0x4(%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x23dfb
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x23e40
movq -0x90(%rbp), %rdi
callq 0x33e0
movq $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)