// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]lyndon-array/lib/lyndon-array.c
// Function: compute_lyndon_bwt_9n
int compute_lyndon_bwt_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 = A;
	sacak(s, SA, n);

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

	//2. compute BWT
	unsigned char* tmp = (unsigned char*) malloc(n*sizeof(unsigned char));
	for(i=0; i<n; i++)
		tmp[i] = s[i];		

	unsigned char* bwt = s;
	//unsigned char* bwt = malloc(n*sizeof(unsigned char));
	for(i=0; i<n; i++)
		bwt[i] = (SA[i])?tmp[SA[i]-1]:0;

	free(tmp);

	#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 = (int_t*) malloc(n*sizeof(int_t));
	int_t *C = (int_t*) malloc(SIGMA*sizeof(int_t));

	int_t sigma = 0;

	//counter
	for(i=0; i<SIGMA; i++) C[i]=0;
	for(i=0; i<n; i++){
		if(!C[bwt[i]]) sigma++;
		C[bwt[i]]++;
	}

	sigma++;
	int_t *F = (int_t*) malloc(sigma*sizeof(int_t));
	unsigned char *Alpha = (unsigned char*) malloc(sigma*sizeof(unsigned char));
	for(i=0; i<sigma; i++) F[i]=0;

	//sentinel
	F[sigma-1]=n;
	Alpha[sigma-1]=0;

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

	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

	//BWT-inversion and 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--){	

		s[i] = find_symbol(pos, F, Alpha, sigma);
		
		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

		#if PRINT
			printf("%d\t%d\t%c\t%d\n", step, pos, bwt[pos], 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);

	free(F);
	free(Alpha);

return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x50(%rsp), %rdi
andq $0x0, (%rdi)
leaq 0x48(%rsp), %rsi
andq $0x0, (%rsi)
callq 0x4af6
movq %r14, %rdi
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
movl %ebp, %edx
callq 0x752b
movslq %ebp, %rbx
movq %rbx, %rdi
callq 0x76d0
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %r15d
cmovgl %ebp, %r15d
cmpq %rcx, %r15
je 0x4e2b
movb (%r14,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
jmp 0x4e1a
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x4e51
movq 0x18(%rsp), %rdx
movslq (%rdx,%rcx,4), %rdx
testq %rdx, %rdx
je 0x4e46
movb -0x1(%rax,%rdx), %dl
jmp 0x4e48
xorl %edx, %edx
movb %dl, (%r14,%rcx)
incq %rcx
jmp 0x4e2d
movq %rax, %rdi
callq 0x77a6
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x76d0
movq %rax, 0x8(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x76d0
movq %rax, %r12
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x4e8b
andl $0x0, (%r12,%rax,4)
incq %rax
jmp 0x4e79
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rax, %r15
je 0x4eae
movzbl (%r14,%rax), %edx
movl (%r12,%rdx,4), %esi
cmpl $0x1, %esi
adcl $0x0, %ecx
incl %esi
movl %esi, (%r12,%rdx,4)
incq %rax
jmp 0x4e8f
movl %ecx, %ebx
leaq 0x4(,%rbx,4), %rdi
movl %ebp, 0x14(%rsp)
leaq 0x1(%rbx), %rbp
callq 0x76d0
movq %rax, %r13
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movl 0x14(%rsp), %ebp
callq 0x76d0
movq %rax, (%rsp)
xorl %eax, %eax
cmpq %rax, 0x20(%rsp)
je 0x4ef1
andl $0x0, (%r13,%rax,4)
incq %rax
jmp 0x4edf
movl %ebp, (%r13,%rbx,4)
movq (%rsp), %rax
movb $0x0, (%rax,%rbx)
xorl %eax, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq (%rsp), %rdi
cmpq $0x100, %rax            # imm = 0x100
je 0x4f30
movl (%r12,%rax,4), %esi
testl %esi, %esi
je 0x4f29
movl %edx, (%r12,%rax,4)
movslq %ecx, %rcx
movl %edx, (%r13,%rcx,4)
movb %al, (%rdi,%rcx)
incl %ecx
addl %esi, %edx
incq %rax
jmp 0x4f08
xorl %eax, %eax
cmpq %rax, %r15
je 0x4f54
movzbl (%r14,%rax), %ecx
movl (%r12,%rcx,4), %edx
leal 0x1(%rdx), %esi
movl %esi, (%r12,%rcx,4)
movq 0x8(%rsp), %rcx
movl %edx, (%rcx,%rax,4)
incq %rax
jmp 0x4f32
movq %r12, 0x38(%rsp)
movabsq $0x50000000000, %rax    # imm = 0x50000000000
leaq 0x28(%rsp), %rbx
movq %rax, 0x8(%rbx)
movq %rbx, %rdi
movl $0x500, %esi            # imm = 0x500
callq 0x48f0
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x491c
movl %ebp, %ebx
pushq $0x1
popq %rbp
movq %r13, 0x40(%rsp)
testl %ebx, %ebx
jle 0x5021
decq %rbx
movl %r15d, %edi
movq %r13, %rsi
movq (%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0x4d7a
movq %r14, %r12
movb %al, (%r14,%rbx)
movq 0x28(%rsp), %rax
movslq 0x30(%rsp), %rdx
leal 0x1(%rdx), %ecx
leaq (%rax,%rdx,8), %rdx
addq $-0x8, %rdx
movq (%rdx), %rax
decl %ecx
addq $-0x8, %rdx
cmpl %eax, %r15d
jl 0x4fcd
movl %ecx, 0x30(%rsp)
movslq %r15d, %rcx
movq 0x8(%rsp), %rdx
movl (%rdx,%rcx,4), %r13d
shrq $0x20, %rax
movl %ebp, %ecx
subl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, (%rax,%rbx,4)
leal 0x1(%rbp), %r14d
leaq 0x28(%rsp), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x491c
movl %r14d, %ebp
movl %r13d, %r15d
movq %r12, %r14
movq 0x40(%rsp), %r13
jmp 0x4f92
leaq 0x35b1(%rip), %rdi      # 0x85d9
callq 0x2080
movq 0x6fc4(%rip), %rax      # 0xbff8
movq (%rax), %rbx
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x4b1a
leaq 0x33ba(%rip), %rsi      # 0x8407
movq %rbx, %rdi
movb $0x1, %al
callq 0x2180
movq 0x28(%rsp), %rdi
callq 0x77a6
movq 0x38(%rsp), %rdi
callq 0x77a6
movq 0x8(%rsp), %rdi
callq 0x77a6
movq %r13, %rdi
callq 0x77a6
movq (%rsp), %rdi
callq 0x77a6
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq