// File: adamnovak[P]rlcsa/misc/utils.cpp
// Function: CSA::simpleSuffixSort(unsigned int const*, unsigned int, unsigned int)
short_pair*
simpleSuffixSort(const usint* sequence, uint n, uint threads)
{
  if(sequence == 0 || n == 0) { return 0; }

  skew_pair* pairs = (skew_pair*)new short_pair[n * sizeof(skew_pair) / sizeof(short_pair) + 1];
  uint* keys = new uint[n];               // In text order.
  std::vector<ss_range> unsorted;
  threads = std::max(threads, (uint)1);
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif

  // Initialize pairs.
  #pragma omp parallel for schedule(static)
  for(uint i = 0; i < n; i++) { pairs[i].first = i; pairs[i].second = sequence[i]; }

  // Sort according to first character.
  parallelSort(pairs, pairs + n, skew_comparator);
  unsorted.push_back(ss_range(0, n - 1));
  uint total = setRanks(pairs, keys, n, unsorted, threads, 1);

  if(sizeof(usint) < 2 * sizeof(uint))
  {
    return prefixDoubling(packPairs(pairs, n), keys, unsorted, n, threads, total, 1);
  }
  else
  {
    return prefixTripling(pairs, keys, unsorted, n, threads, total, 1);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x240f2
cmpl $0x0, -0x14(%rbp)
jne 0x240ff
movq $0x0, -0x8(%rbp)
jmp 0x24327
movl -0x14(%rbp), %eax
shlq $0x3, %rax
shrq $0x3, %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x88(%rbp)
callq 0x3030
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, -0x78(%rbp)
cmpq $0x0, %rax
je 0x2419e
movq -0x78(%rbp), %rax
movq -0x80(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0xe530
jmp 0x24180
movq -0x98(%rbp), %rcx
movq -0xa0(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x90(%rbp)
jne 0x2416b
movq -0x78(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x3030
movq %rax, -0x38(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x14dd0
movl $0x1, -0x54(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x6360
movq %rax, -0xa8(%rbp)
jmp 0x241de
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x24261
movl -0x58(%rbp), %edx
movq -0x20(%rbp), %rax
movl -0x58(%rbp), %ecx
movl %edx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movl -0x58(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movl -0x58(%rbp), %ecx
movl %edx, 0x4(%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x241f1
movq -0x88(%rbp), %rsi
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x3590
jmp 0x24334
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x14ec0
jmp 0x24334
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %eax
leaq (%rdi,%rax,8), %rsi
leaq 0x14664(%rip), %rdx     # 0x388d7
callq 0x25040
jmp 0x2427a
movl $0x0, -0x64(%rbp)
movl -0x14(%rbp), %eax
decl %eax
movl %eax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x64(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0xe260
jmp 0x2429c
leaq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x14df0
jmp 0x242ab
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %r8d
leaq -0x50(%rbp), %rcx
movl $0x1, %r9d
callq 0x24cd0
movl %eax, -0xac(%rbp)
jmp 0x242d1
movl -0xac(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x23be0
movq %rax, %rdi
movq -0x38(%rbp), %rsi
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x6c(%rbp), %r9d
movq %rsp, %rax
movl $0x1, (%rax)
leaq -0x50(%rbp), %rdx
callq 0x23c40
movq %rax, -0xb8(%rbp)
jmp 0x24313
movq -0xb8(%rbp), %rax
movq %rax, -0x8(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x14ec0
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x3530
nopl (%rax)