// File: /workspace/github_cpp_makefile_star2_O3/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 0xfc8f
callq 0x4420
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movq %r12, %rdi
callq 0x4420
xorl %edi, %edi
movq %rax, (%rsp)
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movq %r14, %rdi
callq 0x4420
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movq %rbp, %rdi
callq 0x4420
movq %rax, %rbp
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movq (%rbx), %rax
testq %rax, %rax
je 0xfd68
movq (%rsp), %rcx
addq $0x10, %rcx
xorl %edx, %edx
leaq 0x20bcbfb(%rip), %rsi   # 0x20cc930
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 0xfd35
testl %r14d, %r14d
je 0xfe11
movq (%rsp), %rax
addq $0x10, %rax
xorl %ecx, %ecx
movapd 0xf56d(%rip), %xmm0     # 0x1f2f0
movapd 0xf755(%rip), %xmm1     # 0x1f4e0
movsd 0xc385(%rip), %xmm2     # 0x1c118
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %rax, %r8
xorl %r9d, %r9d
cmpb $0x0, (%r13,%r9)
jne 0xfdf0
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 0xfda1
movl %edx, (%rbp,%rcx,4)
movslq %edx, %rsi
movb $0x1, (%r13,%rsi)
incq %rcx
cmpq %rcx, %r14
jne 0xfd93
leaq 0x1(%r14), %rax
xorl %ecx, %ecx
cmpq %r14, %rcx
jae 0xfe27
movslq (%rbp,%rcx,4), %rdx
movq (%r15,%rdx,8), %rdx
jmp 0xfe29
xorl %edx, %edx
movq %rcx, %rsi
decq %rsi
movq %rbx, %rsi
js 0xfe3d
movslq -0x4(%rbp,%rcx,4), %rsi
movq (%r15,%rsi,8), %rsi
movq %rdx, (%rsi)
incq %rcx
decq %rax
jne 0xfe17
movq %r15, %rdi
callq 0x4390
movq (%rsp), %rdi
callq 0x4390
movq %r13, %rdi
callq 0x4390
movq %rbp, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4390