// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/src/gsacak.c
// Function: SACA_K
int_t SACA_K(int_t *s, uint_t *SA,
  uint_t n, unsigned int K,
  uint_t m, int cs, int level) {
  uint_t i;
  uint_t *bkt=NULL;
  
  #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
  if(!level){
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  }
  #endif

  // stage 1: reduce the problem by at least 1/2.
  if(level==0) {

    bkt=(uint_t *)malloc(sizeof(int_t)*K);
    putSubstr0(SA, s, bkt, n, K, cs);

    induceSAl0(SA, s, bkt, n, K, false, cs);
    induceSAs0(SA, s, bkt, n, K, false, cs);

  }
  else {
    putSubstr1((int_t *)SA, (int_t *)s,(int_t)n, cs);
    induceSAl1((int_t *)SA, (int_t *)s, n ,false, cs);
    induceSAs1((int_t *)SA, (int_t *)s, n, false, cs);
  }

  // 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((!level&&SA[i]>0) || (level&&((int_t *)SA)[i]>0))
      SA[n1++]=SA[i];

  uint_t *SA1=SA, *s1=SA+m-n1;
  uint_t name_ctr;
  name_ctr=nameSubstr(SA,s,s1,n,m,n1,level,cs);

  #if PHASES
  if(!level){
	printf("phase 1:\n");
	time_stop(t_start_phase, c_start_phase);
  }
  #endif

  #if PHASES
  if(!level){
	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, 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]=level?EMPTY_k:0; 

  if(level==0) {
    putSuffix0(SA, s, bkt, n, K, n1, cs);

    #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(SA, s, bkt, n, K, true, cs);

    #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(SA, s, bkt, n, K, true, cs);
    free(bkt);

    #if PHASES
    printf("phase 4:\n");
    time_stop(t_start_phase, c_start_phase);
    #endif
  }
  else {
    putSuffix1((int_t *)SA, (int_t *)s, n1, cs);
    induceSAl1((int_t *)SA, (int_t *)s, n, true, cs);
    induceSAs1((int_t *)SA, (int_t *)s, n, true, cs);
  }

  #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

return depth;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebp
movq %r8, %r13
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
movl 0x70(%rsp), %ebx
testl %ebx, %ebx
movl %r9d, 0x20(%rsp)
movl %ecx, 0x24(%rsp)
je 0x8324
movq %r12, %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x7a31
movq $0x0, 0x30(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x7672
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x7871
jmp 0x8399
movl %ecx, %edi
shlq $0x3, %rdi
callq 0x92c8
movq %r13, 0x28(%rsp)
movq %rax, %r15
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
movq %r14, %rcx
movl 0x24(%rsp), %r13d
movl %r13d, %r8d
movl %ebp, %r9d
callq 0x7453
movl %ebp, (%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x5f45
movl %ebp, (%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r15, 0x30(%rsp)
movq %r15, %rdx
movq 0x28(%rsp), %r13
movq %r14, %rcx
movl 0x24(%rsp), %r8d
xorl %r9d, %r9d
callq 0x5fff
xorl %ebp, %ebp
xorl %eax, %eax
cmpq %rax, %r14
je 0x83c2
movq (%r12,%rax,8), %rcx
testl %ebx, %ebx
je 0x83bb
testq %rcx, %rcx
jle 0x83b6
movq %rcx, (%r12,%rbp,8)
incq %rbp
incq %rax
jmp 0x839d
testq %rcx, %rcx
jne 0x83af
jmp 0x83b6
movq %r13, %r15
leaq (%r12,%r13,8), %r13
leaq (,%rbp,8), %rax
subq %rax, %r13
movl 0x20(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %r15, %r8
movq %rbp, %r9
callq 0x7c40
cmpq %rbp, %rax
jae 0x84cd
subq %rbp, %r15
leal 0x1(%rbx), %eax
movl %eax, (%rsp)
pushq $0x8
popq %r9
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x82ae
incq %rax
movq %rax, 0x28(%rsp)
movl 0x20(%rsp), %eax
movl %eax, (%rsp)
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %rbp, %r8
callq 0x8173
xorl %eax, %eax
cmpq %rax, %rbp
je 0x8459
movq (%r12,%rax,8), %rcx
movq (%r13,%rcx,8), %rcx
movq %rcx, (%r12,%rax,8)
incq %rax
jmp 0x8442
xorl %eax, %eax
testl %ebx, %ebx
setne %al
shlq $0x3f, %rax
cmpq %r14, %rbp
movq %r14, %rcx
cmovaq %rbp, %rcx
movq %rbp, %rdx
cmpq %rdx, %rcx
je 0x847f
movq %rax, (%r12,%rdx,8)
incq %rdx
jmp 0x8471
testl %ebx, %ebx
je 0x84ee
movq %r12, %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %rbp, %rdx
movl 0x20(%rsp), %ebx
movl %ebx, %ecx
callq 0x762a
pushq $0x1
popq %r13
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
movl %ebx, %r8d
callq 0x7672
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
movl %ebx, %r8d
callq 0x7871
jmp 0x855b
xorl %eax, %eax
pushq $0x1
popq %rcx
movq %rcx, 0x28(%rsp)
cmpq %rax, %rbp
je 0x8426
movq (%r13,%rax,8), %rcx
movq %rax, (%r12,%rcx,8)
incq %rax
jmp 0x84d7
movl 0x20(%rsp), %ebx
movl %ebx, (%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %r15
movq %r15, %rdx
movq %r14, %rcx
movl 0x24(%rsp), %r13d
movl %r13d, %r8d
movq %rbp, %r9
callq 0x5bb7
movl %ebx, (%rsp)
pushq $0x1
popq %r9
movq %r12, %rdi
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
movl %r13d, %r8d
callq 0x5f45
movl %ebx, (%rsp)
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %rcx
movl %r13d, %r8d
pushq $0x1
popq %r9
callq 0x5fff
movq %r15, %rdi
callq 0x939e
movq 0x28(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq