// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]lyndon-array/external/gsaca_cl/gsaca.c
// Function: gsaca_unsigned_cl
int gsaca_unsigned_cl(const unsigned char *S, unsigned int *SA, 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 = (unsigned int *)malloc( n * sizeof(unsigned int) );
	PREV = (unsigned int *)malloc( n * sizeof(unsigned int) );
	GSIZE = gsize_calloc( n );
	if (!ISA || !PREV || !GSIZE)	{
		free(ISA); 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(ISA); 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;
		}
		
		//prepare current group for phase 2
		//SA[gendtmp] = gstarttmp; //counter where to place next entry TODO: removed because not necessary
		
		//set remaining prev pointers of own group to correct value TODO: new, was ignored before
		while (gendtmp >= gstart) {
			s = SA[gendtmp--];
			PREV[s] = PREV[PREV[s]]; //use stored references to get correct pointers
		}
	}

	//// FOR FELIPE ////
	//compute for each suffix S[i..n] the length of its group context
	unsigned int *CLEN = SA; //context length of each suffix
	for (i = 0; i < n; i++) { //set all context lengths to 1
		CLEN[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
			CLEN[p] += CLEN[i]; //add own suffix length to that of p
		}
	}
	

	free(ISA); free(PREV); gsize_free(GSIZE);

	return 0;	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
