// File: /workspace/github_c_makefile_star2_O3/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 0x8d9d
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x81b7
movq $0x0, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x7e28
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x8027
jmp 0x8e01
movl %r12d, %edi
shlq $0x3, %rdi
callq 0x9fe6
movq %rax, %r13
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %rbx, %rcx
movl %r12d, %r8d
movl %ebp, %r9d
callq 0x7bfa
movl %ebp, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x64b8
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 0x6573
testq %rbx, %rbx
movq 0x18(%rsp), %r8
movl %r12d, 0x24(%rsp)
je 0x8ea7
xorl %eax, %eax
xorl %r12d, %r12d
movq (%r15,%rax,8), %rcx
testl %r13d, %r13d
je 0x8e38
testq %rcx, %rcx
jle 0x8e2e
movq %rcx, (%r15,%r12,8)
incq %r12
incq %rax
cmpq %rax, %rbx
jne 0x8e19
jmp 0x8e3f
testq %rcx, %rcx
jne 0x8e27
jmp 0x8e2e
leaq (%r15,%r8,8), %r13
leaq (,%r12,8), %rax
subq %rax, %r13
movl %ebp, 0x8(%rsp)
movq %r15, %rdi
movq %r14, 0x28(%rsp)
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r8, %r14
movq %r12, %r9
callq 0x845f
cmpq %r12, %rax
jae 0x8ec5
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 0x8d30
incq %rax
movq %rax, 0x18(%rsp)
movq 0x28(%rsp), %r14
jmp 0x8eec
leaq (%r15,%r8,8), %r13
movl %ebp, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r9d, %r9d
callq 0x845f
jmp 0x8f23
testq %r12, %r12
je 0x8f1e
xorl %eax, %eax
movq 0x28(%rsp), %r14
movq (%r13,%rax,8), %rcx
movq %rax, (%r15,%rcx,8)
incq %rax
cmpq %rax, %r12
jne 0x8ed1
movl $0x1, %eax
movq %rax, 0x18(%rsp)
movl %ebp, (%rsp)
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r12, %r8
callq 0x8bee
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
movq (%r13,%rcx,8), %rcx
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x8f02
movq 0x18(%rsp), %r13
jmp 0x8f40
movq 0x28(%rsp), %r14
movl %ebp, (%rsp)
xorl %r12d, %r12d
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x8bee
movl $0x1, %r13d
movq %rbx, %rdx
subq %r12, %rdx
movl 0x70(%rsp), %r8d
jbe 0x8ff1
xorl %eax, %eax
testl %r8d, %r8d
setne %al
shlq $0x3f, %rax
leaq 0x1(%rdx), %rcx
andq $-0x2, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%r15,%r12,8), %rdx
addq $0x8, %rdx
xorl %esi, %esi
movdqa 0x2b2c(%rip), %xmm1     # 0xbab0
movdqa 0x2b34(%rip), %xmm2     # 0xbac0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movq %rsi, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm1, %xmm4
pxor %xmm2, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm0, %xmm5
pcmpeqd %xmm0, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %edi
notl %edi
testb $0x1, %dil
je 0x8fd5
movq %rax, -0x8(%rdx,%rsi,8)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %edi
testb $0x1, %dil
je 0x8fe8
movq %rax, (%rdx,%rsi,8)
addq $0x2, %rsi
cmpq %rsi, %rcx
jne 0x8f94
testl %r8d, %r8d
je 0x9074
cmpq $0x2, %r12
jl 0x9046
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 0x9027
movq (%r14,%rsi,8), %rdi
jmp 0x902c
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
ja 0x9012
movl $0x1, %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %r8d
callq 0x7e28
movl $0x1, %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %ebp, %r8d
callq 0x8027
jmp 0x90e6
movl %ebp, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, 0x18(%rsp)
movq 0x30(%rsp), %r13
movq %r13, %rdx
movq %rbx, %rcx
movl 0x24(%rsp), %r8d
movq %r12, %r9
callq 0x6141
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 0x64b8
movl %ebp, (%rsp)
movl $0x1, %r9d
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %r12d, %r8d
callq 0x6573
movq %r13, %rdi
movq 0x18(%rsp), %r13
callq 0xa09d
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq