// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movl %r9d, 0x34(%rsp)
movl %r8d, 0x24(%rsp)
movl %ecx, %ebx
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, 0x38(%rsp)
movl 0xc0(%rsp), %r14d
movslq %ecx, %rcx
cmpl $0x1, %r14d
movq %rbx, 0x28(%rsp)
movq %rcx, 0x68(%rsp)
jne 0x70da
leaq (,%rcx,4), %rbx
movq %rbx, %rdi
callq 0x76d0
movq %rax, %r12
movl $0x0, 0x44(%rsp)
jmp 0x70f9
cmpl $0x2, %r14d
sete %al
movl %eax, 0x44(%rsp)
leal -0x1(%r14), %eax
cmpl $0x1, %eax
ja 0x710b
leaq (,%rcx,4), %rbx
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x76d0
movb $0x1, %dil
movq 0x28(%rsp), %rbx
jmp 0x7112
xorl %edi, %edi
xorl %r12d, %r12d
xorl %eax, %eax
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %r15d
cmovgl %ebx, %r15d
cmpq %rcx, %r15
je 0x7135
andl $0x0, (%r13,%rcx,4)
andl $0x0, (%rbp,%rcx,4)
incq %rcx
jmp 0x7120
cmpl $0x1, %r14d
jne 0x7166
leal -0x1(%rbx), %r8d
xorl %ecx, %ecx
testl %r8d, %r8d
movl $0x0, %edx
cmovgl %r8d, %edx
cmpq %rcx, %rdx
je 0x715f
leaq 0x1(%rcx), %rsi
movl %esi, (%r12,%rcx,4)
movq %rsi, %rcx
jmp 0x714d
movslq %r8d, %rcx
movl %r8d, (%r12,%rcx,4)
testb %dil, %dil
je 0x718d
andl $0x0, (%rax)
leal -0x1(%rbx), %ecx
movslq %ecx, %rsi
pushq $0x1
popq %rcx
cmpq %rsi, %rcx
jge 0x7187
leal -0x1(%rcx), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
jmp 0x7177
leal -0x2(%rbx), %ecx
movl %ecx, (%rax,%rsi,4)
cmpl $0x3, %r14d
jne 0x71a8
xorl %edx, %edx
cmpq %rdx, %r15
je 0x71a8
leaq 0x1(%rdx), %rcx
movl %ecx, (%r13,%rdx,4)
movq %rcx, %rdx
jmp 0x7195
movq %rax, 0x50(%rsp)
movq %rdi, 0x70(%rsp)
movq %r13, 0x58(%rsp)
movq %r12, 0x60(%rsp)
movl 0x24(%rsp), %r13d
movl %r13d, %edi
shlq $0x2, %rdi
callq 0x76d0
movq %rax, %r14
movq %rbp, %rdi
movq 0x38(%rsp), %r12
movq %r12, %rsi
movq %rax, %rdx
movl %ebx, %ecx
movl %r13d, %r8d
movl 0xb0(%rsp), %r9d
callq 0x64bc
movl 0xb0(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rcx
xorl %ebx, %ebx
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x5f61
movl 0xb0(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, 0x48(%rsp)
movq %r14, %rdx
movq 0x28(%rsp), %rcx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x601f
xorl %r13d, %r13d
movl 0x34(%rsp), %r14d
movslq %r13d, %r12
cmpq %rbx, %r15
je 0x7262
movl (%rbp,%rbx,4), %eax
testl %eax, %eax
jle 0x725d
leal 0x1(%r12), %r13d
movl %eax, (%rbp,%r12,4)
incq %rbx
jmp 0x7243
movslq %r14d, %r8
leaq (,%r8,4), %rbx
addq %rbp, %rbx
leaq (,%r12,4), %rax
subq %rax, %rbx
movl 0xb0(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
movl %r12d, %r9d
callq 0x6b70
cmpl %r12d, %eax
jge 0x73da
subl %r13d, %r14d
movl 0xb8(%rsp), %eax
incl %eax
movl %eax, (%rsp)
movl %r14d, %r8d
xorl %r14d, %r14d
pushq $0x4
popq %r9
movq %rbx, %rdi
movq %rbp, %rsi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x6dc2
incl %eax
movl %eax, 0x34(%rsp)
testl %r13d, %r13d
cmovgl %r13d, %r14d
movq 0x38(%rsp), %rsi
movl 0xb0(%rsp), %eax
movl %eax, (%rsp)
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
movl %r13d, %r8d
callq 0x6d40
xorl %eax, %eax
cmpq %rax, %r14
je 0x7314
movslq (%rbp,%rax,4), %rcx
movl (%rbx,%rcx,4), %ecx
movl %ecx, (%rbp,%rax,4)
incq %rax
jmp 0x72fe
xorl %eax, %eax
movl 0xb8(%rsp), %ecx
testl %ecx, %ecx
setne %al
shll $0x1f, %eax
movq 0x28(%rsp), %rcx
movq 0x68(%rsp), %rdx
cmpq %rdx, %r12
jge 0x733e
movl %eax, (%rbp,%r12,4)
incq %r12
jmp 0x732f
movl 0xb0(%rsp), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %r14
movq %r14, %rsi
movq 0x48(%rsp), %r12
movq %r12, %rdx
movl 0x24(%rsp), %ebx
movl %ebx, %r8d
movl %r13d, %r9d
movl %eax, %r13d
callq 0x5eda
movl %r13d, (%rsp)
movq 0x28(%rsp), %r13
pushq $0x1
popq %r9
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r13d, %ecx
movl %ebx, %r8d
callq 0x5f61
movl 0xc0(%rsp), %eax
cmpl $0x1, %eax
jne 0x740b
movl 0xb0(%rsp), %eax
movl %eax, 0x18(%rsp)
movl %ebx, 0x8(%rsp)
movl %r13d, (%rsp)
movl $0x1, 0x10(%rsp)
movq %rbp, %rdi
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
movq %rcx, %r14
movq %r12, %r9
callq 0x61c6
jmp 0x74eb
pushq $0x1
popq %rax
movl %eax, 0x34(%rsp)
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %r14d
cmovgl %r13d, %r14d
movq 0x38(%rsp), %rsi
cmpq %rax, %r14
je 0x72e2
movslq (%rbx,%rax,4), %rcx
movl %eax, (%rbp,%rcx,4)
incq %rax
jmp 0x73f5
cmpb $0x0, 0x44(%rsp)
movq 0x60(%rsp), %r14
movq 0x58(%rsp), %r13
movq 0x50(%rsp), %rdx
je 0x745d
movl 0xb0(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
movl $0x1, 0x8(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rcx
movq 0x48(%rsp), %r12
movq %r12, %r8
movq 0x28(%rsp), %r9
callq 0x62bd
jmp 0x74eb
movl 0xc0(%rsp), %eax
cmpl $0x3, %eax
movq 0x48(%rsp), %r12
movl 0xb0(%rsp), %eax
jne 0x74c3
movl %eax, 0x8(%rsp)
movl $0x1, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
movq %r12, %rcx
movq 0x28(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x63c5
xorl %eax, %eax
xorl %ecx, %ecx
pushq $0x1
popq %rdi
cmpq %rcx, %r15
je 0x74eb
movslq (%r13,%rcx,4), %rdx
leal (%rax,%rdx), %esi
cmpq %rdx, %rcx
cmovgl %edi, %esi
movl %esi, (%r13,%rcx,4)
incq %rcx
decl %eax
jmp 0x74a4
movl %eax, 0x8(%rsp)
movl $0x1, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
movq %r12, %rcx
movq 0x28(%rsp), %r8
movl 0x24(%rsp), %r9d
callq 0x60d4
movq %r12, %rdi
callq 0x77a6
movl 0xc0(%rsp), %eax
cmpl $0x1, %eax
jne 0x7507
movq %r14, %rdi
callq 0x77a6
cmpb $0x0, 0x70(%rsp)
je 0x7518
movq 0x50(%rsp), %rdi
callq 0x77a6
movl 0x34(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq