// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/loadtextures.cpp
// Function: CQ_SelectPartition(cq_node_s*)
static void CQ_SelectPartition (cq_node_t *node)
{
	CQ_VectorClear (node->centerofpoints);
	for (int i = 0; i < node->numpoints; i++)
	{
		CQ_VectorAdd (node->centerofpoints, node->refpoints[i], node->centerofpoints);
	}
	CQ_VectorScale (node->centerofpoints, 1 / (double)node->numpoints, node->centerofpoints);

	node->needsplit = false;
	for (int k = 0; k < CQ_DIM; k++)
	{
		double count;
		double counts[256];
		double sum[CQ_DIM];
		double sums[256][CQ_DIM];

		double bucketsums[256][CQ_DIM];
		int bucketsizes[256];

		const unsigned char (*nodepoints)[CQ_DIM] = node->refpoints;
		const int nodenumpoints = node->numpoints;

		memset (bucketsums, 0, 256 * sizeof (double [CQ_DIM]));
		memset (bucketsizes, 0, 256 * sizeof (int));
		for (int i = 0; i < nodenumpoints; i++)
		{
			int j = nodepoints[i][k];
			bucketsizes[j]++;
			CQ_VectorAdd (bucketsums[j], nodepoints[i], bucketsums[j]);
		}
		
		int min = 256;
		int max = -1;
		count = 0;
		CQ_VectorClear (sum);
		for (int j = 0; j < 256; j++)
		{
			counts[j] = count;
			CQ_VectorCopy (sum, sums[j]);
			count += bucketsizes[j];
			CQ_VectorAdd (sum, bucketsums[j], sum);
			if (bucketsizes[j] > 0)
			{
				if (j < min)
				{
					min = j;
				}
				if (j > max)
				{
					max = j;
				}
			}
		}
		if (max < min)
		{
			Error ("CQ_SelectPartition: internal error");
		}
		// sweep along the axis and find the plane that maximize square error reduction
		for (int j = min + 1; j < max + 1; j++)
		{
			double priority = 0; // the decrease in total square deviation
			priority -= CQ_DotProduct (sum, sum) / count;
			priority += CQ_DotProduct (sums[j], sums[j]) / counts[j];
			double remain[CQ_DIM];
			CQ_VectorSubtract (sum, sums[j], remain); // sums and counts are precise (have no round-off error)
			priority += CQ_DotProduct (remain, remain) / (count - counts[j]);
			if (node->needsplit == false ||
				priority > node->splitpriority + 0.1 ||
				priority >= node->splitpriority - 0.1
				&& fabs (counts[j] - count / 2) < fabs (node->bestsplitter.numpoints[0] - count / 2))
			{
				node->needsplit = true;
				node->splitpriority = priority;
				node->bestsplitter.axis = k;
				node->bestsplitter.dist = j;
				node->bestsplitter.numpoints[0] = counts[j];
				node->bestsplitter.numpoints[1] = count - counts[j];
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3c98, %rsp           # imm = 0x3C98
movq %rdi, %rbx
leaq 0x30(%rdi), %r14
movq %r14, %rdi
callq 0x21725
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq 0x20(%rbx), %rax
cmpq %rax, %r12
jge 0x2133a
movq 0x28(%rbx), %rsi
addq %r15, %rsi
movq %r14, %rdi
movq %r14, %rdx
callq 0x21738
incq %r12
addq $0x3, %r15
jmp 0x21316
cvtsi2sd %eax, %xmm1
movsd 0x13d82(%rip), %xmm0    # 0x350c8
divsd %xmm1, %xmm0
movq %r14, %rdi
movq %r14, %rsi
callq 0x21758
movb $0x0, 0x48(%rbx)
xorl %r13d, %r13d
movq %rbx, 0x38(%rsp)
cmpq $0x3, %r13
je 0x2161b
movq 0x28(%rbx), %r14
movl 0x20(%rbx), %r15d
movl $0x1800, %edx           # imm = 0x1800
leaq 0xc90(%rsp), %rdi
xorl %esi, %esi
callq 0x41a0
movl $0x400, %edx            # imm = 0x400
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x41a0
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
subq $0x1, %r15
jb 0x213d9
movzbl (%r14,%r13), %eax
incl 0x90(%rsp,%rax,4)
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0xc90, %rdi            # imm = 0xC90
movq %r14, %rsi
movq %rdi, %rdx
callq 0x21738
addq $0x3, %r14
jmp 0x213a7
movq %r13, 0x18(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x21725
movl $0x100, %ebp            # imm = 0x100
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x2490(%rsp), %rdx
leaq 0xc90(%rsp), %r14
movq %r14, 0x8(%rsp)
xorl %r12d, %r12d
pushq $-0x1
popq %r13
cmpq $0x100, %r12            # imm = 0x100
je 0x214b6
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x490(%rsp,%r12,8)
movl %r13d, 0x20(%rsp)
movl %ebp, %r13d
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %rdx, %r14
movq %rdx, %rsi
callq 0x21774
movl 0x90(%rsp,%r12,4), %ebx
cvtsi2sd %ebx, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %rbp, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
movq %rbp, %rdx
movl %r13d, %ebp
movl 0x20(%rsp), %r13d
callq 0x2178c
movq %r14, %rdx
cmpl %ebp, %r12d
movl %ebp, %eax
cmovll %r12d, %eax
cmpl %r13d, %r12d
movl %r13d, %ecx
cmovgl %r12d, %ecx
testl %ebx, %ebx
cmovgl %eax, %ebp
cmovgl %ecx, %r13d
incq %r12
addq $0x18, %r15
movq %r15, 0x8(%rsp)
addq $0x18, %rdx
jmp 0x21413
cmpl %ebp, %r13d
movq 0x38(%rsp), %rbx
jge 0x214ce
leaq 0x155c8(%rip), %rdi     # 0x36a8f
xorl %eax, %eax
callq 0x8b2f
movsd 0x10(%rsp), %xmm0
mulsd 0x13d84(%rip), %xmm0    # 0x35260
movslq %ebp, %r14
movslq %r13d, %r15
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0x40(%rsp)
incl %ebp
imulq $0x18, %r14, %r13
leaq 0x24a8(%rsp), %rax
addq %rax, %r13
cmpq %r15, %r14
jge 0x2160e
movl %ebp, %r12d
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x217a9
divsd 0x10(%rsp), %xmm0
xorpd %xmm1, %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq %r13, %rdi
movq %r13, %rsi
callq 0x217a9
movsd 0x498(%rsp,%r14,8), %xmm1
movapd %xmm1, 0x20(%rsp)
divsd %xmm1, %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x70(%rsp), %rbp
movq %rbp, %rdx
callq 0x217c9
movq %rbp, %rdi
movq %rbp, %rsi
movl %r12d, %ebp
callq 0x217a9
movapd 0x20(%rsp), %xmm4
movsd 0x10(%rsp), %xmm1
subsd %xmm4, %xmm1
divsd %xmm1, %xmm0
addsd 0x8(%rsp), %xmm0
cmpb $0x0, 0x48(%rbx)
je 0x215b0
movsd 0x68(%rbx), %xmm2
movapd %xmm2, %xmm3
addsd 0x14066(%rip), %xmm3    # 0x35610
ucomisd %xmm3, %xmm0
jbe 0x215dc
movb $0x1, 0x48(%rbx)
movsd %xmm0, 0x68(%rbx)
movq 0x18(%rsp), %rax
movl %eax, 0x50(%rbx)
movl %ebp, 0x54(%rbx)
movsd %xmm4, 0x58(%rbx)
movsd %xmm1, 0x60(%rbx)
incl %ebp
addq $0x18, %r13
incq %r14
jmp 0x214fd
addsd 0x14f14(%rip), %xmm2    # 0x364f8
ucomisd %xmm2, %xmm0
jb 0x215ce
movapd %xmm4, %xmm2
movhpd 0x58(%rbx), %xmm2       # xmm2 = xmm2[0],mem[0]
subpd 0x40(%rsp), %xmm2
andpd 0x1408f(%rip), %xmm2    # 0x35690
pshufd $0xee, %xmm2, %xmm3     # xmm3 = xmm2[2,3,2,3]
ucomisd %xmm2, %xmm3
ja 0x215b0
jmp 0x215ce
movq 0x18(%rsp), %r13
incq %r13
jmp 0x21361
addq $0x3c98, %rsp           # imm = 0x3C98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq