// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/sparseSA.cpp
// Function: sparseSA::construct()
void sparseSA::construct(){
    if(K > 1) {
        long bucketNr = 1;
        int *intSA = new int[N/K+1];  
        for(int i = 0; i < N/K; i++) intSA[i] = i; // Init SA.
        int* t_new = new int[N/K+1];
        long* BucketBegin = new long[256]; // array to save current bucket beginnings
        radixStep(t_new, intSA, bucketNr, BucketBegin, 0, N/K-1, 0); // start radix sort
        t_new[N/K] = 0; // Terminate new integer string.
        delete[] BucketBegin;

        // Suffix sort integer text.
        cerr << "# suffixsort()" << endl;
        suffixsort(t_new, intSA, N/K, bucketNr, 0);
        cerr << "# DONE suffixsort()" << endl;

        delete[] t_new;

        // Translate suffix array.
        SA.resize(N/K);
        for (long i=0; i<N/K; i++) SA[i] = (unsigned int)intSA[i+1] * K;
        delete[] intSA;

        // Build ISA using sparse SA.
        ISA.resize(N/K);
        for(long i = 0; i < N/K; i++) { ISA[SA[i]/K] = i; }
    }
    else {
        SA.resize(N);
        ISA.resize(N);
        int char2int[UCHAR_MAX+1]; // Map from char to integer alphabet.

        // Zero char2int mapping.
        for (int i=0; i<=UCHAR_MAX; i++) char2int[i]=0;

        // Determine which characters are used in the string S.
        for (long i = 0; i < N; i++) char2int[(int)S[i]]=1;

        // Count the size of the alphabet.
        int alphasz = 0;
        for(int i=0; i <= UCHAR_MAX; i++) {
        if (char2int[i]) char2int[i]=alphasz++;
        else char2int[i] = -1;
        }

        // Remap the alphabet.
        for(long i = 0; i < N; i++) ISA[i] = (int)S[i];
        for (long i = 0; i < N; i++) ISA[i]=char2int[ISA[i]] + 1;
        // First "character" equals 1 because of above plus one, l=1 in suffixsort().
        int alphalast = alphasz + 1;

        // Use LS algorithm to construct the suffix array.
        int *SAint = (int*)(&SA[0]);
        suffixsort(&ISA[0], SAint , N-1, alphalast, 1);
    }

    cerr << "size concatenated sequence = " << N << endl;

    LCP.resize(N/K);
    cerr << "number of suffix array values = " << N/K << endl;
    // Use algorithm by Kasai et al to construct LCP array.
    computeLCP();  // SA + ISA -> LCP
    LCP.init();
    if(!hasSufLink){
        {
          vector<int> tmp;
          ISA.swap(tmp);
        }
    }
    if(hasChild){
        CHILD.resize(N/K);
        //Use algorithm by Abouelhoda et al to construct CHILD array
        computeChild();
    }
    if(hasKmer){
        KMR.resize(TABLESIZE, saTuple_t());
        computeKmer();
    }

    NKm1 = N/K-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdi, %rbx
movq 0xe8(%rdi), %rcx
cmpq $0x2, %rcx
jl 0xee8a
movq $0x1, 0x10(%rsp)
movq 0x38(%rbx), %rax
cqto
idivq %rcx
movq %rax, %r12
incq %rax
shrq $0x3e, %rax
leaq 0x4(,%r12,4), %rcx
xorl %r13d, %r13d
negq %rax
movl $0x0, %r15d
sbbq %r15, %r15
orq %rcx, %r15
movq %r15, %rdi
callq 0x7030
movq %rax, %r14
testq %r12, %r12
movl $0x0, %eax
cmovgq %r12, %rax
cmpq %r13, %rax
je 0xeec5
movl %r13d, (%r14,%r13,4)
incq %r13
jmp 0xee7c
leaq 0x70(%rbx), %r14
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x110ba
leaq 0x88(%rbx), %r15
movq 0x38(%rbx), %rsi
movq %r15, %rdi
callq 0x1114c
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0xefc9
andl $0x0, 0x10(%rsp,%rax,4)
incq %rax
jmp 0xeeaf
movq %r15, %rdi
callq 0x7030
movq %rax, %r15
movl $0x800, %edi            # imm = 0x800
callq 0x7030
movq %rax, %r13
decq %r12
movq %r12, (%rsp)
andq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %r9d, %r9d
callq 0xf222
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
andl $0x0, (%r15,%rax,4)
movq %r13, %rdi
callq 0x73f0
movq 0x3b0bf(%rip), %r13     # 0x49fe8
leaq 0x2744a(%rip), %rsi     # 0x3637a
movq %r13, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
movl (%r12), %ecx
movq %r15, %rdi
movq %r14, %rsi
movl %eax, %edx
xorl %r8d, %r8d
callq 0x2f934
leaq 0x27421(%rip), %rsi     # 0x36389
movq %r13, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movq %r15, %rdi
callq 0x73f0
leaq 0x70(%rbx), %rdi
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
movq %rax, %rsi
callq 0x110ba
movq 0xe8(%rbx), %rcx
movq 0x38(%rbx), %rax
movq 0x70(%rbx), %rsi
cqto
idivq %rcx
testq %rax, %rax
cmovleq %rbp, %rax
cmpq %rbp, %rax
je 0xeff6
movl 0x4(%r14,%rbp,4), %edx
imull %ecx, %edx
movl %edx, (%rsi,%rbp,4)
incq %rbp
jmp 0xefb4
movq 0x38(%rbx), %rdx
movq 0x50(%rbx), %rsi
xorl %ecx, %ecx
testq %rdx, %rdx
movl $0x0, %eax
cmovgq %rdx, %rax
cmpq %rcx, %rax
je 0xf05d
movsbq (%rsi,%rcx), %rdi
movl $0x1, 0x10(%rsp,%rdi,4)
incq %rcx
jmp 0xefdf
movq %r14, %rdi
callq 0x73f0
leaq 0x88(%rbx), %rdi
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
movq %rax, %rsi
callq 0x1114c
movq 0xe8(%rbx), %rsi
movq 0x38(%rbx), %rax
movq 0x70(%rbx), %rdi
cqto
idivq %rsi
movq %rax, %rcx
movq 0x88(%rbx), %r8
xorl %r9d, %r9d
testq %rax, %rax
cmovleq %r9, %rcx
cmpq %r9, %rcx
je 0xf0e2
movl (%rdi,%r9,4), %eax
xorl %edx, %edx
idivq %rsi
movl %r9d, (%r8,%rax,4)
incq %r9
jmp 0xf042
xorl %edi, %edi
xorl %ecx, %ecx
cmpq $0x100, %rdi            # imm = 0x100
je 0xf097
movl 0x10(%rsp,%rdi,4), %r8d
cmpl $0x1, %r8d
movl $0x0, %r9d
sbbl %r9d, %r9d
cmpl $0x1, %r8d
movl %ecx, %r8d
sbbl $-0x1, %r8d
orl %ecx, %r9d
movl %r9d, 0x10(%rsp,%rdi,4)
incq %rdi
movl %r8d, %ecx
jmp 0xf061
movq (%r15), %rdi
xorl %r8d, %r8d
cmpq %r8, %rax
je 0xf0b0
movsbl (%rsi,%r8), %r9d
movl %r9d, (%rdi,%r8,4)
incq %r8
jmp 0xf09d
movq (%r15), %rsi
xorl %edi, %edi
cmpq %rdi, %rax
je 0xf0cf
movslq (%rsi,%rdi,4), %r8
movl 0x10(%rsp,%r8,4), %r8d
incl %r8d
movl %r8d, (%rsi,%rdi,4)
incq %rdi
jmp 0xf0b5
incl %ecx
movq (%r14), %rsi
movq (%r15), %rdi
decl %edx
pushq $0x1
popq %r8
callq 0x2f934
movq 0x3aeff(%rip), %r15     # 0x49fe8
leaq 0x272ad(%rip), %rsi     # 0x3639d
movq %r15, %rdi
callq 0x72b0
movq 0x38(%rbx), %rsi
movq %rax, %rdi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
leaq 0xa0(%rbx), %r14
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
movq %r14, %rdi
movq %rax, %rsi
callq 0x110ec
leaq 0x27289(%rip), %rsi     # 0x363bb
movq %r15, %rdi
callq 0x72b0
movq %rax, %rdi
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
movq %rax, %rsi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
movq %rbx, %rdi
callq 0xd6fa
movq %r14, %rdi
callq 0xd07a
cmpb $0x0, 0xf1(%rbx)
jne 0xf1a4
movq 0x98(%rbx), %rax
movups 0x88(%rbx), %xmm0
xorps %xmm1, %xmm1
andq $0x0, 0x98(%rbx)
movups %xmm1, 0x88(%rbx)
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
callq 0xa268
cmpb $0x1, 0xf0(%rbx)
jne 0xf1d1
leaq 0xd0(%rbx), %rdi
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
movq %rax, %rsi
callq 0x1114c
movq %rbx, %rdi
callq 0xd7a6
cmpb $0x1, 0xf8(%rbx)
jne 0xf1fc
leaq 0x100(%rbx), %rdi
leaq 0x10(%rsp), %rdx
andq $0x0, (%rdx)
movl $0x100000, %esi         # imm = 0x100000
callq 0x111b0
movq %rbx, %rdi
callq 0xda7e
movq 0x38(%rbx), %rax
cqto
idivq 0xe8(%rbx)
decq %rax
movq %rax, 0x48(%rbx)
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq