// File: /workspace/github_c_makefile_star2_O3/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
movl %r9d, %ebx
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, 0x8(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0xa0(%rsp), %r9d
movl %ebx, %r13d
testl %ebx, %ebx
movq %r8, 0x30(%rsp)
movl %r9d, 0x4(%rsp)
movl %ebx, 0x24(%rsp)
je 0x683c
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
leaq 0x1(%rcx), %rdx
cmpq %r8, %rdx
jae 0x67fa
movq 0x8(%rsp), %rdx
cmpq $-0x1, 0x8(%rdx,%rcx,8)
je 0x67fa
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
movq %rdx, 0x8(%rbp,%rcx,8)
incq %rax
cmpq %rax, %r13
jne 0x67d1
movq %r12, %rdi
movq %r15, %rsi
movq %r8, %rdx
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x60ac
movq 0x30(%rsp), %rdx
testl %ebx, %ebx
je 0x684f
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
cmpq %rdx, %rcx
jae 0x6832
movq $-0x2, (%rbp,%rcx,8)
incq %rax
cmpq %rax, %r13
jne 0x6820
jmp 0x684f
movq %r12, %rdi
movq %r15, %rsi
movq %r8, %rdx
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x60ac
leaq (,%r13,8), %rbx
movq %rbx, %rdi
callq 0x9fe6
movq %rax, 0x28(%rsp)
movq %rbx, 0x48(%rsp)
movq %rbx, %rdi
callq 0x9fe6
movq %rax, %r14
movl $0x3800, %edi           # imm = 0x3800
callq 0x9fe6
movq %rax, %rbx
movq $0x0, (%rax)
movq $-0x1, 0x8(%rax)
cmpl $0x0, 0x24(%rsp)
je 0x68a8
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
callq 0x2150
incq (%r15)
cmpq $0x0, 0x30(%rsp)
movl 0x4(%rsp), %r11d
je 0x6c47
movq 0x30(%rsp), %rax
decq %rax
movq %rax, 0x50(%rsp)
leaq 0x20(%rbx), %rax
movq %rax, 0x60(%rsp)
leaq 0x28(%rbx), %rax
movq %rax, 0x58(%rsp)
movl $0x1, %eax
xorl %ecx, %ecx
movq %r15, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x8(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
cmpq $-0x1, %rdx
je 0x691f
movq (%rbp,%rcx,8), %rsi
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
cmpq %rdi, %rsi
jne 0x69bb
cmpl $0x8, %r11d
jne 0x692a
movq (%r12,%rdx,8), %rsi
jmp 0x692f
incq %rcx
movq %rcx, %rdx
jmp 0x6c39
movzbl (%r12,%rdx), %esi
movq (%r15,%rsi,8), %rsi
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %r8
cmpq %r8, -0x8(%rdi,%rsi,8)
jae 0x69ab
leaq (%r12,%rdx), %rdi
leaq (%r12,%rdx,8), %r8
movq $-0x1, %rsi
cmpl $0x8, %r11d
jne 0x697b
movq 0x8(%r8,%rsi,8), %r9
movq (%r12,%rdx,8), %r10
movq (%r15,%r10,8), %r10
movq 0x8(%rsp), %r11
movq -0x8(%r11,%r10,8), %r10
leaq (%r12,%r10,8), %r10
movq 0x8(%r10,%rsi,8), %r10
jmp 0x699c
movzbl 0x1(%rdi,%rsi), %r9d
movzbl (%rdi), %r10d
movq (%r15,%r10,8), %r10
movq 0x8(%rsp), %r11
movq -0x8(%r11,%r10,8), %r10
addq %r12, %r10
movzbl 0x1(%rsi,%r10), %r10d
incq %rsi
cmpq %r10, %r9
movl 0x4(%rsp), %r11d
je 0x6953
jmp 0x69ad
xorl %esi, %esi
movq %rsi, (%rbp,%rcx,8)
movq 0x8(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
testq %rdx, %rdx
movq 0x28(%rsp), %r10
je 0x69d2
cmpl $0x8, %r11d
jne 0x69d6
movq -0x8(%r12,%rdx,8), %r8
jmp 0x69dc
xorl %edi, %edi
jmp 0x69e8
movzbl -0x1(%r12,%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), %rbp
shlq $0x4, %rax
movq 0x60(%rsp), %r8
leaq (%r8,%rax), %r15
addq 0x58(%rsp), %rax
movq %r15, %r9
movq %rax, %r8
decq %rbp
addq $-0x10, %r15
addq $-0x10, %rax
cmpq %rsi, -0x28(%r9)
jge 0x6a0b
leaq 0x1(%rcx), %rax
movq %rax, 0x40(%rsp)
movq %rax, -0x10(%r15)
movq %rsi, -0x8(%r15)
leaq 0x1(%rbp), %rax
movq %r8, %rsi
addq $-0x10, %r8
cmpq %rdi, -0x28(%rsi)
ja 0x6a37
testq %rdx, %rdx
je 0x6b4b
movq (%r8), %rsi
leaq -0x1(%rdx), %rdi
cmpl $0x8, %r11d
jne 0x6a7f
movq -0x8(%r12,%rdx,8), %r8
cmpq (%r12,%rdx,8), %r8
setl %r9b
cmpq 0x38(%rsp), %r8
sete %r10b
orb %r9b, %r10b
je 0x6ae7
movq 0x10(%rsp), %r9
jmp 0x6b27
movzbl -0x1(%r12,%rdx), %r8d
movb (%r12,%rdx), %dl
cmpb %dl, %r8b
jb 0x6ad9
cmpq 0x38(%rsp), %r8
je 0x6ad9
movq 0x10(%rsp), %r9
movq (%r9,%r8,8), %rdx
movq 0x8(%rsp), %r11
movq %rdi, (%r11,%rdx,8)
movzbl (%r12,%rdi), %edx
movq (%r9,%rdx,8), %rdx
movq 0x18(%rsp), %r10
movq (%r10,%rdx,8), %r8
addq %r8, %rsi
incq %rsi
movq %rsi, (%r10,%rdx,8)
movzbl (%r12,%rdi), %edx
incq (%r9,%rdx,8)
movq (%r11,%rcx,8), %rdx
movl 0x4(%rsp), %r11d
movb (%r12,%rdx), %dl
movzbl %dl, %edx
movq 0x10(%rsp), %rsi
movq (%rsi,%rdx,8), %rsi
jmp 0x6b2f
movq 0x10(%rsp), %r9
movq (%r9,%r8,8), %r8
movq 0x8(%rsp), %r11
movq %rdi, (%r11,%r8,8)
movq -0x8(%r12,%rdx,8), %rdi
movq (%r9,%rdi,8), %rdi
movq 0x18(%rsp), %r10
movq (%r10,%rdi,8), %r8
addq %r8, %rsi
incq %rsi
movq %rsi, (%r10,%rdi,8)
movq -0x8(%r12,%rdx,8), %rdx
incq (%r9,%rdx,8)
movq (%r11,%rcx,8), %rdx
movl 0x4(%rsp), %r11d
movq (%r12,%rdx,8), %rdx
movq (%r9,%rdx,8), %rsi
decq %rsi
cmpq %rcx, %rsi
jae 0x6b4b
cmpq 0x38(%rsp), %rdx
je 0x6b4b
movq 0x8(%rsp), %rdx
movq $-0x1, (%rdx,%rcx,8)
cmpq $0x37e, %rbp            # imm = 0x37E
jl 0x6c14
movq %r14, %rdi
movq 0x28(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x21b0
movl $0x8, %edx
movq %r14, %rdi
movq %r13, %rsi
leaq -0xc84(%rip), %rcx      # 0x5ef8
callq 0x2090
movq $-0x1, (%r15)
cmpl $0x0, 0x24(%rsp)
je 0x6c20
movl $0x1, %ecx
xorl %edx, %edx
movl $0x1, %eax
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %r11d
movq %rax, %rsi
shlq $0x4, %rsi
movq (%r14,%rdx,8), %r8
incq %r8
cmpq %r8, -0x10(%rbx,%rsi)
jae 0x6bf8
leaq (%rbx,%rsi), %rdi
movq %rcx, %r9
shlq $0x4, %r9
addq %rbx, %r9
movq (%r9), %r10
incq %rcx
addq $0x10, %r9
cmpq %r8, %r10
jb 0x6bcc
leaq -0x1(%rcx), %r8
cmpq %rbp, %r8
jle 0x6be9
movq %r8, %rcx
jmp 0x6bf8
movq -0x8(%r9), %r8
movq %r10, (%rdi)
movq %r8, 0x8(%rbx,%rsi)
incq %rax
incq %rdx
cmpq %r13, %rdx
jne 0x6ba9
cmpq $0x37e, %rax            # imm = 0x37E
movq 0x18(%rsp), %rbp
movq 0x40(%rsp), %rdx
jl 0x6c39
jmp 0x6c6f
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbp
jmp 0x6c34
movl $0x1, %eax
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbp
movl 0x4(%rsp), %r11d
movq 0x40(%rsp), %rdx
movq %rdx, %rcx
cmpq 0x30(%rsp), %rdx
jne 0x68ec
movq %rbx, %rdi
callq 0xa09d
movq 0x28(%rsp), %rdi
callq 0xa09d
movq %r14, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa09d
callq 0x2698