// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlcsg/brush.cpp
// Function: SortSides(brushhull_t*)
void SortSides (brushhull_t *h)
{
	int numsides;
	bface_t **sides;
	vec3_t *normals;
	bool *isused;
	int i, j;
	int *sorted;
	bface_t *f;
	for (numsides = 0, f = h->faces; f; f = f->next)
	{
		numsides++;
	}
	sides = (bface_t **)malloc (numsides * sizeof (bface_t *));
	hlassume (sides != NULL, assume_NoMemory);
	normals = (vec3_t *)malloc (numsides * sizeof (vec3_t));
	hlassume (normals != NULL, assume_NoMemory);
	isused = (bool *)malloc (numsides * sizeof (bool));
	hlassume (isused != NULL, assume_NoMemory);
	sorted = (int *)malloc (numsides * sizeof (int));
	hlassume (sorted != NULL, assume_NoMemory);
	for (i = 0, f = h->faces; f; i++, f = f->next)
	{
		sides[i] = f;
		isused[i] = false;
		const plane_t *p = &g_mapplanes[f->planenum];
		VectorCopy (p->normal, normals[i]);
	}
	for (i = 0; i < numsides; i++)
	{
		int bestside;
		int bestaxial = -1;
		for (j = 0; j < numsides; j++)
		{
			if (isused[j])
			{
				continue;
			}
			int axial = (fabs (normals[j][0]) < NORMAL_EPSILON) + (fabs (normals[j][1]) < NORMAL_EPSILON) + (fabs (normals[j][2]) < NORMAL_EPSILON);
			if (axial > bestaxial)
			{
				bestside = j;
				bestaxial = axial;
			}
		}
		sorted[i] = bestside;
		isused[bestside] = true;
	}
	for (i = -1; i < numsides; i++)
	{
		*(i >= 0? &sides[sorted[i]]->next: &h->faces) = (i + 1 < numsides? sides[sorted[i + 1]]: NULL);
	}
	free (sides);
	free (normals);
	free (isused);
	free (sorted);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
addq $0x30, %rbx
movq $-0x1, %r14
movq $-0x4, %rbp
movq $-0x8, %rdi
movq $-0x18, %r12
movq %rbx, %rax
movq (%rax), %rax
incq %r14
addq $0x4, %rbp
addq $0x8, %rdi
addq $0x18, %r12
testq %rax, %rax
jne 0x10150
callq 0x4430
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movq %r12, %rdi
callq 0x4430
xorl %edi, %edi
movq %rax, (%rsp)
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movq %r14, %rdi
callq 0x4430
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movq %rbp, %rdi
callq 0x4430
movq %rax, %rbp
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movq (%rbx), %rax
testq %rax, %rax
je 0x10229
movq (%rsp), %rcx
addq $0x10, %rcx
xorl %edx, %edx
leaq 0x20bc74a(%rip), %rsi   # 0x20cc940
movq %rax, (%r15,%rdx,8)
movb $0x0, (%r13,%rdx)
movslq 0x8(%rax), %rdi
shlq $0x6, %rdi
movaps (%rdi,%rsi), %xmm0
movups %xmm0, -0x10(%rcx)
movsd 0x10(%rdi,%rsi), %xmm0
movsd %xmm0, (%rcx)
incq %rdx
movq (%rax), %rax
addq $0x18, %rcx
testq %rax, %rax
jne 0x101f6
testl %r14d, %r14d
je 0x102d2
movq (%rsp), %rax
addq $0x10, %rax
xorl %ecx, %ecx
movapd 0xf0cc(%rip), %xmm0     # 0x1f310
movapd 0xf2b4(%rip), %xmm1     # 0x1f500
movsd 0xbebc(%rip), %xmm2     # 0x1c110
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %rax, %r8
xorl %r9d, %r9d
cmpb $0x0, (%r13,%r9)
jne 0x102b1
movupd -0x10(%r8), %xmm3
andpd %xmm0, %xmm3
cmpltpd %xmm1, %xmm3
movmskpd %xmm3, %r10d
movl %r10d, %r11d
shrb %r11b
andl $0x1, %r10d
movsd (%r8), %xmm3
andpd %xmm0, %xmm3
xorl %r12d, %r12d
ucomisd %xmm3, %xmm2
seta %r12b
movzbl %r11b, %r11d
addl %r10d, %r12d
addl %r11d, %r12d
cmpl %esi, %r12d
cmovgl %r9d, %edx
cmovgl %r12d, %esi
incq %r9
addq $0x18, %r8
decq %rdi
jne 0x10262
movl %edx, (%rbp,%rcx,4)
movslq %edx, %rsi
movb $0x1, (%r13,%rsi)
incq %rcx
cmpq %rcx, %r14
jne 0x10254
leaq 0x1(%r14), %rax
xorl %ecx, %ecx
cmpq %r14, %rcx
jae 0x102e8
movslq (%rbp,%rcx,4), %rdx
movq (%r15,%rdx,8), %rdx
jmp 0x102ea
xorl %edx, %edx
movq %rcx, %rsi
decq %rsi
movq %rbx, %rsi
js 0x102fe
movslq -0x4(%rbp,%rcx,4), %rsi
movq (%r15,%rsi,8), %rsi
movq %rdx, (%rsi)
incq %rcx
decq %rax
jne 0x102d8
movq %r15, %rdi
callq 0x43a0
movq (%rsp), %rdi
callq 0x43a0
movq %r13, %rdi
callq 0x43a0
movq %rbp, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43a0