// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/mathutil.cpp
// Function: snap_to_winding_noedge(Winding const&, dplane_t const&, float*, float, float)
vec_t			snap_to_winding_noedge(const Winding& w, const dplane_t& plane, vec_t* const point, vec_t width, vec_t maxmove)
{
	int pass;
	int numplanes;
	dplane_t *planes;
	int x;
	vec3_t v;
	vec_t newwidth;
	vec_t bestwidth;
	vec3_t bestpoint;

	snap_to_winding (w, plane, point);

	planes = (dplane_t *)malloc (w.m_NumPoints * sizeof (dplane_t));
	hlassume (planes != NULL, assume_NoMemory);
	numplanes = 0;
	for (x = 0; x < w.m_NumPoints; x++)
	{
		VectorSubtract (w.m_Points[(x + 1) % w.m_NumPoints], w.m_Points[x], v);
		CrossProduct (v, plane.normal, planes[numplanes].normal);
		if (!VectorNormalize (planes[numplanes].normal))
		{
			continue;
		}
		planes[numplanes].dist = DotProduct (w.m_Points[x], planes[numplanes].normal);
		numplanes++;
	}
	
	bestwidth = 0;
	VectorCopy (point, bestpoint);
	newwidth = width;

	for (pass = 0; pass < 5; pass++) // apply binary search method for 5 iterations to find the maximal distance that the point can be kept away from all the edges
	{
		bool failed;
		vec3_t newpoint;
		Winding *newwinding;

		failed = true;

		newwinding = new Winding (w);
		for (x = 0; x < numplanes && newwinding->m_NumPoints > 0; x++)
		{
			dplane_t clipplane = planes[x];
			clipplane.dist += newwidth;
			newwinding->Clip (clipplane, false);
		}

		if (newwinding->m_NumPoints > 0)
		{
			VectorCopy (point, newpoint);
			snap_to_winding (*newwinding, plane, newpoint);

			VectorSubtract (newpoint, point, v);
			if (VectorLength (v) <= maxmove + ON_EPSILON)
			{
				failed = false;
			}
		}

		delete newwinding;

		if (!failed)
		{
			bestwidth = newwidth;
			VectorCopy (newpoint, bestpoint);
			if (pass == 0)
			{
				break;
			}
			newwidth += width * pow (0.5, pass + 1);
		}
		else
		{
			newwidth -= width * pow (0.5, pass + 1);
		}
	}

	free (planes);

	VectorCopy (bestpoint, point);
	return bestwidth;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movss %xmm1, 0x20(%rsp)
movss %xmm0, 0x8(%rsp)
movq %rdi, %r12
movq %rsi, 0x48(%rsp)
movq %rdx, 0x18(%rsp)
callq 0x21a7f
movl 0x8(%r12), %eax
imulq $0x14, %rax, %rdi
callq 0x4420
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x92cb
xorl %r14d, %r14d
xorl %eax, %eax
xorl %ebp, %ebp
movq %rax, 0x40(%rsp)
movl %eax, %eax
imulq $0x14, %rax, %rbx
addq %r15, %rbx
movl %ebp, %eax
movss 0x8(%rsp), %xmm0
movl 0x8(%r12), %ecx
cmpl %ecx, %eax
jae 0x21f93
movq 0x10(%r12), %rdx
leal 0x1(%rax), %ebp
cmpl %ecx, %ebp
movl %ebp, %ecx
cmovel %r14d, %ecx
imulq $0xc, %rcx, %rcx
movss (%rdx,%rcx), %xmm0
cltq
imulq $0xc, %rax, %r13
subss (%rdx,%r13), %xmm0
movq 0x48(%rsp), %rax
movss (%rax), %xmm1
movsd 0x4(%rdx,%rcx), %xmm2
movsd 0x4(%rdx,%r13), %xmm3
subps %xmm3, %xmm2
movsd 0x4(%rax), %xmm3
movaps %xmm0, %xmm4
shufps $0xd4, %xmm2, %xmm4     # xmm4 = xmm4[0,1],xmm2[1,3]
shufps $0x52, %xmm2, %xmm4     # xmm4 = xmm4[2,0],xmm2[1,1]
mulps %xmm3, %xmm4
movaps %xmm1, %xmm5
shufps $0xd4, %xmm3, %xmm5     # xmm5 = xmm5[0,1],xmm3[1,3]
shufps $0x52, %xmm3, %xmm5     # xmm5 = xmm5[2,0],xmm3[1,1]
mulps %xmm2, %xmm5
subps %xmm4, %xmm5
movlps %xmm5, (%rbx)
mulss %xmm1, %xmm2
mulss %xmm3, %xmm0
subss %xmm2, %xmm0
movss %xmm0, 0x8(%rbx)
movq %rbx, %rdi
callq 0xc021
ucomiss 0x1012e(%rip), %xmm0    # 0x32070
movss 0x8(%rsp), %xmm0
movl %ebp, %eax
jne 0x21f52
jnp 0x21eb0
movq 0x10(%r12), %rax
movss (%rax,%r13), %xmm0
movss 0x4(%rax,%r13), %xmm1
mulss 0x4(%rbx), %xmm1
mulss (%rbx), %xmm0
movss 0x8(%rax,%r13), %xmm2
mulss 0x8(%rbx), %xmm2
addss %xmm1, %xmm0
addss %xmm0, %xmm2
movss %xmm2, 0xc(%rbx)
movq 0x40(%rsp), %rax
incq %rax
jmp 0x21e9a
movq 0x18(%rsp), %rax
movsd (%rax), %xmm1
movaps %xmm1, 0x80(%rsp)
movss 0x8(%rax), %xmm1
movss %xmm1, 0x14(%rsp)
cvtss2sd 0x20(%rsp), %xmm1
addsd 0x135eb(%rip), %xmm1    # 0x355a8
movsd %xmm1, 0x70(%rsp)
cvtss2sd %xmm0, %xmm1
movaps 0x14552(%rip), %xmm2    # 0x36520
movaps %xmm1, 0x90(%rsp)
xorps %xmm1, %xmm2
movaps %xmm2, 0xa0(%rsp)
xorl %r14d, %r14d
xorps %xmm1, %xmm1
movss %xmm1, 0x10(%rsp)
leaq 0x50(%rsp), %rbp
movq %r12, 0x78(%rsp)
cmpl $0x5, %r14d
je 0x22193
movss %xmm0, 0x8(%rsp)
pushq $0x20
popq %rdi
callq 0x42e0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0xaab8
movq %r15, %r12
movq %r15, %r13
movq 0x40(%rsp), %r15
movl 0x8(%rbx), %eax
subq $0x1, %r15
jb 0x22078
testl %eax, %eax
je 0x22102
movups (%r13), %xmm0
movaps %xmm0, 0x50(%rsp)
movl 0x10(%r13), %eax
movl %eax, 0x60(%rsp)
movss 0x5c(%rsp), %xmm0
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x5c(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movss 0x13957(%rip), %xmm0    # 0x359c4
callq 0xb632
addq $0x14, %r13
jmp 0x22028
testl %eax, %eax
je 0x22102
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd %xmm0, 0x30(%rsp)
movss 0x8(%rax), %xmm0
movss %xmm0, 0xc(%rsp)
movss %xmm0, 0x38(%rsp)
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x21a7f
movsd 0x30(%rsp), %xmm4
movss 0x38(%rsp), %xmm3
movaps %xmm3, %xmm0
subss 0xc(%rsp), %xmm0
movaps %xmm4, %xmm1
subps 0x20(%rsp), %xmm1
mulps %xmm1, %xmm1
cvtps2pd %xmm1, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm2, %xmm0
sqrtsd %xmm0, %xmm0
movsd 0x70(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x2214f
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movaps 0xa0(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r12, %r15
movl %r14d, %edi
notl %edi
movsd 0x12f9f(%rip), %xmm0    # 0x350c8
callq 0x4500
cvtss2sd 0x8(%rsp), %xmm1
mulsd 0x20(%rsp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
incl %r14d
movq 0x78(%rsp), %r12
jmp 0x21ff7
movaps %xmm4, 0x80(%rsp)
movss %xmm3, 0xc(%rsp)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
testl %r14d, %r14d
je 0x221e6
movaps 0x90(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
jmp 0x22119
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movq %r15, %rdi
callq 0x4380
movq 0x18(%rsp), %rax
movaps 0x80(%rsp), %xmm0
movlps %xmm0, (%rax)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
movss 0x8(%rsp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %r15
jmp 0x221ab
movq %rax, %r14
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x42f0
movq %r14, %rdi
callq 0x45a0