// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]lyndon-array/lib/lyndon-array.c
// Function: lyndon_check
int lyndon_check(unsigned char *s, int_t *LA, int_t n, int print){

int check=1;
int_t i;

	int_t *SA = (int_t*) malloc(n*sizeof(int_t));
	sacak(s, SA, n);

	int_t *ISA = (int_t*) malloc(n*sizeof(int_t));
	for(i=0; i<n; i++) ISA[SA[i]]=i;

	// print output
	if(print){
		#if PERMUTED
			printf("i\tSA\tpLA\tBWT\tsuffixes\n");
		#else
			printf("i\tSA\tLA\tBWT\tsuffixes\n");
		#endif
	
		for(i = 0; i < n; ++i) {
	
			char j = (SA[i])? s[SA[i]-1]:END_MARKER;
			printf("%d\t%d\t%d\t%c\t",i, SA[i], LA[i],j-1);
			
			#if PERMUTED
			    int start=SA[i];
			#else
			    int start=i;
			#endif
	
			int_t k;	
			for(k = start; k < n; k++) {
				printf("%c", s[k]-1);
			}
			printf("$\n");
		}
	}

	//simple checker
	for(i=0; i<n; i++){
		
		#if PERMUTED
			int start=SA[i];
			int pos = i;
		#else
			int start=i;
			int pos = ISA[i];
		#endif

		if(start==n-1){
			if(LA[i]!=1){
				check=0;
				printf("*%d) %d != %d\n", i, LA[i], 1);
				break;
			}
			continue;			
		}

		int size=1;
		while(ISA[start+size]>pos)size++; //ISA[n-1]=0

		if(LA[i]!=size){
			check=0;
			printf("%d) %d != %d\n", i, LA[i], size);
			break;
		}
	}

	free(SA);
	free(ISA);
	
	return check;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r13d
movl %edx, %r15d
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movslq %edx, %rbp
leaq (,%rbp,4), %r14
movq %r14, %rdi
callq 0x855a
movq %rax, %rbx
movq %r12, 0x20(%rsp)
movq %r12, %rdi
movq %rax, %rsi
movl %ebp, %edx
callq 0x83c9
movq %r14, %rdi
callq 0x855a
movq %rax, %r14
testl %ebp, %ebp
jle 0x5ab1
movq %r15, 0x18(%rsp)
movl %r15d, %r15d
xorl %eax, %eax
movslq (%rbx,%rax,4), %rcx
movl %eax, (%r14,%rcx,4)
incq %rax
cmpq %rax, %r15
jne 0x59aa
movq %r14, 0x8(%rsp)
testl %r13d, %r13d
je 0x5a51
leaq 0x3dc7(%rip), %rdi      # 0x9796
callq 0x2080
cmpl $0x0, 0x18(%rsp)
jle 0x5ac9
movq %r15, %r13
movq 0x20(%rsp), %r12
xorl %ebp, %ebp
movslq (%rbx,%rbp,4), %rdx
testq %rdx, %rdx
je 0x5a02
movq 0x20(%rsp), %rax
movsbl -0x1(%rax,%rdx), %r8d
decl %r8d
jmp 0x5a08
movl $0x23, %r8d
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %ecx
leaq 0x3d63(%rip), %rdi      # 0x977a
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
xorl %r14d, %r14d
movzbl (%r12,%r14), %edi
decl %edi
callq 0x2040
incq %r14
cmpq %r14, %r13
jne 0x5a23
leaq 0x3d6d(%rip), %rdi      # 0x97ab
callq 0x2080
incq %rbp
incq %r12
decq %r13
cmpq %r15, %rbp
jne 0x59e9
movl $0x1, %ebp
movq 0x18(%rsp), %r8
testl %r8d, %r8d
jle 0x5ace
decl %r8d
movq 0x8(%rsp), %r14
movq %r14, %rax
addq $0x4, %rax
xorl %esi, %esi
cmpq %r8, %rsi
jne 0x5a86
movq 0x10(%rsp), %rcx
movl (%rcx,%r8,4), %edx
cmpl $0x1, %edx
je 0x5aa3
jmp 0x5ae0
movl (%r14,%rsi,4), %edx
xorl %ecx, %ecx
movq %rcx, %rdi
incq %rcx
cmpl %edx, (%rax,%rdi,4)
jg 0x5a8c
movq 0x10(%rsp), %rdx
movl (%rdx,%rsi,4), %edx
cmpl %ecx, %edx
jne 0x5ad5
incq %rsi
addq $0x4, %rax
cmpq %r15, %rsi
jne 0x5a71
jmp 0x5af8
movl $0x1, %ebp
testl %r13d, %r13d
je 0x5af8
leaq 0x3cd4(%rip), %rdi      # 0x9796
callq 0x2080
jmp 0x5af8
movl $0x1, %ebp
movq 0x8(%rsp), %r14
jmp 0x5af8
leaq 0x3cac(%rip), %rdi      # 0x9788
xorl %ebp, %ebp
jmp 0x5af1
leaq 0x3ca0(%rip), %rdi      # 0x9787
xorl %ebp, %ebp
movl %r8d, %esi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x20f0
movq %rbx, %rdi
callq 0x8611
movq %r14, %rdi
callq 0x8611
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq