// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc(%rsp)
movss %xmm0, 0x4(%rsp)
movq %rdi, %r15
movq %rsi, 0x58(%rsp)
movq %rdx, 0x18(%rsp)
callq 0x25961
movl 0x8(%r15), %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
callq 0x4410
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
movl 0x8(%r15), %eax
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
testl %eax, %eax
je 0x25f16
xorl %edi, %edi
xorpd %xmm4, %xmm4
movsd 0x1237c(%rip), %xmm5    # 0x38110
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movl %r14d, %ecx
movq 0x10(%r15), %rdx
movslq %r14d, %rsi
leal 0x1(%rcx), %r14d
cmpl %eax, %r14d
movl %r14d, %eax
cmovel %edi, %eax
leaq (%rax,%rax,2), %rax
movss (%rdx,%rax,4), %xmm6
leaq (%rsi,%rsi,2), %r13
subss (%rdx,%r13,4), %xmm6
movslq 0x8(%rsp), %rcx
leaq (%rcx,%rcx,4), %rbp
movq 0x58(%rsp), %rcx
movss (%rcx), %xmm0
movsd 0x4(%rdx,%rax,4), %xmm1
movsd 0x4(%rdx,%r13,4), %xmm2
subps %xmm2, %xmm1
movsd 0x4(%rcx), %xmm2
movaps %xmm6, %xmm3
shufps $0xd4, %xmm1, %xmm3     # xmm3 = xmm3[0,1],xmm1[1,3]
shufps $0x52, %xmm1, %xmm3     # xmm3 = xmm3[2,0],xmm1[1,1]
mulps %xmm2, %xmm3
movaps %xmm0, %xmm7
shufps $0xd4, %xmm2, %xmm7     # xmm7 = xmm7[0,1],xmm2[1,3]
shufps $0x52, %xmm2, %xmm7     # xmm7 = xmm7[2,0],xmm2[1,1]
mulps %xmm1, %xmm7
subps %xmm3, %xmm7
movlps %xmm7, (%r12,%rbp,4)
mulss %xmm0, %xmm1
mulss %xmm2, %xmm6
subss %xmm1, %xmm6
movss %xmm6, 0x8(%r12,%rbp,4)
movaps %xmm7, %xmm0
mulps %xmm7, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm7, %xmm1
mulss %xmm7, %xmm1
addss %xmm0, %xmm1
movaps %xmm6, %xmm0
mulss %xmm6, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
jb 0x25e57
sqrtsd %xmm0, %xmm0
jmp 0x25e7e
movaps %xmm6, 0x30(%rsp)
movaps %xmm7, 0x20(%rsp)
callq 0x4240
movaps 0x20(%rsp), %xmm7
movaps 0x30(%rsp), %xmm6
movsd 0x12298(%rip), %xmm5    # 0x38110
xorpd %xmm4, %xmm4
xorl %edi, %edi
leaq (%r12,%rbp,4), %rax
ucomisd %xmm0, %xmm5
jbe 0x25e98
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
jmp 0x25f09
cvtps2pd %xmm7, %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
movlpd %xmm1, (%rax)
xorps %xmm1, %xmm1
cvtss2sd %xmm6, %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0x8(%rax)
cvtsd2ss %xmm0, %xmm0
ucomiss 0x12276(%rip), %xmm0    # 0x38144
jne 0x25ed2
jnp 0x25f09
movq 0x10(%r15), %rcx
movss (%rcx,%r13,4), %xmm0
movss 0x4(%rcx,%r13,4), %xmm1
mulss 0x4(%rax), %xmm1
mulss (%rax), %xmm0
movss 0x8(%rcx,%r13,4), %xmm2
mulss 0x8(%rax), %xmm2
addss %xmm1, %xmm0
addss %xmm0, %xmm2
movss %xmm2, 0xc(%rax)
incl 0x8(%rsp)
movl 0x8(%r15), %eax
cmpl %eax, %r14d
jb 0x25d9f
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x60(%rsp)
movss 0x8(%rax), %xmm0
movss %xmm0, 0x14(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0xc(%rsp), %xmm0
addsd 0x15708(%rip), %xmm0    # 0x3b648
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
movaps 0x157d7(%rip), %xmm1    # 0x3b730
movaps %xmm0, 0x90(%rsp)
xorps %xmm0, %xmm1
movaps %xmm1, 0xa0(%rsp)
movl 0x8(%rsp), %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %r14
xorps %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
movq %rbx, %r13
movl $0x20, %edi
callq 0x42f0
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rbx
movq %r15, %rsi
callq 0xc3be
cmpl $0x0, 0x8(%rsp)
jle 0x25ff7
xorl %r15d, %r15d
cmpl $0x0, 0x8(%rbp)
je 0x25ff7
movups (%r12,%r15), %xmm0
movaps %xmm0, 0x70(%rsp)
movl 0x10(%r12,%r15), %eax
movl %eax, 0x80(%rsp)
movss 0x7c(%rsp), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x7c(%rsp)
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movss 0x15a9b(%rip), %xmm0    # 0x3ba84
callq 0xd0d2
addq $0x14, %r15
cmpq %r15, %r14
jne 0x25fa9
cmpl $0x0, 0x8(%rbp)
je 0x2609c
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movaps %xmm0, 0x30(%rsp)
movsd %xmm0, 0x48(%rsp)
movss 0x8(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movss %xmm0, 0x50(%rsp)
movq %rbp, %rdi
movq 0x58(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x25961
movsd 0x48(%rsp), %xmm1
movss 0x50(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
subss 0x20(%rsp), %xmm0
movaps %xmm1, 0x20(%rsp)
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
ucomisd 0x15288(%rip), %xmm0    # 0x3b308
jb 0x26088
sqrtsd %xmm0, %xmm0
jmp 0x2608d
callq 0x4240
movsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x260b6
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
movaps 0xa0(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x260f5
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
testl %r13d, %r13d
je 0x2613c
movaps 0x90(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq %rbx, %r15
movq %r13, %rbx
movl %ebx, %edi
notl %edi
movsd 0x15061(%rip), %xmm0    # 0x3b168
callq 0x44e0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rsp), %xmm1
mulsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
xorl %eax, %eax
testb %al, %al
jne 0x26168
incl %ebx
cmpl $0x5, %ebx
jne 0x25f81
jmp 0x26168
movb $0x1, %al
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq %rbx, %r15
movq %r13, %rbx
jmp 0x2612b
movq %r12, %rdi
callq 0x4380
movq 0x18(%rsp), %rax
movaps 0x60(%rsp), %xmm0
movlps %xmm0, (%rax)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x8(%rax)
movss 0x10(%rsp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x20, %esi
movq %rbp, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4590