// File: /workspace/github_c_makefile_star2_O3/halcy[P]quasi_crystals_bot/Engine/Neuquant.c
// Function: BuildNeuquantIndex
void BuildNeuquantIndex(Neuquant *self)
{
	int i,j,smallpos,smallval;
	int *p,*q;
	int previouscol,startpos;

	previouscol = 0;
	startpos = 0;
	for (i=0; i<NeuquantNetworkSize; i++) {
		p = self->network[i];
		smallpos = i;
		smallval = p[1];			/* index on g */
		/* find smallest in i..NeuquantNetworkSize-1 */
		for (j=i+1; j<NeuquantNetworkSize; j++) {
			q = self->network[j];
			if (q[1] < smallval) {		/* index on g */
				smallpos = j;
				smallval = q[1];	/* index on g */
			}
		}
		q = self->network[smallpos];
		/* swap p (i) and q (smallpos) entries */
		if (i != smallpos) {
			j = q[0];   q[0] = p[0];   p[0] = j;
			j = q[1];   q[1] = p[1];   p[1] = j;
			j = q[2];   q[2] = p[2];   p[2] = j;
			j = q[3];   q[3] = p[3];   p[3] = j;
		}
		/* smallval entry is now in position i */
		if (smallval != previouscol) {
			self->netindex[previouscol] = (startpos+i)>>1;
			for (j=previouscol+1; j<smallval; j++) self->netindex[j] = i;
			previouscol = smallval;
			startpos = i;
		}
	}
	self->netindex[previouscol] = (startpos+maxnetpos)>>1;
	for (j=previouscol+1; j<256; j++) self->netindex[j] = maxnetpos; /* really 256 */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq 0x14(%rdi), %rax
leaq 0x100c(%rdi), %rcx
xorl %edx, %edx
movq $-0xff, %rsi
pmovsxbq 0xb3c3(%rip), %xmm0     # 0xe370
pmovsxbq 0xb3bc(%rip), %xmm1     # 0xe372
movdqa 0xb352(%rip), %xmm2     # 0xe310
pcmpeqd %xmm3, %xmm3
xorl %r10d, %r10d
xorl %r9d, %r9d
movq %rdx, %r11
movl %edx, %ebp
shlq $0x4, %r11
movl 0x4(%rdi,%r11), %ebx
cmpq $0xfe, %rdx
movl %ebx, %r8d
ja 0x300b
movq %rsi, %r14
movq %rax, %r15
movl %ebx, %r8d
movl %edx, %ebp
movl (%r15), %r12d
leal 0x100(%r14), %r13d
cmpl %r8d, %r12d
cmovll %r13d, %ebp
cmovll %r12d, %r8d
leaq 0x10(%r15), %r15
incq %r14
jne 0x2fed
movl %ebp, %r14d
cmpq %r14, %rdx
je 0x303e
movslq %ebp, %r14
leaq (%r11,%rdi), %r11
shlq $0x4, %r14
movl (%r11), %ebp
movdqu (%rdi,%r14), %xmm4
movl %ebp, (%rdi,%r14)
movl %ebx, 0x4(%rdi,%r14)
movq 0x8(%r11), %rbx
movq %rbx, 0x8(%rdi,%r14)
movdqu %xmm4, (%r11)
movl %r8d, %r11d
subl %r9d, %r11d
je 0x311a
addl %edx, %r10d
movslq %r9d, %rbx
incl %r9d
sarl %r10d
cmpl %r8d, %r9d
movl %r10d, 0x1000(%rdi,%rbx,4)
jge 0x3114
leal -0x2(%r11), %r11d
movslq %r9d, %r10
movq %r11, %xmm4
leaq 0x4(%r11), %r9
leaq (%rcx,%r10,4), %r10
xorl %r11d, %r11d
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
andq $-0x4, %r9
por %xmm2, %xmm4
movq %r11, %xmm5
movdqa %xmm5, %xmm6
pxor %xmm2, %xmm6
pcmpgtq %xmm4, %xmm6
movd %xmm6, %ebx
notl %ebx
testb $0x1, %bl
je 0x30ad
movl %edx, -0xc(%r10,%r11,4)
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm1, %xmm6
pxor %xmm2, %xmm6
pcmpgtq %xmm4, %xmm6
pxor %xmm3, %xmm6
pextrb $0x8, %xmm6, %ebx
testb $0x1, %bl
je 0x30d7
movl %edx, -0x8(%r10,%r11,4)
por %xmm0, %xmm5
pxor %xmm2, %xmm5
pcmpgtq %xmm4, %xmm5
pxor %xmm3, %xmm5
pextrb $0x0, %xmm5, %ebx
testb $0x1, %bl
je 0x30f8
movl %edx, -0x4(%r10,%r11,4)
pextrb $0x8, %xmm5, %ebx
testb $0x1, %bl
je 0x3107
movl %edx, (%r10,%r11,4)
addq $0x4, %r11
cmpq %r11, %r9
jne 0x308b
movl %r8d, %r9d
movl %edx, %r10d
incq %rdx
addq $0x10, %rax
incq %rsi
cmpq $0x100, %rdx            # imm = 0x100
jne 0x2fc8
addl $0xff, %r10d
movslq %r9d, %rcx
sarl %r10d
cmpl $0xfe, %ecx
movl %r10d, 0x1000(%rdi,%rcx,4)
jg 0x321b
movl $0xfe, %edx
movl $0x102, %eax            # imm = 0x102
pcmpeqd %xmm4, %xmm4
subq %rcx, %rdx
subq %rcx, %rax
leaq 0x1010(%rdi,%rcx,4), %rcx
movq %rdx, %xmm3
andq $-0x4, %rax
xorl %edx, %edx
pshufd $0x44, %xmm3, %xmm3     # xmm3 = xmm3[0,1,0,1]
pxor %xmm2, %xmm3
movq %rdx, %xmm5
movdqa %xmm5, %xmm6
pxor %xmm2, %xmm6
pcmpgtq %xmm3, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0x31a8
movl $0xff, -0xc(%rcx,%rdx,4)
pshufd $0x44, %xmm5, %xmm5     # xmm5 = xmm5[0,1,0,1]
movdqa %xmm5, %xmm6
por %xmm1, %xmm6
pxor %xmm2, %xmm6
pcmpgtq %xmm3, %xmm6
pxor %xmm4, %xmm6
pextrb $0x8, %xmm6, %esi
testb $0x1, %sil
je 0x31d6
movl $0xff, -0x8(%rcx,%rdx,4)
por %xmm0, %xmm5
pxor %xmm2, %xmm5
pcmpgtq %xmm3, %xmm5
pxor %xmm4, %xmm5
pextrb $0x0, %xmm5, %esi
testb $0x1, %sil
je 0x31fb
movl $0xff, -0x4(%rcx,%rdx,4)
pextrb $0x8, %xmm5, %esi
testb $0x1, %sil
je 0x320e
movl $0xff, (%rcx,%rdx,4)
addq $0x4, %rdx
cmpq %rdx, %rax
jne 0x3182
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq