// File: /workspace/github_c_makefile_star2_O3/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, 0x30(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movl 0xb0(%rsp), %r12d
cmpl $0x1, %r12d
movq %rcx, 0x28(%rsp)
movl %r9d, 0x24(%rsp)
jne 0x7c95
movslq %ecx, %r14
shlq $0x2, %r14
movq %r14, %rdi
callq 0x855a
movq %rax, %rbp
movl $0x0, 0x34(%rsp)
jmp 0x7cb3
cmpl $0x2, %r12d
sete %dl
leal -0x1(%r12), %eax
cmpl $0x1, %eax
movl %edx, 0x34(%rsp)
ja 0x7cc5
movslq %ecx, %r14
shlq $0x2, %r14
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x855a
movb $0x1, %dil
movq 0x28(%rsp), %rcx
jmp 0x7ccb
xorl %edi, %edi
xorl %ebp, %ebp
xorl %eax, %eax
testl %ecx, %ecx
jle 0x7ce6
movl %ecx, %r8d
xorl %esi, %esi
xorl %edx, %edx
movl %esi, (%rbx,%rdx,4)
movl %esi, (%r13,%rdx,4)
incq %rdx
cmpq %rdx, %r8
jne 0x7cd6
cmpl $0x1, %r12d
jne 0x7daf
leal -0x1(%rcx), %r9d
cmpl $0x2, %ecx
jl 0x7da7
movl %r9d, %edx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movl %ecx, %r8d
shrl %r8d
andl $0x3fffffff, %r8d       # imm = 0x3FFFFFFF
shlq $0x3, %r8
movdqa 0x1a9a(%rip), %xmm1     # 0x97c0
xorl %edx, %edx
movdqa 0x1aa0(%rip), %xmm2     # 0x97d0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x1ad0(%rip), %xmm4     # 0x9810
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm5     # xmm5 = xmm5[1,1,3,3]
pand %xmm6, %xmm5
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
por %xmm5, %xmm6
movdqa %xmm1, %xmm5
psubq %xmm3, %xmm5
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0x7d80
movd %xmm5, (%rbp,%rdx)
pxor %xmm3, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x7d9a
pshufd $0xee, %xmm5, %xmm5     # xmm5 = xmm5[2,3,2,3]
movd %xmm5, 0x4(%rbp,%rdx)
paddq %xmm4, %xmm1
addq $0x8, %rdx
cmpq %rdx, %r8
jne 0x7d40
movslq %r9d, %rdx
movl %r9d, (%rbp,%rdx,4)
testb %dil, %dil
je 0x7e68
movl $0x0, (%rax)
leal -0x1(%rcx), %r9d
cmpl $0x3, %ecx
jl 0x7e5f
movl %r9d, %edx
movl %edx, %r8d
andl $0x7ffffffe, %r8d       # imm = 0x7FFFFFFE
addq $-0x2, %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
xorl %edx, %edx
movdqa 0x19d0(%rip), %xmm1     # 0x97c0
movdqa 0x19d8(%rip), %xmm2     # 0x97d0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movq %rdx, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm1, %xmm4
pxor %xmm2, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm0, %xmm5
pcmpeqd %xmm0, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %esi
notl %esi
testb $0x1, %sil
je 0x7e40
movl %edx, 0x4(%rax,%rdx,4)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %esi
testb $0x1, %sil
je 0x7e56
leal 0x1(%rdx), %esi
movl %esi, 0x8(%rax,%rdx,4)
addq $0x2, %rdx
cmpq %rdx, %r8
jne 0x7e00
leal -0x2(%rcx), %edx
movslq %r9d, %rsi
movl %edx, (%rax,%rsi,4)
movq %rax, 0x50(%rsp)
testl %ecx, %ecx
setle %al
cmpl $0x3, %r12d
setne %dl
orb %al, %dl
jne 0x7f1f
movl %ecx, %edx
leal 0x1(%rcx), %eax
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %eax
shlq $0x3, %rax
movdqa 0x191f(%rip), %xmm1     # 0x97c0
xorl %ecx, %ecx
movdqa 0x1925(%rip), %xmm2     # 0x97d0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x1955(%rip), %xmm4     # 0x9810
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm5     # xmm5 = xmm5[1,1,3,3]
pand %xmm6, %xmm5
pshufd $0xf5, %xmm6, %xmm6     # xmm6 = xmm6[1,1,3,3]
por %xmm5, %xmm6
movdqa %xmm1, %xmm5
psubq %xmm3, %xmm5
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x7ef9
movd %xmm5, (%rbx,%rcx)
pxor %xmm3, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x7f12
pshufd $0xee, %xmm5, %xmm5     # xmm5 = xmm5[2,3,2,3]
movd %xmm5, 0x4(%rbx,%rcx)
paddq %xmm4, %xmm1
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x7ebb
movq %rbx, 0x38(%rsp)
movq %rbp, 0x60(%rsp)
movq %rdi, 0x58(%rsp)
movl 0x30(%rsp), %ebp
movl %ebp, %edi
shlq $0x2, %rdi
callq 0x855a
movq %rax, %r12
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
movq 0x28(%rsp), %rcx
movl %ebp, %r8d
movq %r15, %rbx
movl 0xa0(%rsp), %r15d
movl %r15d, %r9d
callq 0x6cb3
movl %r15d, (%rsp)
xorl %r14d, %r14d
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x671d
movl %r15d, (%rsp)
movq %r13, %rdi
movq %rbx, 0x48(%rsp)
movq %rbx, %rsi
movq %r12, 0x40(%rsp)
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x67d6
movq 0x28(%rsp), %rcx
testl %ecx, %ecx
jle 0x7fd7
movl %ecx, %eax
xorl %edi, %edi
xorl %r14d, %r14d
movl (%r13,%rdi,4), %edx
testl %edx, %edx
jle 0x7fcf
movslq %r14d, %rsi
incl %r14d
movl %edx, (%r13,%rsi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x7fbb
movl 0xa8(%rsp), %ebx
movl 0x24(%rsp), %r15d
movslq %r15d, %r8
leaq (,%r8,4), %rbp
addq %r13, %rbp
movslq %r14d, %r12
leaq (,%r12,4), %rax
subq %rax, %rbp
movl 0xa0(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r13, %rdi
movq 0x48(%rsp), %rsi
movq %rbp, %rdx
movl %r12d, %r9d
callq 0x74e0
cmpl %r12d, %eax
jge 0x8059
subl %r14d, %r15d
leal 0x1(%rbx), %eax
movl %eax, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movl %r14d, %edx
xorl %ecx, %ecx
movl %r15d, %r8d
movl $0x4, %r9d
callq 0x783d
incl %eax
movl %eax, 0x24(%rsp)
movq 0x28(%rsp), %rcx
movl 0xa0(%rsp), %r15d
jmp 0x808b
testl %r14d, %r14d
jle 0x833a
xorl %eax, %eax
movq 0x28(%rsp), %rcx
movl 0xa0(%rsp), %r15d
movslq (%rbp,%rax,4), %rdx
movl %eax, (%r13,%rdx,4)
incq %rax
cmpq %rax, %r12
jne 0x8071
movl $0x1, 0x24(%rsp)
movl %r15d, (%rsp)
movq 0x48(%rsp), %rsi
movq %rbp, %rdx
movl %r14d, %r8d
callq 0x77b4
testl %r14d, %r14d
jle 0x80bc
xorl %eax, %eax
movslq (%r13,%rax,4), %rcx
movl (%rbp,%rcx,4), %ecx
movl %ecx, (%r13,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x80a6
movq 0x28(%rsp), %rcx
cmpl %ecx, %r14d
movq 0x48(%rsp), %rsi
jge 0x81d4
xorl %eax, %eax
testl %ebx, %ebx
setne %al
shll $0x1f, %eax
movslq %ecx, %rdx
subq %r12, %rdx
leaq 0x3(%rdx), %r8
andq $-0x4, %r8
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0xc(,%r12,4), %rdx
addq %r13, %rdx
xorl %r9d, %r9d
movdqa 0x16a6(%rip), %xmm1     # 0x97b0
movdqa 0x16ae(%rip), %xmm2     # 0x97c0
movdqa 0x16b6(%rip), %xmm3     # 0x97d0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movq %r9, %xmm5
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edi
notl %edi
testb $0x1, %dil
je 0x816a
movl %eax, -0xc(%rdx,%r9,4)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x817e
movl %eax, -0x8(%rdx,%r9,4)
por %xmm1, %xmm5
pxor %xmm3, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
pxor %xmm4, %xmm5
pextrw $0x0, %xmm5, %edi
testb $0x1, %dil
je 0x81b8
movl %eax, -0x4(%rdx,%r9,4)
pextrw $0x4, %xmm5, %edi
testb $0x1, %dil
je 0x81c7
movl %eax, (%rdx,%r9,4)
addq $0x4, %r9
cmpq %r9, %r8
jne 0x8122
movl %r15d, (%rsp)
movq %r13, %rdi
movq 0x40(%rsp), %rbp
movq %rbp, %rdx
movl 0x30(%rsp), %r12d
movl %r12d, %r8d
movq %rsi, %rbx
movl %r14d, %r9d
movq %rcx, %r14
callq 0x668b
movl %r15d, (%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl %r14d, %ecx
movl %r12d, %r8d
movl $0x1, %r9d
callq 0x671d
movl 0xb0(%rsp), %eax
cmpl $0x1, %eax
jne 0x8271
movl %r15d, 0x18(%rsp)
movl %r12d, 0x8(%rsp)
movl %r14d, (%rsp)
movl $0x1, 0x10(%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq 0x50(%rsp), %r12
movq %r12, %rdx
movq 0x60(%rsp), %r14
movq %r14, %rcx
movq %rbx, %r8
movq %rbp, %r9
callq 0x6995
movq %rbp, %rdi
callq 0x8611
movq %r14, %rdi
movq 0x58(%rsp), %r14
jmp 0x839d
cmpb $0x0, 0x34(%rsp)
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %r12
je 0x82bb
movl %r15d, 0x10(%rsp)
movl 0x30(%rsp), %eax
movl %eax, (%rsp)
movl $0x1, 0x8(%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x40(%rsp), %rbx
movq %rbx, %r8
movq 0x28(%rsp), %r9
callq 0x6a96
jmp 0x839a
movl 0xb0(%rsp), %eax
cmpl $0x3, %eax
movq 0x28(%rsp), %r8
movq %rbx, %rdx
jne 0x8374
movl %r15d, 0x8(%rsp)
movl $0x1, (%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
movq 0x40(%rsp), %r15
movq %r15, %rcx
movl 0x30(%rsp), %r9d
callq 0x6baa
movq 0x28(%rsp), %rax
testl %eax, %eax
jle 0x83c4
movl %eax, %eax
xorl %ecx, %ecx
movl $0x1, %edx
xorl %esi, %esi
movl 0x24(%rsp), %ebp
movslq (%rbx,%rsi,4), %rdi
leal (%rdi,%rcx), %r8d
cmpq %rdi, %rsi
cmovgl %edx, %r8d
movl %r8d, (%rbx,%rsi,4)
incq %rsi
decl %ecx
cmpq %rsi, %rax
jne 0x8318
movq %r15, %rdi
jmp 0x83a1
movl 0xa0(%rsp), %eax
movl %eax, (%rsp)
movq 0x48(%rsp), %rsi
movq %rbp, %rdx
movq 0x28(%rsp), %r15
movl %r15d, %ecx
movl %r14d, %r8d
callq 0x77b4
movq %r15, %rcx
movl 0xa0(%rsp), %r15d
movl $0x1, 0x24(%rsp)
jmp 0x80c1
movl %r15d, 0x8(%rsp)
movl $0x1, (%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rcx
movl 0x30(%rsp), %r9d
callq 0x6896
movq %rbx, %rdi
movl 0x24(%rsp), %ebp
callq 0x8611
testb %r14b, %r14b
je 0x83b3
movq %r12, %rdi
callq 0x8611
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
jmp 0x839d