// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlvis/flow.cpp
// Function: BestNormalFromWinding(float const (*) [3], int, float (&) [3])
bool BestNormalFromWinding (const vec3_t *points, int numpoints, vec3_t &normal_out)
{
	const vec3_t *pt1, *pt2, *pt3;
	int k;
	vec3_t d, normal, edge;
	vec_t dist, maxdist;
	if (numpoints < 3)
	{
		return false;
	}
	pt1 = &points[0];
	maxdist = -1;
	for (k = 0; k < numpoints; k++)
	{
		if (&points[k] == pt1)
		{
			continue;
		}
		VectorSubtract (points[k], *pt1, edge);
		dist = DotProduct (edge, edge);
		if (dist > maxdist)
		{
			maxdist = dist;
			pt2 = &points[k];
		}
	}
	if (maxdist <= ON_EPSILON * ON_EPSILON)
	{
		return false;
	}
	maxdist = -1;
	VectorSubtract (*pt2, *pt1, edge);
	VectorNormalize (edge);
	for (k = 0; k < numpoints; k++)
	{
		if (&points[k] == pt1 || &points[k] == pt2)
		{
			continue;
		}
		VectorSubtract (points[k], *pt1, d);
		CrossProduct (edge, d, normal);
		dist = DotProduct (normal, normal);
		if (dist > maxdist)
		{
			maxdist = dist;
			pt3 = &points[k];
		}
	}
	if (maxdist <= ON_EPSILON * ON_EPSILON)
	{
		return false;
	}
	VectorSubtract (*pt3, *pt1, d);
	CrossProduct (edge, d, normal);
	VectorNormalize (normal);
	if (pt3 < pt2)
	{
		VectorScale (normal, -1, normal);
	}
	VectorCopy (normal, normal_out);
	return true;
}

