// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/mathutil.cpp
// Function: snap_to_winding(Winding const&, dplane_t const&, float*)
void			snap_to_winding(const Winding& w, const dplane_t& plane, vec_t* const point)
{
	int				numpoints;
	int				x;
	vec_t			*p1, *p2;
	vec3_t			delta;
	vec3_t			normal;
	vec_t			dist;
	vec_t			dot1, dot2, dot;
	vec3_t			bestpoint;
	vec_t			bestdist;
	bool			in;

	numpoints = w.m_NumPoints;

	in = true;
	for (x = 0; x < numpoints; x++)
	{
		p1 = w.m_Points[x];
		p2 = w.m_Points[(x + 1) % numpoints];
		VectorSubtract (p2, p1, delta);
		CrossProduct (delta, plane.normal, normal);
		dist = DotProduct (point, normal) - DotProduct (p1, normal);

		if (dist < 0.0)
		{
			in = false;

			CrossProduct (plane.normal, normal, delta);
			dot = DotProduct (delta, point);
			dot1 = DotProduct (delta, p1);
			dot2 = DotProduct (delta, p2);
			if (dot1 < dot && dot < dot2)
			{
				dist = dist / DotProduct (normal, normal);
				VectorMA (point, -dist, normal, point);
				return;
			}
		}
	}
	if (in)
	{
		return;
	}

	for (x = 0; x < numpoints; x++)
	{
		p1 = w.m_Points[x];
		VectorSubtract (p1, point, delta);
		dist = DotProduct (delta, plane.normal) / DotProduct (plane.normal, plane.normal);
		VectorMA (delta, -dist, plane.normal, delta);
		dot = DotProduct (delta, delta);

		if (x == 0 || dot < bestdist)
		{
			VectorAdd (point, delta, bestpoint);
			bestdist = dot;
		}
	}
	if (numpoints > 0)
	{
		VectorCopy (bestpoint, point);
	}
	return;
}

