// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x1, 0x20(%rbp)
jne 0xae6a
movslq -0x1c(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x40(%rbp)
cmpl $0x1, 0x20(%rbp)
je 0xae76
cmpl $0x2, 0x20(%rbp)
jne 0xae87
movslq -0x1c(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x38(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xaebf
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xae8e
cmpl $0x1, 0x20(%rbp)
jne 0xaf0b
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xaef5
movl -0x28(%rbp), %edx
addl $0x1, %edx
movq -0x40(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xaecc
movl -0x1c(%rbp), %edx
subl $0x1, %edx
movq -0x40(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x1, 0x20(%rbp)
je 0xaf17
cmpl $0x2, 0x20(%rbp)
jne 0xaf67
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movl $0x1, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xaf51
movl -0x28(%rbp), %edx
subl $0x1, %edx
movq -0x38(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xaf28
movl -0x1c(%rbp), %edx
subl $0x2, %edx
movq -0x38(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x3, 0x20(%rbp)
jne 0xaf9a
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xaf98
movl -0x28(%rbp), %edx
addl $0x1, %edx
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xaf74
jmp 0xaf9a
movl -0x20(%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 -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl 0x10(%rbp), %r9d
callq 0x9600
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl 0x10(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x87f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl 0x10(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x89b0
movl $0x0, -0x44(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xb075
cmpl $0x0, 0x18(%rbp)
jne 0xb034
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jg 0xb048
cmpl $0x0, 0x18(%rbp)
je 0xb068
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0xb068
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movl -0x44(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x44(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xb06a
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xb018
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x44(%rbp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x24(%rbp), %r8d
movl -0x44(%rbp), %r9d
movl 0x18(%rbp), %r10d
movl 0x10(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xa5d0
movl %eax, -0x5c(%rbp)
movl $0x1, -0x60(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0xb10e
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x44(%rbp), %edx
movl -0x24(%rbp), %r8d
subl -0x44(%rbp), %r8d
movl 0x18(%rbp), %eax
addl $0x1, %eax
xorl %ecx, %ecx
movl $0x4, %r9d
movl %eax, (%rsp)
callq 0xaaa0
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0xb140
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0xb13e
movl -0x28(%rbp), %edx
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
movslq -0x28(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xb115
jmp 0xb140
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl 0x18(%rbp), %r9d
movl 0x10(%rbp), %eax
movl %eax, (%rsp)
callq 0xa900
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0xb19a
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x28(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xb169
movl -0x44(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xb1ce
movl 0x18(%rbp), %ecx
xorl %edx, %edx
movl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %ecx
cmovnel %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xb1a0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl -0x44(%rbp), %r9d
movl 0x10(%rbp), %eax
movl %eax, (%rsp)
callq 0x86f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movl 0x10(%rbp), %eax
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x87f0
cmpl $0x1, 0x20(%rbp)
jne 0xb25c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x8(%rbp), %r8
movq -0x30(%rbp), %r9
movl -0x1c(%rbp), %r11d
movl -0x20(%rbp), %r10d
movl 0x10(%rbp), %eax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x8e20
jmp 0xb357
cmpl $0x2, 0x20(%rbp)
jne 0xb29b
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x8(%rbp), %rcx
movq -0x30(%rbp), %r8
movl -0x1c(%rbp), %r9d
movl -0x20(%rbp), %r10d
movl 0x10(%rbp), %eax
movl %r10d, (%rsp)
movl $0x1, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x90a0
jmp 0xb355
cmpl $0x3, 0x20(%rbp)
jne 0xb328
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x20(%rbp), %r9d
movl 0x10(%rbp), %eax
movl $0x1, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x9340
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xb326
movq -0x18(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x64(%rbp), %eax
jge 0xb300
movq -0x18(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0xb319
movq -0x18(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,4), %edx
subl -0x64(%rbp), %edx
movq -0x18(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xb31b
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xb2d7
jmp 0xb353
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x20(%rbp), %r9d
movl 0x10(%rbp), %eax
movl $0x1, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x8bb0
jmp 0xb355
jmp 0xb357
movq -0x30(%rbp), %rdi
callq 0xb810
cmpl $0x1, 0x20(%rbp)
jne 0xb36f
movq -0x40(%rbp), %rdi
callq 0xb810
cmpl $0x1, 0x20(%rbp)
je 0xb37b
cmpl $0x2, 0x20(%rbp)
jne 0xb384
movq -0x38(%rbp), %rdi
callq 0xb810
movl -0x60(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq