// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movl %r8d, %ebx
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movl %ecx, %eax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rdi
callq 0x9fd2
movq %rax, %r15
subq $0x8, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
movl %r14d, %r8d
movl %ebx, %r9d
pushq 0x10(%rsp)
callq 0x7ec4
addq $0x10, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl %r14d, 0x4(%rsp)
movl %r14d, %r8d
xorl %r9d, %r9d
movq 0x8(%rsp), %r14
pushq %r14
pushq %rbx
callq 0x6741
addq $0x10, %rsp
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
pushq %r14
pushq %rbx
callq 0x682b
movq %r14, %rdi
addq $0x10, %rsp
movq %r12, %rax
addq $-0x3, %rax
je 0x92b7
cmpl $0x8, %ebx
jne 0x9286
movq (%rbp,%rax,8), %rcx
jmp 0x928b
movzbl (%rbp,%rax), %ecx
cmpq %rdi, %rcx
jne 0x92b2
cmpl $0x8, %ebx
jne 0x929c
movq (%rbp,%rax,8), %rcx
jmp 0x92a1
movzbl (%rbp,%rax), %ecx
movq (%r15,%rcx,8), %rdx
leaq -0x1(%rdx), %rsi
movq %rsi, (%r15,%rcx,8)
movq %rax, (%r13,%rdx,8)
decq %rax
jne 0x927a
testq %r12, %r12
je 0x92dd
xorl %eax, %eax
xorl %r14d, %r14d
movq (%r13,%rax,8), %rcx
testq %rcx, %rcx
je 0x92d3
movq %rcx, (%r13,%r14,8)
incq %r14
incq %rax
cmpq %rax, %r12
jne 0x92c1
jmp 0x92e0
xorl %r14d, %r14d
movq %r15, 0x20(%rsp)
leaq (,%r12,8), %r15
addq %r13, %r15
leaq (,%r14,8), %rax
subq %rax, %r15
subq $0x20, %rsp
movq %rdi, 0x10(%rsp)
movl %ebx, 0x8(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r12, %r8
movq %r14, %r9
callq 0x887e
addq $0x20, %rsp
cmpq %r14, %rax
jae 0x935d
movl 0x60(%rsp), %eax
movq %r12, %r8
subq %r14, %r8
incl %eax
subq $0x8, %rsp
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
movl $0x8, %r9d
pushq %rax
callq 0x8ecf
addq $0x10, %rsp
incq %rax
movq %rax, 0x10(%rsp)
jmp 0x937f
movl $0x1, %eax
movq %rax, 0x10(%rsp)
testq %r14, %r14
je 0x937f
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
movq %rax, (%r13,%rcx,8)
incq %rax
cmpq %rax, %r14
jne 0x936e
subq $0x8, %rsp
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r14, %r8
pushq %rbx
callq 0x8d3d
addq $0x10, %rsp
testq %r14, %r14
je 0x93b6
xorl %eax, %eax
movq (%r13,%rax,8), %rcx
movq (%r15,%rcx,8), %rcx
movq %rcx, (%r13,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x93a0
movq %r12, %rdx
subq %r14, %rdx
jbe 0x93d4
leaq (,%r14,8), %rdi
addq %r13, %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2150
movq %r13, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %r15
movq %r15, %rdx
movq %r12, %rcx
movl 0x4(%rsp), %r8d
movq %r14, %r9
movq 0x8(%rsp), %r14
pushq %r14
pushq %rbx
callq 0x6297
addq $0x10, %rsp
movl $0x1, %r9d
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl 0x4(%rsp), %r8d
pushq %r14
pushq %rbx
callq 0x6741
addq $0x10, %rsp
movl $0x1, %r9d
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x18(%rsp), %r8
pushq %r14
pushq %rbx
callq 0x682b
addq $0x10, %rsp
movq %r15, %rdi
callq 0xa089
movq 0x10(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq