// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movl %edx, %r14d
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
movslq %edx, %r13
leaq (,%r13,4), %r12
movq %r12, %rdi
callq 0x7f0e
movq %rax, %rbx
movq %r15, 0x20(%rsp)
movq %r15, %rdi
movq %rax, %rsi
movl %r13d, %edx
callq 0x7d7d
movq %r12, %rdi
callq 0x7f0e
movq %r14, 0x18(%rsp)
movl %r14d, %r14d
testl %r13d, %r13d
jle 0x59c0
xorl %edx, %edx
movslq (%rbx,%rdx,4), %rcx
movl %edx, (%rax,%rcx,4)
incq %rdx
cmpq %rdx, %r14
jne 0x59b1
movq %rax, 0x8(%rsp)
testl %ebp, %ebp
je 0x5a53
leaq 0x3dc2(%rip), %rdi      # 0x9796
callq 0x2080
cmpl $0x0, 0x18(%rsp)
jle 0x5a53
movq %r14, %r12
movq 0x20(%rsp), %r13
xorl %ebp, %ebp
movslq (%rbx,%rbp,4), %rdx
testq %rdx, %rdx
je 0x5a03
movq 0x20(%rsp), %rax
movsbl -0x1(%rax,%rdx), %r8d
decl %r8d
jmp 0x5a09
movl $0x23, %r8d
movq 0x10(%rsp), %rax
movl (%rax,%rbp,4), %ecx
leaq 0x3d62(%rip), %rdi      # 0x977a
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
xorl %r15d, %r15d
movzbl (%r13,%r15), %edi
decl %edi
callq 0x2040
incq %r15
cmpq %r15, %r12
jne 0x5a24
leaq 0x3d6b(%rip), %rdi      # 0x97ab
callq 0x2080
incq %rbp
incq %r13
decq %r12
cmpq %r14, %rbp
jne 0x59ea
movq 0x18(%rsp), %rsi
testl %esi, %esi
jle 0x5aec
decl %esi
movq 0x8(%rsp), %r12
addq $0x4, %r12
movl $0x1, %ebp
xorl %r13d, %r13d
movq %rsi, %r15
cmpq %rsi, %r13
jne 0x5a9c
movq 0x10(%rsp), %rax
movl (%rax,%r13,4), %edx
xorl %eax, %eax
cmpl $0x1, %edx
je 0x5ada
leaq 0x3cf5(%rip), %rdi      # 0x9787
movl %r13d, %esi
movl $0x1, %ecx
jmp 0x5acc
movq 0x8(%rsp), %rax
movl (%rax,%r13,4), %eax
xorl %ecx, %ecx
movq %rcx, %rdx
incq %rcx
cmpl %eax, (%r12,%rdx,4)
jg 0x5aa7
movq 0x10(%rsp), %rax
movl (%rax,%r13,4), %edx
xorl %eax, %eax
cmpl %ecx, %edx
je 0x5ada
leaq 0x3cbf(%rip), %rdi      # 0x9788
movl %r13d, %esi
xorl %eax, %eax
callq 0x20f0
movq %r15, %rsi
movb $0x1, %al
xorl %ebp, %ebp
testb %al, %al
jne 0x5af1
incq %r13
addq $0x4, %r12
cmpq %r14, %r13
jne 0x5a76
jmp 0x5af1
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x7fc5
movq 0x8(%rsp), %rdi
callq 0x7fc5
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq