// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: getclosestcol
int getclosestcol(int r, int g, int b)
{
	int i, j, k, dist, mindist, retcol;
	unsigned char *pal1;

	j = (r>>3)*FASTPALGRIDSIZ*FASTPALGRIDSIZ+(g>>3)*FASTPALGRIDSIZ+(b>>3)+FASTPALGRIDSIZ*FASTPALGRIDSIZ+FASTPALGRIDSIZ+1;
	mindist = min(rdist[coldist[r&7]+64+8],gdist[coldist[g&7]+64+8]);
	mindist = min(mindist,bdist[coldist[b&7]+64+8]);
	mindist++;

	r = 64-r; g = 64-g; b = 64-b;

	retcol = -1;
	for(k=26;k>=0;k--)
	{
		i = colscan[k]+j; if ((colhere[i>>3]&pow2char[i&7]) == 0) continue;
		i = colhead[i];
		do
		{
			pal1 = &palette[i*3];
			dist = gdist[pal1[1]+g];
			if (dist < mindist)
			{
				dist += rdist[pal1[0]+r];
				if (dist < mindist)
				{
					dist += bdist[pal1[2]+b];
					if (dist < mindist) { mindist = dist; retcol = i; }
				}
			}
			i = colnext[i];
		} while (i >= 0);
	}
	if (retcol >= 0) return(retcol);

	mindist = 0x7fffffff;
	pal1 = &palette[768-3];
	for(i=255;i>=0;i--,pal1-=3)
	{
		dist = gdist[pal1[1]+g]; if (dist >= mindist) continue;
		dist += rdist[pal1[0]+r]; if (dist >= mindist) continue;
		dist += bdist[pal1[2]+b]; if (dist >= mindist) continue;
		mindist = dist; retcol = i;
	}
	return(retcol);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edi, %eax
movslq %edi, %r10
movl %edi, %r11d
andl $-0x8, %r11d
movl %esi, %edi
movslq %esi, %r15
movl %esi, %ebx
andl $-0x8, %ebx
movl %edx, %esi
movslq %edx, %r12
movl %edx, %r14d
sarl $0x3, %r14d
andl $0x7, %eax
leaq 0x52134(%rip), %r8      # 0x77108
movzbl (%rax,%r8), %eax
leaq 0x256ac0(%rip), %rcx    # 0x27baa0
movl 0x120(%rcx,%rax,4), %eax
andl $0x7, %edi
movzbl (%rdi,%r8), %edi
leaq 0x256cba(%rip), %rdx    # 0x27bcb0
movl 0x120(%rdx,%rdi,4), %r9d
cmpl %r9d, %eax
cmovll %eax, %r9d
andl $0x7, %esi
movzbl (%rsi,%r8), %eax
leaq 0x256eac(%rip), %rsi    # 0x27bec0
movl 0x120(%rsi,%rax,4), %eax
cmpl %eax, %r9d
cmovgel %eax, %r9d
incl %r9d
movl $0x40, %edi
movl $0x40, %r8d
subq %r10, %r8
movl $0x40, %r10d
subq %r15, %r10
subq %r12, %rdi
leal (%rbx,%r11,8), %eax
leal (%r14,%rax), %ebx
addl $0x49, %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1a, %r11d
movslq %ebx, %rsi
movq %rsi, -0x8(%rsp)
leaq 0x25754c(%rip), %rbp    # 0x27c5b0
leaq 0x257845(%rip), %r13    # 0x27c8b0
leaq 0x25705e(%rip), %rsi    # 0x27c0d0
movslq (%rsi,%r11,4), %rbx
addq -0x8(%rsp), %rbx
movl %ebx, %r14d
sarl $0x3, %r14d
movslq %r14d, %r14
movl %ebx, %r15d
andl $0x7, %r15d
leaq 0x7f70d(%rip), %rsi     # 0xa47a0
movb (%r15,%rsi), %r15b
leaq 0x2570a2(%rip), %rsi    # 0x27c140
testb %r15b, (%r14,%rsi)
je 0x25100
leaq 0x257115(%rip), %rsi    # 0x27c1c0
movzbl (%rbx,%rsi), %ebx
movl %ebx, %r15d
leaq (%r15,%r15,2), %r14
movzbl 0x1(%rbp,%r14), %r12d
addq %r10, %r12
movl (%rdx,%r12,4), %r12d
cmpl %r9d, %r12d
jge 0x250f7
movzbl (%r14,%rbp), %esi
addq %r8, %rsi
addl (%rcx,%rsi,4), %r12d
cmpl %r9d, %r12d
jge 0x250f7
movzbl 0x2(%r14,%rbp), %esi
addq %rdi, %rsi
leaq 0x256dd7(%rip), %r14    # 0x27bec0
addl (%r14,%rsi,4), %r12d
cmpl %r9d, %r12d
cmovll %r12d, %r9d
cmovll %ebx, %eax
movl (%r13,%r15,4), %ebx
testl %ebx, %ebx
jns 0x250af
subq $0x1, %r11
jae 0x2506b
testl %eax, %eax
jns 0x25173
movl $0xff, %r9d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0x257789(%rip), %rbx    # 0x27c8af
movslq %r10d, %r10
movslq %r8d, %r8
movslq %edi, %rdi
leaq 0x256d8a(%rip), %r14    # 0x27bec0
movzbl -0x1(%rbx), %esi
addq %r10, %rsi
movl (%rdx,%rsi,4), %ebp
cmpl %r11d, %ebp
jge 0x25169
movzbl -0x2(%rbx), %esi
addq %r8, %rsi
addl (%rcx,%rsi,4), %ebp
cmpl %r11d, %ebp
jge 0x25169
movzbl (%rbx), %esi
addq %rdi, %rsi
addl (%r14,%rsi,4), %ebp
cmpl %r11d, %ebp
cmovll %ebp, %r11d
cmovll %r9d, %eax
addq $-0x3, %rbx
addl $-0x1, %r9d
jb 0x25136
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq