// File: /workspace/github_c_makefile_star2_O2/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 0x49dd8(%rip), %r8      # 0x6bfb0
movzbl (%rax,%r8), %eax
leaq 0x2528bc(%rip), %rcx    # 0x274aa0
movl 0x120(%rcx,%rax,4), %eax
andl $0x7, %edi
movzbl (%rdi,%r8), %edi
leaq 0x252ab6(%rip), %rdx    # 0x274cb0
movl 0x120(%rdx,%rdi,4), %r9d
cmpl %r9d, %eax
cmovll %eax, %r9d
andl $0x7, %esi
movzbl (%rsi,%r8), %eax
leaq 0x252ca8(%rip), %rsi    # 0x274ec0
movl 0x120(%rsi,%rax,4), %eax
cmpl %eax, %r9d
cmovgel %eax, %r9d
incl %r9d
movabsq $0x40, %rdi
movq %rdi, %r8
subq %r10, %r8
movq %rdi, %r10
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 0x253349(%rip), %rbp    # 0x2755b0
leaq 0x253642(%rip), %r13    # 0x2758b0
testl %r11d, %r11d
js 0x22317
movl %r11d, %ebx
leaq 0x252e4f(%rip), %rsi    # 0x2750d0
movslq (%rsi,%rbx,4), %rbx
addq -0x8(%rsp), %rbx
movl %ebx, %r14d
sarl $0x3, %r14d
movslq %r14d, %r14
movl %ebx, %r15d
andl $0x7, %r15d
leaq 0x7b4fe(%rip), %rsi     # 0x9d7a0
movb (%r15,%rsi), %r15b
leaq 0x252e93(%rip), %rsi    # 0x275140
testb %r15b, (%r14,%rsi)
je 0x2230f
leaq 0x252f06(%rip), %rsi    # 0x2751c0
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 0x22306
movzbl (%r14,%rbp), %esi
addq %r8, %rsi
addl (%rcx,%rsi,4), %r12d
cmpl %r9d, %r12d
jge 0x22306
movzbl 0x2(%r14,%rbp), %esi
addq %rdi, %rsi
leaq 0x252bc8(%rip), %r14    # 0x274ec0
addl (%r14,%rsi,4), %r12d
cmpl %r9d, %r12d
cmovll %r12d, %r9d
cmovll %ebx, %eax
movl (%r13,%r15,4), %ebx
testl %ebx, %ebx
jns 0x222be
decl %r11d
jmp 0x2226e
testl %eax, %eax
js 0x22326
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl $0xff, %r11d
leaq 0x253571(%rip), %rbx    # 0x2758af
movslq %r10d, %r10
movslq %r8d, %r8
movslq %edi, %rdi
leaq 0x252b72(%rip), %r14    # 0x274ec0
testl %r11d, %r11d
js 0x2231b
movzbl -0x1(%rbx), %esi
addq %r10, %rsi
movl (%rdx,%rsi,4), %ebp
cmpl %r9d, %ebp
jge 0x22386
movzbl -0x2(%rbx), %esi
addq %r8, %rsi
addl (%rcx,%rsi,4), %ebp
cmpl %r9d, %ebp
jge 0x22386
movzbl (%rbx), %esi
addq %rdi, %rsi
addl (%r14,%rsi,4), %ebp
cmpl %r9d, %ebp
cmovll %ebp, %r9d
cmovll %r11d, %eax
decl %r11d
addq $-0x3, %rbx
jmp 0x2234e