// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl -0x8(%rbp), %eax
sarl $0x3, %eax
shll $0x3, %eax
shll $0x3, %eax
movl -0xc(%rbp), %ecx
sarl $0x3, %ecx
shll $0x3, %ecx
addl %ecx, %eax
movl -0x10(%rbp), %ecx
sarl $0x3, %ecx
addl %ecx, %eax
addl $0x40, %eax
addl $0x8, %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x8(%rbp), %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0xa999c(%rip), %rax     # 0xe1134
movzbl (%rax,%rcx), %eax
addl $0x40, %eax
addl $0x8, %eax
movslq %eax, %rcx
leaq 0x280e54(%rip), %rax    # 0x2b8600
movl (%rax,%rcx,4), %eax
movl -0xc(%rbp), %ecx
andl $0x7, %ecx
movslq %ecx, %rdx
leaq 0xa9975(%rip), %rcx     # 0xe1134
movzbl (%rcx,%rdx), %ecx
addl $0x40, %ecx
addl $0x8, %ecx
movslq %ecx, %rdx
leaq 0x28103d(%rip), %rcx    # 0x2b8810
cmpl (%rcx,%rdx,4), %eax
jge 0x37804
movl -0x8(%rbp), %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0xa994c(%rip), %rax     # 0xe1134
movzbl (%rax,%rcx), %eax
addl $0x40, %eax
addl $0x8, %eax
movslq %eax, %rcx
leaq 0x280e04(%rip), %rax    # 0x2b8600
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
jmp 0x3782e
movl -0xc(%rbp), %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0xa9920(%rip), %rax     # 0xe1134
movzbl (%rax,%rcx), %eax
addl $0x40, %eax
addl $0x8, %eax
movslq %eax, %rcx
leaq 0x280fe8(%rip), %rax    # 0x2b8810
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl -0x10(%rbp), %ecx
andl $0x7, %ecx
movslq %ecx, %rdx
leaq 0xa98ed(%rip), %rcx     # 0xe1134
movzbl (%rcx,%rdx), %ecx
addl $0x40, %ecx
addl $0x8, %ecx
movslq %ecx, %rdx
leaq 0x2811c5(%rip), %rcx    # 0x2b8a20
cmpl (%rcx,%rdx,4), %eax
jge 0x37868
movl -0x24(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x37892
movl -0x10(%rbp), %eax
andl $0x7, %eax
movslq %eax, %rcx
leaq 0xa98bc(%rip), %rax     # 0xe1134
movzbl (%rax,%rcx), %eax
addl $0x40, %eax
addl $0x8, %eax
movslq %eax, %rcx
leaq 0x281194(%rip), %rax    # 0x2b8a20
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl $0x40, %eax
subl -0x8(%rbp), %eax
movl %eax, -0x8(%rbp)
movl $0x40, %eax
subl -0xc(%rbp), %eax
movl %eax, -0xc(%rbp)
movl $0x40, %eax
subl -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movl $0x1a, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x379f2
movslq -0x1c(%rbp), %rcx
leaq 0x28134b(%rip), %rax    # 0x2b8c30
movl (%rax,%rcx,4), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
sarl $0x3, %eax
movslq %eax, %rcx
leaq 0x2813a2(%rip), %rax    # 0x2b8ca0
movzbl (%rax,%rcx), %eax
movl -0x14(%rbp), %ecx
andl $0x7, %ecx
movslq %ecx, %rdx
leaq 0xa937e(%rip), %rcx     # 0xe0c90
movzbl (%rcx,%rdx), %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x37922
jmp 0x379e4
movslq -0x14(%rbp), %rcx
leaq 0x2813f3(%rip), %rax    # 0x2b8d20
movzbl (%rax,%rcx), %eax
movl %eax, -0x14(%rbp)
imull $0x3, -0x14(%rbp), %eax
movslq %eax, %rcx
leaq 0x2817ce(%rip), %rax    # 0x2b9110
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %eax
addl -0xc(%rbp), %eax
movslq %eax, %rcx
leaq 0x280eb2(%rip), %rax    # 0x2b8810
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x379c7
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
addl -0x8(%rbp), %eax
movslq %eax, %rcx
leaq 0x280c80(%rip), %rax    # 0x2b8600
movl (%rax,%rcx,4), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x379c5
movq -0x30(%rbp), %rax
movzbl 0x2(%rax), %eax
addl -0x10(%rbp), %eax
movslq %eax, %rcx
leaq 0x28107a(%rip), %rax    # 0x2b8a20
movl (%rax,%rcx,4), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x379c3
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x379c5
jmp 0x379c7
movslq -0x14(%rbp), %rcx
leaq 0x281a3e(%rip), %rax    # 0x2b9410
movl (%rax,%rcx,4), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x37934
jmp 0x379e4
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x378d0
cmpl $0x0, -0x28(%rbp)
jl 0x37a03
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x37acc
movl $0x7fffffff, -0x24(%rbp) # imm = 0x7FFFFFFF
leaq 0x2816ff(%rip), %rax    # 0x2b9110
addq $0x2fd, %rax            # imm = 0x2FD
movq %rax, -0x30(%rbp)
movl $0xff, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jl 0x37ac6
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %eax
addl -0xc(%rbp), %eax
movslq %eax, %rcx
leaq 0x280dcf(%rip), %rax    # 0x2b8810
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x37a51
jmp 0x37aac
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
addl -0x8(%rbp), %eax
movslq %eax, %rcx
leaq 0x280b9b(%rip), %rax    # 0x2b8600
movl (%rax,%rcx,4), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x37a78
jmp 0x37aac
movq -0x30(%rbp), %rax
movzbl 0x2(%rax), %eax
addl -0x10(%rbp), %eax
movslq %eax, %rcx
leaq 0x280f93(%rip), %rax    # 0x2b8a20
movl (%rax,%rcx,4), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x37aa0
jmp 0x37aac
movl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x30(%rbp), %rax
addq $-0x3, %rax
movq %rax, -0x30(%rbp)
jmp 0x37a22
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)