// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/tools/src/gsacak.c
// Function: gSACA_K_LCP
int_t gSACA_K_LCP(uint_t *s, uint_t *SA, int_t *LCP,
  uint_t n, unsigned int K,
  int cs, uint_t separator, int level) {
  uint_t i;
  uint_t *bkt=NULL;
  uint_t m=n;
  
  #if PHASES
  time_t t_start_phase = 0.0; 
  clock_t c_start_phase = 0.0;
  #endif

  #if DEPTH
  time_t t_start = time(NULL);
  clock_t c_start =  clock();
  #endif

  #if PHASES
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  #endif
  // stage 1: reduce the problem by at least 1/2.

  bkt=(uint_t *)malloc(sizeof(int_t)*K);
  putSubstr0_generalized(SA, s, bkt, n, K, cs, separator);

  #if DEBUG
  printf("bucket LMS-subs\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  #endif
 
  induceSAl0_generalized(SA, s, bkt, n, K, false, cs, separator);

  #if DEBUG
  printf("L-type\n");
  for(i=0; i<n; i++)
	if(SA[i]!=0)  printf("%" PRIdN "\t", SA[i]+1);
        else printf("-1\t");
  printf("\n");
  #endif

  induceSAs0_generalized(SA, s, bkt, n, K, false, cs, separator);

  #if DEBUG
  printf("S-type\n");
  for(i=0; i<n; i++)
	if(SA[i]!=0)  printf("%" PRIdN "\t", SA[i]+1);
        else printf("-1\t");
  printf("\n");
  #endif
  
  // insert separator suffixes in their buckets
  // bkt[separator]=1; // gsa-is
  for(i=n-3; i>0; i--)
    if(chr(i)==separator)
      SA[bkt[chr(i)]--]=i;

  #if DEBUG
  printf("S-type (separators)\n");
  for(i=0; i<n; i++)
	if(SA[i]!=0)  printf("%" PRIdN "\t", SA[i]+1);
        else printf("-1\t");
  printf("\n");
  #endif

  // now, all the LMS-substrings are sorted and 
  //   stored sparsely in SA.

  // compact all the sorted substrings into
  //   the first n1 items of SA.
  // 2*n1 must be not larger than n.
  uint_t n1=0;
  for(i=0; i<n; i++) 
    if((SA[i]>0))
      SA[n1++]=SA[i];

  uint_t *SA1=SA, *s1=SA+m-n1;
  uint_t name_ctr;

  #if DEBUG
  printf("\nSA\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

  name_ctr=nameSubstr_generalized_LCP(SA,LCP,s,s1,n,m,n1,level,cs,separator);

  #if DEBUG
  printf("nameSubstr:\n");
  printf("SA\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

  #if PHASES
	printf("phase 1:\n");
	time_stop(t_start_phase, c_start_phase);
  #endif

  #if PHASES
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  #endif

  // stage 2: solve the reduced problem.
  int_t depth=1;
  // recurse if names are not yet unique.
  if(name_ctr<n1)
    depth += SACA_K((int_t*)s1, SA1, 
          n1, 0, m-n1, sizeof(int_t), level+1);
  else // get the suffix array of s1 directly.
    for(i=0; i<n1; i++) SA1[s1[i]]=i;

  // stage 3: induce SA(S) from SA(S1).
  #if DEBUG
  printf("recursive:\n");
  printf("SA\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

  getSAlms(SA, (int_t*)s, s1, n, n1, level, cs);

  #if DEBUG
  printf("getSAlms:\n");
  printf("SA\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

  uint_t *RA=s1;
  int_t *PLCP=LCP+m-n1;//PHI is stored in PLCP array

  //compute the LCP of consecutive LMS-suffixes
  compute_lcp_phi_sparse((int_t*)s, SA1, RA, LCP, PLCP, n1, cs, separator); 

  #if DEBUG
  printf("\nPHI-algorithm:\n");
  printf("--\nSA1\n");
  for(i=0; i<n1; i++)//SA1
        printf("%" PRIdN "\t", SA1[i]+1);
  printf("| ");
  for(i=0; i<n1; i++)//s1
        printf("%" PRIdN "\t", RA[i]+1);
  printf("\n");
  printf("LCP\n");
  for(i=0; i<n1; i++)
        printf("%" PRIdN "\t", LCP[i]);
  printf("| ");
  for(i=0; i<n1; i++)
        printf("%" PRIdN "\t", PLCP[i]);
  printf("\n");
  #endif

  for(i=0; i<n1; i++) SA[i]=s1[SA[i]];
  for(i=n1; i<n; i++) SA[i]=U_MAX; 
  for(i=n1;i<n;i++) LCP[i]=0;

  #if DEBUG
  printf("\nstage 3:\n\n");
  printf("mapping back:\n");
  printf("SA\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

/*
  if(!lcp_array_check((unsigned char*)s, SA1, LCP, n, cs, separator))printf("isNotLCP (lms)!!\n"); 
  else printf("isLCP (lms)!!\n");
*/

  putSuffix0_generalized_LCP(SA, LCP, s, bkt, n, K, n1, cs, separator);
  
  #if PHASES
  if(!level){
	printf("phase 2:\n");
	time_stop(t_start_phase, c_start_phase);
  }
  #endif

  #if PHASES
  if(!level){
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  }
  #endif


  #if DEBUG
  printf("SA (mapped)\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

  induceSAl0_generalized_LCP(SA, LCP, s, bkt, n, K, cs, separator);

  #if DEBUG
  printf("L-type\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

  #if PHASES
  if(!level){
      printf("phase 3:\n");
      time_stop(t_start_phase, c_start_phase);
  }
  #endif
  
  #if PHASES
  if(!level){
      t_start_phase = time(NULL);
      c_start_phase =  clock();
  }
  #endif

  induceSAs0_generalized_LCP(SA, LCP, s, bkt, n, K, cs, separator);

  #if DEBUG
  printf("S-type\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
  free(bkt);

  #if DEPTH
  printf("depth %" PRIdN ":\nname_ctr = %" PRIdN ", n1 =%" PRIdN ", n = %" PRIdN "\n", depth, name_ctr, n1, n);
  time_stop(t_start, c_start);
  #endif

  #if PHASES
  if(!level){
      printf("phase 4:\n");
      time_stop(t_start_phase, c_start_phase);
  }
  #endif

return depth;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xc8, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x10960
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movl -0x30(%rbp), %r9d
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0xc7f0
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
xorl %r9d, %r9d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x8f50
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %eax
movl %eax, %r8d
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
xorl %r9d, %r9d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9130
movq -0x28(%rbp), %rax
subq $0x3, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jbe 0xf83d
movslq -0x30(%rbp), %rax
cmpq $0x8, %rax
jne 0xf784
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x88(%rbp)
jmp 0xf797
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq 0x10(%rbp), %rax
jne 0xf82a
movq -0x38(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movslq -0x30(%rbp), %rax
cmpq $0x8, %rax
jne 0xf7e8
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
jmp 0xf7fb
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rdx
movq -0x90(%rbp), %rsi
movq -0xa8(%rbp), %rdi
movq (%rsi,%rdi,8), %rcx
movq %rcx, %r8
addq $-0x1, %r8
movq %r8, (%rsi,%rdi,8)
movq %rdx, (%rax,%rcx,8)
jmp 0xf82c
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf75a
movq $0x0, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0xf899
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jbe 0xf889
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0xf88b
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf84d
movq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x50(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x50(%rbp), %rbx
movl 0x18(%rbp), %r11d
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xe510
movq %rax, -0x68(%rbp)
movq $0x1, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0xf94b
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %r8
subq -0x50(%rbp), %r8
movl 0x18(%rbp), %eax
addl $0x1, %eax
xorl %ecx, %ecx
movl $0x8, %r9d
movl %eax, (%rsp)
callq 0xeea0
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0xf985
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0xf983
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf953
jmp 0xf985
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x50(%rbp), %r8
movl 0x18(%rbp), %r9d
movl -0x30(%rbp), %eax
movl %eax, (%rsp)
callq 0xe9d0
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x50(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x80(%rbp), %r8
movq -0x50(%rbp), %r9
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x7d50
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0xfa3e
movq -0x60(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xfa06
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0xfa6e
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xfa46
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0xfa9e
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xfa76
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %r9d
movq -0x50(%rbp), %r11
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x8530
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %r9d
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9360
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %eax
movl %eax, %r9d
movl -0x30(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9e80
movq -0x40(%rbp), %rdi
callq 0x10ad0
movq -0x70(%rbp), %rax
addq $0xc8, %rsp
popq %rbx
popq %rbp
retq
nopw (%rax,%rax)