// File: /workspace/github_c_makefile_star2_O1/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 0x51509(%rip), %r8      # 0x75f70
movzbl (%rax,%r8), %eax
leaq 0x25602d(%rip), %rcx    # 0x27aaa0
movl 0x120(%rcx,%rax,4), %eax
andl $0x7, %edi
movzbl (%rdi,%r8), %edi
leaq 0x256227(%rip), %rdx    # 0x27acb0
movl 0x120(%rdx,%rdi,4), %r9d
cmpl %r9d, %eax
cmovll %eax, %r9d
andl $0x7, %esi
movzbl (%rsi,%r8), %eax
leaq 0x256419(%rip), %rsi    # 0x27aec0
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 0x256ab9(%rip), %rbp    # 0x27b5b0
leaq 0x256db2(%rip), %r13    # 0x27b8b0
leaq 0x2565cb(%rip), %rsi    # 0x27b0d0
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 0x7ec7a(%rip), %rsi     # 0xa37a0
movb (%r15,%rsi), %r15b
leaq 0x25660f(%rip), %rsi    # 0x27b140
testb %r15b, (%r14,%rsi)
je 0x24b93
leaq 0x256682(%rip), %rsi    # 0x27b1c0
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 0x24b8a
movzbl (%r14,%rbp), %esi
addq %r8, %rsi
addl (%rcx,%rsi,4), %r12d
cmpl %r9d, %r12d
jge 0x24b8a
movzbl 0x2(%r14,%rbp), %esi
addq %rdi, %rsi
leaq 0x256344(%rip), %r14    # 0x27aec0
addl (%r14,%rsi,4), %r12d
cmpl %r9d, %r12d
cmovll %r12d, %r9d
cmovll %ebx, %eax
movl (%r13,%r15,4), %ebx
testl %ebx, %ebx
jns 0x24b42
subq $0x1, %r11
jae 0x24afe
testl %eax, %eax
jns 0x24c06
movl $0xff, %r9d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0x256cf6(%rip), %rbx    # 0x27b8af
movslq %r10d, %r10
movslq %r8d, %r8
movslq %edi, %rdi
leaq 0x2562f7(%rip), %r14    # 0x27aec0
movzbl -0x1(%rbx), %esi
addq %r10, %rsi
movl (%rdx,%rsi,4), %ebp
cmpl %r11d, %ebp
jge 0x24bfc
movzbl -0x2(%rbx), %esi
addq %r8, %rsi
addl (%rcx,%rsi,4), %ebp
cmpl %r11d, %ebp
jge 0x24bfc
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 0x24bc9
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq