// File: /workspace/github_c_makefile_star2_O1/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, %r15
xorl %edi, %edi
callq 0x21b0
movq %rax, %r12
callq 0x20a0
movq %rax, %r13
movq %r15, %rdi
movq %rbx, 0x48(%rsp)
movq %rbx, %rsi
movl %ebp, %edx
callq 0x7d7d
movslq %ebp, %rbx
movq %rbx, %rdi
callq 0x7f0e
movq %rax, %r14
movl %ebp, 0x4(%rsp)
movl %ebp, %edx
testl %ebx, %ebx
movq %rdx, 0x8(%rsp)
jle 0x27cf
movq %r14, %rdi
movq %r15, %rsi
callq 0x21a0
movq 0x8(%rsp), %rdx
cmpl $0x0, 0x4(%rsp)
jle 0x27fb
xorl %eax, %eax
movq 0x48(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
testq %rcx, %rcx
je 0x27ed
movb -0x1(%r14,%rcx), %cl
jmp 0x27ef
xorl %ecx, %ecx
movb %cl, (%r15,%rax)
incq %rax
cmpq %rax, %rdx
jne 0x27d8
movq %r12, 0x20(%rsp)
movq %r14, %rdi
callq 0x7fc5
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x7f0e
movq %rax, %rbx
movl $0x400, %edi            # imm = 0x400
callq 0x7f0e
movq %rax, %r14
xorl %ebp, %ebp
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
xorl %esi, %esi
callq 0x2130
movq 0x8(%rsp), %rsi
movl $0x0, %edi
cmpl $0x0, 0x4(%rsp)
jle 0x2867
xorl %eax, %eax
xorl %edi, %edi
movzbl (%r15,%rax), %ecx
movl (%r14,%rcx,4), %edx
cmpl $0x1, %edx
adcl $0x0, %edi
incl %edx
movl %edx, (%r14,%rcx,4)
incq %rax
cmpq %rax, %rsi
jne 0x284a
movq %r15, 0x40(%rsp)
movq %r14, 0x18(%rsp)
movslq %edi, %r14
incl %edi
movl %edi, 0x10(%rsp)
leaq 0x1(%r14), %r15
leaq 0x4(,%r14,4), %rdi
callq 0x7f0e
movq %r15, %rdi
movq %rax, %r15
callq 0x7f0e
movq %rax, %r12
movl %r14d, %eax
leaq 0x4(,%rax,4), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
movl 0x4(%rsp), %eax
movl %eax, (%r15,%r14,4)
movb $0x0, (%r12,%r14)
movq 0x18(%rsp), %r14
xorl %eax, %eax
xorl %ecx, %ecx
movl (%r14,%rax,4), %edx
testl %edx, %edx
je 0x28dd
movl %ebp, (%r14,%rax,4)
movslq %ecx, %rcx
movl %ebp, (%r15,%rcx,4)
movb %al, (%r12,%rcx)
incl %ecx
addl %edx, %ebp
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x28c4
movq %r12, 0x30(%rsp)
cmpl $0x0, 0x4(%rsp)
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %r8
jle 0x291c
xorl %eax, %eax
movzbl (%rdi,%rax), %ecx
movl (%r14,%rcx,4), %edx
leal 0x1(%rdx), %esi
movl %esi, (%r14,%rcx,4)
movl %edx, (%rbx,%rax,4)
incq %rax
cmpq %rax, %r8
jne 0x2902
movq %rbx, 0x38(%rsp)
movl $0x2800, %edi           # imm = 0x2800
callq 0x7f0e
movq %rax, %rdi
andq $0x0, (%rax)
cmpl $0x0, 0x4(%rsp)
movq 0x20(%rsp), %r12
jle 0x2a48
movq %r15, 0x28(%rsp)
movq %r13, 0x50(%rsp)
movl $0x500, %esi            # imm = 0x500
pushq $0x1
popq %r12
pushq $0x1
popq %r13
xorl %ebx, %ebx
movq 0x8(%rsp), %rdx
movl %esi, 0x14(%rsp)
movl %ebx, %r15d
movq %rdx, %r14
leaq -0x1(%rdx), %rax
movq %rax, 0x8(%rsp)
movq %rdi, %rbp
movl %ebx, %edi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl 0x10(%rsp), %ecx
callq 0x5901
movq %rbp, %rdi
movq 0x40(%rsp), %rcx
movb %al, -0x1(%rcx,%r14)
movslq %r12d, %r14
leaq 0x4(,%r14,8), %rax
addq %rbp, %rax
xorl %ebp, %ebp
movq %rbp, %rcx
decq %rbp
cmpl -0xc(%rax,%rcx,8), %r15d
jl 0x29a8
notl %r12d
movl 0x14(%rsp), %esi
addl %esi, %r12d
leaq (%rdi,%r14,8), %rax
movslq %r15d, %rcx
movq 0x38(%rsp), %rdx
movl (%rdx,%rcx,4), %ebx
movl %r13d, %ecx
subl 0x4(%rax,%rbp,8), %ecx
movq 0x48(%rsp), %rax
movq 0x8(%rsp), %rdx
movl %ecx, (%rax,%rdx,4)
cmpl %ebp, %r12d
jne 0x2a07
addl $0x500, %esi            # imm = 0x500
movl %esi, %r12d
movslq %esi, %rsi
shlq $0x3, %rsi
callq 0x808e
movl %r12d, %esi
movq 0x8(%rsp), %rdx
movq %rax, %rdi
leaq (%rdi,%r14,8), %rax
movl %r15d, 0x8(%rax,%rbp,8)
movl %r13d, 0xc(%rax,%rbp,8)
leal 0x1(%r13), %eax
leaq (%r14,%rbp), %r12
addq $0x2, %r12
cmpl 0x4(%rsp), %r13d
movl %eax, %r13d
jne 0x2960
movq %rdi, %rbp
movq 0x20(%rsp), %r12
movq 0x50(%rsp), %r13
movq 0x18(%rsp), %r14
movq 0x28(%rsp), %r15
jmp 0x2a4b
movq %rdi, %rbp
leaq 0x6b87(%rip), %rdi      # 0x95d9
callq 0x2080
movq 0xa59a(%rip), %rax      # 0xcff8
movq (%rax), %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0x58a7
leaq 0x6994(%rip), %rsi      # 0x9407
movq %rbx, %rdi
movb $0x1, %al
callq 0x2180
movq %rbp, %rdi
callq 0x7fc5
movq %r14, %rdi
callq 0x7fc5
movq 0x38(%rsp), %rdi
callq 0x7fc5
movq %r15, %rdi
callq 0x7fc5
movq 0x30(%rsp), %rdi
callq 0x7fc5
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq