// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %edx, %ebp
movq %rsi, 0x30(%rsp)
movq %rdi, %r15
xorl %edi, %edi
callq 0x21b0
movq %rax, %rbx
callq 0x20a0
movq %rax, %r13
movslq %ebp, %r14
leaq (,%r14,4), %rdi
callq 0x855a
movq %r15, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
movl %r14d, %edx
callq 0x83c9
movl %ebp, 0x4(%rsp)
movl %ebp, %r12d
movq %r14, 0x10(%rsp)
testl %r14d, %r14d
jle 0x2ba2
xorl %eax, %eax
movq 0x8(%rsp), %rsi
movslq (%rsi,%rax,4), %rdx
movl $0x0, %ecx
testq %rdx, %rdx
je 0x2b6b
movzbl -0x1(%r15,%rdx), %ecx
movl %ecx, (%rsi,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x2b57
pushq $0x1
popq %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x86b3
movq %rax, %r14
cmpl $0x0, 0x4(%rsp)
jle 0x2bb2
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
incl (%r14,%rcx,4)
incq %rax
cmpq %rax, %r12
jne 0x2b8f
jmp 0x2bb2
pushq $0x1
popq %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x86b3
movq %rax, %r14
movq 0x8(%rsp), %rdi
pushq $0x1
popq %rbp
xorl %eax, %eax
xorl %ecx, %ecx
movl (%r14,%rax,4), %edx
addl %ecx, %edx
movl %ecx, (%r14,%rax,4)
incq %rax
movl %edx, %ecx
cmpq $0x100, %rax            # imm = 0x100
jne 0x2bbe
cmpl $0x0, 0x4(%rsp)
jle 0x2ce6
xorl %eax, %eax
movslq (%rdi,%rax,4), %rcx
movl (%r14,%rcx,4), %edx
leal 0x1(%rdx), %esi
movl %esi, (%r14,%rcx,4)
movl %edx, (%rdi,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x2be2
movl $0x2800, %edi           # imm = 0x2800
callq 0x855a
movq %rax, %rdi
andq $0x0, (%rax)
cmpl $0x0, 0x4(%rsp)
jle 0x2ce1
movq %r13, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movl $0x500, %r8d            # imm = 0x500
pushq $0x1
popq %r15
xorl %r9d, %r9d
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rsi
movl %r9d, %r12d
movslq %ebp, %r13
leaq (%rdi,%r13,8), %rcx
addq $-0x8, %rcx
xorl %ebx, %ebx
movq (%rcx,%rbx,8), %rax
decq %rbx
cmpl %eax, %r12d
jl 0x2c49
notl %ebp
addl %r8d, %ebp
movslq %r12d, %rcx
movl (%rdx,%rcx,4), %r9d
shrq $0x20, %rax
movl %r15d, %ecx
subl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, -0x4(%rax,%rsi,4)
cmpl %ebx, %ebp
jne 0x2cac
addl $0x500, %r8d            # imm = 0x500
movl %r8d, 0x1c(%rsp)
movq %rsi, 0x10(%rsp)
movslq %r8d, %rsi
shlq $0x3, %rsi
movq %r9, %rbp
callq 0x86da
movq %rbp, %r9
movl 0x1c(%rsp), %r8d
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rax, %rdi
decq %rsi
leaq (%rdi,%r13,8), %rax
movl %r12d, 0x8(%rax,%rbx,8)
movl %r15d, 0xc(%rax,%rbx,8)
leal 0x1(%r15), %eax
leaq (%rbx,%r13), %rbp
addq $0x2, %rbp
cmpl 0x4(%rsp), %r15d
movl %eax, %r15d
jne 0x2c39
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r13
movq %rdi, %r15
jmp 0x2cf7
movl $0x2800, %edi           # imm = 0x2800
callq 0x855a
movq %rax, %r15
andq $0x0, (%rax)
leaq 0x68db(%rip), %rdi      # 0x95d9
callq 0x2080
movq 0xa2ee(%rip), %rax      # 0xcff8
movq (%rax), %r12
movq %rbx, %rdi
movq %r13, %rsi
callq 0x589a
leaq 0x66e8(%rip), %rsi      # 0x9407
movq %r12, %rdi
movb $0x1, %al
callq 0x2180
movq %r15, %rdi
callq 0x8611
movq %r14, %rdi
callq 0x8611
movq 0x8(%rsp), %rdi
callq 0x8611
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq