// File: /workspace/github_c_makefile_star2_O0/DavidSM64[P]n64graphics_ci/exoquant/exoquant.c
// Function: exq_get_palette
void exq_get_palette(exq_data *pExq, unsigned char *pPal, int nColors)
{
	int i, j;
	exq_float r, g, b, a;
	unsigned char channelMask = 0xff00 >> pExq->numBitsPerChannel;

	if(nColors > pExq->numColors)
		nColors = pExq->numColors;

	if(!pExq->optimized)
		exq_optimize_palette(pExq, 4);

	for(i = 0; i < nColors; i++)
	{
		r = pExq->node[i].avg.r;
		g = pExq->node[i].avg.g;
		b = pExq->node[i].avg.b;
		a = pExq->node[i].avg.a;

		if(pExq->transparency == 1 && a != 0)
		{
			r /= a; g/= a; b/= a;
		}

		pPal[0] = (unsigned char)(r / SCALE_R * 255.9f);
		pPal[1] = (unsigned char)(g / SCALE_G * 255.9f);
		pPal[2] = (unsigned char)(b / SCALE_B * 255.9f);
		pPal[3] = (unsigned char)(a / SCALE_A * 255.9f);

		for(j = 0; j < 3; j++)
			pPal[j] = (pPal[j] + (1 << (8 - pExq->numBitsPerChannel)) / 2) & channelMask;
		pPal += 4;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movl 0x86804(%rax), %ecx
movl $0xff00, %eax           # imm = 0xFF00
sarl %cl, %eax
movb %al, -0x41(%rbp)
movl -0x14(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x86800(%rcx), %eax
jle 0x2ba3
movq -0x8(%rbp), %rax
movl 0x86800(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x86808(%rax)
jne 0x2bbe
movq -0x8(%rbp), %rdi
movl $0x4, %esi
callq 0x2950
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2d95
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x18(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x18(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x28(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x18(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x8(%rbp), %rax
addq $0x80000, %rax          # imm = 0x80000
movslq -0x18(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x8680c(%rax)
jne 0x2ca5
movsd -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2c6c
jp 0x2c6c
jmp 0x2ca5
movsd -0x40(%rbp), %xmm1
movsd -0x28(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x40(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x40(%rbp), %xmm1
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x28(%rbp), %xmm0
movsd 0x1e37e(%rip), %xmm1    # 0x21030
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, (%rax)
movsd -0x30(%rbp), %xmm0
movsd 0x1e369(%rip), %xmm2    # 0x21038
divsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x1(%rax)
movsd -0x38(%rbp), %xmm0
movsd 0x1e34f(%rip), %xmm2    # 0x21040
divsd %xmm2, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x2(%rax)
movsd -0x40(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x3(%rax)
movl $0x0, -0x1c(%rbp)
cmpl $0x3, -0x1c(%rbp)
jge 0x2d7b
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movl $0x8, %ecx
subl 0x86804(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x48(%rbp), %eax
addl %ecx, %eax
movzbl -0x41(%rbp), %ecx
andl %ecx, %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2d23
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2bc5
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)