// File: /workspace/github_c_makefile_star2_O3/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:
testl %edx, %edx
je 0x6054
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
movl %edx, %ebx
movq %rdi, %rbp
leal -0x1(%rbx), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x0, (%rdi,%rcx)
jne 0x6042
movq %rsi, %r14
movq %rcx, 0x8(%rsp)
movl %ebx, %r13d
leaq (,%r13,4), %r12
movq %r12, %rdi
callq 0x855a
movq %rax, %r15
movq %r12, %rdi
callq 0x855a
movq %rax, %r12
movl $0x4, %esi
movq %r13, 0x18(%rsp)
movq %r13, %rdi
callq 0x86b3
movq %rax, %r13
testq %r15, %r15
sete %al
testq %r12, %r12
sete %cl
orb %al, %cl
testq %r13, %r13
sete %al
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
orb %cl, %al
jne 0x6026
xorl %eax, %eax
movl $0x0, 0x24(%rsp,%rax,8)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x5c91
xorl %eax, %eax
movq 0x18(%rsp), %rdx
movzbl (%rbp,%rax), %ecx
incl 0x24(%rsp,%rcx,8)
incq %rax
cmpq %rax, %rdx
jne 0x5cab
cmpl $0x1, 0x24(%rsp)
jne 0x6057
xorl %eax, %eax
xorl %ecx, %ecx
movl 0x24(%rsp,%rax,8), %edx
testl %edx, %edx
je 0x5ce0
movl %ecx, 0x20(%rsp,%rax,8)
movl %ecx, %esi
movl %edx, (%r13,%rsi,4)
addl %edx, %ecx
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x5ccb
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, (%r12,%rcx,4)
movl %edx, (%r15,%rcx,4)
movl %eax, (%r14,%rdx,4)
decl %eax
cmpl %ebx, %eax
jb 0x5cf0
cmpl $0x0, 0x8(%rsp)
je 0x5f05
movq 0x8(%rsp), %rax
movl %eax, %ecx
movl (%r14,%rcx,4), %ecx
movl (%r12,%rcx,4), %ecx
movl $0x0, (%r13,%rcx,4)
xorl %edx, %edx
movl %eax, %esi
movl $0x0, %edi
cmpl %ecx, %eax
jb 0x5dbe
movl %esi, %edi
movl (%r14,%rdi,4), %edi
leal -0x1(%rdi), %r8d
cmpl %ebx, %r8d
jae 0x5d7d
movl %r8d, %r10d
movl (%r15,%r10,4), %r9d
cmpl %eax, %r9d
jbe 0x5d6f
movl (%r12,%r10,4), %r8d
jmp 0x5d58
cmpl %ecx, %r9d
jb 0x5d7d
movl $0x1, (%r13,%r9,4)
movl %r8d, (%r12,%rdi,4)
decl %esi
cmpl %ecx, %esi
jae 0x5d4e
movl $0x0, %edi
cmpl %ecx, %eax
jb 0x5dbe
xorl %edi, %edi
movl %ecx, %esi
movl %esi, %esi
movl (%r14,%rsi,4), %r8d
movl %eax, (%r15,%r8,4)
cmpl $0x0, (%r13,%rsi,4)
jne 0x5db8
leal (%rdi,%rcx), %r9d
movl (%r14,%r9,4), %r10d
movl %r10d, (%r14,%rsi,4)
incl %edi
movl %r8d, (%r14,%r9,4)
incl %esi
cmpl %eax, %esi
jbe 0x5d94
addl %ecx, %edi
movl %ecx, %esi
leal -0x1(%rdi), %r9d
cmpl %esi, %r9d
jae 0x5dd0
movl %edi, %r8d
jmp 0x5e2c
movl %edi, %r8d
movl %r9d, %r10d
movl (%r14,%r10,4), %ebp
movl (%r12,%rbp,4), %r11d
cmpl %ebx, %r11d
jae 0x5dfd
decl %r9d
cmpl %ecx, (%r15,%r11,4)
jae 0x5e0c
decl %r8d
movl (%r14,%r8,4), %ebp
movl %ebp, (%r14,%r10,4)
movl %r11d, (%r14,%r8,4)
jmp 0x5e18
movl %esi, %r11d
incl %esi
movl (%r14,%r11,4), %r11d
movl %r11d, (%r14,%r10,4)
jmp 0x5e18
movl (%r12,%r11,4), %r10d
movl %r10d, (%r12,%rbp,4)
movl %ebp, (%r12,%r11,4)
cmpl %esi, %r9d
jae 0x5dd3
subl %r8d, %edi
jbe 0x5e2c
movl %r8d, %r9d
movl %edi, (%r13,%r9,4)
incl %edx
movl %r8d, %edi
cmpl %r8d, %esi
jb 0x5dc2
testl %edx, %edx
je 0x5ef7
movl %edx, 0x14(%rsp)
movl %esi, %r8d
movl %esi, %edi
movl (%r13,%rdi,4), %esi
leal (%rsi,%r8), %r9d
decl %r9d
cmpl %r8d, %r9d
jb 0x5e93
movl %r9d, %r10d
movl (%r14,%r10,4), %r10d
movl (%r12,%r10,4), %r11d
movl (%r13,%r11,4), %ebp
leal -0x1(%rbp), %edx
movl %edx, (%r13,%r11,4)
leal -0x1(%rbp,%r11), %edx
movl (%r14,%rdx,4), %r11d
movl (%r15,%r10,4), %ebp
movl %r11d, (%r14,%rbp,4)
movl %ebp, (%r15,%r11,4)
movl %r10d, (%r14,%rdx,4)
movl %edx, (%r15,%r10,4)
decl %r9d
cmpl %edi, %r9d
jae 0x5e56
addl %r8d, %esi
cmpl %esi, %edi
jae 0x5ed5
movl %esi, %r8d
movq %rdi, %r9
movl (%r14,%r9,4), %edx
movl (%r12,%rdx,4), %r10d
movl (%r13,%r10,4), %r11d
addl %r10d, %r11d
movl %r11d, (%r12,%rdx,4)
incq %r9
cmpq %r9, %r8
jne 0x5ea0
cmpl %esi, %edi
jae 0x5ed5
movl (%r14,%rdi,4), %edx
movl (%r12,%rdx,4), %edx
incl (%r13,%rdx,4)
incq %rdi
cmpq %rdi, %r8
jne 0x5ec0
movl 0x14(%rsp), %edx
decl %edx
jne 0x5e3c
jmp 0x5ef7
movl %eax, %edx
decl %eax
movl (%r14,%rdx,4), %edx
movl (%r12,%rdx,4), %edi
movl (%r12,%rdi,4), %edi
movl %edi, (%r12,%rdx,4)
cmpl %esi, %eax
jae 0x5ee3
decl %ecx
movl %ecx, %eax
jne 0x5d2e
movq 0x18(%rsp), %rcx
leaq 0x3(%rcx), %rax
andq $-0x4, %rax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x3889(%rip), %xmm1     # 0x97b0
movdqa 0x3891(%rip), %xmm2     # 0x97c0
xorl %ecx, %ecx
movdqa 0x3897(%rip), %xmm3     # 0x97d0
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x3897(%rip), %xmm5     # 0x97e0
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edx
notl %edx
testb $0x1, %dl
je 0x5f85
movl $0x1, (%r14,%rcx,4)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x5f9c
movl $0x1, 0x4(%r14,%rcx,4)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edx
testb $0x1, %dl
je 0x5fdc
movl $0x1, 0x8(%r14,%rcx,4)
pextrw $0x4, %xmm6, %edx
testb $0x1, %dl
je 0x5fef
movl $0x1, 0xc(%r14,%rcx,4)
addq $0x4, %rcx
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
cmpq %rcx, %rax
jne 0x5f49
xorl %esi, %esi
cmpl $0x0, 0x8(%rsp)
je 0x6026
leal -0x2(%rbx), %eax
movl (%r12,%rax,4), %ecx
cmpl %ebx, %ecx
jae 0x6020
movl (%r14,%rax,4), %edx
addl %edx, (%r14,%rcx,4)
addq $-0x1, %rax
jb 0x6010
movq %r15, %rdi
movl %esi, %ebx
callq 0x8611
movq %r12, %rdi
callq 0x8611
movq %r13, %rdi
callq 0x8611
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 0x2f0a
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jmp 0x6026