testl %edx, %edx
je 0x56be
movl %edx, %r12d
movq %rdi, %rbp
leal -0x1(%r12), %eax
cmpb $0x0, (%rdi,%rax)
je 0x56c5
pushq $-0x1
popq %rbp
jmp 0x59c5
xorl %ebp, %ebp
jmp 0x59c5
movq %rsi, %r13
movq %rax, 0x8(%rsp)
movl %r12d, %eax
movq %rax, 0x18(%rsp)
leaq (,%rax,4), %r14
movq %r14, %rdi
callq 0x76d0
movq %rax, %rbx
movq %r14, %rdi
callq 0x76d0
movq %rax, %r14
movl %r12d, %edi
callq 0x59fc
movq %rax, %r15
testq %rbx, %rbx
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
testq %r15, %r15
sete %al
orb %cl, %al
jne 0x572a
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x5732
andl $0x0, 0x24(%rsp,%rax,8)
incq %rax
jmp 0x5718
pushq $-0x2
popq %rbp
jmp 0x59ad
xorl %eax, %eax
cmpq %rax, 0x18(%rsp)
je 0x5749
movzbl (%rbp,%rax), %ecx
incl 0x24(%rsp,%rcx,8)
incq %rax
jmp 0x5734
cmpl $0x1, 0x24(%rsp)
jne 0x59d9
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x100, %rax            # imm = 0x100
je 0x5779
movl 0x24(%rsp,%rax,8), %edx
testl %edx, %edx
je 0x5774
movl %ecx, 0x20(%rsp,%rax,8)
movl %ecx, %esi
movl %edx, (%r15,%rsi,4)
addl %edx, %ecx
incq %rax
jmp 0x5758
movq 0x8(%rsp), %rax
cmpl %r12d, %eax
jae 0x57af
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, (%r14,%rcx,4)
movl %edx, (%rbx,%rcx,4)
movl %eax, (%r13,%rdx,4)
decl %eax
jmp 0x577e
movq 0x8(%rsp), %rax
testl %eax, %eax
je 0x5974
movl %eax, %ecx
movl (%r13,%rcx,4), %ecx
movl (%r14,%rcx,4), %ecx
andl $0x0, (%r15,%rcx,4)
movl %eax, %edx
cmpl %ecx, %edx
jb 0x5808
movl %edx, %esi
movl (%r13,%rsi,4), %esi
leal -0x1(%rsi), %edi
cmpl %r12d, %edi
jae 0x5800
movl %edi, %r9d
movl (%rbx,%r9,4), %r8d
cmpl %eax, %r8d
jbe 0x57f3
movl (%r14,%r9,4), %edi
jmp 0x57dc
cmpl %ecx, %r8d
jb 0x5800
movl $0x1, (%r15,%r8,4)
movl %edi, (%r14,%rsi,4)
decl %edx
jmp 0x57ce
xorl %edi, %edi
movl %ecx, %edx
cmpl %eax, %edx
ja 0x583b
movl %edx, %r8d
movl (%r13,%r8,4), %esi
movl %eax, (%rbx,%rsi,4)
cmpl $0x0, (%r15,%r8,4)
jne 0x5837
leal (%rdi,%rcx), %r9d
movl (%r13,%r9,4), %r10d
movl %r10d, (%r13,%r8,4)
incl %edi
movl %esi, (%r13,%r9,4)
incl %edx
jmp 0x580c
addl %ecx, %edi
xorl %edx, %edx
movl %ecx, %esi
movl %edi, %r8d
leal -0x1(%r8), %r9d
cmpl %esi, %r9d
jb 0x589c
movl %r9d, %r10d
movl (%r13,%r10,4), %ebp
movl (%r14,%rbp,4), %r11d
cmpl %r12d, %r11d
jae 0x5875
cmpl %ecx, (%rbx,%r11,4)
jb 0x5886
movl (%r14,%r11,4), %r10d
movl %r10d, (%r14,%rbp,4)
movl %ebp, (%r14,%r11,4)
decl %r9d
jmp 0x5848
movl %esi, %r11d
incl %esi
movl (%r13,%r11,4), %r11d
movl %r11d, (%r13,%r10,4)
jmp 0x5848
decl %edi
movl (%r13,%rdi,4), %ebp
movl %ebp, (%r13,%r10,4)
movl %r11d, (%r13,%rdi,4)
decl %r9d
jmp 0x5848
subl %edi, %r8d
jbe 0x58aa
movl %edi, %r9d
movl %r8d, (%r15,%r9,4)
incl %edx
cmpl %edi, %esi
jb 0x5841
jmp 0x5948
movl %edx, 0x14(%rsp)
movl %esi, %edi
movl (%r15,%rdi,4), %r8d
leal (%r8,%rsi), %r9d
decl %r9d
addl %esi, %r8d
cmpl %esi, %r9d
jb 0x5908
movl %r9d, %r10d
movl (%r13,%r10,4), %r10d
movl (%r14,%r10,4), %r11d
movl (%r15,%r11,4), %ebp
leal -0x1(%rbp), %edx
movl %edx, (%r15,%r11,4)
leal -0x1(%rbp,%r11), %edx
movl (%r13,%rdx,4), %r11d
movl (%rbx,%r10,4), %ebp
movl %r11d, (%r13,%rbp,4)
movl %ebp, (%rbx,%r11,4)
movl %r10d, (%r13,%rdx,4)
movl %edx, (%rbx,%r10,4)
decl %r9d
jmp 0x58c7
movl %r8d, %esi
movq %rdi, %r9
cmpq %rsi, %r9
jae 0x593c
movl (%r13,%r9,4), %edx
movl (%r14,%rdx,4), %r10d
movl (%r15,%r10,4), %r11d
addl %r10d, %r11d
movl %r11d, (%r14,%rdx,4)
incq %r9
jmp 0x590e
movl (%r13,%rdi,4), %edx
movl (%r14,%rdx,4), %edx
incl (%r15,%rdx,4)
incq %rdi
cmpq %rsi, %rdi
jb 0x592c
movl %r8d, %esi
movl 0x14(%rsp), %edx
subl $0x1, %edx
jae 0x58b3
jmp 0x5968
movl %eax, %edx
decl %eax
movl (%r13,%rdx,4), %edx
movl (%r14,%rdx,4), %edi
movl (%r14,%rdi,4), %edi
movl %edi, (%r14,%rdx,4)
cmpl %esi, %eax
jae 0x5953
leal -0x1(%rcx), %eax
jmp 0x57b4
xorl %eax, %eax
cmpq %rax, 0x18(%rsp)
je 0x598b
movl $0x1, (%r13,%rax,4)
incq %rax
jmp 0x5976
movq 0x8(%rsp), %rdx
subq $0x1, %rdx
jb 0x59ab
movl (%r14,%rdx,4), %eax
cmpl %r12d, %eax
jae 0x5990
movl (%r13,%rdx,4), %ecx
addl %ecx, (%r13,%rax,4)
jmp 0x5990
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x77a6
movq %r14, %rdi
callq 0x77a6
movq %r15, %rdi
callq 0x77a6
movl %ebp, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6618(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x2dc3(%rip), %rdi      # 0x87ad
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
pushq $-0x1
jmp 0x572c