// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]lyndon-array/external/sacak-lyndon.c
// Function: SACA_K
int_t SACA_K(int_t *s, int_t *SA,
  int_t n, unsigned int K,
  int_t m, int cs, int level) {
  int_t i;
  int_t *bkt=NULL;

  #if TIME
    time_t t_time = 0; 
    clock_t c_time = 0; 
  #endif

  #if DEPTH
  time_t t_start = time(NULL);
  clock_t c_start =  clock();
  #endif

  // stage 1: reduce the problem by at least 1/2.
  if(level==0) {

    bkt=(int_t *)malloc(sizeof(int_t)*K);
    putSubstr0(SA, s, bkt, n, K, cs);

    #if DEBUG
    printf("bucket LMS-subs\n");
    for(i=0; i<n; i++)
          printf("%" PRIdN "\t", SA[i]);
    printf("\n");
    #endif
    induceSAl0(SA, s, bkt, n, K, false, cs);

    #if DEBUG
    printf("L-type\n");
    for(i=0; i<n; i++)
          printf("%" PRIdN "\t", SA[i]);
    printf("\n");
    #endif

    induceSAs0(SA, s, bkt, n, K, false, cs);

    #if DEBUG
    printf("S-type\n");
    for(i=0; i<n; i++)
          printf("%" PRIdN "\t", SA[i]);
    printf("\n");
    #endif
  }
  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.
  int_t n1=0;
  for(i=0; i<n; i++) 
    if((!level&&SA[i]>0) || (level&&((int_t *)SA)[i]>0))
      SA[n1++]=SA[i];

  #if DEBUG
  if(level==0) {
    printf("SA1\n");
    for(i=0; i<n1; i++)
          printf("%" PRIdN "\t", SA[i]);
    printf("\n");
  }
  #endif

  int_t *SA1=SA, *s1=SA+m-n1;
  int_t name_ctr;
  name_ctr=nameSubstr(SA,s,s1,n,m,n1,level,cs);

  // 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).

  #if DEBUG
  if(level==0) {
    printf("SA1\n");
    for(i=0; i<n1; i++)
          printf("%" PRIdN "\t", SA1[i]);
    printf("\n");
    printf("\nstage 3:\n\n");
  }
  #endif
  
  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 DEBUG
  if(level==0) {
    printf("SA\n");
    for(i=0; i<n; i++)
      printf("%" PRIdN "\t", SA[i]);
    printf("\n");
  }
  #endif

  if(level==0) {

    #if TIME
      t_start(&t_time, &c_time);
    #endif

    putSuffix0(SA, s, bkt, n, K, n1, cs);

    #if TIME
      printf("SA (mapped):\n");
      t_stop(t_time, c_time);
    #endif

    #if DEBUG
    printf("SA (mapped)\n");
    for(i=0; i<n; i++)
          printf("%" PRIdN "\t", SA[i]+1);
    printf("\n");
    #endif

    #if TIME
      t_start(&t_time, &c_time);
    #endif

    induceSAl0(SA, s, bkt, n, K, true, cs);

    #if TIME
      printf("L-type:\n");
      t_stop(t_time, c_time);
    #endif

	  #if DEBUG
	  printf("L-type\n");
	  for(i=0; i<n; i++)
	        printf("%" PRIdN "\t", SA[i]+1);
	  printf("\n");
	  #endif

    #if TIME
      t_start(&t_time, &c_time);
    #endif
    induceSAs0(SA, s, bkt, n, K, true, cs);

		#if DEBUG
  	printf("S-type\n");
  	for(i=0; i<n; i++)
  	      printf("%" PRIdN "\t", SA[i]+1);
  	printf("\n");
  	#endif

    #if TIME
      printf("S-type:\n");
      t_stop(t_time, c_time);
    #endif

    free(bkt);
  }
  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);
  t_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, %ebx
