// File: /workspace/github_c_makefile_star2_O3/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
movl 0x86800(%rdi), %eax
testl %eax, %eax
jne 0x17d3
movq $0x0, 0x80058(%r14)
xorl %ecx, %ecx
xorl %eax, %eax
movq (%r14,%rax,8), %rsi
testq %rsi, %rsi
je 0x17a6
movq %rsi, %rdx
movq %rcx, 0x60(%rsi)
movq %rsi, 0x80058(%r14)
movq 0x68(%rsi), %rsi
movq %rdx, %rcx
testq %rsi, %rsi
jne 0x178a
jmp 0x17a9
movq %rcx, %rdx
incq %rax
movq %rdx, %rcx
cmpq $0x10000, %rax          # imm = 0x10000
jne 0x1781
leaq 0x80000(%r14), %rdi
callq 0x18fc
movl $0x1, 0x86800(%r14)
movl $0x1, %eax
cmpl %r12d, %eax
jge 0x18d3
leaq 0x80000(%r14), %r13
movslq %eax, %rbx
leaq 0x80040(%r14), %rax
movq %rax, -0x38(%rbp)
testq %rbx, %rbx
jle 0x1835
movl %ebx, %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 0x1804
movslq %esi, %rax
jmp 0x1837
xorl %eax, %eax
imulq $0x68, %rax, %rax
leaq (%rax,%r13), %rdi
movq 0x58(%r13,%rax), %rcx
movq $0x0, 0x58(%r13,%rax)
imulq $0x68, %rbx, %r15
addq %r13, %r15
testq %rcx, %rcx
je 0x1879
movq 0x60(%rdi), %rdx
xorl %esi, %esi
movq %rcx, %rax
cmpq %rdx, %rcx
je 0x1881
movq 0x60(%rax), %rcx
movq %rsi, 0x60(%rax)
movq %rax, %rsi
testq %rcx, %rcx
jne 0x185f
jmp 0x187b
xorl %eax, %eax
movq %rax, 0x58(%r15)
jmp 0x18a0
movq %rsi, 0x58(%r15)
movq 0x58(%rdi), %rcx
movq %rax, %rdx
movq 0x60(%rax), %rax
movq %rcx, 0x60(%rdx)
movq %rdx, %rcx
testq %rax, %rax
jne 0x1889
movq %rdx, 0x58(%rdi)
callq 0x18fc
movq %r15, %rdi
callq 0x18fc
incq %rbx
movl %ebx, 0x86800(%r14)
cmpl $0x0, -0x2c(%rbp)
je 0x18ca
movq %r14, %rdi
movl $0x1, %esi
callq 0x1f37
cmpl %ebx, %r12d
jne 0x17f1
movl $0x0, 0x86808(%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq