// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x61d0
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0xb390
movslq -0x14(%rbp), %rdi
shlq $0x0, %rdi
callq 0xb6a0
movq %rax, -0x38(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x66ec
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x38(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x66c3
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x675a
movq -0x30(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x6732
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0xa8(%rbp)
jmp 0x673c
xorl %eax, %eax
movl %eax, -0xa8(%rbp)
jmp 0x673c
movl -0xa8(%rbp), %eax
movb %al, %dl
movq -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x66fb
movq -0x38(%rbp), %rdi
callq 0xb810
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x48(%rbp)
movl $0x400, %edi            # imm = 0x400
callq 0xb6a0
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x67b3
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6790
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6805
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x67e1
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x67ba
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
movq %rax, -0x60(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x0, %rdi
callq 0xb6a0
movq %rax, -0x68(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x6859
movq -0x60(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6837
movl -0x14(%rbp), %edx
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x690f
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x6902
movl -0x6c(%rbp), %edx
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
subl (%rax,%rcx,4), %edx
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x60(%rbp), %rax
movslq -0x70(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
movb %al, %dl
movq -0x68(%rbp), %rax
movslq -0x70(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x6904
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6892
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x694f
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movl (%rax,%rcx,4), %edx
movl %edx, %esi
addl $0x1, %esi
movl %esi, (%rax,%rcx,4)
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6916
movl $0x0, -0x78(%rbp)
movl $0x500, -0x74(%rbp)     # imm = 0x500
leaq -0x80(%rbp), %rdi
movl $0x500, %esi            # imm = 0x500
callq 0x5e20
leaq -0x80(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x5e80
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x1, -0x90(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jl 0x6a7a
movl -0x8c(%rbp), %edi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl -0x54(%rbp), %ecx
callq 0x65a0
movb %al, %dl
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
leaq -0x80(%rbp), %rdi
callq 0x5f20
movq %rax, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jle 0x69f6
leaq -0x80(%rbp), %rdi
callq 0x5f70
jmp 0x69cd
movq -0x48(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x9c(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0xac(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5f20
movl -0xac(%rbp), %edx
movq %rax, -0xa4(%rbp)
subl -0xa0(%rbp), %edx
movq -0x88(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x8c(%rbp), %esi
movl -0x90(%rbp), %edx
movl %edx, %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5e80
movl -0x9c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x69a0
leaq 0x5a51(%rip), %rdi      # 0xc4d2
movb $0x0, %al
callq 0x20e0
movq 0x8569(%rip), %rax      # 0xeff8
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6210
movq -0xb8(%rbp), %rdi
leaq 0x5a26(%rip), %rsi      # 0xc4da
movb $0x1, %al
callq 0x2160
movq -0x80(%rbp), %rdi
callq 0xb810
movq -0x50(%rbp), %rdi
callq 0xb810
movq -0x48(%rbp), %rdi
callq 0xb810
movq -0x60(%rbp), %rdi
callq 0xb810
movq -0x68(%rbp), %rdi
callq 0xb810
xorl %eax, %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)