// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]lyndon-array/lib/lyndon-array.c
// Function: compute_lyndon_bwt_10n
int compute_lyndon_bwt_10n(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
	#if PERMUTED  	//1. uses 1 array for SA -> LF -> pLA
		int_t *SA = A;
	#else		//0. uses 2 arrays, SA -> LF, and LA
		int_t *SA = (int_t*) malloc(n*sizeof(int_t));
	#endif

	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* bwt = malloc(n*sizeof(unsigned char));
	for(i=0; i<n; i++){
		bwt[i] = (SA[i])?s[SA[i]-1]:0;
	}	

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

	//counter
	for(i=0; i<SIGMA; i++) C[i]=0;
	for(i=0; i<n; i++) C[bwt[i]]++;

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

	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--){	

		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(bwt);

	#if PERMUTED == 0
		free(SA);
	#endif

return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebx
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
leaq 0x30(%rsp), %rdi
andq $0x0, (%rdi)
leaq 0x28(%rsp), %rsi
andq $0x0, (%rsi)
callq 0x4af6
movslq %ebx, %r12
leaq (,%r12,4), %rdi
callq 0x76d0
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
movl %r12d, %edx
callq 0x752b
movq %r12, %rdi
callq 0x76d0
movq %rax, %r14
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %r15d
cmovgl %r12d, %r15d
cmpq %rax, %r15
je 0x4bfe
movslq (%rbx,%rax,4), %rcx
testq %rcx, %rcx
je 0x4bf3
movb -0x1(%r13,%rcx), %cl
jmp 0x4bf5
xorl %ecx, %ecx
movb %cl, (%r14,%rax)
incq %rax
jmp 0x4bde
movl $0x400, %edi            # imm = 0x400
callq 0x76d0
movq %rax, %r13
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x4c20
andl $0x0, (%r13,%rax,4)
incq %rax
jmp 0x4c0d
xorl %eax, %eax
cmpq %rax, %r15
je 0x4c36
movzbl (%r14,%rax), %ecx
incl (%r13,%rcx,4)
incq %rax
jmp 0x4c22
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x100, %rax            # imm = 0x100
je 0x4c55
movl (%r13,%rax,4), %edx
addl %ecx, %edx
movl %ecx, (%r13,%rax,4)
incq %rax
movl %edx, %ecx
jmp 0x4c3a
xorl %eax, %eax
cmpq %rax, %r15
je 0x4c76
movzbl (%r14,%rax), %ecx
movl (%r13,%rcx,4), %edx
leal 0x1(%rdx), %esi
movl %esi, (%r13,%rcx,4)
movl %edx, (%rbx,%rax,4)
incq %rax
jmp 0x4c57
movq %rbx, 0x8(%rsp)
movabsq $0x50000000000, %rax    # imm = 0x50000000000
leaq 0x10(%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
pushq $0x1
popq %rdx
testq %r12, %r12
jle 0x4d0f
decq %r12
movq 0x10(%rsp), %rax
movslq 0x18(%rsp), %rsi
leal 0x1(%rsi), %ecx
leaq (%rax,%rsi,8), %rsi
addq $-0x8, %rsi
movq 0x8(%rsp), %rdi
movq (%rsi), %rax
decl %ecx
addq $-0x8, %rsi
cmpl %eax, %r15d
jl 0x4ccf
movl %ecx, 0x18(%rsp)
movslq %r15d, %rcx
movl (%rdi,%rcx,4), %ebp
shrq $0x20, %rax
movl %edx, %ecx
subl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax,%r12,4)
leal 0x1(%rdx), %ebx
leaq 0x10(%rsp), %rdi
movl %r15d, %esi
callq 0x491c
movl %ebx, %edx
movl %ebp, %r15d
jmp 0x4cad
leaq 0x38c3(%rip), %rdi      # 0x85d9
callq 0x2080
movq 0x72d6(%rip), %rax      # 0xbff8
movq (%rax), %r15
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x4b1a
leaq 0x36cc(%rip), %rsi      # 0x8407
movq %r15, %rdi
movb $0x1, %al
callq 0x2180
movq 0x10(%rsp), %rdi
callq 0x77a6
movq %r13, %rdi
callq 0x77a6
movq %r14, %rdi
callq 0x77a6
movq 0x8(%rsp), %rdi
callq 0x77a6
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq