// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/src/gsacak.c
// Function: induceSAl0_generalized_LCP
void induceSAl0_generalized_LCP(uint_t *SA, int_t *LCP,
  uint_t *s, uint_t *bkt,
  uint_t n, unsigned int K, int cs, uint_t separator) {
  uint_t i, j;

  for(i=0;i<K;i++)
    if(bkt[i]+1<n) if(SA[bkt[i]+1]!=U_MAX) LCP[bkt[i]+1]=I_MIN;

  // find the head of each bucket.
  getBuckets_k((int_t*)s, bkt, n, K, false, cs);

  for(i=0;i<K;i++)
    if(bkt[i]<n) LCP[bkt[i]]=-2;

  #if RMQ_L == 1 
  int_t *M=(int_t *)malloc(sizeof(int_t)*K);
  for(i=0;i<K;i++){
    M[i]=I_MAX;
  }
  #elif RMQ_L == 2
  uint_t* last_occ = (uint_t*) malloc(K*sizeof(uint_t));
  uint_t* tmp = (uint_t*) malloc(K*sizeof(uint_t));

  t_pair_k* STACK = (t_pair_k*) malloc((STACK_SIZE_L+2)*sizeof(t_pair_k));
  int_t top = 0;
  stack_push_k(STACK, &top, 0, -1);//init
  for(i=0;i<K;i++) last_occ[i]=0;
  #endif 

  #if DEBUG
  printf("inducing..\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("LCP\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", LCP[i]);
  printf("\n\n");
  #endif

  bkt[0]++; // skip the virtual sentinel.
  for(i=0; i<n; i++){
    if(SA[i]!=U_MAX){

      if(LCP[i]==I_MIN){ //is a L/S-seam position
  	  int_t l=0;
	  if(SA[bkt[chr(SA[i])]-1]<n-1)	
   	    while(chr(SA[i]+l)==chr(SA[bkt[chr(SA[i])]-1]+l))++l;
  	  LCP[i]=l;
      }
      #if RMQ_L == 1
        uint_t k;
        for(k=0; k<K; k++) if(M[k]>LCP[i]) M[k] = max(0,LCP[i]);
      #elif RMQ_L == 2
        int_t min_lcp=0;
        uint_t last;

        if(!SA[i]) last = 0;
        else{
          last = last_occ[chr(SA[i]-1)];
          last_occ[chr(SA[i]-1)] = i+1;
        }
 
        int_t lcp=max(0,LCP[i]);
        while(STACK[(top)-1].lcp>=lcp) (top)--;	

        stack_push_k(STACK, &top, i+1, lcp);
        j = top-1;

        while(STACK[j].idx>last) j--;
        min_lcp=STACK[(j+1)].lcp;

      #endif

      if(SA[i]>0) {
        j=SA[i]-1;
        if(chr(j)>=chr(j+1))
  	if(chr(j)!=separator){//gsa-is
            SA[bkt[chr(j)]]=j;

            #if RMQ_L == 1
  	      LCP[bkt[chr(j)]]+=M[chr(j)]+1;
    	      M[chr(j)] = I_MAX;
            #elif RMQ_L == 2
  	      LCP[bkt[chr(j)]]+=min_lcp+1; 
            #endif
  
            bkt[chr(j)]++;
	}
      	if(bkt[chr(SA[i])]-1<i){ //if is LMS-type
	  if(chr(SA[i])!=separator)
	  SA[i]=U_MAX;
        }

      }
      #if RMQ_L == 2
      if(top>STACK_SIZE_L){//if stack is full

        int_t j;
        memcpy(tmp, last_occ, K*sizeof(uint_t));
        qsort(tmp, K, sizeof(uint_t), compare_k);
       
        int_t curr=1, end=1;
        STACK[top].idx=U_MAX;
        for(j=0;j<K; j++){

          if(STACK[end-1].idx < tmp[j]+1){

            while(STACK[curr].idx<tmp[j]+1) curr++;

            if(curr<top){
	      stack_push_k(STACK, &end, STACK[curr].idx, STACK[curr].lcp);
	      curr++;
            }
	  }
        }
 
        if(end>=STACK_SIZE_L){
          fprintf(stderr,"ERROR: induceSAl0_LCP\n");
          exit(1);
        }

        top = end;
      }
      #endif
    }
  }
  #if RMQ_L == 1
  free(M);
  #elif RMQ_L == 2
  free(STACK);
  free(last_occ);
  free(tmp);
  #endif

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r8
movq %rsi, %r12
movq %rdi, %r13
movq 0xa8(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x4(%rsp)
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
movl %r9d, %ebp
xorl %eax, %eax
cmpq %rax, %rbp
je 0x627c
movq (%r14,%rax,8), %rcx
leaq 0x1(%rcx), %rdx
cmpq %rbx, %rdx
jae 0x6277
cmpq $-0x1, 0x8(%r13,%rcx,8)
je 0x6277
movq %rsi, 0x8(%r12,%rcx,8)
incq %rax
jmp 0x6258
xorl %r15d, %r15d
movq %r8, 0x30(%rsp)
movq %r8, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %r9d, %ecx
xorl %r8d, %r8d
movl 0x4(%rsp), %r9d
callq 0x5b5d
cmpq %r15, %rbp
je 0x62b8
movq (%r14,%r15,8), %rax
cmpq %rbx, %rax
jae 0x62b3
movq $-0x2, (%r12,%rax,8)
incq %r15
jmp 0x629d
movq %r13, 0x60(%rsp)
leaq (,%rbp,8), %r13
movq %r13, %rdi
callq 0x92c8
movq %rax, %r15
movq %r13, 0x48(%rsp)
movq %r13, %rdi
movq 0x60(%rsp), %r13
callq 0x92c8
movq %rax, 0x10(%rsp)
movl $0x3800, %edi           # imm = 0x3800
callq 0x92c8
andq $0x0, (%rax)
movq %rax, 0x28(%rsp)
orq $-0x1, 0x8(%rax)
xorl %eax, %eax
movq 0x30(%rsp), %r11
cmpq %rax, %rbp
je 0x6315
andq $0x0, (%r15,%rax,8)
incq %rax
jmp 0x6306
movq %r15, 0x20(%rsp)
movq %r12, 0x38(%rsp)
incq (%r14)
leaq -0x1(%rbx), %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rax
leaq 0x20(%rax), %rcx
movq %rcx, 0x58(%rsp)
addq $0x28, %rax
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rax
xorl %r10d, %r10d
movq %r10, %rcx
movq 0x38(%rsp), %r15
movl 0x4(%rsp), %r12d
cmpq %rcx, %rbx
je 0x664f
movq (%r13,%rcx,8), %rdx
cmpq $-0x1, %rdx
jne 0x636e
incq %rcx
jmp 0x6355
movq (%r15,%rcx,8), %rsi
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
cmpq %rdi, %rsi
jne 0x6405
cmpl $0x8, %r12d
jne 0x6391
movq (%r11,%rdx,8), %rsi
jmp 0x6396
movzbl (%r11,%rdx), %esi
movq (%r14,%rsi,8), %rsi
movq 0x40(%rsp), %rdi
cmpq %rdi, -0x8(%r13,%rsi,8)
jae 0x63fa
leaq (%r11,%rdx), %rdi
leaq (%r11,%rdx,8), %r8
pushq $-0x1
popq %rsi
cmpl $0x8, %r12d
jne 0x63d4
movq 0x8(%r8,%rsi,8), %r9
movq (%r11,%rdx,8), %r10
movq (%r14,%r10,8), %r10
movq -0x8(%r13,%r10,8), %r10
leaq (%r11,%r10,8), %r10
movq 0x8(%r10,%rsi,8), %r10
jmp 0x63f0
movzbl 0x1(%rdi,%rsi), %r9d
movzbl (%rdi), %r10d
movq (%r14,%r10,8), %r10
movq -0x8(%r13,%r10,8), %r10
addq %r11, %r10
movzbl 0x1(%rsi,%r10), %r10d
incq %rsi
cmpq %r10, %r9
je 0x63b1
jmp 0x63fc
xorl %esi, %esi
movq %rsi, (%r15,%rcx,8)
movq (%r13,%rcx,8), %rdx
testq %rdx, %rdx
movq 0x20(%rsp), %r10
je 0x641c
cmpl $0x8, %r12d
jne 0x6420
movq -0x8(%r11,%rdx,8), %r8
jmp 0x6426
xorl %edi, %edi
jmp 0x6432
movzbl -0x1(%r11,%rdx), %r8d
movq (%r10,%r8,8), %rdi
leaq 0x1(%rcx), %r9
movq %r9, (%r10,%r8,8)
testq %rsi, %rsi
movl $0x0, %r8d
cmovleq %r8, %rsi
leaq 0x1(%rax), %r15
shlq $0x4, %rax
movq 0x58(%rsp), %r8
leaq (%r8,%rax), %r12
addq 0x50(%rsp), %rax
movq %r12, %r9
movq %rax, %r8
decq %r15
addq $-0x10, %r12
addq $-0x10, %rax
cmpq %rsi, -0x28(%r9)
jge 0x6455
leaq 0x1(%rcx), %r10
movq %r10, -0x10(%r12)
movq %rsi, -0x8(%r12)
leaq 0x1(%r15), %rax
movq %r8, %rsi
addq $-0x10, %r8
cmpq %rdi, -0x28(%rsi)
ja 0x647e
testq %rdx, %rdx
je 0x6568
movq (%r8), %rdi
leaq -0x1(%rdx), %rsi
cmpl $0x8, 0x4(%rsp)
jne 0x64c9
movq %r10, 0x8(%rsp)
movq -0x8(%r11,%rdx,8), %r8
cmpq (%r11,%rdx,8), %r8
setl %r9b
cmpq 0x18(%rsp), %r8
sete %r10b
orb %r9b, %r10b
je 0x64f4
movq 0x8(%rsp), %r10
jmp 0x6541
movzbl -0x1(%r11,%rdx), %r9d
movb (%r11,%rdx), %r8b
cmpb %r8b, %r9b
jb 0x654b
cmpq 0x18(%rsp), %r9
je 0x654b
movq %r10, 0x8(%rsp)
movq (%r14,%r9,8), %r8
movq %rsi, (%r13,%r8,8)
movzbl (%r11,%rsi), %r8d
jmp 0x6502
movq (%r14,%r8,8), %r8
movq %rsi, (%r13,%r8,8)
movq -0x8(%r11,%rdx,8), %r8
movq (%r14,%r8,8), %r8
movq 0x38(%rsp), %r10
movq (%r10,%r8,8), %r9
addq %r9, %rdi
incq %rdi
movq %rdi, (%r10,%r8,8)
cmpl $0x8, 0x4(%rsp)
jne 0x6527
movq -0x8(%r11,%rdx,8), %rdx
jmp 0x652c
movzbl (%r11,%rsi), %edx
incq (%r14,%rdx,8)
movq (%r13,%rcx,8), %rdx
cmpl $0x8, 0x4(%rsp)
movq 0x8(%rsp), %r10
jne 0x6547
movq (%r11,%rdx,8), %rdx
jmp 0x654f
movb (%r11,%rdx), %r8b
movzbl %r8b, %edx
movq (%r14,%rdx,8), %rsi
decq %rsi
cmpq %rcx, %rsi
jae 0x6568
cmpq 0x18(%rsp), %rdx
je 0x6568
orq $-0x1, (%r13,%rcx,8)
cmpq $0x37e, %r15            # imm = 0x37E
jl 0x6348
movq %r10, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x21b0
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
pushq $0x8
popq %rdx
leaq -0xbcc(%rip), %rcx      # 0x59d4
callq 0x2090
orq $-0x1, (%r12)
pushq $0x1
popq %rdx
movq %rdx, %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %r11
cmpq %rbp, %rcx
je 0x6615
movq %rax, %rsi
shlq $0x4, %rsi
movq 0x10(%rsp), %rdi
movq (%rdi,%rcx,8), %r8
incq %r8
cmpq %r8, -0x10(%r11,%rsi)
jae 0x6610
leaq (%r11,%rsi), %rdi
movq %rdx, %r9
shlq $0x4, %r9
addq %r11, %r9
movq (%r9), %r10
incq %rdx
addq $0x10, %r9
cmpq %r8, %r10
jb 0x65e4
leaq -0x1(%rdx), %r8
cmpq %r15, %r8
jle 0x6601
movq %r8, %rdx
jmp 0x6610
movq -0x8(%r9), %r8
movq %r10, (%rdi)
movq %r8, 0x8(%r11,%rsi)
incq %rax
incq %rcx
jmp 0x65b7
cmpq $0x37e, %rax            # imm = 0x37E
movq 0x30(%rsp), %r11
movq 0x8(%rsp), %r10
jl 0x6348
movq 0x79c6(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x4474(%rip), %rdi      # 0xaab0
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
pushq $0x1
popq %rdi
callq 0x2270
movq 0x28(%rsp), %rdi
callq 0x939e
movq 0x20(%rsp), %rdi
callq 0x939e
movq 0x10(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x939e