// File: 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:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movl %esi, 0x54(%rsp)
movq %rdx, 0x48(%rsp)
cmpl $0x3, 0x54(%rsp)
jge 0x117d3
movb $0x0, 0x67(%rsp)
jmp 0x11ce3
movq 0x58(%rsp), %rax
movq %rax, 0x40(%rsp)
movss 0xacb7(%rip), %xmm0     # 0x1c49c
movss %xmm0, (%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x11906
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpq 0x40(%rsp), %rax
jne 0x1181d
jmp 0x118f6
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0x40(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x8(%rsp), %xmm1
movss 0x8(%rsp), %xmm2
movss 0xc(%rsp), %xmm0
mulss 0xc(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x10(%rsp), %xmm0
movss 0x10(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movss 0x4(%rsp), %xmm0
ucomiss (%rsp), %xmm0
jbe 0x118f4
movss 0x4(%rsp), %xmm0
movss %xmm0, (%rsp)
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0x118f6
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x117f2
movss (%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xad29(%rip), %xmm0     # 0x1c640
ucomisd %xmm1, %xmm0
jb 0x11927
movb $0x0, 0x67(%rsp)
jmp 0x11ce3
movss 0xab6d(%rip), %xmm0     # 0x1c49c
movss %xmm0, (%rsp)
movq 0x38(%rsp), %rax
movss (%rax), %xmm0
movq 0x40(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x38(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0xc(%rsp)
movq 0x38(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x10(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x10780
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x11b57
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpq 0x40(%rsp), %rax
je 0x119d0
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpq 0x38(%rsp), %rax
jne 0x119d5
jmp 0x11b47
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0x40(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movss 0xc(%rsp), %xmm0
movss 0x28(%rsp), %xmm2
movss 0x10(%rsp), %xmm1
mulss 0x24(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x10(%rsp), %xmm0
movss 0x20(%rsp), %xmm2
movss 0x8(%rsp), %xmm1
mulss 0x28(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x8(%rsp), %xmm0
movss 0x24(%rsp), %xmm2
movss 0xc(%rsp), %xmm1
mulss 0x20(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x14(%rsp), %xmm1
movss 0x14(%rsp), %xmm2
movss 0x18(%rsp), %xmm0
mulss 0x18(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x1c(%rsp), %xmm0
movss 0x1c(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4(%rsp)
movss 0x4(%rsp), %xmm0
ucomiss (%rsp), %xmm0
jbe 0x11b45
movss 0x4(%rsp), %xmm0
movss %xmm0, (%rsp)
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
jmp 0x11b47
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x11992
movss (%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0xaad8(%rip), %xmm0     # 0x1c640
ucomisd %xmm1, %xmm0
jb 0x11b78
movb $0x0, 0x67(%rsp)
jmp 0x11ce3
movq 0x30(%rsp), %rax
movss (%rax), %xmm0
movq 0x40(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x30(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x30(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x40(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movss 0xc(%rsp), %xmm0
movss 0x28(%rsp), %xmm2
movss 0x10(%rsp), %xmm1
mulss 0x24(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x10(%rsp), %xmm0
movss 0x20(%rsp), %xmm2
movss 0x8(%rsp), %xmm1
mulss 0x28(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x8(%rsp), %xmm0
movss 0x24(%rsp), %xmm2
movss 0xc(%rsp), %xmm1
mulss 0x20(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
leaq 0x14(%rsp), %rdi
callq 0x10780
movq 0x30(%rsp), %rax
cmpq 0x38(%rsp), %rax
jae 0x11caf
movss 0xa821(%rip), %xmm0     # 0x1c49c
mulss 0x14(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0xa80d(%rip), %xmm0     # 0x1c49c
mulss 0x18(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
movss 0xa7f9(%rip), %xmm0     # 0x1c49c
mulss 0x1c(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x14(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, (%rax)
movss 0x18(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0x1c(%rsp), %xmm0
movq 0x48(%rsp), %rax
movss %xmm0, 0x8(%rax)
movb $0x1, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nop