// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rsi, (%rsp)
movq %rdi, %r12
movslq %edx, %r13
leaq (,%r13,4), %r14
movq %r14, %rdi
callq 0x76d0
movq %rax, %r15
movq %r12, 0x8(%rsp)
movq %r12, %rdi
movq %rax, %rsi
movl %r13d, %edx
callq 0x752b
movq %r14, %rdi
callq 0x76d0
xorl %edx, %edx
testl %r13d, %r13d
movq %rbx, %r13
movl $0x0, %r8d
cmovgl %r13d, %r8d
cmpq %rdx, %r8
je 0x52e8
movslq (%r15,%rdx,4), %rcx
movl %edx, (%rax,%rcx,4)
incq %rdx
jmp 0x52d7
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
je 0x539b
movq %r8, 0x20(%rsp)
leaq 0x3495(%rip), %rdi      # 0x8796
callq 0x2080
movq 0x8(%rsp), %r12
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r13, 0x18(%rsp)
cmpl %ebx, %r13d
movl %ebx, %ebp
cmovgl %r13d, %ebp
movq 0x20(%rsp), %r8
cmpq %r8, %rbx
je 0x539b
movq %r15, %r13
movslq (%r15,%rbx,4), %rdx
pushq $0x23
popq %r8
testq %rdx, %rdx
je 0x5346
movq 0x8(%rsp), %rax
movsbl -0x1(%rax,%rdx), %r8d
decl %r8d
addq %r14, %rbp
movq (%rsp), %rax
movl (%rax,%rbx,4), %ecx
leaq 0x3423(%rip), %rdi      # 0x877a
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x5379
movzbl (%r12,%r15), %edi
decl %edi
callq 0x2040
incq %r15
jmp 0x5363
leaq 0x342b(%rip), %rdi      # 0x87ab
callq 0x2080
incq %rbx
decq %r14
incq %r12
movq %r13, %r15
movq 0x18(%rsp), %r13
jmp 0x5315
decl %r13d
movq 0x10(%rsp), %rbx
movq %rbx, %rax
addq $0x4, %rax
xorl %esi, %esi
pushq $0x1
popq %rbp
cmpq %r8, %rsi
je 0x540d
cmpq %r13, %rsi
jne 0x53c8
movq (%rsp), %rcx
movl (%rcx,%r13,4), %edx
cmpl $0x1, %edx
je 0x53e3
jmp 0x53ec
movl (%rbx,%rsi,4), %edx
xorl %ecx, %ecx
movq %rcx, %rdi
incq %rcx
cmpl %edx, (%rax,%rdi,4)
jg 0x53cd
movq (%rsp), %rdx
movl (%rdx,%rsi,4), %edx
cmpl %ecx, %edx
jne 0x53fd
incq %rsi
addq $0x4, %rax
jmp 0x53af
leaq 0x3394(%rip), %rdi      # 0x8787
xorl %ebp, %ebp
pushq $0x1
popq %rcx
movl %r13d, %esi
jmp 0x5406
leaq 0x3384(%rip), %rdi      # 0x8788
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
movq %r15, %rdi
callq 0x77a6
movq %rbx, %rdi
callq 0x77a6
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq