// File: /workspace/github_c_makefile_star2_O0/halcy[P]quasi_crystals_bot/Engine/Neuquant.c
// Function: SearchNeuquantIndex
int SearchNeuquantIndex(Neuquant *self,int r,int g,int b)
{
	int i,j,dist,a,bestd;
	int *p;
	int best;

	bestd = 1000;		/* biggest possible dist is 256*3 */
	best = -1;
	i = self->netindex[g];	/* index on g */
	j = i-1;		/* start at netindex[g] and work outwards */

	while ((i<NeuquantNetworkSize) || (j>=0)) {
		if (i<NeuquantNetworkSize) {
			p = self->network[i];
			dist = p[1] - g;		/* inx key */
			if (dist >= bestd) i = NeuquantNetworkSize;	/* stop iter */
			else {
				i++;
				if (dist<0) dist = -dist;
				a = p[0] - b;   if (a<0) a = -a;
				dist += a;
				if (dist<bestd) {
					a = p[2] - r;   if (a<0) a = -a;
					dist += a;
					if (dist<bestd) {bestd=dist; best=p[3];}
				}
			}
		}
		if (j>=0) {
			p = self->network[j];
			dist = g - p[1]; /* inx key - reverse dif */
			if (dist >= bestd) j = -1; /* stop iter */
			else {
				j--;
				if (dist<0) dist = -dist;
				a = p[0] - b;   if (a<0) a = -a;
				dist += a;
				if (dist<bestd) {
					a = p[2] - r;   if (a<0) a = -a;
					dist += a;
					if (dist<bestd) {bestd=dist; best=p[3];}
				}
			}
		}
	}
	return(best);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl $0x3e8, -0x28(%rbp)     # imm = 0x3E8
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl 0x1000(%rax,%rcx,4), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
movb $0x1, %al
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
movb %al, -0x35(%rbp)
jl 0x4242
cmpl $0x0, -0x1c(%rbp)
setge %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
testb $0x1, %al
jne 0x424e
jmp 0x43da
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x4313
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
subl -0x10(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jl 0x428f
movl $0x100, -0x18(%rbp)     # imm = 0x100
jmp 0x4311
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x42a6
xorl %eax, %eax
subl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x42c0
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x430f
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
subl -0xc(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x42ec
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x430d
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x34(%rbp)
jmp 0x430f
jmp 0x4311
jmp 0x4313
cmpl $0x0, -0x1c(%rbp)
jl 0x43d5
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x10(%rbp), %eax
movq -0x30(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jl 0x4351
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
jmp 0x43d3
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x4368
xorl %eax, %eax
subl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4382
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x43d1
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
subl -0xc(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x43ae
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x43cf
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x34(%rbp)
jmp 0x43d1
jmp 0x43d3
jmp 0x43d5
jmp 0x422a
movl -0x34(%rbp), %eax
popq %rbp
retq
nop