// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movl %r9d, %ebx
movl %ecx, %r12d
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movl 0x60(%rsp), %eax
testl %eax, %eax
movl %r8d, 0x14(%rsp)
je 0x7674
movl %r8d, %r13d
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %ebx, %ecx
callq 0x70df
movq $0x0, 0x20(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
callq 0x6d2c
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
callq 0x6f2a
jmp 0x76d5
movl %r12d, %edi
shlq $0x2, %rdi
callq 0x7f0e
movq %rax, %r13
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
movl %ebx, %r9d
callq 0x6be8
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x652d
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, 0x20(%rsp)
movq %r13, %rdx
movl 0x14(%rsp), %r13d
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x660a
movl %r12d, 0x1c(%rsp)
testl %ebp, %ebp
movl %ebp, 0x18(%rsp)
jle 0x7708
movl %ebp, %eax
xorl %ecx, %ecx
xorl %r12d, %r12d
cmpl $0x0, (%r15,%rcx,4)
jle 0x76fe
movl (%r15,%rcx,4), %edx
movslq %r12d, %rsi
incl %r12d
movl %edx, (%r15,%rsi,4)
incq %rcx
cmpq %rcx, %rax
jne 0x76e9
jmp 0x770b
xorl %r12d, %r12d
movslq %r13d, %r8
leaq (%r15,%r8,4), %rbp
movslq %r12d, %r13
leaq (,%r13,4), %rax
subq %rax, %rbp
movl %ebx, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl 0x18(%rsp), %ecx
movl %r13d, %r9d
callq 0x735f
cmpl %r13d, %eax
jge 0x776d
movl 0x14(%rsp), %r8d
subl %r12d, %r8d
movl 0x60(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
movl $0x4, %r9d
callq 0x760a
incl %eax
movl %eax, 0x14(%rsp)
jmp 0x7790
movl $0x1, 0x14(%rsp)
testl %r12d, %r12d
jle 0x7790
movl %r12d, %eax
xorl %ecx, %ecx
movslq (%rbp,%rcx,4), %rdx
movl %ecx, (%r15,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x777f
movl %ebx, (%rsp)
movq %r14, %rsi
movq %rbp, %rdx
movl 0x18(%rsp), %ecx
movl %r12d, %r8d
callq 0x7561
testl %r12d, %r12d
jle 0x77c3
movl %r12d, %eax
xorl %ecx, %ecx
movslq (%r15,%rcx,4), %rdx
movl (%rbp,%rdx,4), %edx
movl %edx, (%r15,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x77af
movl 0x18(%rsp), %ebp
cmpl %ebp, %r12d
jge 0x77e9
xorl %eax, %eax
movl 0x60(%rsp), %ecx
testl %ecx, %ecx
setne %al
shll $0x1f, %eax
movslq %ebp, %rcx
movl %eax, (%r15,%r13,4)
incq %r13
cmpq %r13, %rcx
jne 0x77dd
movl 0x60(%rsp), %eax
testl %eax, %eax
je 0x786a
cmpl $0x2, %r12d
jl 0x783e
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 0x781e
movl (%r14,%rsi,4), %esi
jmp 0x7823
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
jg 0x7802
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x6d2c
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl $0x1, %ecx
movl %ebx, %r8d
callq 0x6f2a
jmp 0x78cf
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq 0x20(%rsp), %r13
movq %r13, %rdx
movl %ebp, %ecx
movl 0x1c(%rsp), %r8d
movl %r12d, %r9d
callq 0x649b
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movl 0x1c(%rsp), %r12d
movl %r12d, %r8d
movl $0x1, %r9d
callq 0x652d
movl %ebx, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
movl $0x1, %r9d
callq 0x660a
movq %r13, %rdi
callq 0x7fc5
movl 0x14(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq