// File: adamnovak[P]rlcsa/suffixarray.cpp
// Function: CSA::SuffixArray::SuffixArray(unsigned char*, unsigned int, unsigned int)
SuffixArray::SuffixArray(uchar* _data, uint bytes, uint threads) :
  ok(false),
  data(_data), sa(0), original_sa(0), ranks(0), data_size(bytes),
  sequences(0)
{
  if(_data == 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;
  }

  short_pair* pairs = simpleSuffixSort(this->data, this->data_size, this->sequences, threads);
  this->sa = new uint[this->data_size];
  for(uint i = 0; i < this->data_size; i++) { this->sa[i] = pairs[i].first; }
  delete[] pairs;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movb $0x0, (%rax)
movq -0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq $0x0, 0x18(%rax)
movq $0x0, 0x20(%rax)
movl -0x14(%rbp), %ecx
movl %ecx, 0x28(%rax)
movl $0x0, 0x2c(%rax)
cmpq $0x0, -0x10(%rbp)
je 0x1b31b
cmpl $0x0, -0x14(%rbp)
jne 0x1b342
movq 0x1ccce(%rip), %rdi     # 0x37ff0
leaq 0xe53f(%rip), %rsi      # 0x29868
callq 0x32a0
movq %rax, %rdi
movq 0x1cc78(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x1b483
movl $0x0, -0x1c(%rbp)
movq -0x38(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0x1b383
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x1b376
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x1b378
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1b349
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rax
movl 0x28(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1b3c3
movq 0x1cc4d(%rip), %rdi     # 0x37ff0
leaq 0xe4f8(%rip), %rsi      # 0x298a2
callq 0x32a0
movq %rax, %rdi
movq 0x1cbf7(%rip), %rsi     # 0x37fb0
callq 0x3300
jmp 0x1b483
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x28(%rax), %esi
movl 0x2c(%rax), %edx
movl -0x18(%rbp), %ecx
callq 0x24470
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x28(%rbp)
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 -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0x1b449
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %edx
movl (%rcx,%rdx,8), %edx
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1b41a
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x1b460
movq -0x40(%rbp), %rdi
callq 0x33e0
movq -0x38(%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 $0x40, %rsp
popq %rbp
retq
nopl (%rax)