// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r8, %r10
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, %r8
movq %rdi, 0x10(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0xb0(%rsp), %ebx
movl %r9d, %eax
movq %rax, 0x20(%rsp)
movl %r9d, 0xc(%rsp)
testl %r9d, %r9d
je 0x6999
xorl %eax, %eax
movq (%r13,%rax,8), %rcx
leaq 0x1(%rcx), %rdx
cmpq %r10, %rdx
jae 0x698f
movq 0x10(%rsp), %rdx
cmpq $-0x1, 0x8(%rdx,%rcx,8)
je 0x698f
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
movq %rdx, 0x8(%r8,%rcx,8)
incq %rax
cmpq %rax, 0x20(%rsp)
jne 0x6965
movq %r12, %rdi
movq %r13, %rsi
movq %r10, %rdx
movq %r8, 0x40(%rsp)
movl 0xc(%rsp), %ebp
movl %ebp, %ecx
xorl %r8d, %r8d
movl %ebx, %r9d
movq %r10, 0x30(%rsp)
callq 0x6180
movq 0x30(%rsp), %rdx
testl %ebp, %ebp
movq 0x40(%rsp), %rbp
je 0x69ea
xorl %eax, %eax
movq (%r13,%rax,8), %rcx
cmpq %rdx, %rcx
jae 0x69e0
movq $-0x2, (%rbp,%rcx,8)
incq %rax
cmpq %rax, 0x20(%rsp)
jne 0x69cd
movq 0x20(%rsp), %rax
leaq (,%rax,8), %r15
movq %r15, %rdi
callq 0x9fd2
movq %rax, 0x38(%rsp)
movq %r15, %rdi
callq 0x9fd2
movq %rax, 0x48(%rsp)
movl $0x3800, %edi           # imm = 0x3800
callq 0x9fd2
movq %rax, %r14
movq $0x0, (%rax)
movq $-0x1, 0x8(%rax)
cmpl $0x0, 0xc(%rsp)
je 0x6a43
movq 0x38(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2150
incq (%r13)
movq 0x30(%rsp), %r11
testq %r11, %r11
movq 0x10(%rsp), %r10
je 0x6dce
movq %r15, %rdx
leaq -0x1(%r11), %rax
movq %rax, 0x58(%rsp)
leaq 0x20(%r14), %rax
movq %rax, 0x68(%rsp)
leaq 0x28(%r14), %rax
movq %rax, 0x60(%rsp)
movl $0x1, %eax
xorl %r15d, %r15d
movq %r13, 0x18(%rsp)
movq %rdx, 0x70(%rsp)
movl %ebx, 0x2c(%rsp)
movq (%r10,%r15,8), %rcx
cmpq $-0x1, %rcx
je 0x6db8
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
cmpq %rdx, (%rbp,%r15,8)
jne 0x6b3a
cmpl $0x8, %ebx
jne 0x6abc
movq (%r12,%rcx,8), %rdx
jmp 0x6ac1
movzbl (%r12,%rcx), %edx
movq (%r13,%rdx,8), %rdx
movq 0x58(%rsp), %rsi
cmpq %rsi, -0x8(%r10,%rdx,8)
jae 0x6b33
leaq (%r12,%rcx), %rsi
leaq (%r12,%rcx,8), %rdi
movq $-0x1, %rdx
cmpl $0x8, %ebx
jne 0x6aed
movq 0x8(%rdi,%rdx,8), %r8
jmp 0x6af3
movzbl 0x1(%rsi,%rdx), %r8d
cmpl $0x8, %ebx
jne 0x6b11
movq (%r12,%rcx,8), %r9
movq (%r13,%r9,8), %r9
movq -0x8(%r10,%r9,8), %r9
leaq (%r12,%r9,8), %r9
movq 0x8(%r9,%rdx,8), %r9
jmp 0x6b29
movzbl (%r12,%rcx), %r9d
movq (%r13,%r9,8), %r9
movq -0x8(%r10,%r9,8), %r9
addq %r12, %r9
movzbl 0x1(%rdx,%r9), %r9d
incq %rdx
cmpq %r9, %r8
je 0x6ae1
jmp 0x6b35
xorl %edx, %edx
movq %rdx, (%rbp,%r15,8)
movq (%r10,%r15,8), %rcx
testq %rcx, %rcx
je 0x6b4f
cmpl $0x8, %ebx
jne 0x6b53
movq -0x8(%r12,%rcx,8), %rdx
jmp 0x6b59
xorl %esi, %esi
jmp 0x6b7c
movzbl -0x1(%r12,%rcx), %edx
cmpl $0x8, %ebx
jne 0x6b65
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6b6b
movzbl -0x1(%r12,%rcx), %edi
leaq 0x1(%r15), %r8
movq 0x38(%rsp), %r9
movq (%r9,%rdx,8), %rsi
movq %r8, (%r9,%rdi,8)
movq (%rbp,%r15,8), %rdi
testq %rdi, %rdi
movl $0x0, %edx
cmovleq %rdx, %rdi
leaq 0x1(%rax), %rbp
shlq $0x4, %rax
movq 0x68(%rsp), %rdx
leaq (%rdx,%rax), %r13
addq 0x60(%rsp), %rax
movq %r13, %r8
movq %rax, %rdx
decq %rbp
addq $-0x10, %r13
addq $-0x10, %rax
cmpq %rdi, -0x28(%r8)
jge 0x6ba3
leaq 0x1(%r15), %rax
movq %rax, -0x10(%r13)
movq %rdi, -0x8(%r13)
leaq 0x1(%rbp), %rax
movq %rdx, %rdi
addq $-0x10, %rdx
cmpq %rsi, -0x28(%rdi)
ja 0x6bca
testq %rcx, %rcx
je 0x6ccb
leaq -0x1(%rcx), %rsi
cmpl $0x8, %ebx
jne 0x6bf0
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6bf5
movzbl (%r12,%rsi), %edi
cmpl $0x8, %ebx
jne 0x6c00
movq (%r12,%rcx,8), %r8
jmp 0x6c05
movzbl (%r12,%rcx), %r8d
cmpq %r8, %rdi
jl 0x6c83
cmpl $0x8, %ebx
jne 0x6c16
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6c1b
movzbl (%r12,%rsi), %edi
cmpq 0x50(%rsp), %rdi
je 0x6c83
cmpl $0x8, %ebx
jne 0x6c2e
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6c33
movzbl (%r12,%rsi), %edi
movq (%rdx), %rdx
movq 0x18(%rsp), %r8
movq (%r8,%rdi,8), %rdi
movq %rsi, (%r10,%rdi,8)
cmpl $0x8, %ebx
jne 0x6c4f
movq -0x8(%r12,%rcx,8), %rdi
jmp 0x6c54
movzbl (%r12,%rsi), %edi
incq %rdx
movq 0x18(%rsp), %r8
movq (%r8,%rdi,8), %rdi
movq 0x40(%rsp), %r8
addq %rdx, (%r8,%rdi,8)
cmpl $0x8, %ebx
jne 0x6c75
movq -0x8(%r12,%rcx,8), %rcx
jmp 0x6c7a
movzbl (%r12,%rsi), %ecx
movq 0x18(%rsp), %rdx
incq (%rdx,%rcx,8)
movq (%r10,%r15,8), %rcx
cmpl $0x8, %ebx
jne 0x6c92
movq (%r12,%rcx,8), %rcx
jmp 0x6c97
movzbl (%r12,%rcx), %ecx
movq 0x18(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
decq %rcx
cmpq %r15, %rcx
jae 0x6ccb
movq (%r10,%r15,8), %rcx
cmpl $0x8, %ebx
jne 0x6cb7
movq (%r12,%rcx,8), %rcx
jmp 0x6cbc
movzbl (%r12,%rcx), %ecx
cmpq 0x50(%rsp), %rcx
je 0x6ccb
movq $-0x1, (%r10,%r15,8)
cmpq $0x37e, %rbp            # imm = 0x37E
movq 0x70(%rsp), %rdx
jl 0x6db8
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x21b0
movl $0x8, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
leaq -0xd6b(%rip), %rcx      # 0x5f98
callq 0x2090
movq $-0x1, (%r13)
cmpl $0x0, 0xc(%rsp)
je 0x6d9d
movl $0x1, %ecx
xorl %edx, %edx
movl $0x1, %eax
movq 0x30(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x20(%rsp), %r13
movl 0x2c(%rsp), %ebx
movq %rax, %rsi
shlq $0x4, %rsi
movq 0x48(%rsp), %rdi
movq (%rdi,%rdx,8), %r8
incq %r8
cmpq %r8, -0x10(%r14,%rsi)
jae 0x6d93
leaq (%r14,%rsi), %rdi
movq %rcx, %r9
shlq $0x4, %r9
addq %r14, %r9
movq (%r9), %r10
incq %rcx
addq $0x10, %r9
cmpq %r8, %r10
jb 0x6d62
leaq -0x1(%rcx), %r8
cmpq %rbp, %r8
jle 0x6d7f
movq %r8, %rcx
jmp 0x6d8e
movq -0x8(%r9), %r8
movq %r10, (%rdi)
movq %r8, 0x8(%r14,%rsi)
incq %rax
movq 0x10(%rsp), %r10
incq %rdx
cmpq %r13, %rdx
jne 0x6d3a
jmp 0x6db0
movl $0x1, %eax
movq 0x30(%rsp), %r11
movq 0x10(%rsp), %r10
movl 0x2c(%rsp), %ebx
cmpq $0x37e, %rax            # imm = 0x37E
jge 0x6df8
incq %r15
cmpq %r11, %r15
movq 0x18(%rsp), %r13
movq 0x40(%rsp), %rbp
jne 0x6a8e
movq %r14, %rdi
callq 0xa089
movq 0x38(%rsp), %rdi
callq 0xa089
movq 0x48(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa089
callq 0x26fb