// File: /workspace/github_c_makefile_star2_O0/DavidSM64[P]n64graphics_ci/exoquant/exoquant.c
// Function: exq_map_image_dither
void exq_map_image_dither(exq_data *pExq, int width, int height,
						  unsigned char *pIn, unsigned char *pOut, int ordered)
{
	int x, y, i, j, d;
	exq_color p, scale, tmp;
	exq_histogram *pHist;
	const exq_float dither_matrix[4] = { -0.375, 0.125, 0.375, -0.125 };

	if(!pExq->optimized)
		exq_optimize_palette(pExq, 4);

	for(y = 0; y < height; y++)
		for(x = 0; x < width; x++)
		{
			if(ordered)
				d = (x & 1) + (y & 1) * 2;
			else
				d = rand() & 3;
			pHist = exq_find_histogram(pExq, pIn);
			p.r = *pIn++ / 255.0f * SCALE_R;
			p.g = *pIn++ / 255.0f * SCALE_G;
			p.b = *pIn++ / 255.0f * SCALE_B;
			p.a = *pIn++ / 255.0f * SCALE_A;

			if(pExq->transparency)
			{
				p.r *= p.a; p.g *= p.a; p.b *= p.a;
			}

			if(pHist == NULL || pHist->ditherScale.r < 0)
			{
				i = exq_find_nearest_color(pExq, &p);
				scale.r = pExq->node[i].avg.r - p.r;
				scale.g = pExq->node[i].avg.g - p.g;
				scale.b = pExq->node[i].avg.b - p.b;
				scale.a = pExq->node[i].avg.a - p.a;
				tmp.r = p.r - scale.r / 3;
				tmp.g = p.g - scale.g / 3;
				tmp.b = p.b - scale.b / 3;
				tmp.a = p.a - scale.a / 3;
				j = exq_find_nearest_color(pExq, &tmp);
				if(i == j)
				{
					tmp.r = p.r - scale.r * 3;
					tmp.g = p.g - scale.g * 3;
					tmp.b = p.b - scale.b * 3;
					tmp.a = p.a - scale.a * 3;
					j = exq_find_nearest_color(pExq, &tmp);
				}
				if(i != j)
				{
					scale.r = (pExq->node[j].avg.r - pExq->node[i].avg.r) * 0.8f;
					scale.g = (pExq->node[j].avg.g - pExq->node[i].avg.g) * 0.8f;
					scale.b = (pExq->node[j].avg.b - pExq->node[i].avg.b) * 0.8f;
					scale.a = (pExq->node[j].avg.a - pExq->node[i].avg.a) * 0.8f;
					if(scale.r < 0) scale.r = -scale.r;
					if(scale.g < 0) scale.g = -scale.g;
					if(scale.b < 0) scale.b = -scale.b;
					if(scale.a < 0) scale.a = -scale.a;
				}
				else
					scale.r = scale.g = scale.b = scale.a = 0;

				if(pHist != NULL)
				{
					pHist->ditherScale.r = scale.r;
					pHist->ditherScale.g = scale.g;
					pHist->ditherScale.b = scale.b;
					pHist->ditherScale.a = scale.a;
				}
			}
			else
			{
				scale.r = pHist->ditherScale.r;
				scale.g = pHist->ditherScale.g;
				scale.b = pHist->ditherScale.b;
				scale.a = pHist->ditherScale.a;
			}

			if(pHist != NULL && pHist->ditherIndex[d] >= 0)
				*pOut++ = (unsigned char)pHist->ditherIndex[d];
			else
			{
				tmp.r = p.r + scale.r * dither_matrix[d];
				tmp.g = p.g + scale.g * dither_matrix[d];
				tmp.b = p.b + scale.b * dither_matrix[d];
				tmp.a = p.a + scale.a * dither_matrix[d];
				*pOut = exq_find_nearest_color(pExq, &tmp);
				if(pHist != NULL)
					pHist->ditherIndex[d] = *pOut;
				pOut++;
			}
		}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq 0x1d9d8(%rip), %rax     # 0x21060
movq %rax, -0xc0(%rbp)
movq 0x1d9d2(%rip), %rax     # 0x21068
movq %rax, -0xb8(%rbp)
movq 0x1d9cc(%rip), %rax     # 0x21070
movq %rax, -0xb0(%rbp)
movq 0x1d9c6(%rip), %rax     # 0x21078
movq %rax, -0xa8(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x86808(%rax)
jne 0x36d4
movq -0x8(%rbp), %rdi
movl $0x4, %esi
callq 0x2950
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x3e6c
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x3e5c
cmpl $0x0, -0x24(%rbp)
je 0x3715
movl -0x28(%rbp), %eax
andl $0x1, %eax
movl -0x2c(%rbp), %ecx
andl $0x1, %ecx
shll %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
jmp 0x3720
callq 0x1290
andl $0x3, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3510
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss 0x1d8b6(%rip), %xmm1    # 0x21008
divss %xmm1, %xmm0
movss 0x1d8a6(%rip), %xmm1    # 0x21004
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss 0x1d87f(%rip), %xmm1    # 0x21008
divss %xmm1, %xmm0
movss 0x1d87b(%rip), %xmm1    # 0x21010
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss 0x1d848(%rip), %xmm1    # 0x21008
divss %xmm1, %xmm0
movss 0x1d840(%rip), %xmm1    # 0x2100c
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movzbl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss 0x1d811(%rip), %xmm1    # 0x21008
divss %xmm1, %xmm0
movss 0x1d801(%rip), %xmm1    # 0x21004
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8680c(%rax)
je 0x384a
movsd -0x40(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x40(%rbp), %xmm0
mulsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x40(%rbp), %xmm0
mulsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x3869
movq -0xa0(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd 0x28(%rax), %xmm0
jbe 0x3ce8
movq -0x8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3180
movzbl %al, %eax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x20(%rax), %xmm0
subsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x28(%rax), %xmm0
subsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x30(%rax), %xmm0
subsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x38(%rax), %xmm0
subsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x58(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
movsd 0x1d732(%rip), %xmm2    # 0x21050
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x50(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
movsd 0x1d710(%rip), %xmm2    # 0x21050
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x48(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
movsd 0x1d6ee(%rip), %xmm2    # 0x21050
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x40(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
movsd 0x1d6cc(%rip), %xmm2    # 0x21050
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3180
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0x3aaa
movsd -0x58(%rbp), %xmm1
movsd -0x78(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x1d674(%rip), %xmm2    # 0x21050
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x50(%rbp), %xmm1
movsd -0x70(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x1d63b(%rip), %xmm2    # 0x21050
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x48(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x1d602(%rip), %xmm2    # 0x21050
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x40(%rbp), %xmm1
movsd -0x60(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x1d5c9(%rip), %xmm2    # 0x21050
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3180
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x3c78
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x20(%rax), %xmm0
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
subsd 0x20(%rax), %xmm0
movsd 0x1d54e(%rip), %xmm1    # 0x21040
mulsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x28(%rax), %xmm0
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
subsd 0x28(%rax), %xmm0
movsd 0x1d509(%rip), %xmm1    # 0x21040
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x30(%rax), %xmm0
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
subsd 0x30(%rax), %xmm0
movsd 0x1d4c4(%rip), %xmm1    # 0x21040
mulsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x38(%rax), %xmm0
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x30(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
subsd 0x38(%rax), %xmm0
movsd 0x1d47f(%rip), %xmm1    # 0x21040
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x78(%rbp), %xmm0
jbe 0x3bf5
movsd -0x78(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x78(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x70(%rbp), %xmm0
jbe 0x3c20
movsd -0x70(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x70(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x68(%rbp), %xmm0
jbe 0x3c4b
movsd -0x68(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x60(%rbp), %xmm0
jbe 0x3c76
movsd -0x60(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x3c98
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x3ce6
movsd -0x78(%rbp), %xmm0
movq -0xa0(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movsd -0x70(%rbp), %xmm0
movq -0xa0(%rbp), %rax
movsd %xmm0, 0x30(%rax)
movsd -0x68(%rbp), %xmm0
movq -0xa0(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movsd -0x60(%rbp), %xmm0
movq -0xa0(%rbp), %rax
movsd %xmm0, 0x40(%rax)
jmp 0x3d2c
movq -0xa0(%rbp), %rax
movsd 0x28(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0xa0(%rbp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0xa0(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0xa0(%rbp), %rax
movsd 0x40(%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x3d6f
movq -0xa0(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpl $0x0, 0x48(%rax,%rcx,4)
jl 0x3d6f
movq -0xa0(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl 0x48(%rax,%rcx,4), %eax
movb %al, %cl
movq -0x20(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x20(%rbp)
movb %cl, (%rax)
jmp 0x3e4c
movsd -0x58(%rbp), %xmm1
movsd -0x78(%rbp), %xmm0
movslq -0x38(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x50(%rbp), %xmm1
movsd -0x70(%rbp), %xmm0
movslq -0x38(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x48(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
movslq -0x38(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x40(%rbp), %xmm1
movsd -0x60(%rbp), %xmm0
movslq -0x38(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3180
movb %al, %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
cmpq $0x0, -0xa0(%rbp)
je 0x3e40
movq -0x20(%rbp), %rax
movzbl (%rax), %edx
movq -0xa0(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl %edx, 0x48(%rax,%rcx,4)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x3e4e
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x36ee
jmp 0x3e5e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x36db
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)