// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %edx, %r13d
movq %rsi, 0x40(%rsp)
movq %rdi, %r14
xorl %edi, %edi
callq 0x21b0
movq %rax, %r15
callq 0x20a0
movq %rax, 0x30(%rsp)
movslq %r13d, %r12
leaq (,%r12,4), %rdi
callq 0x7f0e
movq %rax, %rbx
movq %r14, %rdi
movq %rax, %rsi
movl %r12d, %edx
callq 0x7d7d
movq %r12, %rdi
callq 0x7f0e
movq %rax, %rbp
movl %r13d, 0xc(%rsp)
movl %r13d, %r13d
testl %r12d, %r12d
jle 0x259c
xorl %eax, %eax
movslq (%rbx,%rax,4), %rcx
testq %rcx, %rcx
je 0x258e
movb -0x1(%r14,%rcx), %cl
jmp 0x2590
xorl %ecx, %ecx
movb %cl, (%rbp,%rax)
incq %rax
cmpq %rax, %r13
jne 0x257e
movq %rbx, 0x10(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x7f0e
movq %rax, %r14
xorl %ebx, %ebx
movl $0x400, %edx            # imm = 0x400
movq %rax, %rdi
xorl %esi, %esi
callq 0x2130
cmpl $0x0, 0xc(%rsp)
jle 0x25d9
xorl %eax, %eax
movzbl (%rbp,%rax), %ecx
incl (%r14,%rcx,4)
incq %rax
cmpq %rax, %r13
jne 0x25c8
xorl %eax, %eax
movl (%r14,%rax,4), %ecx
addl %ebx, %ecx
movl %ebx, (%r14,%rax,4)
incq %rax
movl %ecx, %ebx
cmpq $0x100, %rax            # imm = 0x100
jne 0x25db
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rdi
jle 0x261b
xorl %eax, %eax
movzbl (%rbp,%rax), %ecx
movl (%r14,%rcx,4), %edx
leal 0x1(%rdx), %esi
movl %esi, (%r14,%rcx,4)
movl %edx, (%rdi,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x2600
movl $0x2800, %edi           # imm = 0x2800
callq 0x7f0e
movq %rax, %rdi
andq $0x0, (%rax)
cmpl $0x0, 0xc(%rsp)
jle 0x26fe
movq %r14, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movl $0x500, %r14d           # imm = 0x500
pushq $0x1
popq %rax
pushq $0x1
popq %rbx
xorl %r9d, %r9d
movq 0x10(%rsp), %rsi
movl %r9d, %ebp
movslq %eax, %r15
leaq (%rdi,%r15,8), %rcx
addq $0x4, %rcx
xorl %r12d, %r12d
movq %r12, %rdx
decq %r12
cmpl -0xc(%rcx,%rdx,8), %ebp
jl 0x266b
notl %eax
addl %r14d, %eax
leaq (%rdi,%r15,8), %rcx
movslq %ebp, %rdx
movl (%rsi,%rdx,4), %r9d
movl %ebx, %edx
subl 0x4(%rcx,%r12,8), %edx
movq 0x40(%rsp), %rcx
movl %edx, -0x4(%rcx,%r13,4)
cmpl %r12d, %eax
jne 0x26c2
addl $0x500, %r14d           # imm = 0x500
movslq %r14d, %rsi
shlq $0x3, %rsi
movq %r9, 0x38(%rsp)
callq 0x808e
movq 0x38(%rsp), %r9
movq 0x10(%rsp), %rsi
movq %rax, %rdi
decq %r13
leaq (%rdi,%r15,8), %rax
movl %ebp, 0x8(%rax,%r12,8)
movl %ebx, 0xc(%rax,%r12,8)
leal 0x1(%rbx), %ecx
leaq (%r15,%r12), %rax
addq $0x2, %rax
cmpl 0xc(%rsp), %ebx
movl %ecx, %ebx
jne 0x265a
movq %rdi, %rbx
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rbp
movq 0x18(%rsp), %r14
jmp 0x2701
movq %rdi, %rbx
leaq 0x6ed1(%rip), %rdi      # 0x95d9
callq 0x2080
movq 0xa8e4(%rip), %rax      # 0xcff8
movq (%rax), %r13
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0x58a7
leaq 0x6cdc(%rip), %rsi      # 0x9407
movq %r13, %rdi
movb $0x1, %al
callq 0x2180
movq %rbx, %rdi
callq 0x7fc5
movq %r14, %rdi
callq 0x7fc5
movq %rbp, %rdi
callq 0x7fc5
movq 0x10(%rsp), %rdi
callq 0x7fc5
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq