// File: adamnovak[P]rlcsa/suffixarray.cpp
// Function: CSA::SuffixArray::SuffixArray(unsigned char*, unsigned int*, unsigned int, unsigned int)
SuffixArray::SuffixArray(uchar* _data, usint* ra, uint bytes, uint threads) :
  ok(false),
  data(_data), sa(0), original_sa(0), ranks(ra), data_size(bytes),
  sequences(0)
{
  if(_data == 0 || ra == 0 || bytes == 0)
  {
    std::cerr << "Error: No input data given for suffix array construction!" << std::endl;
    return;
  }

  for(uint i = 0; i < this->data_size; i++) { if(this->data[i] == '\0') { this->sequences++; } }
  if(this->data[this->data_size - 1] != '\0')
  {
    std::cerr << "Error: Input data must end with \\0!" << std::endl;
    return;
  }

  pair_type* pairs = new pair_type[this->data_size];
  for(usint i = 0; i < this->data_size; i++) { pairs[i] = pair_type(this->ranks[i], i); }
  #ifdef MULTITHREAD_SUPPORT
  omp_set_num_threads(threads);
  #endif
  parallelSort(pairs, pairs + this->data_size);
  this->sa = new uint[this->data_size];
  for(uint i = 0; i < this->data_size; i++) { this->sa[i] = pairs[i].second; }
  delete[] pairs;

  this->original_sa = this->sa; this->sa += this->sequences;
  this->ok = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movb $0x0, (%rax)
movq -0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq $0x0, 0x18(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x20(%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0x28(%rax)
movl $0x0, 0x2c(%rax)
cmpq $0x0, -0x10(%rbp)
je 0x1b4fa
cmpq $0x0, -0x18(%rbp)
je 0x1b4fa
cmpl $0x0, -0x1c(%rbp)
jne 0x1b521
movq 0x1caef(%rip), %rdi     # 0x37ff0
leaq 0xe360(%rip), %rsi      # 0x29868
callq 0x32a0
movq %rax, %rdi
movq 0x1ca99(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x1b766
movl $0x0, -0x24(%rbp)
movq -0x58(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0x1b562
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1b555
movq -0x58(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x1b557
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1b528
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rax
movl 0x28(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1b5a2
movq 0x1ca6e(%rip), %rdi     # 0x37ff0
leaq 0xe319(%rip), %rsi      # 0x298a2
callq 0x32a0
movq %rax, %rdi
movq 0x1ca18(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x1b766
movq -0x58(%rbp), %rax
movl 0x28(%rax), %eax
movq %rax, -0x68(%rbp)
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x70(%rbp)
callq 0x3030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x60(%rbp)
cmpq $0x0, %rax
je 0x1b627
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0xe530
jmp 0x1b60f
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x78(%rbp)
jne 0x1b5fd
movq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x58(%rbp), %rcx
movl -0x40(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0x1b6a1
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x40(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
leaq -0x48(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0xdec0
movq -0x30(%rbp), %rdi
movl -0x40(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0xdb70
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1b636
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x3590
jmp 0x1b76f
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl 0x28(%rax), %eax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x14f60
movq -0x58(%rbp), %rax
movl 0x28(%rax), %eax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x4c(%rbp)
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0x1b726
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
movl -0x4c(%rbp), %edx
movl 0x4(%rcx,%rdx,8), %edx
movq 0x10(%rax), %rax
movl -0x4c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1b6f6
movq -0x30(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x1b743
movq -0x90(%rbp), %rdi
callq 0x33e0
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x18(%rax)
movl 0x2c(%rax), %edx
movq 0x10(%rax), %rcx
movl %edx, %edx
shlq $0x2, %rdx
addq %rdx, %rcx
movq %rcx, 0x10(%rax)
movb $0x1, (%rax)
addq $0x90, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x3530
nopl (%rax,%rax)