// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %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
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0xb6a0
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 0x634b
movq -0x30(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x6323
movq -0x8(%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, -0x88(%rbp)
jmp 0x632d
xorl %eax, %eax
movl %eax, -0x88(%rbp)
jmp 0x632d
movl -0x88(%rbp), %eax
movb %al, %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 0x62ec
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x400, %edi            # imm = 0x400
callq 0xb6a0
movq %rax, -0x48(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x638b
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6368
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x63be
movq -0x48(%rbp), %rax
movq -0x38(%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 0x6392
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x640a
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %edx
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
subl (%rax,%rcx,4), %edx
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 0x63cc
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x644a
movq -0x48(%rbp), %rax
movq -0x38(%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 -0x40(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6411
movl $0x0, -0x58(%rbp)
movl $0x500, -0x54(%rbp)     # imm = 0x500
leaq -0x60(%rbp), %rdi
movl $0x500, %esi            # imm = 0x500
callq 0x5e20
leaq -0x60(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x5e80
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x1, -0x70(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jl 0x6524
jmp 0x649e
leaq -0x60(%rbp), %rdi
callq 0x5f20
movq %rax, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jle 0x64be
leaq -0x60(%rbp), %rdi
callq 0x5f70
jmp 0x649e
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x7c(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x8c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5f20
movl -0x8c(%rbp), %edx
movq %rax, -0x84(%rbp)
subl -0x80(%rbp), %edx
movq -0x68(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x6c(%rbp), %esi
movl -0x70(%rbp), %edx
movl %edx, %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5e80
movl -0x7c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6492
leaq 0x5fa7(%rip), %rdi      # 0xc4d2
movb $0x0, %al
callq 0x20e0
movq 0x8abf(%rip), %rax      # 0xeff8
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x6210
movq -0x98(%rbp), %rdi
leaq 0x5f7c(%rip), %rsi      # 0xc4da
movb $0x1, %al
callq 0x2160
movq -0x60(%rbp), %rdi
callq 0xb810
movq -0x48(%rbp), %rdi
callq 0xb810
movq -0x38(%rbp), %rdi
callq 0xb810
movq -0x30(%rbp), %rdi
callq 0xb810
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)