// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movl %ecx, %r12d
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movl 0x70(%rsp), %r13d
testl %r13d, %r13d
je 0x8f3c
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x8433
movq $0x0, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x8060
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x827e
jmp 0x8fa0
movl %r12d, %edi
shlq $0x3, %rdi
callq 0x9fd2
movq %rax, %r13
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %rbx, %rcx
movl %r12d, %r8d
movl %ebp, %r9d
callq 0x7dd5
movl %ebp, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x6586
movl %ebp, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, 0x30(%rsp)
movq %r13, %rdx
movl 0x70(%rsp), %r13d
movq %rbx, %rcx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x6660
movq %r14, 0x28(%rsp)
movl %r12d, 0x24(%rsp)
testq %rbx, %rbx
je 0x8fe3
xorl %eax, %eax
xorl %r12d, %r12d
movq 0x18(%rsp), %r14
movq (%r15,%rax,8), %rcx
testl %r13d, %r13d
je 0x8fdc
testq %rcx, %rcx
jle 0x8fd2
movq (%r15,%rax,8), %rcx
movq %rcx, (%r15,%r12,8)
incq %r12
incq %rax
cmpq %rax, %rbx
jne 0x8fb9
jmp 0x8feb
testq %rcx, %rcx
jne 0x8fc7
jmp 0x8fd2
xorl %r12d, %r12d
movq 0x18(%rsp), %r14
leaq (%r15,%r14,8), %r13
leaq (,%r12,8), %rax
subq %rax, %r13
movl %ebp, 0x8(%rsp)
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x868e
cmpq %r12, %rax
jae 0x904b
subq %r12, %r14
movl 0x70(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movq %r14, %r8
movl $0x8, %r9d
callq 0x8ecf
incq %rax
movq %rax, 0x18(%rsp)
jmp 0x9074
movl $0x1, %eax
movq %rax, 0x18(%rsp)
testq %r12, %r12
je 0x9074
xorl %eax, %eax
movq 0x28(%rsp), %r14
movq (%r13,%rax,8), %rcx
movq %rax, (%r15,%rcx,8)
incq %rax
cmpq %rax, %r12
jne 0x9061
jmp 0x9079
movq 0x28(%rsp), %r14
movl %ebp, (%rsp)
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r12, %r8
callq 0x8d3d
testq %r12, %r12
je 0x90a9
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
movq (%r13,%rcx,8), %rcx
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x9094
cmpq %rbx, %r12
movl 0x70(%rsp), %edx
jae 0x90cc
xorl %eax, %eax
testl %edx, %edx
setne %al
shlq $0x3f, %rax
movq %r12, %rcx
movq %rax, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x90c0
testl %edx, %edx
je 0x914e
cmpq $0x2, %r12
jl 0x9120
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
leaq 0x1(%r12), %rcx
movq $-0x1, %rdx
movq -0x10(%r15,%rcx,8), %rsi
movq %rax, -0x10(%r15,%rcx,8)
cmpl $0x8, %ebp
jne 0x9101
movq (%r14,%rsi,8), %rdi
jmp 0x9106
movzbl (%r14,%rsi), %edi
cmpq %rdx, %rdi
cmovneq %rdi, %r12
movq %rsi, (%r15,%r12,8)
decq %r12
decq %rcx
movq %rdi, %rdx
cmpq $0x2, %rcx
jg 0x90ec
movl $0x1, %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %r8d
callq 0x8060
movl $0x1, %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %r8d
callq 0x827e
jmp 0x91b6
movl %ebp, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq 0x30(%rsp), %r13
movq %r13, %rdx
movq %rbx, %rcx
movl 0x24(%rsp), %r8d
movq %r12, %r9
callq 0x6215
movl %ebp, (%rsp)
movl $0x1, %r9d
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl 0x24(%rsp), %r12d
movl %r12d, %r8d
callq 0x6586
movl %ebp, (%rsp)
movl $0x1, %r9d
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %r12d, %r8d
callq 0x6660
movq %r13, %rdi
callq 0xa089
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq