// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
cmpl $0x100, -0xc(%rbp)      # imm = 0x100
jle 0x1922
movl $0x100, -0xc(%rbp)      # imm = 0x100
movq -0x8(%rbp), %rax
cmpl $0x0, 0x86800(%rax)
jne 0x19c4
movq -0x8(%rbp), %rax
movq $0x0, 0x80058(%rax)
movl $0x0, -0x34(%rbp)
cmpl $0x10000, -0x34(%rbp)   # imm = 0x10000
jge 0x19a6
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1999
movq -0x8(%rbp), %rax
movq 0x80058(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x80058(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x1962
jmp 0x199b
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1949
movq -0x8(%rbp), %rdi
addq $0x80000, %rdi          # imm = 0x80000
callq 0x1c40
movq -0x8(%rbp), %rax
movl $0x1, 0x86800(%rax)
movq -0x8(%rbp), %rax
movl 0x86800(%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x1bfc
xorps %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1a4e
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x38(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x40(%rax), %xmm0
ucomisd -0x20(%rbp), %xmm0
jb 0x1a41
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x38(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x40(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x1a43
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x19f3
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x14(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x58(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x14(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x58(%rax)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x58(%rax)
xorl %eax, %eax
cmpq $0x0, -0x28(%rbp)
movb %al, -0x39(%rbp)
je 0x1ad5
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
addq $0x80000, %rcx          # imm = 0x80000
movslq -0x14(%rbp), %rdx
imulq $0x68, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x60(%rcx), %rax
setne %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
testb $0x1, %al
jne 0x1ade
jmp 0x1b35
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x58(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x34(%rbp), %rdx
imulq $0x68, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x58(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x1aa5
jmp 0x1b37
cmpq $0x0, -0x28(%rbp)
je 0x1b92
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x14(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x58(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rcx
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x14(%rbp), %rdx
imulq $0x68, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x58(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x1b37
movq -0x8(%rbp), %rdi
addq $0x80000, %rdi          # imm = 0x80000
movslq -0x14(%rbp), %rax
imulq $0x68, %rax, %rax
addq %rax, %rdi
callq 0x1c40
movq -0x8(%rbp), %rdi
addq $0x80000, %rdi          # imm = 0x80000
movslq -0x34(%rbp), %rax
imulq $0x68, %rax, %rax
addq %rax, %rdi
callq 0x1c40
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x86800(%rax)
cmpl $0x0, -0x10(%rbp)
je 0x1bec
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x2950
jmp 0x1bee
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x19d1
movq -0x8(%rbp), %rax
movl $0x0, 0x86808(%rax)
addq $0x40, %rsp
popq %rbp
retq