// File: adamnovak[P]rlcsa/misc/utils.cpp
// Function: unsigned int CSA::setRanks<std::pair<unsigned int, unsigned int>>(std::pair<unsigned int, 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)
uint
setRanks(T* pairs, uint* keys, uint n, std::vector<ss_range>& unsorted, uint threads, uint chunk)
{
  uint total = 0;

  #ifdef MULTITHREAD_SUPPORT
  std::vector<ss_range> buffers[threads];
  uint subtotals[threads];
  for(uint i = 0; i < threads; i++) { subtotals[i] = 0; }
  #else
  std::vector<ss_range> next_unsorted;
  #endif

  #pragma omp parallel for schedule(dynamic, chunk)
  for(uint i = 0; i < unsorted.size(); i++)
  {
    #ifdef MULTITHREAD_SUPPORT
    uint thread = omp_get_thread_num();
    #endif
    T* prev = pairs + unsorted[i].first;
    keys[prev->first] = unsorted[i].first;
    T* limit = pairs + unsorted[i].second;
    while(prev < limit)
    {
      T* curr = prev + 1;
      if(curr->second != prev->second)
      {
        ++prev; keys[prev->first] = prev - pairs;
        continue;
      }
      keys[curr->first] = prev - pairs;

      for(++curr; curr <= limit && curr->second == prev->second; ++curr)
      {
        keys[curr->first] = prev - pairs;
      }

      #ifdef MULTITHREAD_SUPPORT
      buffers[thread].push_back(ss_range(prev - pairs, (curr - 1) - pairs));
      subtotals[thread] += curr - prev;
      #else
      next_unsorted.push_back(ss_range(prev - pairs, (curr - 1) - pairs));
      total += curr - prev;
      #endif
      prev = curr; if(prev <= limit) { keys[prev->first] = prev - pairs; }
    }
  }

  #ifdef MULTITHREAD_SUPPORT
  unsorted.clear();
  for(uint i = 0; i < threads; i++)
  {
    unsorted.insert(unsorted.end(), buffers[i].begin(), buffers[i].end());
    total += subtotals[i];
  }
  #else
  unsorted.assign(next_unsorted.begin(), next_unsorted.end());
  #endif

  return total;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x14dd0
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x157f0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
jae 0x24f59
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0xb8(%rbp), %rax
movl (%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movl (%rax), %edx
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, %esi
callq 0x14e60
movq %rax, %rcx
movq -0xb0(%rbp), %rax
movl 0x4(%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x24f49
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x58(%rbp), %rcx
cmpl 0x4(%rcx), %eax
je 0x24e03
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x24dad
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rcx), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rcx
xorl %eax, %eax
cmpq -0x60(%rbp), %rcx
movb %al, -0xb9(%rbp)
ja 0x24e56
movq -0x68(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x58(%rbp), %rcx
cmpl 0x4(%rcx), %eax
sete %al
movb %al, -0xb9(%rbp)
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0x24e62
jmp 0x24e8e
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rcx), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
jmp 0x24e2d
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
addq $-0x8, %rax
sarq $0x3, %rax
movq %rax, -0x80(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x26940
jmp 0x24ecb
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x14df0
jmp 0x24eda
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rax
subq %rax, %rcx
sarq $0x3, %rcx
movl -0x2c(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
ja 0x24f44
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x24f44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x14ec0
jmp 0x24fcb
jmp 0x24dad
jmp 0x24f4b
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x24d09
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0xe2e0
movq -0xd0(%rbp), %rdi
movq %rax, -0x98(%rbp)
callq 0xe350
movq -0xc8(%rbp), %rdi
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x26970
jmp 0x24faa
movl -0x2c(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x14ec0
movl -0xd4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x3530
nopw (%rax,%rax)