// Assembly:
pushq %rbx
movq %rdx, %rcx
movl 0x8(%rdi), %r8d
movq 0x10(%rdi), %rdi
movss 0x8(%rsi), %xmm9
movsd (%rsi), %xmm1
movaps %xmm1, %xmm4
shufps $0x55, %xmm1, %xmm4     # xmm4 = xmm4[1,1],xmm1[1,1]
movaps 0x137ed(%rip), %xmm0    # 0x35290
movaps %xmm9, %xmm2
xorps %xmm0, %xmm2
movaps %xmm2, -0x10(%rsp)
movaps %xmm1, %xmm2
xorps %xmm0, %xmm2
movaps %xmm2, -0x20(%rsp)
movaps %xmm4, %xmm2
xorps %xmm0, %xmm2
movaps %xmm2, -0x30(%rsp)
xorl %esi, %esi
testl %r8d, %r8d
movl $0x0, %r9d
cmovgl %r8d, %r9d
movaps %xmm1, %xmm8
shufps $0x0, %xmm1, %xmm8      # xmm8 = xmm8[0,0],xmm1[0,0]
movss %xmm9, %xmm8            # xmm8 = xmm9[0],xmm8[1,2,3]
movaps %xmm9, -0x60(%rsp)
shufps $0xd4, %xmm1, %xmm9     # xmm9 = xmm9[0,1],xmm1[1,3]
shufps $0x52, %xmm1, %xmm9     # xmm9 = xmm9[2,0],xmm1[1,1]
leaq 0x4(%rdi), %r10
movb $0x1, %r11b
imulq $0xc, %rsi, %rbx
addq %r10, %rbx
cmpq %rsi, %r9
je 0x21d2f
incq %rsi
movl %esi, %eax
cltd
idivl %r8d
imulq $0xc, %rdx, %rax
movss (%rdi,%rax), %xmm5
movss -0x4(%rbx), %xmm6
movaps %xmm5, %xmm11
subss %xmm6, %xmm11
movsd 0x4(%rdi,%rax), %xmm15
movsd (%rbx), %xmm7
movaps %xmm15, %xmm12
subps %xmm7, %xmm12
movaps %xmm11, %xmm0
shufps $0xd4, %xmm12, %xmm0    # xmm0 = xmm0[0,1],xmm12[1,3]
shufps $0x52, %xmm12, %xmm0    # xmm0 = xmm0[2,0],xmm12[1,1]
mulps %xmm9, %xmm0
movaps %xmm1, %xmm2
mulss %xmm12, %xmm2
mulps %xmm8, %xmm12
subps %xmm0, %xmm12
mulss %xmm4, %xmm11
subss %xmm2, %xmm11
movsd (%rcx), %xmm14
movss 0x8(%rcx), %xmm13
movaps %xmm7, %xmm0
shufps $0x10, %xmm12, %xmm0    # xmm0 = xmm0[0,0],xmm12[1,0]
shufps $0xe2, %xmm12, %xmm0    # xmm0 = xmm0[2,0],xmm12[2,3]
movaps %xmm12, %xmm2
shufps $0x11, %xmm14, %xmm2    # xmm2 = xmm2[1,0],xmm14[1,0]
shufps $0xe2, %xmm14, %xmm2    # xmm2 = xmm2[2,0],xmm14[2,3]
mulps %xmm0, %xmm2
movaps %xmm6, %xmm0
movlhps %xmm14, %xmm0           # xmm0 = xmm0[0],xmm14[0]
shufps $0xe2, %xmm14, %xmm0    # xmm0 = xmm0[2,0],xmm14[2,3]
movaps %xmm12, %xmm10
shufps $0x0, %xmm12, %xmm10    # xmm10 = xmm10[0,0],xmm12[0,0]
mulps %xmm0, %xmm10
addps %xmm2, %xmm10
movaps %xmm7, %xmm0
movss %xmm13, %xmm0           # xmm0 = xmm13[0],xmm0[1,2,3]
movaps %xmm11, %xmm3
shufps $0x0, %xmm11, %xmm3     # xmm3 = xmm3[0,0],xmm11[0,0]
mulps %xmm0, %xmm3
addps %xmm10, %xmm3
movaps %xmm3, %xmm0
shufps $0x55, %xmm3, %xmm0     # xmm0 = xmm0[1,1],xmm3[1,1]
subss %xmm0, %xmm3
addq $0xc, %rbx
xorps %xmm0, %xmm0
ucomiss %xmm3, %xmm0
jbe 0x21b00
movaps %xmm12, %xmm0
shufps $0x55, %xmm12, %xmm0    # xmm0 = xmm0[1,1],xmm12[1,1]
movaps %xmm0, %xmm2
mulss -0x10(%rsp), %xmm2
movaps %xmm4, %xmm10
mulss %xmm11, %xmm10
addss %xmm2, %xmm10
movss %xmm10, -0x74(%rsp)
movaps %xmm11, %xmm2
mulss -0x20(%rsp), %xmm2
movaps -0x60(%rsp), %xmm10
mulss %xmm12, %xmm10
addss %xmm2, %xmm10
movaps %xmm12, %xmm2
mulss -0x30(%rsp), %xmm2
movaps %xmm2, -0x70(%rsp)
movaps %xmm1, %xmm2
movaps %xmm0, -0x50(%rsp)
mulss %xmm0, %xmm2
addss -0x70(%rsp), %xmm2
movss %xmm2, -0x70(%rsp)
movaps %xmm14, %xmm2
shufps $0x55, %xmm14, %xmm2    # xmm2 = xmm2[1,1],xmm14[1,1]
mulss %xmm10, %xmm2
movss -0x74(%rsp), %xmm0
mulss %xmm14, %xmm0
addss %xmm2, %xmm0
movss -0x70(%rsp), %xmm2
mulss %xmm13, %xmm2
addss %xmm0, %xmm2
movaps %xmm7, %xmm0
movaps %xmm10, -0x40(%rsp)
mulss %xmm10, %xmm0
mulss -0x74(%rsp), %xmm6
addss %xmm0, %xmm6
movss -0x70(%rsp), %xmm0
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
mulss %xmm0, %xmm7
addss %xmm6, %xmm7
xorl %r11d, %r11d
ucomiss %xmm7, %xmm2
jbe 0x21af9
movaps -0x40(%rsp), %xmm6
mulss %xmm15, %xmm6
movss -0x74(%rsp), %xmm10
mulss %xmm5, %xmm10
addss %xmm6, %xmm10
shufps $0x55, %xmm15, %xmm15   # xmm15 = xmm15[1,1,1,1]
mulss %xmm15, %xmm0
addss %xmm10, %xmm0
xorl %r11d, %r11d
ucomiss %xmm2, %xmm0
jbe 0x21af9
movaps -0x50(%rsp), %xmm1
mulss %xmm1, %xmm1
movaps %xmm12, %xmm0
mulss %xmm12, %xmm0
addss %xmm1, %xmm0
movaps %xmm11, %xmm1
mulss %xmm11, %xmm1
addss %xmm0, %xmm1
xorps 0x13585(%rip), %xmm3    # 0x35290
divss %xmm1, %xmm3
movaps %xmm3, %xmm0
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
mulps %xmm12, %xmm0
addps %xmm14, %xmm0
movlps %xmm0, (%rcx)
mulss %xmm11, %xmm3
addss %xmm13, %xmm3
jmp 0x21e3b
testb $0x1, %r11b
jne 0x21e40
mulss %xmm4, %xmm4
movaps %xmm1, %xmm0
mulss %xmm1, %xmm0
addss %xmm4, %xmm0
movaps -0x60(%rsp), %xmm11
movaps %xmm11, %xmm4
mulss %xmm11, %xmm4
addss %xmm0, %xmm4
imulq $0xc, %r9, %rax
xorl %edx, %edx
movaps 0x13527(%rip), %xmm12   # 0x35290
cmpq %rdx, %rax
je 0x21e33
movss 0x8(%rdi,%rdx), %xmm8
movss 0x8(%rcx), %xmm2
subss %xmm2, %xmm8
movsd (%rdi,%rdx), %xmm10
movsd (%rcx), %xmm7
subps %xmm7, %xmm10
movaps %xmm10, %xmm0
mulps %xmm1, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm10, %xmm9
mulss %xmm1, %xmm9
addss %xmm0, %xmm9
movaps %xmm8, %xmm0
mulss %xmm11, %xmm0
addss %xmm9, %xmm0
xorps %xmm12, %xmm0
divss %xmm4, %xmm0
movaps %xmm0, %xmm9
shufps $0x0, %xmm0, %xmm9      # xmm9 = xmm9[0,0],xmm0[0,0]
mulps %xmm1, %xmm9
addps %xmm10, %xmm9
mulss %xmm11, %xmm0
addss %xmm8, %xmm0
movaps %xmm9, %xmm8
mulps %xmm9, %xmm8
shufps $0x55, %xmm8, %xmm8     # xmm8 = xmm8[1,1,1,1]
movaps %xmm9, %xmm10
mulss %xmm9, %xmm10
addss %xmm8, %xmm10
movaps %xmm0, %xmm8
mulss %xmm0, %xmm8
addss %xmm10, %xmm8
testq %rdx, %rdx
sete %sil
ucomiss %xmm8, %xmm6
seta %r9b
orb %sil, %r9b
je 0x21e2a
addps %xmm9, %xmm7
addss %xmm0, %xmm2
movaps %xmm2, %xmm3
movaps %xmm8, %xmm6
movaps %xmm7, %xmm5
addq $0xc, %rdx
jmp 0x21d69
testl %r8d, %r8d
jle 0x21e40
movlps %xmm5, (%rcx)
movss %xmm3, 0x8(%rcx)
popq %rbx
retq