// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdi, %rax
addq $0x30, %rax
pushq $-0x1
popq %r14
pushq $-0x4
popq %rbp
pushq $-0x8
popq %rdi
pushq $-0x18
popq %r13
movq %rax, 0x10(%rsp)
movq (%rax), %rax
incq %r14
addq $0x4, %rbp
addq $0x8, %rdi
addq $0x18, %r13
testq %rax, %rax
jne 0xdd09
callq 0x4450
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %r12
movl %r12d, %esi
callq 0x9517
movq %r13, %rdi
callq 0x4450
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl %r12d, %esi
callq 0x9517
movq %r14, %rdi
callq 0x4450
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl %r12d, %esi
callq 0x9517
movq %rbp, %rdi
callq 0x4450
movq %rax, %rbp
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl %r12d, %esi
callq 0x9517
movq %rbx, 0x8(%rsp)
leaq 0x10(%rbx), %rcx
leaq 0x1(%r14), %rax
xorl %edx, %edx
leaq 0x20bcb99(%rip), %rsi   # 0x20ca940
movq 0x10(%rsp), %rdi
movq (%rdi), %rdi
testq %rdi, %rdi
je 0xdde3
movq %rdi, (%r15,%rdx,8)
movb $0x0, (%r13,%rdx)
movslq 0x8(%rdi), %r8
shlq $0x6, %r8
movaps (%r8,%rsi), %xmm0
movups %xmm0, -0x10(%rcx)
movsd 0x10(%r8,%rsi), %xmm0
movsd %xmm0, (%rcx)
incq %rdx
addq $0x18, %rcx
jmp 0xddac
movq 0x8(%rsp), %rcx
addq $0x10, %rcx
xorl %edx, %edx
movapd 0xe45a(%rip), %xmm0     # 0x1c250
movapd 0xe652(%rip), %xmm1     # 0x1c450
movsd 0xb27a(%rip), %xmm2     # 0x19080
cmpq %rdx, %r14
je 0xde84
movq %rcx, %r8
movq %r14, %r9
xorl %r10d, %r10d
pushq $-0x1
popq %r11
subq $0x1, %r9
jb 0xde72
cmpb $0x0, (%r13,%r10)
jne 0xde69
movupd -0x10(%r8), %xmm3
andpd %xmm0, %xmm3
cmpltpd %xmm1, %xmm3
movmskpd %xmm3, %r12d
movl %r12d, %ebx
shrb %bl
andl $0x1, %r12d
movsd (%r8), %xmm3
andpd %xmm0, %xmm3
xorl %esi, %esi
ucomisd %xmm3, %xmm2
seta %sil
movzbl %bl, %ebx
addl %r12d, %esi
addl %ebx, %esi
cmpl %r11d, %esi
cmovgl %r10d, %edi
cmovgl %esi, %r11d
incq %r10
addq $0x18, %r8
jmp 0xde18
movl %edi, (%rbp,%rdx,4)
movslq %edi, %rsi
movb $0x1, (%r13,%rsi)
incq %rdx
jmp 0xde06
pushq $-0x1
popq %rdx
movq %rbp, %rcx
subq $0x1, %rax
jb 0xdec2
leaq 0x1(%rdx), %rsi
cmpq %r14, %rsi
jae 0xdea2
movslq (%rcx), %rdi
movq (%r15,%rdi,8), %rdi
jmp 0xdea4
xorl %edi, %edi
movq 0x10(%rsp), %r8
testq %rdx, %rdx
js 0xdeb6
movslq -0x4(%rcx), %rdx
movq (%r15,%rdx,8), %r8
movq %rdi, (%r8)
addq $0x4, %rcx
movq %rsi, %rdx
jmp 0xde8a
movq %r15, %rdi
callq 0x43b0
movq 0x8(%rsp), %rdi
callq 0x43b0
movq %r13, %rdi
callq 0x43b0
movq %rbp, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43b0