movl %r8d, %r13d
movl %ecx, %r12d
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movl 0x70(%rsp), %eax
testl %eax, %eax
je 0x78a2
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x7193
movq $0x0, 0x28(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
callq 0x6def
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
callq 0x6ff0
jmp 0x7908
movl %r12d, %edi
shlq $0x2, %rdi
callq 0x855a
movl %r13d, 0x1c(%rsp)
movq %rax, %r13
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
movl %ebx, %r9d
callq 0x6cb3
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x671d
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, 0x28(%rsp)
movq %r13, %rdx
movl 0x1c(%rsp), %r13d
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x67d6
movl %r12d, 0x24(%rsp)
testl %ebp, %ebp
movl %ebp, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
jle 0x793c
movl %ebp, %eax
xorl %ecx, %ecx
xorl %r12d, %r12d
movl (%r15,%rcx,4), %edx
testl %edx, %edx
jle 0x7932
movslq %r12d, %rsi
incl %r12d
movl %edx, (%r15,%rsi,4)
incq %rcx
cmpq %rcx, %rax
jne 0x7920
jmp 0x793f
xorl %r12d, %r12d
movslq %r13d, %r8
leaq (%r15,%r8,4), %rbp
movslq %r12d, %rbx
leaq (,%rbx,4), %rax
subq %rax, %rbp
movl 0x20(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl 0x18(%rsp), %ecx
movl %ebx, %r9d
callq 0x74e0
movq %rbx, 0x30(%rsp)
cmpl %ebx, %eax
jge 0x79ab
subl %r12d, %r13d
movl 0x70(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
movl %r13d, %r8d
movl $0x4, %r9d
callq 0x783d
incl %eax
movl %eax, 0x1c(%rsp)
movl 0x20(%rsp), %ebx
jmp 0x79d2
testl %r12d, %r12d
jle 0x7a16
movl %r12d, %eax
xorl %ecx, %ecx
movl 0x20(%rsp), %ebx
movslq (%rbp,%rcx,4), %rdx
movl %ecx, (%r15,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x79b9
movl $0x1, 0x1c(%rsp)
movl %ebx, (%rsp)
movq %r14, %rsi
movq %rbp, %rdx
movl 0x18(%rsp), %ecx
movl %r12d, %r8d
callq 0x77b4
testl %r12d, %r12d
jle 0x7a10
movl %r12d, %eax
xorl %ecx, %ecx
movl 0x70(%rsp), %r8d
movslq (%r15,%rcx,4), %rdx
movl (%rbp,%rdx,4), %edx
movl %edx, (%r15,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x79f6
movl 0x18(%rsp), %ebp
jmp 0x7a3e
movl 0x18(%rsp), %ebp
jmp 0x7a39
movl 0x20(%rsp), %ebx
movl %ebx, (%rsp)
movq %r14, %rsi
movq %rbp, %rdx
movl 0x18(%rsp), %ebp
movl %ebp, %ecx
movl %r12d, %r8d
callq 0x77b4
movl $0x1, 0x1c(%rsp)
movl 0x70(%rsp), %r8d
cmpl %ebp, %r12d
jge 0x7b4a
xorl %eax, %eax
testl %r8d, %r8d
setne %al
shll $0x1f, %eax
movslq %ebp, %rdx
movq 0x30(%rsp), %rsi
subq %rsi, %rdx
leaq 0x3(%rdx), %rcx
andq $-0x4, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%r15,%rsi,4), %rdx
addq $0xc, %rdx
xorl %esi, %esi
movdqa 0x1d2c(%rip), %xmm1     # 0x97b0
movdqa 0x1d34(%rip), %xmm2     # 0x97c0
movdqa 0x1d3c(%rip), %xmm3     # 0x97d0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movq %rsi, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edi
notl %edi
testb $0x1, %dil
je 0x7ae3
movl %eax, -0xc(%rdx,%rsi,4)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x7af6
movl %eax, -0x8(%rdx,%rsi,4)
por %xmm1, %xmm5
pxor %xmm3, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
pxor %xmm4, %xmm5
pextrw $0x0, %xmm5, %edi
testb $0x1, %dil
je 0x7b2f
movl %eax, -0x4(%rdx,%rsi,4)
pextrw $0x4, %xmm5, %edi
testb $0x1, %dil
je 0x7b3d
movl %eax, (%rdx,%rsi,4)
addq $0x4, %rsi
cmpq %rsi, %rcx
jne 0x7a9c
testl %r8d, %r8d
je 0x7bc8
cmpl $0x2, %r12d
jl 0x7b9c
movl %r12d, %eax
incq %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl -0x8(%r15,%rax,4), %edx
movl $0x80000000, -0x8(%r15,%rax,4) # imm = 0x80000000
movslq %edx, %rsi
cmpl $0x4, %ebx
jne 0x7b7c
movl (%r14,%rsi,4), %esi
jmp 0x7b81
movzbl (%r14,%rsi), %esi
cmpl %ecx, %esi
cmovnel %esi, %r12d
movslq %r12d, %rcx
decl %r12d
movl %edx, (%r15,%rcx,4)
decq %rax
movl %esi, %ecx
cmpq $0x2, %rax
ja 0x7b60
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x6def
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x6ff0
jmp 0x7c32
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %ecx
movq 0x28(%rsp), %rbp
movq %rbp, %rdx
movl 0x24(%rsp), %r13d
movl %r13d, %r8d
movl %r12d, %r9d
callq 0x668b
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl 0x18(%rsp), %r12d
movl %r12d, %ecx
movl %r13d, %r8d
movl $0x1, %r9d
callq 0x671d
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl %r12d, %ecx
movl %r13d, %r8d
movl $0x1, %r9d
callq 0x67d6
movq %rbp, %rdi
callq 0x8611
movl 0x1c(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq