// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebx
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
leaq 0x20(%rsp), %rdi
andq $0x0, (%rdi)
leaq 0x18(%rsp), %rsi
andq $0x0, (%rsi)
callq 0x4af6
movslq %ebx, %r15
leaq (,%r15,4), %rdi
callq 0x76d0
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
movl %r15d, %edx
callq 0x752b
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %ebp
cmovgl %r15d, %ebp
cmpq %rax, %rbp
je 0x5115
movslq (%rbx,%rax,4), %rcx
testq %rcx, %rcx
je 0x510b
movzbl -0x1(%r13,%rcx), %ecx
jmp 0x510d
xorl %ecx, %ecx
movl %ecx, (%rbx,%rax,4)
incq %rax
jmp 0x50f5
movl $0x400, %edi            # imm = 0x400
callq 0x76d0
movq %rax, %r12
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x5136
andl $0x0, (%r12,%rax,4)
incq %rax
jmp 0x5124
xorl %eax, %eax
cmpq %rax, %rbp
je 0x514c
movzbl (%r13,%rax), %ecx
incl (%r12,%rcx,4)
incq %rax
jmp 0x5138
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x100, %rax            # imm = 0x100
je 0x5169
movl (%r12,%rax,4), %edx
addl %ecx, %edx
movl %ecx, (%r12,%rax,4)
incq %rax
movl %edx, %ecx
jmp 0x5150
xorl %eax, %eax
cmpq %rax, %rbp
je 0x5187
movslq (%rbx,%rax,4), %rcx
movl (%r12,%rcx,4), %edx
leal 0x1(%rdx), %esi
movl %esi, (%r12,%rcx,4)
movl %edx, (%rbx,%rax,4)
incq %rax
jmp 0x516b
movabsq $0x50000000000, %rax    # imm = 0x50000000000
movq %rsp, %r14
movq %rax, 0x8(%r14)
movq %r14, %rdi
movl $0x500, %esi            # imm = 0x500
callq 0x48f0
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x491c
pushq $0x1
popq %rdx
testq %r15, %r15
jle 0x5211
decq %r15
movq (%rsp), %rax
movslq 0x8(%rsp), %rsi
leal 0x1(%rsi), %ecx
leaq (%rax,%rsi,8), %rsi
addq $-0x8, %rsi
movq (%rsi), %rax
decl %ecx
addq $-0x8, %rsi
cmpl %eax, %ebp
jl 0x51d2
movl %ecx, 0x8(%rsp)
movslq %ebp, %rcx
movl (%rbx,%rcx,4), %r14d
shrq $0x20, %rax
movl %edx, %ecx
subl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax,%r15,4)
leal 0x1(%rdx), %r13d
movq %rsp, %rdi
movl %ebp, %esi
callq 0x491c
movl %r13d, %edx
movl %r14d, %ebp
jmp 0x51b6
leaq 0x33c1(%rip), %rdi      # 0x85d9
callq 0x2080
movq 0x6dd4(%rip), %rax      # 0xbff8
movq (%rax), %r14
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x4b1a
leaq 0x31ca(%rip), %rsi      # 0x8407
movq %r14, %rdi
movb $0x1, %al
callq 0x2180
movq (%rsp), %rdi
callq 0x77a6
movq %r12, %rdi
callq 0x77a6
movq %rbx, %rdi
callq 0x77a6
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq