// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x80(%rsp), %r9d
movq %rdx, %rdi
movq %rcx, %rsi
movq %r8, %rdx
movl %ebp, %ecx
movl $0x1, %r8d
movl %r9d, 0x4(%rsp)
callq 0x6180
leaq (,%rbp,8), %r12
movq %r12, %rdi
callq 0x9fd2
movq %rax, %r15
movq %r12, 0x38(%rsp)
movq %r12, %rdi
callq 0x9fd2
movq %rax, %r12
movl $0x3800, %edi           # imm = 0x3800
callq 0x9fd2
movq %r15, %rdx
movq %rax, %r15
movq %r14, (%rax)
movq $-0x1, 0x8(%rax)
testq %rbp, %rbp
je 0x6ea6
leaq -0x1(%r14), %rax
xorl %ecx, %ecx
movq %rax, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x6e9a
movq %rdx, 0x10(%rsp)
decq %r14
je 0x722d
leaq -0x1(%rbp), %rax
movq %rax, 0x30(%rsp)
leaq 0x18(%r15), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %eax
movq %rbx, 0x40(%rsp)
movq 0x8(%rsp), %rcx
movq (%rcx,%r14,8), %rcx
testq %rcx, %rcx
je 0x7119
leaq -0x1(%rcx), %rdx
cmpl $0x8, 0x4(%rsp)
jne 0x6ef4
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6efa
movzbl (%r13,%rdx), %esi
cmpl $0x8, 0x4(%rsp)
jne 0x6f08
movq (%r13,%rcx,8), %rdi
jmp 0x6f0e
movzbl (%r13,%rcx), %edi
cmpq %rdi, %rsi
jg 0x7119
cmpl $0x8, 0x4(%rsp)
jne 0x6f25
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6f2b
movzbl (%r13,%rdx), %esi
cmpq %r14, (%rbx,%rsi,8)
jae 0x7119
cmpl $0x8, 0x4(%rsp)
jne 0x6f43
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6f49
movzbl (%r13,%rdx), %esi
cmpq 0x28(%rsp), %rsi
je 0x7119
cmpl $0x8, 0x4(%rsp)
jne 0x6f62
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6f68
movzbl (%r13,%rdx), %esi
movq (%rbx,%rsi,8), %rsi
movq 0x8(%rsp), %rdi
movq %rdx, (%rdi,%rsi,8)
cmpl $0x8, 0x4(%rsp)
jne 0x6f83
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x6f89
movzbl (%r13,%rdx), %esi
movq 0x10(%rsp), %rdi
movq (%rdi,%rsi,8), %rdi
movq %rax, %rsi
shlq $0x4, %rsi
addq 0x20(%rsp), %rsi
movq %rsi, %r8
addq $-0x10, %rsi
cmpq %rdi, -0x28(%r8)
jbe 0x6f9e
cmpl $0x8, 0x4(%rsp)
jne 0x6fb9
movq -0x8(%r13,%rcx,8), %rdi
jmp 0x6fbf
movzbl (%r13,%rdx), %edi
movq (%rsi), %rsi
movq 0x10(%rsp), %r8
movq %r14, (%r8,%rdi,8)
cmpl $0x8, 0x4(%rsp)
jne 0x6fd9
movq -0x8(%r13,%rcx,8), %rdi
jmp 0x6fdf
movzbl (%r13,%rdx), %edi
movq (%rbx,%rdi,8), %rdi
movq 0x18(%rsp), %r8
cmpq $0x0, 0x8(%r8,%rdi,8)
js 0x7015
cmpl $0x8, 0x4(%rsp)
jne 0x6ffe
movq -0x8(%r13,%rcx,8), %rdi
jmp 0x7004
movzbl (%r13,%rdx), %edi
incq %rsi
movq (%rbx,%rdi,8), %rdi
movq 0x18(%rsp), %r8
movq %rsi, 0x8(%r8,%rdi,8)
cmpl $0x8, 0x4(%rsp)
jne 0x7023
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x7029
movzbl (%r13,%rdx), %esi
decq (%rbx,%rsi,8)
cmpl $0x8, 0x4(%rsp)
jne 0x703b
movq -0x8(%r13,%rcx,8), %rsi
jmp 0x7041
movzbl (%r13,%rdx), %esi
movq (%rbx,%rsi,8), %rsi
movq 0x8(%rsp), %rdi
cmpq $-0x1, (%rdi,%rsi,8)
je 0x7119
movq $-0x1, %rsi
cmpl $0x8, 0x4(%rsp)
jne 0x7088
movq -0x8(%r13,%rcx,8), %rdi
movq (%rbx,%rdi,8), %rdi
movq 0x8(%rsp), %r8
movq 0x8(%r8,%rdi,8), %rdi
leaq (,%rdi,8), %rdi
addq %r13, %rdi
movq 0x8(%rdi,%rsi,8), %rdi
jmp 0x70a4
movzbl (%r13,%rdx), %edi
movq (%rbx,%rdi,8), %rdi
movq 0x8(%rsp), %r8
movq 0x8(%r8,%rdi,8), %rdi
addq %r13, %rdi
movzbl 0x1(%rsi,%rdi), %edi
cmpl $0x8, 0x4(%rsp)
jne 0x70cf
movq -0x8(%r13,%rcx,8), %r8
movq (%rbx,%r8,8), %r8
movq 0x8(%rsp), %r9
movq (%r9,%r8,8), %r8
leaq (,%r8,8), %r8
addq %r13, %r8
movq 0x8(%r8,%rsi,8), %r8
jmp 0x70eb
movzbl (%r13,%rdx), %r8d
movq (%rbx,%r8,8), %r8
movq 0x8(%rsp), %r9
movq (%r9,%r8,8), %r8
addq %r13, %r8
movzbl 0x1(%rsi,%r8), %r8d
incq %rsi
cmpq %r8, %rdi
je 0x705c
cmpl $0x8, 0x4(%rsp)
jne 0x7105
movq -0x8(%r13,%rcx,8), %rcx
jmp 0x710b
movzbl (%r13,%rdx), %ecx
movq (%rbx,%rcx,8), %rcx
movq 0x18(%rsp), %rdx
movq %rsi, 0x8(%rdx,%rcx,8)
movq %rbp, %rbx
movq 0x18(%rsp), %rcx
cmpq $0x0, (%rcx,%r14,8)
jns 0x7130
movq $0x0, (%rcx,%r14,8)
movq (%rcx,%r14,8), %rcx
testq %rcx, %rcx
movl $0x0, %edx
cmovleq %rdx, %rcx
leaq 0x1(%rax), %rbp
shlq $0x4, %rax
addq 0x20(%rsp), %rax
movq %rax, %rdx
decq %rbp
addq $-0x10, %rax
cmpq %rcx, -0x20(%rdx)
jge 0x714d
movq %r14, -0x8(%rax)
movq %rcx, (%rax)
cmpq $0x37d, %rbp            # imm = 0x37D
jl 0x7207
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x21b0
movl $0x8, %edx
movq %r12, %rdi
movq %rbx, %rsi
leaq -0x11fd(%rip), %rcx     # 0x5f98
callq 0x2090
movq 0x30(%rsp), %rcx
testq %rcx, %rcx
js 0x720f
movl $0x1, %edx
movl $0x1, %eax
movq (%r12,%rcx,8), %rdi
movq %rax, %rsi
shlq $0x4, %rsi
cmpq -0x10(%r15,%rsi), %rdi
jae 0x71f9
leaq (%r15,%rsi), %r8
movq %rdx, %r9
shlq $0x4, %r9
addq %r15, %r9
movq %rdx, %r11
movq (%r9), %r10
incq %rdx
addq $0x10, %r9
cmpq %rbp, %r11
jg 0x71e5
cmpq %rdi, %r10
ja 0x71ce
cmpq %rbp, %r11
jg 0x7214
movq -0x8(%r9), %rdi
movq %r10, (%r8)
movq %rdi, 0x8(%r15,%rsi)
incq %rax
leaq -0x1(%rcx), %rsi
testq %rcx, %rcx
movq %rsi, %rcx
jg 0x71ae
jmp 0x7214
incq %rbp
movq %rbp, %rax
jmp 0x721c
movl $0x1, %eax
cmpq $0x37e, %rax            # imm = 0x37E
jge 0x7261
decq %r14
movq %rbx, %rbp
movq 0x40(%rsp), %rbx
jne 0x6ed0
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq %r15, %rdi
callq 0xa089
movq 0x10(%rsp), %rdi
callq 0xa089
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa089
callq 0x2720