// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/src/gsacak.c
// Function: gSACA_K
int_t gSACA_K(uint_t *s, uint_t *SA,
  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);
 
  induceSAl0_generalized(SA, s, bkt, n, K, false, cs, separator);
  induceSAs0_generalized(SA, s, bkt, n, K, false, cs, separator);
  
  // 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;

  // 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;
  name_ctr=nameSubstr_generalized(SA,s,s1,n,m,n1,level,cs,separator);

  #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).

  getSAlms(SA, (int_t*)s, s1, n, n1, level, cs);

  for(i=0; i<n1; i++) SA[i]=s1[SA[i]];
  for(i=n1; i<n; i++) SA[i]=0; 

  putSuffix0_generalized(SA, s, bkt, n, K, n1, cs, separator);
  
  #if PHASES
	printf("phase 2:\n");
	time_stop(t_start_phase, c_start_phase);
  #endif

  #if PHASES
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  #endif

  induceSAl0_generalized(SA, s, bkt, n, K, true, cs, separator);

  #if PHASES
      printf("phase 3:\n");
      time_stop(t_start_phase, c_start_phase);
  #endif
  
  #if PHASES
      t_start_phase = time(NULL);
      c_start_phase =  clock();
  #endif

  induceSAs0_generalized(SA, s, bkt, n, K, true, cs, separator);

  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
      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 $0x28, %rsp
movq %r9, %r14
movl %r8d, %ebx
movl %ecx, 0x4(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movl %ecx, %eax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rdi
callq 0x92c8
movq %rax, %r15
subq $0x8, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
movl 0xc(%rsp), %r8d
movl %ebx, %r9d
pushq %r14
callq 0x750b
popq %rax
popq %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl 0x4(%rsp), %r8d
xorl %r9d, %r9d
pushq %r14
pushq %rbx
callq 0x60b5
popq %rax
popq %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
pushq %r14
pushq %rbx
callq 0x6169
popq %rax
popq %rcx
leaq -0x3(%r12), %rax
movq %rax, %rcx
subq $0x1, %rax
jb 0x863d
cmpl $0x8, %ebx
jne 0x8620
cmpq %r14, (%rbp,%rcx,8)
jne 0x8609
jmp 0x862a
movzbl (%rbp,%rcx), %edx
cmpq %r14, %rdx
jne 0x8609
movq (%r15,%r14,8), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, (%r15,%r14,8)
movq %rcx, (%r13,%rdx,8)
jmp 0x8609
movq %rbx, 0x8(%rsp)
xorl %ebx, %ebx
xorl %eax, %eax
cmpq %rax, %r12
je 0x8662
movq (%r13,%rax,8), %rcx
testq %rcx, %rcx
je 0x865d
movq %rcx, (%r13,%rbx,8)
incq %rbx
incq %rax
jmp 0x8646
movq %r15, 0x20(%rsp)
leaq (,%r12,8), %r15
addq %r13, %r15
leaq (,%rbx,8), %rax
subq %rax, %r15
subq $0x20, %rsp
movq %r14, 0x10(%rsp)
movq 0x28(%rsp), %rax
movl %eax, 0x8(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r12, %r8
movq %rbx, %r9
callq 0x7df4
addq $0x20, %rsp
cmpq %rbx, %rax
jae 0x87c1
movq %r12, %r8
subq %rbx, %r8
movl 0x60(%rsp), %eax
incl %eax
subq $0x8, %rsp
pushq $0x8
popq %r9
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
pushq %rax
callq 0x82ae
popq %rcx
popq %rdx
incq %rax
movq %rax, 0x10(%rsp)
subq $0x8, %rsp
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rbx, %r8
pushq 0x10(%rsp)
callq 0x8173
popq %rax
popq %rcx
xorl %eax, %eax
cmpq %rax, %rbx
je 0x8717
movq (%r13,%rax,8), %rcx
movq (%r15,%rcx,8), %rcx
movq %rcx, (%r13,%rax,8)
incq %rax
jmp 0x86ff
cmpq %r12, %rbx
movq %r12, %rax
cmovaq %rbx, %rax
movq %rbx, %rcx
movq 0x20(%rsp), %rdx
cmpq %rcx, %rax
je 0x8739
andq $0x0, (%r13,%rcx,8)
incq %rcx
jmp 0x8729
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rcx
movl 0x4(%rsp), %r15d
movl %r15d, %r8d
movq %rbx, %r9
pushq %r14
movq 0x10(%rsp), %rax
movq %rdx, %rbx
pushq %rax
callq 0x5c3e
popq %rax
popq %rcx
pushq $0x1
popq %r9
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movl %r15d, %r8d
pushq %r14
movq 0x10(%rsp), %rbx
pushq %rbx
callq 0x60b5
popq %rax
popq %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %r15
movq %r15, %rdx
movq %r12, %rcx
movq 0x18(%rsp), %r8
pushq $0x1
popq %r9
pushq %r14
pushq %rbx
callq 0x6169
popq %rax
popq %rcx
movq %r15, %rdi
callq 0x939e
movq 0x10(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
pushq $0x1
popq %rcx
movq %rcx, 0x10(%rsp)
cmpq %rax, %rbx
je 0x86e2
movq (%r15,%rax,8), %rcx
movq %rax, (%r13,%rcx,8)
incq %rax
jmp 0x87cb