// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %r9, %r14
movl %r8d, %ebp
movl %ecx, %r15d
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r13
movl %ecx, %eax
movl %ecx, 0x24(%rsp)
movq %rax, 0x40(%rsp)
leaq (,%rax,8), %rdi
callq 0x9fe6
movq %rax, 0x38(%rsp)
movq %r14, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %rbx, %rcx
movl %r15d, %r8d
movl %ebp, %r9d
callq 0x7cad
movq %r14, 0x8(%rsp)
movl %ebp, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %r15
movq %r15, %rdx
movq %rbx, %rcx
movl 0x24(%rsp), %r8d
xorl %r9d, %r9d
callq 0x6625
movq %r14, 0x8(%rsp)
movl %ebp, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq 0x40(%rsp), %r8
xorl %r9d, %r9d
callq 0x66dc
movq %rbx, %rax
addq $-0x3, %rax
movq %r13, 0x28(%rsp)
je 0x91dc
cmpl $0x8, %ebp
jne 0x91b3
cmpq %r14, (%r13,%rax,8)
je 0x91be
jmp 0x91ce
movzbl (%r13,%rax), %ecx
cmpq %r14, %rcx
jne 0x91ce
movq (%r15,%r14,8), %rcx
leaq -0x1(%rcx), %rdx
movq %rdx, (%r15,%r14,8)
movq %rax, (%r12,%rcx,8)
decq %rax
jne 0x91a5
testq %rbx, %rbx
je 0x92ab
xorl %eax, %eax
xorl %r13d, %r13d
movq (%r12,%rax,8), %rcx
testq %rcx, %rcx
je 0x91f1
movq %rcx, (%r12,%r13,8)
incq %r13
incq %rax
cmpq %rax, %rbx
jne 0x91e1
leaq (%r12,%rbx,8), %r15
leaq (,%r13,8), %rax
subq %rax, %r15
movq %r14, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x86cf
cmpq %r13, %rax
jae 0x925c
movq %rbx, %r8
subq %r13, %r8
movl 0x80(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
movl $0x8, %r9d
callq 0x8d30
incq %rax
jmp 0x9278
testq %r13, %r13
je 0x92d4
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
movq %rax, (%r12,%rcx,8)
incq %rax
cmpq %rax, %r13
jne 0x9263
movl $0x1, %eax
movq %rax, 0x30(%rsp)
movl %ebp, (%rsp)
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x8bee
xorl %eax, %eax
movq (%r12,%rax,8), %rcx
movq (%r15,%rcx,8), %rcx
movq %rcx, (%r12,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x9295
jmp 0x92f7
leaq (%r12,%rbx,8), %r15
movq %r14, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x86cf
movq %r13, %rsi
jmp 0x92d9
movq 0x28(%rsp), %rsi
movl %ebp, (%rsp)
xorl %r13d, %r13d
movq %r15, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x8bee
movl $0x1, %eax
movq %rax, 0x30(%rsp)
movq %rbx, %rdx
subq %r13, %rdx
jbe 0x930e
leaq (%r12,%r13,8), %rdi
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2150
movq %r14, 0x8(%rsp)
movl %ebp, (%rsp)
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %r15
movq %r15, %rdx
movq %rbx, %rcx
movl 0x24(%rsp), %r8d
movq %r13, %r9
callq 0x61c3
movq %r14, 0x8(%rsp)
movl %ebp, (%rsp)
movl $0x1, %r9d
movq %r12, %rdi
movq 0x28(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
movl 0x24(%rsp), %r8d
movq %rbx, %rcx
callq 0x6625
movq %r14, 0x8(%rsp)
movl %ebp, (%rsp)
movl $0x1, %r9d
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq 0x40(%rsp), %r8
callq 0x66dc
movq %r15, %rdi
callq 0xa09d
movq 0x30(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq