// File: /workspace/github_c_makefile_star2_O1/DavidSM64[P]n64graphics_ci/exoquant/exoquant.c
// Function: exq_quantize_ex
void exq_quantize_ex(exq_data *pExq, int nColors, int hq)
{
	int besti;
	exq_float beste;
	exq_histogram *pCur, *pNext;
	int i, j;

	if(nColors > 256)
		nColors = 256;

	if(pExq->numColors == 0)
	{
		pExq->node[0].pHistogram = NULL;
		for(i = 0; i < EXQ_HASH_SIZE; i++)
			for(pCur = pExq->pHash[i]; pCur != NULL; pCur = pCur->pNextInHash)
			{
				pCur->pNext = pExq->node[0].pHistogram;
				pExq->node[0].pHistogram = pCur;
			}
		
		exq_sum_node(&pExq->node[0]);

		pExq->numColors = 1;
	}

	for(i = pExq->numColors; i < nColors; i++)
	{
		beste = 0;
		besti = 0;
		for(j = 0; j < i; j++)
			if(pExq->node[j].vdif >= beste)
			{
				beste = pExq->node[j].vdif;
				besti = j;
			}

//		printf("node %d: %d, %f\n", besti, pExq->node[besti].num, beste);

		pCur = pExq->node[besti].pHistogram;
		pExq->node[besti].pHistogram = NULL;
		pExq->node[i].pHistogram = NULL;
		while(pCur != NULL && pCur != pExq->node[besti].pSplit)
		{
			pNext = pCur->pNext;
			pCur->pNext = pExq->node[i].pHistogram;
			pExq->node[i].pHistogram = pCur;
			pCur = pNext;
		}

		while(pCur != NULL)
		{
			pNext = pCur->pNext;
			pCur->pNext = pExq->node[besti].pHistogram;
			pExq->node[besti].pHistogram = pCur;
			pCur = pNext;
		}

		exq_sum_node(&pExq->node[besti]);
		exq_sum_node(&pExq->node[i]);

		pExq->numColors = i + 1;
		if(hq)
			exq_optimize_palette(pExq, 1);
	}

	pExq->optimized = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, -0x2c(%rbp)
movq %rdi, %r14
movl $0x100, %r12d           # imm = 0x100
cmpl %r12d, %esi
cmovll %esi, %r12d
cmpl $0x0, 0x86800(%rdi)
jne 0x17e0
movq $0x0, 0x80058(%r14)
xorl %eax, %eax
movq (%r14,%rax,8), %rcx
testq %rcx, %rcx
je 0x17be
movq 0x80058(%r14), %rdx
movq %rdx, 0x60(%rcx)
movq %rcx, 0x80058(%r14)
movq %rcx, %rdx
movq 0x68(%rcx), %rsi
movq %rsi, %rcx
testq %rsi, %rsi
jne 0x17a4
incq %rax
cmpq $0x10000, %rax          # imm = 0x10000
jne 0x1794
leaq 0x80000(%r14), %rdi
callq 0x1912
movl $0x1, 0x86800(%r14)
movslq 0x86800(%r14), %r13
cmpl %r12d, %r13d
jge 0x18e9
leaq 0x80000(%r14), %rbx
leaq 0x80040(%r14), %rax
movq %rax, -0x38(%rbp)
testq %r13, %r13
jle 0x1847
movl %r13d, %eax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movq -0x38(%rbp), %rdx
xorl %esi, %esi
movsd (%rdx), %xmm1
ucomisd %xmm0, %xmm1
cmovael %ecx, %esi
movapd %xmm0, %xmm2
cmpnlesd %xmm1, %xmm2
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm2, %xmm0
incq %rcx
addq $0x68, %rdx
cmpq %rcx, %rax
jne 0x1816
movslq %esi, %rax
jmp 0x1849
xorl %eax, %eax
imulq $0x68, %rax, %rcx
leaq (%rbx,%rcx), %rdi
movq 0x58(%rbx,%rcx), %rax
movq $0x0, 0x58(%rbx,%rcx)
testq %rax, %rax
je 0x1884
movq 0x60(%rdi), %rdx
xorl %ecx, %ecx
movq %rax, %rsi
cmpq %rdx, %rax
je 0x1888
movq 0x60(%rsi), %rax
movq %rcx, 0x60(%rsi)
movq %rsi, %rcx
testq %rax, %rax
jne 0x186a
jmp 0x188b
xorl %ecx, %ecx
jmp 0x188b
movq %rsi, %rax
imulq $0x68, %r13, %r15
addq %rbx, %r15
movq %rcx, 0x58(%r15)
testq %rax, %rax
je 0x18b6
movq 0x58(%rdi), %rcx
movq %rax, %rdx
movq 0x60(%rax), %rax
movq %rcx, 0x60(%rdx)
movq %rdx, %rcx
testq %rax, %rax
jne 0x189f
movq %rdx, 0x58(%rdi)
callq 0x1912
movq %r15, %rdi
callq 0x1912
incq %r13
movl %r13d, 0x86800(%r14)
cmpl $0x0, -0x2c(%rbp)
je 0x18e0
movq %r14, %rdi
movl $0x1, %esi
callq 0x1f97
cmpl %r13d, %r12d
jne 0x1802
movl $0x0, 0x86808(%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq