// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movss %xmm0, 0x4(%rsp)
movq %rdi, %rbx
movq %rsi, 0x50(%rsp)
movq %rdx, 0x10(%rsp)
callq 0x24be8
movl 0x8(%rbx), %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
callq 0x4400
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %r15d
movl $0x1, %esi
callq 0xa6ed
movq %rbx, 0x58(%rsp)
movl 0x8(%rbx), %eax
xorl %r14d, %r14d
movl $0x0, %ebx
testl %eax, %eax
je 0x2517a
movq 0x58(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x50(%rsp), %rdx
movsd 0x4(%rdx), %xmm0
movss (%rdx), %xmm1
movaps %xmm1, %xmm2
shufps $0xd4, %xmm0, %xmm2     # xmm2 = xmm2[0,1],xmm0[1,3]
shufps $0xe2, %xmm0, %xmm2     # xmm2 = xmm2[2,0],xmm0[2,3]
negl %eax
xorl %edx, %edx
movsd 0x120d8(%rip), %xmm3    # 0x37118
xorps %xmm4, %xmm4
xorl %ebx, %ebx
movl %eax, %esi
addl %r15d, %esi
movl %r15d, %esi
cmovel %edx, %esi
leal -0x1(%r15), %edi
leaq (%rsi,%rsi,2), %r8
movss (%rcx,%r8,4), %xmm7
movslq %edi, %rsi
leaq (%rsi,%rsi,2), %rdi
movss (%rcx,%rdi,4), %xmm5
subss %xmm5, %xmm7
movslq %ebx, %rsi
leaq (%rsi,%rsi,4), %rsi
leaq (%r12,%rsi,4), %rsi
movsd 0x4(%rcx,%r8,4), %xmm8
movsd 0x4(%rcx,%rdi,4), %xmm6
subps %xmm6, %xmm8
movaps %xmm7, %xmm9
shufps $0xd4, %xmm8, %xmm9     # xmm9 = xmm9[0,1],xmm8[1,3]
shufps $0x52, %xmm8, %xmm9     # xmm9 = xmm9[2,0],xmm8[1,1]
mulps %xmm0, %xmm9
movaps %xmm1, %xmm10
mulss %xmm8, %xmm10
mulps %xmm2, %xmm8
subps %xmm9, %xmm8
mulss %xmm0, %xmm7
subss %xmm10, %xmm7
movaps %xmm8, %xmm9
mulps %xmm8, %xmm9
shufps $0x55, %xmm9, %xmm9     # xmm9 = xmm9[1,1,1,1]
movaps %xmm8, %xmm10
mulss %xmm8, %xmm10
addss %xmm9, %xmm10
movaps %xmm7, %xmm9
mulss %xmm7, %xmm9
addss %xmm10, %xmm9
cvtss2sd %xmm9, %xmm9
sqrtsd %xmm9, %xmm9
ucomisd %xmm9, %xmm3
jbe 0x25100
movl $0x0, 0x8(%rsi)
movq $0x0, (%rsi)
jmp 0x25163
cvtps2pd %xmm8, %xmm8
cvtss2sd %xmm7, %xmm7
divsd %xmm9, %xmm7
cvtsd2ss %xmm7, %xmm7
xorps %xmm10, %xmm10
cvtsd2ss %xmm9, %xmm10
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
divpd %xmm9, %xmm8
cvtpd2ps %xmm8, %xmm8
movss %xmm7, 0x8(%rsi)
movlpd %xmm8, (%rsi)
ucomiss %xmm4, %xmm10
jne 0x2513b
jnp 0x25163
mulss %xmm8, %xmm5
shufps $0x55, %xmm8, %xmm8     # xmm8 = xmm8[1,1,1,1]
mulss %xmm6, %xmm8
addss %xmm5, %xmm8
shufps $0x55, %xmm6, %xmm6     # xmm6 = xmm6[1,1,1,1]
mulss %xmm7, %xmm6
addss %xmm8, %xmm6
movss %xmm6, 0xc(%rsi)
incl %ebx
leal (%rax,%r15), %esi
incl %esi
movl %r15d, %edi
incl %edi
movl %edi, %r15d
cmpl $0x1, %esi
jne 0x25045
movq 0x10(%rsp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp), %xmm0
addsd 0x1545f(%rip), %xmm0    # 0x3a5f8
movsd %xmm0, 0x88(%rsp)
movss 0x8(%rax), %xmm0
movss %xmm0, 0xc(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
movaps 0x15523(%rip), %xmm1    # 0x3a6e0
movaps %xmm0, 0x90(%rsp)
xorps %xmm0, %xmm1
movaps %xmm1, 0xa0(%rsp)
movl %ebx, %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %r15
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
leaq 0x70(%rsp), %r13
movl %ebx, 0x1c(%rsp)
movl $0x20, %edi
callq 0x42e0
movq %rax, %rbp
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0xc1f2
testl %ebx, %ebx
jle 0x2525c
xorl %ebx, %ebx
cmpl $0x0, 0x8(%rbp)
je 0x252eb
movups (%r12,%rbx), %xmm0
movaps %xmm0, 0x70(%rsp)
movl 0x10(%r12,%rbx), %eax
movl %eax, 0x80(%rsp)
movss 0x7c(%rsp), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x7c(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
movss 0x157e6(%rip), %xmm0    # 0x3aa34
callq 0xcf28
addq $0x14, %rbx
cmpq %rbx, %r15
jne 0x2520c
cmpl $0x0, 0x8(%rbp)
je 0x252eb
movq 0x10(%rsp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x30(%rsp)
movsd %xmm0, 0x40(%rsp)
movss 0x8(%rax), %xmm0
movss %xmm0, 0x60(%rsp)
movss %xmm0, 0x48(%rsp)
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x24be8
movsd 0x40(%rsp), %xmm3
movss 0x48(%rsp), %xmm4
movaps %xmm4, %xmm0
subss 0x60(%rsp), %xmm0
movaps %xmm3, %xmm1
subps 0x30(%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 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x25305
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
movaps 0xa0(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x25353
movss %xmm4, 0x20(%rsp)
movaps %xmm3, 0x60(%rsp)
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
testl %r14d, %r14d
je 0x253cb
movaps 0x90(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x20(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movl %r14d, %edi
notl %edi
movsd 0x14e08(%rip), %xmm0    # 0x3a168
callq 0x44d0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rsp), %xmm1
mulsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
incl %r14d
cmpl $0x5, %r14d
movl 0x1c(%rsp), %ebx
jne 0x251ec
movq %r12, %rdi
callq 0x4370
movq 0x10(%rsp), %rax
movaps 0x20(%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
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x20(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
jmp 0x25393
movq %rax, %rbx
movl $0x20, %esi
movq %rbp, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4580