// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]lyndon-array/external/sacak-lyndon.c
// Function: SACA_K_LA
int_t SACA_K_LA(int_t *s, int_t *SA, int_t *LA,
  int_t n, unsigned int K,
  int_t m, int cs, int level, 
  int MODE) {  //MODE: 1 (17n linear), 2 (13n linear), 3 (9n linear), 0 (9n non-linear), 

  int_t i;
  int_t *bkt=NULL;

	int_t *PREV=NULL;
	int_t *NEXT=NULL;


	if(MODE==1)
		NEXT = (int_t*) malloc(n*sizeof(int_t));

	if(MODE==1 || MODE==2)
		PREV = (int_t*) malloc(n*sizeof(int_t));

  for(i=0;i<n;i++)
		SA[i]=LA[i]=0;
  
	if(MODE==1){
		for(i=0;i<n-1;i++){
				NEXT[i]=i+1;
		}
		NEXT[n-1]=n-1;
	}

	if(MODE==1 || MODE==2){
		PREV[0]=0;
		for(i=1;i<n-1;i++){
				PREV[i]=i-1;
		}
		PREV[n-1]=n-2;
	}

	if(MODE==3){
    for(i=0;i<n;i++) LA[i]=i+1; //LA[i]=next_i
   // LA[n-1]=n-1;
  }

  #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.
  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

  // 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
  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
  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
  printf("SA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]);
  printf("\n\n");
  #endif

  #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");
	printf("LA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", LA[i]);
  printf("\n\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");
	printf("LA\n");
  for(i=0; i<n; i++)
		printf("%" PRIdN "\t", LA[i]);
	printf("\n\n");
	if(MODE==1){
		printf("NEXT\n");
		for(i=0; i<n; i++)
			printf("%" PRIdN "\t", NEXT[i]+1);
	  printf("\n\n");
	}
	if(MODE==1 || MODE==2){
		printf("PREV\n");
		for(i=0; i<n; i++)
			printf("%" PRIdN "\t", PREV[i]+1);
	  printf("\n\n");
	}
	#endif

  #if TIME
    t_start(&t_time, &c_time);
  #endif

	if(MODE==1)
	  induceSAs0_LA_17n(SA, LA, PREV, NEXT, s, bkt, n, K, true, cs);
	else if(MODE==2)
	  induceSAs0_LA_13n(SA, LA, PREV, s, bkt, n, K, true, cs);
	else if(MODE==3){
	  induceSAs0_LA_9n(SA, LA, s, bkt, n, K, true, cs);
    //overwrite the pointer list with LA
    int_t j;
    for(j=0; j<n; j++){ 
      if(LA[j]<j) LA[j]=1;
      else LA[j]=LA[j]-j;
    }
  }
	else
	  induceSAs0_LA(SA, LA, 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");
	printf("LA\n");
  for(i=0; i<n; i++)
		printf("%" PRIdN "\t", LA[i]);
	printf("\n\n");
	if(MODE==1){
		printf("NEXT\n");
		for(i=0; i<n; i++)
			printf("%" PRIdN "\t", NEXT[i]+1);
	  printf("\n\n");
	}
	if(MODE==1 || MODE==2){
		printf("PREV\n");
		for(i=0; i<n; i++)
			printf("%" PRIdN "\t", PREV[i]+1);
	  printf("\n\n");
	}
  #endif

  #if TIME
    printf("S-type:\n");
    t_stop(t_time, c_time);
  #endif

  free(bkt);
  
	if(MODE==1)
		free(NEXT);

	if(MODE==1 || MODE==2)
		free(PREV);

  #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 $0x68, %rsp
movl %r8d, 0x2c(%rsp)
movl %ecx, %r12d
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, 0x30(%rsp)
movl 0xb0(%rsp), %ebx
cmpl $0x1, %ebx
movl %r9d, 0x44(%rsp)
jne 0x7925
movslq %r12d, %rdi
shlq $0x2, %rdi
callq 0x7f0e
movq %rax, %r14
jmp 0x7928
xorl %r14d, %r14d
leal -0x1(%rbx), %r13d
cmpl $0x1, %r13d
ja 0x7940
movslq %r12d, %rdi
shlq $0x2, %rdi
callq 0x7f0e
jmp 0x7942
xorl %eax, %eax
testl %r12d, %r12d
jle 0x795e
movl %r12d, %esi
xorl %ecx, %ecx
xorl %edx, %edx
movl %ecx, (%r15,%rdx,4)
movl %ecx, (%rbp,%rdx,4)
incq %rdx
cmpq %rdx, %rsi
jne 0x794e
cmpl $0x1, %ebx
jne 0x7989
leal -0x1(%r12), %edi
cmpl $0x2, %r12d
jl 0x7982
movl %edi, %ecx
xorl %edx, %edx
leaq 0x1(%rdx), %rsi
movl %esi, (%r14,%rdx,4)
movq %rsi, %rdx
cmpq %rsi, %rcx
jne 0x7972
movslq %edi, %rcx
movl %edi, (%r14,%rcx,4)
cmpl $0x1, %r13d
ja 0x79be
movl $0x0, (%rax)
leal -0x1(%r12), %esi
cmpl $0x3, %r12d
jl 0x79b3
movl %esi, %ecx
decq %rcx
xorl %edx, %edx
movl %edx, 0x4(%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x79a7
leal -0x2(%r12), %ecx
movslq %esi, %rdx
movl %ecx, (%rax,%rdx,4)
movl %r13d, 0x5c(%rsp)
movq %r14, 0x60(%rsp)
movq %rax, 0x50(%rsp)
cmpl $0x3, %ebx
setne %al
testl %r12d, %r12d
setle %cl
orb %al, %cl
jne 0x79f2
movl %r12d, %eax
xorl %ecx, %ecx
leaq 0x1(%rcx), %rdx
movl %edx, (%r15,%rcx,4)
movq %rdx, %rcx
cmpq %rdx, %rax
jne 0x79e2
movq %r12, 0x38(%rsp)
movl 0x2c(%rsp), %r12d
movl %r12d, %edi
shlq $0x2, %rdi
callq 0x7f0e
movq %rax, %r14
movq %rbp, %rdi
movq 0x30(%rsp), %r13
movq %r13, %rsi
movq %rax, %rdx
movq 0x38(%rsp), %rcx
movl %r12d, %r8d
movl 0xa0(%rsp), %ebx
movl %ebx, %r9d
callq 0x6be8
movl %ebx, (%rsp)
xorl %ebx, %ebx
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq 0x38(%rsp), %rcx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x652d
movl 0xa0(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, 0x48(%rsp)
movq %r14, %rdx
movq 0x38(%rsp), %rcx
movl %r12d, %r8d
movq 0x38(%rsp), %r12
xorl %r9d, %r9d
callq 0x660a
testl %r12d, %r12d
movl 0x44(%rsp), %r9d
jle 0x7aa8
movl %r12d, %eax
xorl %ecx, %ecx
xorl %ebx, %ebx
cmpl $0x0, (%rbp,%rcx,4)
jle 0x7aa0
movl (%rbp,%rcx,4), %edx
movslq %ebx, %rsi
incl %ebx
movl %edx, (%rbp,%rsi,4)
incq %rcx
cmpq %rcx, %rax
jne 0x7a8c
movslq %r9d, %r8
leaq (,%r8,4), %r13
addq %rbp, %r13
movslq %ebx, %r14
leaq (,%r14,4), %rax
subq %rax, %r13
movq %r12, %rcx
movl 0xa0(%rsp), %r12d
movl %r12d, 0x8(%rsp)
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movq %r13, %rdx
movl %r9d, %r12d
movl %r14d, %r9d
callq 0x735f
cmpl %r14d, %eax
jge 0x7b1e
subl %ebx, %r12d
movl 0xa8(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movl %ebx, %edx
xorl %ecx, %ecx
movl %r12d, %r8d
movl $0x4, %r9d
callq 0x760a
incl %eax
movl %eax, 0x44(%rsp)
jmp 0x7b49
movl $0x1, 0x44(%rsp)
testl %ebx, %ebx
jle 0x7b49
xorl %eax, %eax
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %r12
movslq (%r13,%rax,4), %rcx
movl %eax, (%rbp,%rcx,4)
incq %rax
cmpq %rax, %r14
jne 0x7b36
jmp 0x7b53
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %r12
movl 0xa0(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdx
movl %r12d, %ecx
movl %ebx, %r8d
callq 0x7561
testl %ebx, %ebx
jle 0x7b87
xorl %eax, %eax
movslq (%rbp,%rax,4), %rcx
movl (%r13,%rcx,4), %ecx
movl %ecx, (%rbp,%rax,4)
incq %rax
cmpq %rax, %r14
jne 0x7b71
cmpl %r12d, %ebx
movl 0xa8(%rsp), %ecx
jge 0x7bad
xorl %eax, %eax
testl %ecx, %ecx
setne %al
shll $0x1f, %eax
movslq %r12d, %rcx
movl %eax, (%rbp,%r14,4)
incq %r14
cmpq %r14, %rcx
jne 0x7ba0
movl 0xa0(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r12, %rcx
movq 0x30(%rsp), %r13
movq %r13, %rsi
movq 0x48(%rsp), %r12
movq %r12, %rdx
movl 0x2c(%rsp), %r14d
movl %r14d, %r8d
movl %ebx, %r9d
callq 0x649b
movl 0xa0(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %r13
movq 0x38(%rsp), %r12
movq %r13, %rdx
movl %r12d, %ecx
movl %r14d, %r8d
movl $0x1, %r9d
callq 0x652d
movl 0xb0(%rsp), %ebx
movq %rbx, %rax
cmpl $0x1, %eax
jne 0x7c5c
movq %rax, %rbx
movl 0xa0(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %r14d, 0x8(%rsp)
movl %r12d, (%rsp)
movl $0x1, 0x10(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq 0x50(%rsp), %rdx
movq 0x60(%rsp), %r14
movq %r14, %rcx
movq 0x30(%rsp), %r8
movq %r13, %r9
callq 0x6830
jmp 0x7d44
cmpl $0x2, %eax
movq 0x60(%rsp), %r14
jne 0x7caa
movq %rax, %rbx
movl 0xa0(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, (%rsp)
movl $0x1, 0x8(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq 0x50(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x48(%rsp), %r13
movq %r13, %r8
movq 0x38(%rsp), %r9
callq 0x696f
jmp 0x7d44
movq %rax, %rbx
cmpl $0x3, %eax
movl 0xa0(%rsp), %eax
movq 0x38(%rsp), %r12
jne 0x7d19
movl %eax, 0x8(%rsp)
movl $0x1, (%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq 0x30(%rsp), %rdx
movq 0x48(%rsp), %r13
movq %r13, %rcx
movl %r12d, %r8d
movl 0x2c(%rsp), %r9d
callq 0x6ab1
testl %r12d, %r12d
jle 0x7d44
movl %r12d, %eax
xorl %ecx, %ecx
movl $0x1, %edx
xorl %esi, %esi
movslq (%r15,%rsi,4), %rdi
leal (%rdi,%rcx), %r8d
cmpq %rdi, %rsi
cmovgl %edx, %r8d
movl %r8d, (%r15,%rsi,4)
incq %rsi
decl %ecx
cmpq %rsi, %rax
jne 0x7cfa
jmp 0x7d44
movl %eax, 0x8(%rsp)
movl $0x1, (%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq 0x30(%rsp), %rdx
movq 0x48(%rsp), %r13
movq %r13, %rcx
movl %r12d, %r8d
movl 0x2c(%rsp), %r9d
callq 0x6704
movq %r13, %rdi
callq 0x7fc5
cmpl $0x1, %ebx
jne 0x7d59
movq %r14, %rdi
callq 0x7fc5
cmpl $0x1, 0x5c(%rsp)
ja 0x7d6a
movq 0x50(%rsp), %rdi
callq 0x7fc5
movl 0x44(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq