// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
cmpl $0x0, 0x10(%rbp)
jne 0xab41
movl -0x18(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x20(%rbp), %r9d
callq 0x9600
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x20(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x87f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x20(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x89b0
jmp 0xab80
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0xa080
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x20(%rbp), %r8d
xorl %ecx, %ecx
callq 0x98e0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x20(%rbp), %r8d
xorl %ecx, %ecx
callq 0x9cf0
movl $0x0, -0x34(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xabeb
cmpl $0x0, 0x10(%rbp)
jne 0xabaa
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jg 0xabbe
cmpl $0x0, 0x10(%rbp)
je 0xabde
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0xabde
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xabe0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xab8e
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x34(%rbp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x1c(%rbp), %r8d
movl -0x34(%rbp), %r9d
movl 0x10(%rbp), %r10d
movl -0x20(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xa5d0
movl %eax, -0x4c(%rbp)
movl $0x1, -0x50(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0xac84
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x34(%rbp), %edx
movl -0x1c(%rbp), %r8d
subl -0x34(%rbp), %r8d
movl 0x10(%rbp), %eax
addl $0x1, %eax
xorl %ecx, %ecx
movl $0x4, %r9d
movl %eax, (%rsp)
callq 0xaaa0
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xacb6
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0xacb4
movl -0x24(%rbp), %edx
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rcx
movslq -0x24(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xac8b
jmp 0xacb6
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x34(%rbp), %r8d
movl 0x10(%rbp), %r9d
movl -0x20(%rbp), %eax
movl %eax, (%rsp)
callq 0xa900
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0xad10
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xacdf
movl -0x34(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xad44
movl 0x10(%rbp), %ecx
xorl %edx, %edx
movl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %ecx
cmovnel %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xad16
cmpl $0x0, 0x10(%rbp)
jne 0xadbf
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x34(%rbp), %r9d
movl -0x20(%rbp), %eax
movl %eax, (%rsp)
callq 0x86f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x20(%rbp), %eax
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x87f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl -0x20(%rbp), %eax
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x89b0
movq -0x30(%rbp), %rdi
callq 0xb810
jmp 0xae04
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x34(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0x9820
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x20(%rbp), %r8d
movl $0x1, %ecx
callq 0x98e0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x20(%rbp), %r8d
movl $0x1, %ecx
callq 0x9cf0
movl -0x50(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)