// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]lyndon-array/external/gsaca_cl/gsaca.c
// Function: gsaca_unsigned_cl_la
int gsaca_unsigned_cl_la(const unsigned char *S, unsigned int *SA, unsigned int *LA, unsigned int n) {
	unsigned int *ISA, *PREV;
	void *GSIZE;
	unsigned int i,j;
	unsigned int gstart, gend;
	unsigned int s, p, sr;
	unsigned int tmp, gstarttmp, gendtmp;
	unsigned int C[2*C_SIZE]; //counts and cumulative counts

	if (n == 0)		return 0;
	if (S[n-1] != '\0')	return -1;

	//set up needed structures
	ISA = LA;
	PREV = (unsigned int *)malloc( n * sizeof(unsigned int) );
	GSIZE = gsize_calloc( n );
	if (!PREV || !GSIZE)	{
		free(PREV); gsize_free(GSIZE);
		return -2;
	}

	//// PHASE 1: pre-sort suffixes ////
	//build initial group structure
	for (i = 0; i < C_SIZE; ++i)	C_CNT(i) = 0;
	for (i = 0; i < n; ++i) ++C_CNT(S[i]); //count characters

	if (C_CNT(0) != 1) { //more than 1 nullbyte
		fprintf(stderr, "more than 1 nullbyte\n");
		free(PREV); gsize_free(GSIZE);
		return -1;
	}
	j = 0;
	for (i = 0; i < C_SIZE; ++i) { //build cumulative counts and set up GSIZE
		if (C_CNT(i) > 0) {
			C_CUM(i) = j;
			gsize_set(GSIZE, j, C_CNT(i));
			j += C_CNT(i);
		}
	}
	for (i = n-1; i < n; --i) { //set up ISA, GLINK and SA
		gstart = C_CUM(S[i]);
		sr = gstart + --C_CNT(S[i]);
		GLINK[i] = gstart;
		ISA[i] = sr;
		SA[sr] = i;
	}

	//process groups from highest to lowest
	for (gend = n-1; gend > 0; gend = gstarttmp-1) {
		gstart = GLINK[SA[gend]];
		gstarttmp = gstart;	gendtmp = gend;

		//clear GSIZE group size for marking
		gsize_clear(GSIZE, gstart);

		//compute prev - pointers and mark suffixes of own group that
		//have a prev-pointer of own group pointing to them
		for (i = gend; i >= gstart; --i) {
			s = SA[i]; //use prev - pointers from already used groups
			for (p = s-1; p < n; p = PREV[p]) {
				if (ISA[p] <= gend) {
					if (ISA[p] >= gstart) {
						gsize_set(GSIZE, ISA[p], 1); //mark ISA[p]
					}
					break;
				}
			}
			PREV[s] = p;
		}

		//set GENDLINK of all suffixes for phase 2 and move unmarked
		// suffixes to the front of the actual group
		j = 0;
		for (i = gstart; i <= gend; ++i) {
			s = SA[i];
			GENDLINK[s] = gend;
			if (gsize_get(GSIZE, i) == 0) { //i is not marked
				//swap SA[i] and SA[gstart+j] TODO: changed, now a swap is performed
				SA[i] = SA[gstart+j];
				SA[gstart+(j++)] = s;
			}
		}

		//order the suffixes according on how much suffixes of same
		//group are jumped by them
		gend = gstart + j; //exclusive bound by now
		j = 0;
		do {
			i = gend-1; sr = gend;
			while (i >= gstart) {
				s = SA[i];
				p = PREV[s];
				if (p < n) {
					if (ISA[p] < gstarttmp) { //p is in a lex. smaller group
						SA[i--] = SA[--gend];
						SA[gend] = p; //push prev to back
					} else { //p is in same group
						PREV[s] = PREV[p];
						PREV[p] = s; //set a reference from p to s, as s's prev pointer will be correct later TODO changed, could be ignored before
						--i;
					}
				} else { //prev points to nothing
					SA[i] = SA[gstart++]; //remove entry
				}
			}
			//write number of suffixes written to end on stack using GSIZE
			if (gend < sr) {
				gsize_set(GSIZE, gend, sr-gend);
				++j; //also, count number of splitted groups
			}
		} while (gstart < gend);

		//rearrange previous suffixes stored in other groups
		while (j--) {
			gend = gstart + gsize_get(GSIZE, gstart);
			//decrement group count of previous group suffixes, and move them to back
			for (i = gend-1; i >= gstart; --i) {
				p = SA[i];
				sr = GLINK[p];
				sr += gsize_dec_get(GSIZE, sr);
				//move p to back by exchanging it with last suffix s of group
				s = SA[sr];
				tmp = ISA[p];
				SA[tmp] = s;	ISA[s] = tmp;
				SA[sr] = p;	ISA[p] = sr;				
			}
			//set new GLINK for moved suffixes
			for (i = gstart; i < gend; ++i) {
				p = SA[i];
				sr = GLINK[p];
				sr += gsize_get(GSIZE, sr);
				GLINK[p] = sr;
			}
			//set up GSIZE for newly created groups
			for (i = gstart; i < gend; ++i) {
				p = SA[i];
				sr = GLINK[p];
				gsize_inc(GSIZE, sr);
			}
			gstart = gend;
		}
		
		//set remaining prev pointers of own group to correct value TODO: new, was ignored before
		for (i = gendtmp; i >= gstart; --i) {
			s = SA[i];
			PREV[s] = PREV[PREV[s]]; //use stored references to get correct pointers
		}

		//prepare current group for phase 2
		SA[gendtmp] = gstarttmp; //counter where to place next entry TODO: removed because not necessary
	}

	//// PHASE 2: sort suffixes finally ////
	SA[0] = n-1;
	for (i = 0; i < n; i++) {
		s = SA[i]-1;
		while (s < n) {
			sr = GENDLINK[s];
			if (sr == n) //suffix already placed to SA, stop
				break;
			sr = SA[sr]++; //get position where to write s
			SA[sr] = s;
			//mark that suffix is placed in SA already
			GENDLINK[s] = n; 
			/* NOTE: Using annother technique
			   to indicate if a suffix is placed to SA already,
			   ISA could be computed too. A possible way would be to
			   set PREV[s] = s, and check that in the above if. 
			*/
			s = PREV[s]; //process next suffix
		}
	}

	//// PHASE 3: compute Lyndon array ////
	//compute for each suffix S[i..n] the length of its group context
	for (i = 0; i < n; i++) { //set all context lengths to 1
		LA[i] = 1;
	}

	//use prev pointers to compute context lengths
	for (i = n-1; i-- > 0; ) { //run from back to front
		p = PREV[i];
		if (p < n) { //ensure previous suffix exists
			LA[p] += LA[i]; //add own suffix length to that of p
		}
	}
	

	free(PREV); gsize_free(GSIZE);

	return 0;	
}