// Assembly:
cmpl $0x3, %esi
jge 0xb612
xorl %eax, %eax
retq
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdx, %rbx
movq %rdi, %r14
movl %esi, %eax
imulq $0xc, %rax, %r13
movss 0x7c15(%rip), %xmm2     # 0x13248
xorl %eax, %eax
cmpq %rax, %r13
je 0xb691
testq %rax, %rax
je 0xb685
movsd (%r14,%rax), %xmm0
movsd (%r14), %xmm3
movss 0x8(%r14,%rax), %xmm1
subss 0x8(%r14), %xmm1
subps %xmm3, %xmm0
movaps %xmm0, %xmm3
mulps %xmm0, %xmm3
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
movaps %xmm0, %xmm4
mulss %xmm0, %xmm4
addss %xmm3, %xmm4
movaps %xmm1, %xmm3
mulss %xmm1, %xmm3
addss %xmm4, %xmm3
ucomiss %xmm2, %xmm3
jbe 0xb685
leaq (%r14,%rax), %r12
jmp 0xb688
movaps %xmm2, %xmm3
addq $0xc, %rax
movaps %xmm3, %xmm2
jmp 0xb635
movss %xmm1, 0x18(%rsp)
movlps %xmm0, 0x10(%rsp)
cvtss2sd %xmm2, %xmm0
movsd 0x7d30(%rip), %xmm1     # 0x133d8
ucomisd %xmm0, %xmm1
jae 0xb89c
movsd (%r12), %xmm0
movsd (%r14), %xmm1
subps %xmm1, %xmm0
leaq 0x10(%rsp), %r15
movlps %xmm0, (%r15)
movss 0x8(%r12), %xmm0
subss 0x8(%r14), %xmm0
movss %xmm0, 0x8(%r15)
movq %r15, %rdi
callq 0xae51
movsd 0x4(%r15), %xmm1
movss (%r15), %xmm0
movaps %xmm0, %xmm3
shufps $0xd4, %xmm1, %xmm3     # xmm3 = xmm3[0,1],xmm1[1,3]
shufps $0x52, %xmm1, %xmm3     # xmm3 = xmm3[2,0],xmm1[1,1]
movaps 0x7b0f(%rip), %xmm4     # 0x13210
xorps %xmm4, %xmm3
movaps %xmm1, %xmm2
xorps %xmm4, %xmm2
movss 0x7b36(%rip), %xmm5     # 0x13248
xorl %eax, %eax
cmpq %rax, %r13
je 0xb7cd
testq %rax, %rax
je 0xb7b8
leaq (%r14,%rax), %rcx
cmpq %r12, %rcx
je 0xb7b8
movss 0x4(%r14,%rax), %xmm4
subss 0x4(%r14), %xmm4
movss (%r14,%rax), %xmm6
movss 0x8(%r14,%rax), %xmm7
unpcklps %xmm6, %xmm7            # xmm7 = xmm7[0],xmm6[0],xmm7[1],xmm6[1]
movss (%r14), %xmm6
movss 0x8(%r14), %xmm8
unpcklps %xmm6, %xmm8            # xmm8 = xmm8[0],xmm6[0],xmm8[1],xmm6[1]
subps %xmm8, %xmm7
movaps %xmm7, %xmm8
shufps $0x0, %xmm7, %xmm8      # xmm8 = xmm8[0,0],xmm7[0,0]
movss %xmm4, %xmm8            # xmm8 = xmm4[0],xmm8[1,2,3]
mulps %xmm3, %xmm8
movaps %xmm1, %xmm6
mulps %xmm7, %xmm6
addps %xmm8, %xmm6
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
mulss %xmm2, %xmm7
mulss %xmm0, %xmm4
addss %xmm7, %xmm4
movaps %xmm6, %xmm7
mulps %xmm6, %xmm7
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
movaps %xmm6, %xmm8
mulss %xmm6, %xmm8
addss %xmm7, %xmm8
movaps %xmm4, %xmm7
mulss %xmm4, %xmm7
addss %xmm8, %xmm7
ucomiss %xmm5, %xmm7
ja 0xb7be
movq %r15, %rcx
movaps %xmm5, %xmm7
addq $0xc, %rax
movq %rcx, %r15
movaps %xmm7, %xmm5
jmp 0xb714
movss %xmm4, 0x8(%rsp)
movlps %xmm6, (%rsp)
cvtss2sd %xmm5, %xmm4
movsd 0x7bf5(%rip), %xmm5     # 0x133d8
ucomisd %xmm4, %xmm5
jae 0xb89c
movss 0x4(%r15), %xmm4
subss 0x4(%r14), %xmm4
movss (%r15), %xmm5
movss 0x8(%r15), %xmm6
unpcklps %xmm5, %xmm6            # xmm6 = xmm6[0],xmm5[0],xmm6[1],xmm5[1]
movss (%r14), %xmm5
movss 0x8(%r14), %xmm7
unpcklps %xmm5, %xmm7            # xmm7 = xmm7[0],xmm5[0],xmm7[1],xmm5[1]
subps %xmm7, %xmm6
mulss %xmm4, %xmm0
unpcklps %xmm6, %xmm4            # xmm4 = xmm4[0],xmm6[0],xmm4[1],xmm6[1]
mulps %xmm3, %xmm4
mulps %xmm6, %xmm1
addps %xmm4, %xmm1
movq %rsp, %r14
movlps %xmm1, (%r14)
shufps $0x55, %xmm6, %xmm6     # xmm6 = xmm6[1,1,1,1]
mulss %xmm2, %xmm6
addss %xmm0, %xmm6
movss %xmm6, 0x8(%r14)
movq %r14, %rdi
callq 0xae51
xorl %eax, %eax
movd 0x8(%r14), %xmm0
cmpq %r12, %r15
setb %cl
movdqa 0x79b1(%rip), %xmm3     # 0x13210
jae 0xb865
pxor %xmm3, %xmm0
movq (%rsp), %xmm1
pxor %xmm1, %xmm3
movb %cl, %al
movd %eax, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
pslld $0x1f, %xmm2
psrad $0x1f, %xmm2
pand %xmm2, %xmm3
pandn %xmm1, %xmm2
por %xmm3, %xmm2
movq %xmm2, (%rbx)
movd %xmm0, 0x8(%rbx)
movb $0x1, %al
jmp 0xb89e
xorl %eax, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq