// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]egap/tools/src/gsacak.c
// Function: gSACA_K_DA
int_t gSACA_K_DA(uint_t *s, uint_t *SA, 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\n");
  #endif

  name_ctr=nameSubstr_generalized(SA,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\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\n");
  #endif

  int_t *d1=DA+m-n1;
  
  getSAlms_DA(SA, d1, (int_t*)s, s1, n, n1, level, cs, separator);

  #if DEBUG
  printf("getSAlms:\n");
  printf("SA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\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]=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("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif

/**/
  putSuffix0_generalized_DA(SA, 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("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif

/**/
  induceSAl0_generalized_DA(SA, 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("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_DA(SA, 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("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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %r15d
movl %r8d, %ebx
movl %ecx, %ebp
movl %ecx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r13
movq %rdi, %r12
movl 0x90(%rsp), %r14d
movl %r8d, %edi
shlq $0x2, %rdi
callq 0xa40a
movl %r14d, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq %rax, %r14
movq %rax, 0x50(%rsp)
movl %ebp, %ecx
movl %ebx, %r8d
movl %ebx, 0x3c(%rsp)
movl %r15d, %r9d
callq 0x7cf8
movl 0x90(%rsp), %eax
movl %eax, 0x8(%rsp)
movl %r15d, (%rsp)
xorl %ebp, %ebp
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl 0x28(%rsp), %r14d
movl %r14d, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x6679
movl 0x90(%rsp), %eax
movl %eax, 0x8(%rsp)
movl %r15d, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq 0x50(%rsp), %rbx
movq %rbx, %rdx
movl %r14d, %ecx
movl 0x3c(%rsp), %r8d
xorl %r9d, %r9d
callq 0x672f
movl 0x90(%rsp), %edi
movl %r14d, %eax
addl $-0x3, %eax
movl %r15d, 0x24(%rsp)
movq %r12, 0x40(%rsp)
je 0x9b76
movl %eax, %eax
movslq %edi, %rcx
cmpl $0x4, %r15d
jne 0x9b4f
cmpl %edi, (%r12,%rax,4)
je 0x9b58
jmp 0x9b66
movzbl (%r12,%rax), %edx
cmpl %edi, %edx
jne 0x9b66
movl (%rbx,%rcx,4), %edx
leal -0x1(%rdx), %esi
movl %esi, (%rbx,%rcx,4)
movl %eax, (%r13,%rdx,4)
decq %rax
testl %eax, %eax
jne 0x9b41
testl %r14d, %r14d
je 0x9cb9
movl %r14d, %r12d
xorl %eax, %eax
movl (%r13,%rax,4), %ecx
testl %ecx, %ecx
je 0x9b8d
movl %ebp, %edx
incl %ebp
movl %ecx, (%r13,%rdx,4)
incq %rax
cmpq %rax, %r12
jne 0x9b7b
leaq (,%r12,4), %rbx
addq %r13, %rbx
movl %ebp, %r15d
movq %r15, %rax
negq %rax
movq %rax, 0x48(%rsp)
leaq (,%r15,4), %rax
subq %rax, %rbx
movl %edi, 0x10(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq %rbx, %rdx
movl 0x28(%rsp), %ecx
movl %ecx, %r14d
movl %ecx, %r8d
movl %ebp, %r9d
callq 0x8810
cmpl %ebp, %eax
jae 0x9c29
movl %r14d, %r8d
subl %ebp, %r8d
movl 0x98(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl $0x4, %r9d
callq 0x8f73
movl %r14d, %r8d
incl %eax
movl %eax, 0x2c(%rsp)
movq 0x30(%rsp), %rsi
movl 0x90(%rsp), %edx
movq 0x48(%rsp), %rdi
jmp 0x9c61
testl %ebp, %ebp
movq 0x30(%rsp), %rsi
je 0x9cfe
xorl %eax, %eax
movl 0x90(%rsp), %edx
movq 0x48(%rsp), %rdi
movl (%rbx,%rax,4), %ecx
movl %eax, (%r13,%rcx,4)
incq %rax
cmpq %rax, %r15
jne 0x9c44
movl $0x1, 0x2c(%rsp)
movl 0x28(%rsp), %r8d
leaq (%rsi,%r12,4), %rax
leaq (%rax,%rdi,4), %r14
movl %edx, 0x10(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rsi
movq 0x40(%rsp), %rdx
movq %rbx, %rcx
movl %ebp, %r9d
callq 0x8e6d
movq 0x30(%rsp), %rdx
xorl %eax, %eax
movl (%r13,%rax,4), %ecx
movl (%r14,%rcx,4), %ecx
movl %ecx, (%rdx,%rax,4)
movl (%r13,%rax,4), %ecx
movl (%rbx,%rcx,4), %ecx
movl %ecx, (%r13,%rax,4)
incq %rax
cmpq %rax, %r15
jne 0x9c8f
movl 0x28(%rsp), %ebx
jmp 0x9d49
movl %r14d, %eax
leaq (,%rax,4), %rbx
addq %r13, %rbx
movl %edi, 0x10(%rsp)
movl %r15d, 0x8(%rsp)
movl %r14d, %ecx
xorl %r14d, %r14d
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl %ecx, %r8d
xorl %r9d, %r9d
callq 0x8810
movl 0x90(%rsp), %ecx
movq %r12, %rdx
xorl %r12d, %r12d
movq 0x30(%rsp), %rsi
jmp 0x9d14
movl 0x24(%rsp), %r15d
movq 0x40(%rsp), %rdx
movl 0x90(%rsp), %ecx
movq 0x48(%rsp), %r14
leaq (%rsi,%r12,4), %rax
leaq (%rax,%r14,4), %rsi
movl %ecx, 0x10(%rsp)
movl %r15d, 0x8(%rsp)
xorl %ebp, %ebp
movq %rbx, %rcx
movl 0x28(%rsp), %ebx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x8e6d
movq 0x30(%rsp), %rdx
movl $0x1, 0x2c(%rsp)
xorl %r15d, %r15d
cmpl %ebx, %ebp
jae 0x9d66
movl $0x0, (%r13,%r15,4)
movl $0xffffffff, (%rdx,%r15,4) # imm = 0xFFFFFFFF
incq %r15
cmpq %r15, %r12
jne 0x9d4d
movl 0x90(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movl %ebp, (%rsp)
movq %r13, %rdi
movq %rdx, %r12
movq %rdx, %rsi
movq 0x40(%rsp), %rbp
movq %rbp, %rdx
movq 0x50(%rsp), %r14
movq %r14, %rcx
movl %ebx, %r15d
movl %ebx, %r8d
movl 0x3c(%rsp), %ebx
movl %ebx, %r9d
callq 0x6342
movl 0x90(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
movl %r15d, %r8d
movl %ebx, %r9d
callq 0x7109
movl 0x90(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
movl %r15d, %r8d
movl %ebx, %r9d
callq 0x71dd
movq %r14, %rdi
callq 0xa4c1
movl 0x2c(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq