// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl $0x1, -0x1c(%rbp)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0xb390
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x30(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6eaf
movl -0x20(%rbp), %edx
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x20(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x6e86
cmpl $0x0, -0x18(%rbp)
je 0x6fa0
leaq 0x597b(%rip), %rdi      # 0xc83b
movb $0x0, %al
callq 0x20e0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6f9e
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x6f06
movq -0x8(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x4c(%rbp)
jmp 0x6f10
movl $0x24, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6f10
movl -0x4c(%rbp), %eax
movb %al, -0x31(%rbp)
movl -0x20(%rbp), %esi
movq -0x28(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movsbl -0x31(%rbp), %r8d
subl $0x1, %r8d
leaq 0x5912(%rip), %rdi      # 0xc851
movb $0x0, %al
callq 0x20e0
movl -0x20(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6f82
movq -0x8(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzbl (%rax,%rcx), %esi
subl $0x1, %esi
leaq 0x56c8(%rip), %rdi      # 0xc638
movb $0x0, %al
callq 0x20e0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6f52
leaq 0x58d5(%rip), %rdi      # 0xc85e
movb $0x0, %al
callq 0x20e0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x6ece
jmp 0x6fa0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x707d
movl -0x20(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x700e
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
cmpl $0x1, (%rax,%rcx,4)
je 0x700c
movl $0x0, -0x1c(%rbp)
movl -0x20(%rbp), %esi
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x5863(%rip), %rdi      # 0xc861
movl $0x1, %ecx
movb $0x0, %al
callq 0x20e0
jmp 0x707d
jmp 0x706f
movl $0x1, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
addl -0x48(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x44(%rbp), %eax
jle 0x7035
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x7015
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x48(%rbp), %eax
je 0x706d
movl $0x0, -0x1c(%rbp)
movl -0x20(%rbp), %esi
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x48(%rbp), %ecx
leaq 0x57fe(%rip), %rdi      # 0xc862
movb $0x0, %al
callq 0x20e0
jmp 0x707d
jmp 0x706f
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x6fa7
movq -0x28(%rbp), %rdi
callq 0xb810
movq -0x30(%rbp), %rdi
callq 0xb810
movl -0x1c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)