// File: /workspace/github_c_makefile_star2_O2/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, %ebx            # imm = 0x100
cmpl %ebx, %esi
cmovll %esi, %ebx
movl 0x86800(%rdi), %r15d
testl %r15d, %r15d
jne 0x16b7
andq $0x0, 0x80058(%r14)
pushq $0x1
popq %r15
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x10000, %rcx          # imm = 0x10000
je 0x16a0
leaq (%r14,%rcx,8), %rdx
movq (%rdx), %rsi
testq %rsi, %rsi
je 0x169b
movq %rax, 0x60(%rsi)
movq %rsi, 0x80058(%r14)
leaq 0x68(%rsi), %rdx
movq %rsi, %rax
jmp 0x167f
incq %rcx
jmp 0x1672
leaq 0x80000(%r14), %rdi
callq 0x17ce
movl $0x1, 0x86800(%r14)
leaq 0x80000(%r14), %r13
movslq %r15d, %r15
movslq %ebx, %rbx
leaq 0x80040(%r14), %rax
movq %rax, -0x38(%rbp)
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
cmpq %rbx, %r15
jge 0x17a8
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq -0x38(%rbp), %rdx
xorl %esi, %esi
cmpq %rcx, %rax
je 0x171e
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
jmp 0x16f0
movslq %esi, %rax
imulq $0x68, %rax, %rcx
movq 0x58(%r13,%rcx), %rax
andq $0x0, 0x58(%r13,%rcx)
leaq (%rcx,%r13), %rdi
imulq $0x68, %r15, %r12
addq %r13, %r12
xorl %ecx, %ecx
testq %rax, %rax
je 0x1758
cmpq 0x60(%rdi), %rax
je 0x1758
movq 0x60(%rax), %rdx
movq %rcx, 0x60(%rax)
movq %rax, %rcx
movq %rdx, %rax
jmp 0x173d
movq %rcx, 0x58(%r12)
testq %rax, %rax
je 0x1777
movq 0x60(%rax), %rcx
movq 0x58(%rdi), %rdx
movq %rdx, 0x60(%rax)
movq %rax, 0x58(%rdi)
movq %rcx, %rax
jmp 0x175d
callq 0x17ce
movq %r12, %rdi
callq 0x17ce
incq %r15
movl %r15d, 0x86800(%r14)
cmpl $0x0, -0x2c(%rbp)
je 0x16cf
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x1dc3
jmp 0x16cf
andl $0x0, 0x86808(%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq