// File: /workspace/github_c_makefile_star2_O2/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, %r14d
movl %r8d, 0x18(%rsp)
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movl 0x70(%rsp), %eax
testl %eax, %eax
movq %rdi, 0x28(%rsp)
movl %ecx, 0x20(%rsp)
je 0x6e30
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x6982
movq $0x0, 0x30(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0x65d9
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0x67f0
jmp 0x6e90
movl %ecx, %r12d
movl %ecx, %edi
shlq $0x2, %rdi
callq 0x76d0
movq %rax, %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
movl %r14d, %r9d
callq 0x64bc
movl %r14d, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x5f61
movl %r14d, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, 0x30(%rsp)
movq %r13, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x601f
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %ecx
movl %ebp, 0x1c(%rsp)
cmovgl %ebp, %ecx
xorl %r12d, %r12d
movl 0x18(%rsp), %ebp
movslq %r12d, %r13
cmpq %rax, %rcx
je 0x6ec4
movl (%r15,%rax,4), %edx
testl %edx, %edx
jle 0x6ebf
leal 0x1(%r13), %r12d
movl %edx, (%r15,%r13,4)
incq %rax
jmp 0x6ea7
movslq %ebp, %r8
leaq (%r15,%r8,4), %rbx
leaq (,%r13,4), %rax
subq %rax, %rbx
movl %r14d, 0x24(%rsp)
movl %r14d, 0x8(%rsp)
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
movl 0x1c(%rsp), %ecx
movl %r13d, %r9d
callq 0x6b70
cmpl %r13d, %eax
jge 0x6fd1
subl %r12d, %ebp
movl 0x70(%rsp), %eax
incl %eax
movl %eax, (%rsp)
xorl %r14d, %r14d
pushq $0x4
popq %r9
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
movl %ebp, %r8d
callq 0x6dc2
incl %eax
movl %eax, 0x18(%rsp)
testl %r12d, %r12d
cmovgl %r12d, %r14d
movl 0x1c(%rsp), %ebp
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
callq 0x6d40
xorl %eax, %eax
movl 0x70(%rsp), %edx
cmpq %rax, %r14
je 0x6f6b
movslq (%r15,%rax,4), %rcx
movl (%rbx,%rcx,4), %ecx
movl %ecx, (%r15,%rax,4)
incq %rax
jmp 0x6f56
xorl %eax, %eax
testl %edx, %edx
setne %al
shll $0x1f, %eax
movslq %ebp, %rcx
movl 0x24(%rsp), %r14d
cmpq %rcx, %r13
jge 0x6f8b
movl %eax, (%r15,%r13,4)
incq %r13
jmp 0x6f7d
testl %edx, %edx
je 0x7001
movq %r15, %rdi
movq 0x28(%rsp), %r13
movq %r13, %rsi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x6595
pushq $0x1
popq %rbx
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
movl %ebx, %ecx
movl %r14d, %r8d
callq 0x65d9
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
movl %ebx, %ecx
movl %r14d, %r8d
callq 0x67f0
jmp 0x706a
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %r14d
cmovgl %r12d, %r14d
pushq $0x1
popq %rcx
movl %ecx, 0x18(%rsp)
movl 0x1c(%rsp), %ebp
cmpq %rax, %r14
je 0x6f37
movslq (%rbx,%rax,4), %rcx
movl %eax, (%r15,%rcx,4)
incq %rax
jmp 0x6feb
movl %r14d, (%rsp)
movq %r15, %rdi
movq 0x28(%rsp), %r13
movq %r13, %rsi
movq 0x30(%rsp), %rbx
movq %rbx, %rdx
movl %ebp, %ecx
movl 0x20(%rsp), %r8d
movl %r12d, %r9d
callq 0x5eda
movl %r14d, (%rsp)
pushq $0x1
popq %r9
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
movl 0x20(%rsp), %r12d
movl %r12d, %r8d
callq 0x5f61
movl %r14d, (%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
movl %r12d, %r8d
pushq $0x1
popq %r9
callq 0x601f
movq %rbx, %rdi
callq 0x77a6
movl 0x18(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq