// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]lyndon-array/lib/lyndon-array.c
// Function: compute_lyndon_nsv
int compute_lyndon_nsv(unsigned char *s, int_t *A, int_t n){

int_t i;

	#if TIME
		time_t t_total= 0;clock_t c_total= 0;
		tstart(&t_total, &c_total); 
	#endif

	#if STEP_TIME
		time_t t_time = 0;clock_t c_time = 0;
		tstart(&t_time, &c_time); 
		printf("1. SA construction:\n");
	#endif
	
	// 1. sort
	//int_t *SA = A;
	int_t *SA = (int_t*) malloc(n*sizeof(int_t));
	sacak(s, SA, n);

	#if STEP_TIME
		fprintf(stderr,"%.6lf\n", tstop(t_time, c_time));
		tstart(&t_time, &c_time);
		printf("2. Compute ISA:\n"); 
	#endif

	//2. compute ISA
	int_t *ISA = A;
	for(i=0; i<n; i++) ISA[SA[i]]=i;

	#if STEP_TIME
		fprintf(stderr,"%.6lf\n", tstop(t_time, c_time));
		tstart(&t_time, &c_time);
		printf("3. Compute NSV-array:\n"); 
	#endif

	//3. compute NSV
	int_t *NSV = SA; //(in the space of SA[0,n-1])
	compute_nsv(NSV, ISA, n);
	NSV[n-1]=n;

	#if STEP_TIME
		fprintf(stderr,"%.6lf\n", tstop(t_time, c_time));
		tstart(&t_time, &c_time);
		printf("4. Compute Lyndon:\n");
	#endif


	#if PRINT
		printf("step\tpos\tT^{rev}\tLyndon\n");
	#endif

	int_t *LA = A;
	for(i=0; i < n; i++){	
		LA[i] = NSV[i]-i;
	}

	#if STEP_TIME
		fprintf(stderr,"%.6lf\n", tstop(t_time, c_time));
	#endif

	#if TIME
		printf("TOTAL:\n");
		fprintf(stderr,"%.6lf\n", tstop(t_total, c_total)); 
	#endif

	free(SA);
	//free(ISA);
	//free(NSV);

return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x61d0
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0xb390
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7229
movl -0x18(%rbp), %edx
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x7200
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x70a0
movl -0x14(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x728f
movq -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %edx
subl -0x18(%rbp), %edx
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x7263
leaq 0x523c(%rip), %rdi      # 0xc4d2
movb $0x0, %al
callq 0x20e0
movq 0x7d54(%rip), %rax      # 0xeff8
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6210
movq -0x50(%rbp), %rdi
leaq 0x5217(%rip), %rsi      # 0xc4da
movb $0x1, %al
callq 0x2160
movq -0x30(%rbp), %rdi
callq 0xb810
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)