// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]lyndon-array/lib/lyndon-array.c
// Function: compute_lyndon_text_9n
int compute_lyndon_text_9n(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 = (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 BWT and C:\n"); 
	#endif

	//2. compute BWT
	int_t* bwt = SA; 
	for(i=0; i<n; i++){
		bwt[i] = (SA[i])?s[SA[i]-1]:0;
	}	

	//2. compute C 

	int_t *C = (int_t*) malloc(SIGMA*sizeof(int_t));
	for(i=0; i<SIGMA; i++) C[i]=0;
	for(i=0; i<n; i++) C[s[i]]++;

	int_t sum=0;
	for(i=0; i<SIGMA; i++){
		sum+=C[i]; C[i]=sum-C[i];
	}

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

	//3. compute LF-array (in the space of SA[0,n-1])
	int_t *LF = SA; 
	for(i=0; i<n; i++) LF[i] = C[bwt[i]]++;

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

	//Lyndon array construction
	stack_pair S;
	S.top=0; S.size=STACK_SIZE;
	stack_pair_init(&S,STACK_SIZE);
	stack_pair_push(&S, 0, 0);//(0, 0)

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

	int_t *LA = A;

	int_t pos = 0;
	int_t step = 1;//(n-1)-i

	for(i=n-1; i >= 0; i--){	

		while(stack_pair_top(&S).i > pos) stack_pair_pop(&S);
		int_t next = LF[pos];

		#if PERMUTED
			LA[pos] = step-stack_pair_top(&S).j;
		#else
			LA[i] = step-stack_pair_top(&S).j;
		#endif

		stack_pair_push(&S, pos, step++);
		pos = next; //pos = LF(pos)
	}

	#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(S.array);
	free(C);
	free(LF);

return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %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 -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6bc0
movq -0x30(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x6b9a
movq -0x8(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x88(%rbp)
jmp 0x6ba4
xorl %eax, %eax
movl %eax, -0x88(%rbp)
jmp 0x6ba4
movl -0x88(%rbp), %edx
movq -0x38(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6b63
movl $0x400, %edi            # imm = 0x400
callq 0xb6a0
movq %rax, -0x40(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x6bf8
movq -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6bd5
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6c2b
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6bff
movl $0x0, -0x44(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x6c77
movq -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
subl (%rax,%rcx,4), %edx
movq -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6c39
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6cbf
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %edx
movl %edx, %esi
addl $0x1, %esi
movl %esi, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6c86
movl $0x0, -0x58(%rbp)
movl $0x500, -0x54(%rbp)     # imm = 0x500
leaq -0x60(%rbp), %rdi
movl $0x500, %esi            # imm = 0x500
callq 0x5e20
leaq -0x60(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x5e80
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x1, -0x70(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jl 0x6d99
jmp 0x6d13
leaq -0x60(%rbp), %rdi
callq 0x5f20
movq %rax, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jle 0x6d33
leaq -0x60(%rbp), %rdi
callq 0x5f70
jmp 0x6d13
movq -0x50(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x8c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5f20
movl -0x8c(%rbp), %edx
movq %rax, -0x84(%rbp)
subl -0x80(%rbp), %edx
movq -0x68(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x6c(%rbp), %esi
movl -0x70(%rbp), %edx
movl %edx, %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5e80
movl -0x7c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6d07
leaq 0x5732(%rip), %rdi      # 0xc4d2
movb $0x0, %al
callq 0x20e0
movq 0x824a(%rip), %rax      # 0xeff8
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6210
movq -0x98(%rbp), %rdi
leaq 0x5707(%rip), %rsi      # 0xc4da
movb $0x1, %al
callq 0x2160
movq -0x60(%rbp), %rdi
callq 0xb810
movq -0x40(%rbp), %rdi
callq 0xb810
movq -0x50(%rbp), %rdi
callq 0xb810
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbp
retq