// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %r8, %r14
movq %rdx, %r13
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
movq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x80(%rsp), %r9d
movq %rdx, %rdi
movq %rcx, 0x28(%rsp)
movq %rcx, %rsi
movq %r8, %rdx
movl %r15d, %ecx
movl $0x1, %r8d
movl %r9d, 0xc(%rsp)
callq 0x60ac
leaq (,%r15,8), %r12
movq %r12, %rdi
callq 0x9fe6
movq %rax, %rbx
movq %r12, 0x40(%rsp)
movq %r12, %rdi
callq 0x9fe6
movq %rax, %r12
movl $0x3800, %edi           # imm = 0x3800
callq 0x9fe6
movq %rbx, %rsi
movq %rax, %rbx
movq %r14, (%rax)
movq $-0x1, 0x8(%rax)
decq %r14
movq %r15, 0x10(%rsp)
testq %r15, %r15
je 0x6da9
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %rax
andq $-0x2, %rax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x4d76(%rip), %xmm1     # 0xbab0
xorl %ecx, %ecx
movdqa 0x4d7c(%rip), %xmm2     # 0xbac0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x4d7c(%rip), %xmm4     # 0xbad0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x6d89
movq %r14, (%rsi,%rcx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x6d9c
movq %r14, 0x8(%rsi,%rcx,8)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x6d54
movq %rsi, (%rsp)
testq %r14, %r14
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0xc(%rsp), %r15d
je 0x7085
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x38(%rsp)
leaq 0x18(%rbx), %rax
movq %rax, 0x30(%rsp)
movl $0x1, %eax
movq (%rbp,%r14,8), %rcx
testq %rcx, %rcx
je 0x6f7d
leaq -0x1(%rcx), %rdx
cmpl $0x8, %r15d
jne 0x6e31
movq -0x8(%r13,%rcx,8), %rdi
cmpq (%r13,%rcx,8), %rdi
jg 0x6f7d
movq (%r10,%rdi,8), %rsi
cmpq %r14, %rsi
setae %r8b
cmpq 0x18(%rsp), %rdi
sete %dil
orb %r8b, %dil
jne 0x6f7d
movq %rdx, (%rbp,%rsi,8)
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6e6a
movzbl -0x1(%r13,%rcx), %esi
cmpb (%r13,%rcx), %sil
ja 0x6f7d
movq (%r10,%rsi,8), %rdi
cmpq %r14, %rdi
setae %r8b
cmpq 0x18(%rsp), %rsi
sete %sil
orb %r8b, %sil
jne 0x6f7d
movq %rdx, (%rbp,%rdi,8)
movzbl (%r13,%rdx), %esi
movq (%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 0x6e7e
movq (%rsi), %rsi
cmpl $0x8, %r15d
jne 0x6ec5
movq -0x8(%r13,%rcx,8), %rdi
movq (%rsp), %r8
movq %r14, (%r8,%rdi,8)
movq (%r10,%rdi,8), %rdi
cmpq $0x0, 0x8(%r11,%rdi,8)
js 0x6eb5
incq %rsi
movq %rsi, 0x8(%r11,%rdi,8)
movq -0x8(%r13,%rcx,8), %rsi
decq (%r10,%rsi,8)
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6f06
movzbl (%r13,%rdx), %edi
leal (,%rdi,8), %r8d
movq (%rsp), %r9
movq %r14, (%r9,%r8)
movq (%r10,%r8), %r8
cmpq $0x0, 0x8(%r11,%r8,8)
js 0x6ef9
incq %rsi
movq %rsi, 0x8(%r11,%r8,8)
movzbl (%r13,%rdx), %edi
movq (%r10,%rdi,8), %r8
decq %r8
movq %r8, (%r10,%rdi,8)
movzbl (%r13,%rdx), %esi
movq (%r10,%rsi,8), %rsi
cmpq $-0x1, (%rbp,%rsi,8)
je 0x6f7d
xorl %esi, %esi
cmpl $0x8, %r15d
jne 0x6f4f
movq -0x8(%r13,%rcx,8), %rdi
movq (%r10,%rdi,8), %rdi
movq (%rbp,%rdi,8), %r8
movq 0x8(%rbp,%rdi,8), %r9
leaq (,%r9,8), %r9
addq %r13, %r9
movq (%r9,%rsi,8), %r9
leaq (,%r8,8), %r8
addq %r13, %r8
cmpq (%r8,%rsi,8), %r9
je 0x6f73
jmp 0x6f78
movzbl (%r13,%rdx), %edi
movq (%r10,%rdi,8), %rdi
movq 0x8(%rbp,%rdi,8), %r8
addq %r13, %r8
movb (%rsi,%r8), %r8b
movq (%rbp,%rdi,8), %r9
addq %r13, %r9
cmpb (%rsi,%r9), %r8b
jne 0x6f78
incq %rsi
jmp 0x6f14
movq %rsi, 0x8(%r11,%rdi,8)
movq (%r11,%r14,8), %rcx
testq %rcx, %rcx
jns 0x6f90
movq $0x0, (%r11,%r14,8)
xorl %ecx, %ecx
leaq 0x1(%rax), %r15
shlq $0x4, %rax
addq 0x30(%rsp), %rax
movq %rax, %rdx
decq %r15
addq $-0x10, %rax
cmpq %rcx, -0x20(%rdx)
jge 0x6f9d
movq %r14, -0x8(%rax)
movq %rcx, (%rax)
cmpq $0x37d, %r15            # imm = 0x37D
jl 0x7060
movq %r12, %rdi
movq (%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x21b0
movl $0x8, %edx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
leaq -0x10ee(%rip), %rcx     # 0x5ef8
callq 0x2090
movq 0x38(%rsp), %rcx
testq %rcx, %rcx
js 0x7068
movl $0x1, %edx
movl $0x1, %eax
movq (%r12,%rcx,8), %rdi
movq %rax, %rsi
shlq $0x4, %rsi
cmpq -0x10(%rbx,%rsi), %rdi
jae 0x704a
leaq (%rbx,%rsi), %r8
movq %rdx, %r9
shlq $0x4, %r9
addq %rbx, %r9
movq %rdx, %r11
movq (%r9), %r10
incq %rdx
addq $0x10, %r9
cmpq %r15, %r11
jg 0x7036
cmpq %rdi, %r10
ja 0x701f
cmpq %r15, %r11
jg 0x7056
movq -0x8(%r9), %rdi
movq %r10, (%r8)
movq %rdi, 0x8(%rbx,%rsi)
incq %rax
leaq -0x1(%rcx), %rsi
testq %rcx, %rcx
movq %rsi, %rcx
jg 0x6fff
cmpq $0x37e, %rax            # imm = 0x37E
jl 0x706d
jmp 0x70b3
incq %r15
movq %r15, %rax
jmp 0x706d
movl $0x1, %eax
decq %r14
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0xc(%rsp), %r15d
jne 0x6de0
movq $0x0, (%r11)
movq %rbx, %rdi
callq 0xa09d
movq (%rsp), %rdi
callq 0xa09d
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa09d
callq 0x26bd