// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/tools/src/gsacak.c
// Function: gSACA_K_LCP_DA
int_t gSACA_K_LCP_DA(uint_t *s, uint_t *SA, int_t *LCP, int_t *DA,
  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

  int_t *d1=DA+m-n1;
  
  getSAlms_DA(SA, d1, (int_t*)s, s1, n, n1, level, cs, separator);


//FELIPE  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");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[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++) {DA[i]=d1[SA[i]]; SA[i]=s1[SA[i]];}
  for(i=n1; i<n; i++) {SA[i]=U_MAX; DA[i]=-1;}
  for(i=n1;i<n;i++) LCP[i]=0;

//DA  for(i=n1; i<n; i++) {SA[i]=0; DA[i]=-1;}

  #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");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[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_DA(SA, LCP, DA, 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_DA(SA, LCP, DA, 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");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[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_DA(SA, LCP, DA, 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");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[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 $0xd8, %rsp
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x34(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x10960
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %r8d
movl 0x10(%rbp), %r9d
movq 0x18(%rbp), %rax
movq %rax, (%rsp)
callq 0xc7f0
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %r8d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
xorl %r9d, %r9d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x8f50
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %eax
movl %eax, %r8d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
xorl %r9d, %r9d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9130
movq -0x30(%rbp), %rax
subq $0x3, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jbe 0x101a0
movslq 0x10(%rbp), %rax
cmpq $0x8, %rax
jne 0x100e7
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x100fa
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq 0x18(%rbp), %rax
jne 0x1018d
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
movslq 0x10(%rbp), %rax
cmpq $0x8, %rax
jne 0x1014b
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
jmp 0x1015e
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0xb8(%rbp)
movq -0xa8(%rbp), %rax
movq -0xb0(%rbp), %rdx
movq -0xa0(%rbp), %rsi
movq -0xb8(%rbp), %rdi
movq (%rsi,%rdi,8), %rcx
movq %rcx, %r8
addq $-0x1, %r8
movq %r8, (%rsi,%rdi,8)
movq %rdx, (%rax,%rcx,8)
jmp 0x1018f
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x100bd
movq $0x0, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x101fc
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jbe 0x101ec
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x58(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x101ee
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x101b0
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %rbx
movl 0x20(%rbp), %r11d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xe510
movq %rax, -0x70(%rbp)
movq $0x1, -0x78(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x102ae
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %r8
subq -0x58(%rbp), %r8
movl 0x20(%rbp), %eax
addl $0x1, %eax
xorl %ecx, %ecx
movl $0x8, %r9d
movl %eax, (%rsp)
callq 0xeea0
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x102e8
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x102e6
movq -0x40(%rbp), %rdx
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x40(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x102b6
jmp 0x102e8
movq -0x28(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x58(%rbp), %r9
movl 0x20(%rbp), %r11d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xebc0
movq -0x68(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x58(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x90(%rbp), %r8
movq -0x58(%rbp), %r9
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x7d50
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x10401
movq -0x80(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x103a9
movq -0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x10441
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x10409
movq -0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x10471
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x10449
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x30(%rbp), %r9
movl -0x34(%rbp), %ebx
movq -0x58(%rbp), %r11
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %ebx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x88c0
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x30(%rbp), %r9
movl -0x34(%rbp), %r11d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xae60
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x30(%rbp), %r9
movl -0x34(%rbp), %eax
movl %eax, %r11d
movl 0x10(%rbp), %r10d
movq 0x18(%rbp), %rax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xba00
movq -0x48(%rbp), %rdi
callq 0x10ad0
movq -0x78(%rbp), %rax
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)