// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/src/gsacak.c
// Function: induceSAs0_generalized_LCP
void induceSAs0_generalized_LCP(uint_t *SA, int_t* LCP,
  uint_t *s, uint_t *bkt,
  uint_t n, uint_t K, int cs, uint_t separator) {
  uint_t i, j;

  // find the end of each bucket.
  getBuckets_k((int_t*)s, bkt, n, K, true, cs);

  #if RMQ_S == 1
  int_t *M=(int_t *)malloc(sizeof(int_t)*K);
  for(i=0;i<K;i++) M[i]=I_MAX;
  #elif RMQ_S == 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_S+2)*sizeof(t_pair_k));
  int_t top = 0;
  stack_push_k(STACK, &top, n, -1); //init
  for(i=0;i<K;i++) last_occ[i]=n-1;
  #endif

  for(i=n-1; i>0; i--){
    if(SA[i]>0) {
      j=SA[i]-1;
      if(chr(j)<=chr(j+1) && bkt[chr(j)]<i)// induce S-type
        if(chr(j)!=separator){
	  SA[bkt[chr(j)]]=j;

          #if RMQ_S == 1
  	    if(LCP[bkt[chr(j)]+1]>=0) 
  	      LCP[bkt[chr(j)]+1]=M[chr(j)]+1;
  	  
	    if(LCP[bkt[chr(j)]]>0) 
  	      LCP[bkt[chr(j)]]=I_MAX;

          #elif RMQ_S == 2
            int_t min = I_MAX, end = top-1; 
  
  	    int_t last=last_occ[chr(j)];
            while(STACK[end].idx<=last) end--;
  
            min=STACK[(end+1)].lcp;
            last_occ[chr(j)] = i;
  
  	    if(LCP[bkt[chr(j)]+1]>=0) 
              LCP[bkt[chr(j)]+1]=min+1;
          #endif
  

          #if RMQ_S == 1
  	  M[chr(j)] = I_MAX;
          #endif
  
          bkt[chr(j)]--;
 
  	  if(SA[bkt[chr(j)]]!=U_MAX) {//L/S-seam
            int_t l=0;	
            while(chr(SA[bkt[chr(j)]+1]+l)==chr(SA[bkt[chr(j)]]+l))++l;
            LCP[bkt[chr(j)]+1]=l;
  	  }
  	}
     }

    if(LCP[i]<0) LCP[i]=0;
    #if RMQ_S == 1
      int_t k;
      for(k=0; k<K; k++) if(M[k]>LCP[i]) M[k] = LCP[i];
    #elif RMQ_S == 2

      int_t lcp=max(0,LCP[i]);

      while(STACK[(top)-1].lcp>=lcp) (top)--;
      stack_push_k(STACK, &top, i, lcp);

      if(top>=STACK_SIZE_S){

          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;

	   for(j=K-1;j>=0; j--){

            if(tmp[j] < STACK[end-1].idx){

  	      while(STACK[curr].idx>tmp[j] && curr < top) curr++;
	      if(curr>=top) break;
	      stack_push_k(STACK, &end, STACK[curr].idx, STACK[curr].lcp);
	      curr++;
            }
          } 

          if(end>=STACK_SIZE_S){
            fprintf(stderr,"ERROR: induceSAl0_LCP\n");
            exit(1);
          }
          top = end;
      }
      #endif

  }

  LCP[0]=0;

  //variant 1
  #if RMQ_S == 1
  free(M);
  #elif RMQ_S == 2
  free(STACK);
  free(last_occ);
  free(tmp);
  #endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rdx, %r12
movq %rsi, 0x8(%rsp)
movq %rdi, %r13
movq 0x88(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x80(%rsp), %r9d
pushq $0x1
popq %r8
movq %rdx, %rdi
movq %rcx, 0x28(%rsp)
movq %rcx, %rsi
movq %r14, %rdx
movl %ebx, %ecx
movl %r9d, 0x4(%rsp)
callq 0x5b5d
leaq (,%rbx,8), %r15
movq %r15, %rdi
callq 0x92c8
movq %rax, %rbp
movq %r15, 0x38(%rsp)
movq %r15, %rdi
callq 0x92c8
movq %rax, %r15
movl $0x3800, %edi           # imm = 0x3800
callq 0x92c8
movq %r14, (%rax)
movq %rax, 0x18(%rsp)
orq $-0x1, 0x8(%rax)
leaq -0x1(%r14), %rax
xorl %ecx, %ecx
cmpq %rcx, %rbx
je 0x671c
movq %rax, (%rbp,%rcx,8)
incq %rcx
jmp 0x670d
movq %rbp, 0x10(%rsp)
movq %rbx, 0x40(%rsp)
movq 0x18(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x30(%rsp)
pushq $0x1
popq %rax
movq 0x28(%rsp), %r10
movl 0x4(%rsp), %r11d
movq %r14, %rbx
decq %rbx
je 0x69fa
movq -0x8(%r13,%r14,8), %rcx
testq %rcx, %rcx
je 0x68d9
leaq -0x1(%rcx), %rdx
cmpl $0x8, %r11d
jne 0x679d
movq -0x8(%r12,%rcx,8), %rdi
cmpq (%r12,%rcx,8), %rdi
jg 0x68d9
movq (%r10,%rdi,8), %rsi
cmpq %rbx, %rsi
setae %r8b
cmpq 0x20(%rsp), %rdi
sete %dil
orb %r8b, %dil
jne 0x68d9
movq %rdx, (%r13,%rsi,8)
movq -0x8(%r12,%rcx,8), %rsi
jmp 0x67d4
movzbl -0x1(%r12,%rcx), %esi
cmpb (%r12,%rcx), %sil
ja 0x68d9
movq (%r10,%rsi,8), %rdi
cmpq %rbx, %rdi
setae %r8b
cmpq 0x20(%rsp), %rsi
sete %sil
orb %r8b, %sil
jne 0x68d9
movq %rdx, (%r13,%rdi,8)
movzbl (%r12,%rdx), %esi
movq 0x10(%rsp), %rdi
movq (%rdi,%rsi,8), %rdi
movq %rax, %rsi
shlq $0x4, %rsi
addq 0x30(%rsp), %rsi
movq %rsi, %r8
addq $-0x10, %rsi
cmpq %rdi, -0x28(%r8)
jbe 0x67e9
cmpl $0x8, %r11d
jne 0x6803
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6808
movzbl (%r12,%rdx), %edi
movq (%rsi), %rsi
movq 0x10(%rsp), %r8
movq %rbx, (%r8,%rdi,8)
movq (%r10,%rdi,8), %rdi
movq 0x8(%rsp), %r8
cmpq $0x0, 0x8(%r8,%rdi,8)
js 0x6848
cmpl $0x8, %r11d
jne 0x6832
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6837
movzbl (%r12,%rdx), %edi
incq %rsi
movq (%r10,%rdi,8), %rdi
movq 0x8(%rsp), %r8
movq %rsi, 0x8(%r8,%rdi,8)
cmpl $0x8, %r11d
jne 0x685e
movq -0x8(%r12,%rcx,8), %rsi
decq (%r10,%rsi,8)
movq -0x8(%r12,%rcx,8), %rsi
jmp 0x686c
movzbl (%r12,%rdx), %esi
decq (%r10,%rsi,8)
movzbl (%r12,%rdx), %esi
movq (%r10,%rsi,8), %rsi
cmpq $-0x1, (%r13,%rsi,8)
je 0x68d9
xorl %esi, %esi
cmpl $0x8, %r11d
jne 0x68a7
movq -0x8(%r12,%rcx,8), %rdi
movq (%r10,%rdi,8), %rdi
movq (%r13,%rdi,8), %r8
movq 0x8(%r13,%rdi,8), %r9
leaq (%r12,%r9,8), %r9
movq (%r9,%rsi,8), %r9
leaq (%r12,%r8,8), %r8
cmpq (%r8,%rsi,8), %r9
je 0x68ca
jmp 0x68cf
movzbl (%r12,%rdx), %edi
movq (%r10,%rdi,8), %rdi
movq 0x8(%r13,%rdi,8), %r8
addq %r12, %r8
movb (%rsi,%r8), %r8b
movq (%r13,%rdi,8), %r9
addq %r12, %r9
cmpb (%rsi,%r9), %r8b
jne 0x68cf
incq %rsi
jmp 0x687a
movq 0x8(%rsp), %rcx
movq %rsi, 0x8(%rcx,%rdi,8)
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%r14,8), %rcx
testq %rcx, %rcx
jns 0x68f5
movq 0x8(%rsp), %rcx
andq $0x0, -0x8(%rcx,%r14,8)
xorl %ecx, %ecx
leaq 0x1(%rax), %rbp
shlq $0x4, %rax
addq 0x30(%rsp), %rax
movq %rax, %rdx
decq %rbp
addq $-0x10, %rax
cmpq %rcx, -0x20(%rdx)
jge 0x6902
movq %rbx, -0x8(%rax)
movq %rcx, (%rax)
leaq 0x1(%rbp), %rax
movq %rbx, %r14
cmpq $0x37d, %rbp            # imm = 0x37D
jl 0x6741
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x21b0
movq %r15, %rdi
movq 0x40(%rsp), %r14
movq %r14, %rsi
pushq $0x8
popq %rdx
leaq -0xf80(%rip), %rcx      # 0x59d4
callq 0x2090
movl 0x4(%rsp), %r11d
pushq $0x1
popq %rdx
movq %rdx, %rax
movq %r14, %rcx
movq 0x18(%rsp), %r14
movq %rax, %rsi
shlq $0x4, %rsi
leaq (%r14,%rsi), %rdi
decq %rcx
js 0x69c2
movq (%r15,%rcx,8), %r8
cmpq -0x10(%rdi), %r8
jae 0x6977
movq %rdx, %r9
shlq $0x4, %r9
addq %r14, %r9
movq %rdx, %r11
movq (%r9), %r10
incq %rdx
addq $0x10, %r9
cmpq %rbp, %r11
jg 0x69a7
cmpq %r8, %r10
ja 0x6990
cmpq %rbp, %r11
movl 0x4(%rsp), %r11d
jg 0x69c2
movq -0x8(%r9), %r8
movq %r10, (%rdi)
movq %r8, 0x8(%r14,%rsi)
incq %rax
jmp 0x696c
movq %rbx, %r14
cmpq $0x37e, %rax            # imm = 0x37E
movq 0x28(%rsp), %r10
jb 0x6741
movq 0x761b(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x40c9(%rip), %rdi      # 0xaab0
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
pushq $0x1
popq %rdi
callq 0x2270
movq 0x8(%rsp), %rax
andq $0x0, (%rax)
movq 0x18(%rsp), %rdi
callq 0x939e
movq 0x10(%rsp), %rdi
callq 0x939e
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x939e