// File: 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:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movss %xmm0, 0xbc(%rsp)
movss %xmm1, 0xb8(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
callq 0x4a4a0
movq 0xd0(%rsp), %rax
movl 0x8(%rax), %eax
imulq $0x14, %rax, %rdi
callq 0x4430
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movl $0x0, 0xb0(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movq 0xd0(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x4b022
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x30(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movq 0xd0(%rsp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movq 0x30(%rsp), %rax
movl %edx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xa4(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss (%rax), %xmm0
movss %xmm0, 0x98(%rsp)
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x38(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movq 0xd0(%rsp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movq 0x38(%rsp), %rax
movl %edx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xa4(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x9c(%rsp)
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x40(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movq 0xd0(%rsp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movq 0x40(%rsp), %rax
movl %edx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xa4(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0xa0(%rsp)
movss 0x9c(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movss 0x8(%rax), %xmm2
movss 0xa0(%rsp), %xmm1
movq 0xc8(%rsp), %rax
mulss 0x4(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movss 0xa0(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movss (%rax), %xmm2
movss 0x98(%rsp), %xmm1
movq 0xc8(%rsp), %rax
mulss 0x8(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movss 0x98(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movss 0x4(%rax), %xmm2
movss 0x9c(%rsp), %xmm1
movq 0xc8(%rsp), %rax
mulss (%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movq 0xa8(%rsp), %rdi
movslq 0xb0(%rsp), %rax
imulq $0x14, %rax, %rax
addq %rax, %rdi
callq 0x11980
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x4af1d
jp 0x4af1d
jmp 0x4b00c
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xa4(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm1
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm2
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xa4(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xa4(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xa8(%rsp), %rax
movslq 0xb0(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0xc(%rax)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x4ac9c
xorps %xmm0, %xmm0
movss %xmm0, 0x90(%rsp)
movq 0xc0(%rsp), %rax
movss (%rax), %xmm0
movss %xmm0, 0x84(%rsp)
movq 0xc0(%rsp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x88(%rsp)
movq 0xc0(%rsp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x8c(%rsp)
movss 0xbc(%rsp), %xmm0
movss %xmm0, 0x94(%rsp)
movl $0x0, 0xb4(%rsp)
cmpl $0x5, 0xb4(%rsp)
jge 0x4b421
movb $0x1, 0x83(%rsp)
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq 0xd0(%rsp), %rsi
callq 0xea00
jmp 0x4b0c6
movq 0x28(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %ecx
xorl %eax, %eax
cmpl 0xb0(%rsp), %ecx
movb %al, 0x27(%rsp)
jge 0x4b101
movq 0x68(%rsp), %rax
cmpl $0x0, 0x8(%rax)
seta %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x4b10e
jmp 0x4b1a3
movq 0xa8(%rsp), %rax
movslq 0xa4(%rsp), %rcx
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x50(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x58(%rsp)
movss 0x94(%rsp), %xmm0
addss 0x54(%rsp), %xmm0
movss %xmm0, 0x54(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
xorl %edx, %edx
movss 0x22a2a(%rip), %xmm0    # 0x6db90
callq 0x10450
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x4b0db
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x4b480
movq 0x68(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jbe 0x4b2cf
movq 0xc0(%rsp), %rax
movss (%rax), %xmm0
movss %xmm0, 0x74(%rsp)
movq 0xc0(%rsp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, 0x78(%rsp)
movq 0xc0(%rsp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, 0x7c(%rsp)
movq 0x68(%rsp), %rdi
movq 0xc8(%rsp), %rsi
leaq 0x74(%rsp), %rdx
callq 0x4a4a0
movss 0x74(%rsp), %xmm0
movq 0xc0(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x98(%rsp)
movss 0x78(%rsp), %xmm0
movq 0xc0(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x9c(%rsp)
movss 0x7c(%rsp), %xmm0
movq 0xc0(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0xa0(%rsp)
movss 0x98(%rsp), %xmm0
mulss 0x98(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x9c(%rsp), %xmm1
mulss 0x9c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0xa0(%rsp), %xmm1
mulss 0xa0(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movaps %xmm0, %xmm1
movss 0xb8(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x22525(%rip), %xmm2    # 0x6d7e0
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x4b2cd
movb $0x0, 0x83(%rsp)
jmp 0x4b2cf
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x4b2ea
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
testb $0x1, 0x83(%rsp)
jne 0x4b39d
movss 0x94(%rsp), %xmm0
movss %xmm0, 0x90(%rsp)
movss 0x74(%rsp), %xmm0
movss %xmm0, 0x84(%rsp)
movss 0x78(%rsp), %xmm0
movss %xmm0, 0x88(%rsp)
movss 0x7c(%rsp), %xmm0
movss %xmm0, 0x8c(%rsp)
cmpl $0x0, 0xb4(%rsp)
jne 0x4b346
jmp 0x4b421
movss 0xbc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0xb4(%rsp), %edi
addl $0x1, %edi
movsd 0x22185(%rip), %xmm0    # 0x6d4f0
callq 0x4d1a0
movaps %xmm0, %xmm2
movsd 0x10(%rsp), %xmm0
movss 0x94(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x94(%rsp)
jmp 0x4b409
movss 0xbc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0xb4(%rsp), %edi
addl $0x1, %edi
movsd 0x2212e(%rip), %xmm0    # 0x6d4f0
callq 0x4d1a0
movaps %xmm0, %xmm2
movsd 0x8(%rsp), %xmm0
movss 0x94(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x94(%rsp)
jmp 0x4b40b
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x4b08c
movq 0xa8(%rsp), %rdi
callq 0x43a0
movss 0x84(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movss %xmm0, (%rax)
movss 0x88(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0x8c(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movss %xmm0, 0x8(%rax)
movss 0x90(%rsp), %xmm0
addq $0xd8, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x45c0
nopw (%rax,%rax)