// Assembly:
testl %ecx, %ecx
je 0x6342
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
movl %ecx, %ebx
movq %rdi, %rbp
leal -0x1(%rbx), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x0, (%rdi,%rcx)
jne 0x6330
movq %rdx, %r14
movq %rsi, %r13
movq %rcx, 0x8(%rsp)
movl %ebx, %r12d
leaq (,%r12,4), %rdi
callq 0x7f0e
movq %rax, %r15
movl $0x4, %esi
movq %r12, 0x10(%rsp)
movq %r12, %rdi
callq 0x8067
movq %rax, %r12
testq %r15, %r15
sete %al
testq %r12, %r12
sete %cl
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
orb %al, %cl
jne 0x631c
xorl %eax, %eax
movl $0x0, 0x24(%rsp,%rax,8)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x6018
xorl %eax, %eax
movq 0x10(%rsp), %rdx
movzbl (%rbp,%rax), %ecx
incl 0x24(%rsp,%rcx,8)
incq %rax
cmpq %rax, %rdx
jne 0x6032
cmpl $0x1, 0x24(%rsp)
jne 0x6345
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x24(%rsp,%rax,8), %edx
testl %edx, %edx
je 0x6066
movl %ecx, 0x20(%rsp,%rax,8)
movl %ecx, %esi
movl %edx, (%r12,%rsi,4)
addl %edx, %ecx
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x6052
movq 0x8(%rsp), %rax
movl %eax, %ecx
movzbl (%rbp,%rcx), %edx
movl 0x20(%rsp,%rdx,8), %esi
movl 0x24(%rsp,%rdx,8), %edi
leal -0x1(%rdi), %r8d
movl %r8d, 0x24(%rsp,%rdx,8)
leal -0x1(%rdi,%rsi), %edx
movl %esi, (%r15,%rcx,4)
movl %edx, (%r14,%rcx,4)
movl %eax, (%r13,%rdx,4)
decl %eax
cmpl %ebx, %eax
jb 0x6076
cmpl $0x0, 0x8(%rsp)
je 0x629b
movq 0x8(%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x18(%rsp)
movl (%r13,%rcx,4), %ecx
movl (%r15,%rcx,4), %edx
movl $0x0, (%r12,%rdx,4)
movl %eax, %esi
cmpl %edx, %eax
jb 0x610a
movl %esi, %ecx
movl (%r13,%rcx,4), %edi
leal -0x1(%rdi), %r8d
cmpl %ebx, %r8d
jae 0x6100
movl %r8d, %r9d
movl (%r14,%r9,4), %ecx
cmpl %eax, %ecx
jbe 0x60f4
movl (%r15,%r9,4), %r8d
jmp 0x60de
cmpl %edx, %ecx
jb 0x6100
movl $0x1, (%r12,%rcx,4)
movl %r8d, (%r15,%rdi,4)
decl %esi
cmpl %edx, %esi
jae 0x60d3
xorl %esi, %esi
movl $0x0, %r8d
cmpl %eax, %edx
ja 0x6149
xorl %r8d, %r8d
movl %edx, %edi
movl %edi, %edi
movl (%r13,%rdi,4), %ecx
movl %eax, (%r14,%rcx,4)
cmpl $0x0, (%r12,%rdi,4)
jne 0x6143
leal (%r8,%rdx), %r9d
movl (%r13,%r9,4), %r10d
movl %r10d, (%r13,%rdi,4)
incl %r8d
movl %ecx, (%r13,%r9,4)
incl %edi
cmpl %eax, %edi
jbe 0x611b
addl %edx, %r8d
movl %edx, %edi
leal -0x1(%r8), %r10d
movl %r8d, %r9d
cmpl %edi, %r10d
jb 0x61a5
movl %r10d, %r11d
movl (%r13,%r11,4), %ecx
movl (%r15,%rcx,4), %ebp
cmpl %ebx, %ebp
jae 0x6187
decl %r10d
cmpl %edx, (%r14,%rbp,4)
jae 0x6197
decl %r9d
movl (%r13,%r9,4), %ecx
movl %ecx, (%r13,%r11,4)
movl %ebp, (%r13,%r9,4)
jmp 0x6155
movl %edi, %ecx
incl %edi
movl (%r13,%rcx,4), %ecx
movl %ecx, (%r13,%r11,4)
jmp 0x6155
movl (%r15,%rbp,4), %r11d
movl %r11d, (%r15,%rcx,4)
movl %ecx, (%r15,%rbp,4)
jmp 0x6155
subl %r9d, %r8d
jbe 0x61b3
movl %r9d, %ecx
movl %r8d, (%r12,%rcx,4)
incl %esi
movl %r9d, %r8d
cmpl %r9d, %edi
jb 0x614e
testl %esi, %esi
je 0x6283
movl %esi, 0x10(%rsp)
movl %edi, %r9d
movl %edi, %r8d
movl (%r12,%r8,4), %edi
leal (%rdi,%r9), %r10d
decl %r10d
cmpl %r9d, %r10d
jb 0x621c
movl %r10d, %ecx
movl (%r13,%rcx,4), %ecx
movl (%r15,%rcx,4), %r11d
movl (%r12,%r11,4), %ebp
leal -0x1(%rbp), %esi
movl %esi, (%r12,%r11,4)
leal -0x1(%rbp,%r11), %esi
movl (%r13,%rsi,4), %r11d
movl (%r14,%rcx,4), %ebp
movl %r11d, (%r13,%rbp,4)
movl %ebp, (%r14,%r11,4)
movl %ecx, (%r13,%rsi,4)
movl %esi, (%r14,%rcx,4)
decl %r10d
cmpl %r8d, %r10d
jae 0x61dd
addl %r9d, %edi
movl %edi, %r9d
cmpl %edi, %r8d
jae 0x6246
movq %r8, %r10
movl (%r13,%r10,4), %ecx
movl (%r15,%rcx,4), %esi
movl (%r12,%rsi,4), %r11d
addl %esi, %r11d
movl %r11d, (%r15,%rcx,4)
incq %r10
cmpq %r10, %r9
jne 0x622a
cmpl %edi, %r8d
jae 0x6260
movl (%r13,%r8,4), %ecx
movl (%r15,%rcx,4), %ecx
incl (%r12,%rcx,4)
incq %r8
cmpq %r8, %r9
jne 0x624b
movl 0x10(%rsp), %esi
decl %esi
jne 0x61c3
jmp 0x6283
movl %eax, %ecx
movl (%r13,%rcx,4), %ecx
movl (%r15,%rcx,4), %esi
movl (%r15,%rsi,4), %esi
movl %esi, (%r15,%rcx,4)
decl %eax
cmpl %edi, %eax
jae 0x626e
movq 0x18(%rsp), %rax
movl %edx, (%r13,%rax,4)
decl %edx
movl %edx, %eax
jne 0x60b5
movq 0x8(%rsp), %rax
movl %eax, (%r13)
cmpl $0x1, %ebx
movl %ebx, %eax
adcl $0x0, %eax
xorl %ecx, %ecx
movl (%r13,%rcx,4), %edx
decl %edx
cmpl %ebx, %edx
jae 0x62e0
movl %edx, %esi
movl (%r14,%rsi,4), %edi
cmpl %ebx, %edi
je 0x62e0
movl (%r13,%rdi,4), %r8d
leal 0x1(%r8), %r9d
movl %r9d, (%r13,%rdi,4)
movl %edx, (%r13,%r8,4)
movl %ebx, (%r14,%rsi,4)
movl (%r15,%rsi,4), %edx
jmp 0x62b5
incq %rcx
cmpq %rax, %rcx
jne 0x62ae
xorl %ecx, %ecx
movl $0x1, (%r14,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x62ea
xorl %esi, %esi
cmpl $0x0, 0x8(%rsp)
je 0x631c
leal -0x2(%rbx), %eax
movl (%r15,%rax,4), %ecx
cmpl %ebx, %ecx
jae 0x6316
movl (%r14,%rax,4), %edx
addl %edx, (%r14,%rcx,4)
addq $-0x1, %rax
jb 0x6306
movq %r15, %rdi
movl %esi, %ebx
callq 0x7fc5
movq %r12, %rdi
callq 0x7fc5
movl %ebx, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq
callq 0x2ebf
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jmp 0x631c