// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlrad/mathutil.cpp
// Function: GetAlternateOrigin(float const*, float const*, patch_s const*, float (&) [3])
void GetAlternateOrigin (const vec3_t pos, const vec3_t normal, const patch_t *patch, vec3_t &origin)
{
	const dplane_t *faceplane;
	const vec_t *faceplaneoffset;
	const vec_t *facenormal;
	dplane_t clipplane;
	Winding w;

	faceplane = getPlaneFromFaceNumber (patch->faceNumber);
	faceplaneoffset = g_face_offset[patch->faceNumber];
	facenormal = faceplane->normal;
	VectorCopy (normal, clipplane.normal);
	clipplane.dist = DotProduct (pos, clipplane.normal);
	
	w = *patch->winding;
	if (w.WindingOnPlaneSide (clipplane.normal, clipplane.dist) != SIDE_CROSS)
	{
		VectorCopy (patch->origin, origin);
	}
	else
	{
		w.Clip (clipplane, false);
		if (w.m_NumPoints == 0)
		{
			VectorCopy (patch->origin, origin);
		}
		else
		{
			vec3_t center;
			bool found;
			vec3_t bestpoint;
			vec_t bestdist = -1.0;
			vec3_t point;
			vec_t dist;
			vec3_t v;

			w.getCenter (center);
			found = false;

			VectorMA (center, PATCH_HUNT_OFFSET, facenormal, point);
			if (HuntForWorld (point, faceplaneoffset, faceplane, 2, 1.0, PATCH_HUNT_OFFSET))
			{
				VectorSubtract (point, center, v);
				dist = VectorLength (v);
				if (!found || dist < bestdist)
				{
					found = true;
					VectorCopy (point, bestpoint);
					bestdist = dist;
				}
			}
			if (!found)
			{
				for (int i = 0; i < w.m_NumPoints; i++)
				{
					const vec_t *p1;
					const vec_t *p2;
					p1 = w.m_Points[i];
					p2 = w.m_Points[(i + 1) % w.m_NumPoints];
					VectorAdd (p1, p2, point);
					VectorAdd (point, center, point);
					VectorScale (point, 1.0/3.0, point);
					VectorMA (point, PATCH_HUNT_OFFSET, facenormal, point);
					if (HuntForWorld (point, faceplaneoffset, faceplane, 1, 0.0, PATCH_HUNT_OFFSET))
					{
						VectorSubtract (point, center, v);
						dist = VectorLength (v);
						if (!found || dist < bestdist)
						{
							found = true;
							VectorCopy (point, bestpoint);
							bestdist = dist;
						}
					}
				}
			}

			if (found)
			{
				VectorCopy (bestpoint, origin);
			}
			else
			{
				VectorCopy (patch->origin, origin);
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
leaq 0x30(%rsp), %rdi
callq 0xc05e
movl 0x58(%r14), %edi
callq 0x2f29b
movq %rax, %r15
movslq 0x58(%r14), %rbx
movsd (%r13), %xmm0
movsd %xmm0, 0x58(%rsp)
movss 0x8(%r13), %xmm2
movss %xmm2, 0x60(%rsp)
movss (%r12), %xmm1
mulss %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss 0x4(%r12), %xmm0
mulss 0x8(%r12), %xmm2
addss %xmm0, %xmm1
addss %xmm1, %xmm2
movss %xmm2, 0xc(%rsp)
movss %xmm2, 0x64(%rsp)
movq 0x28(%r14), %rsi
leaq 0x30(%rsp), %rdi
callq 0xc14c
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movss 0x14a34(%rip), %xmm1    # 0x3aa34
movss 0xc(%rsp), %xmm0
callq 0xce80
cmpl $-0x2, %eax
jne 0x2628a
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movss 0x14a0e(%rip), %xmm0    # 0x3aa34
xorl %edx, %edx
callq 0xcf28
cmpl $0x0, 0x38(%rsp)
je 0x2628a
leaq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xbb2c
leaq (%rbx,%rbx,2), %r12
shlq $0x2, %r12
addq 0x20f22(%rip), %r12     # 0x46f78
cvtps2pd 0x20(%rsp), %xmm0
cvtps2pd (%r15), %xmm1
mulpd 0x154d9(%rip), %xmm1    # 0x3b540
addpd %xmm0, %xmm1
cvtpd2ps %xmm1, %xmm0
leaq 0x10(%rsp), %rdi
movlpd %xmm0, (%rdi)
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8(%r15), %xmm1
mulsd 0x1426e(%rip), %xmm1    # 0x3a300
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x8(%rdi)
movss 0x10ffa(%rip), %xmm0    # 0x370a4
movss 0x149a6(%rip), %xmm1    # 0x3aa58
movq %r12, %rsi
movq %r15, %rdx
movl $0x2, %ecx
callq 0x2f451
testq %rax, %rax
je 0x260dc
movsd 0x10(%rsp), %xmm1
movss 0x18(%rsp), %xmm0
movlps %xmm1, (%rbp)
jmp 0x262a6
movl 0x38(%rsp), %eax
testl %eax, %eax
je 0x2628a
movq %rbp, 0x50(%rsp)
movss 0x1428b(%rip), %xmm0    # 0x3a380
movss %xmm0, 0xc(%rsp)
movl $0x8, %ebp
movl $0x1, %ebx
xorl %r13d, %r13d
movq 0x40(%rsp), %rcx
movl %eax, %eax
cmpq %rax, %rbx
movl $0x0, %eax
cmovneq %rbx, %rax
leaq (%rax,%rax,2), %rax
movss -0x8(%rcx,%rbp), %xmm0
addss (%rcx,%rax,4), %xmm0
movss %xmm0, 0x10(%rsp)
movss -0x4(%rcx,%rbp), %xmm1
addss 0x4(%rcx,%rax,4), %xmm1
movss %xmm1, 0x14(%rsp)
movss (%rcx,%rbp), %xmm2
addss 0x8(%rcx,%rax,4), %xmm2
movsd 0x20(%rsp), %xmm3
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
addss 0x28(%rsp), %xmm2
addps %xmm3, %xmm0
cvtps2pd %xmm0, %xmm0
mulpd 0x159b6(%rip), %xmm0    # 0x3bb20
cvtpd2ps %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
mulsd 0x159b3(%rip), %xmm1    # 0x3bb30
cvtsd2ss %xmm1, %xmm1
cvtps2pd %xmm0, %xmm0
cvtps2pd (%r15), %xmm2
mulpd 0x153b0(%rip), %xmm2    # 0x3b540
addpd %xmm0, %xmm2
cvtpd2ps %xmm2, %xmm0
movlpd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8(%r15), %xmm1
mulsd 0x1414a(%rip), %xmm1    # 0x3a300
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movl $0x1, %ecx
movss 0x14876(%rip), %xmm1    # 0x3aa58
callq 0x2f451
testq %rax, %rax
je 0x26258
movsd 0x10(%rsp), %xmm0
movss 0x18(%rsp), %xmm1
movaps %xmm1, %xmm2
subss 0x28(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
movaps %xmm0, %xmm4
subps %xmm3, %xmm4
mulps %xmm4, %xmm4
cvtps2pd %xmm4, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
mulss %xmm2, %xmm2
cvtss2sd %xmm2, %xmm2
addsd %xmm4, %xmm2
sqrtsd %xmm2, %xmm2
cvtsd2ss %xmm2, %xmm2
testb $0x1, %r13b
je 0x26244
movss 0xc(%rsp), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x26258
movb $0x1, %r13b
movss %xmm1, 0x2c(%rsp)
movss %xmm2, 0xc(%rsp)
movaps %xmm0, 0x70(%rsp)
movl 0x38(%rsp), %eax
addq $0xc, %rbp
leaq 0x1(%rbx), %rcx
cmpq %rax, %rbx
movq %rcx, %rbx
jb 0x26108
testb $0x1, %r13b
movq 0x50(%rsp), %rbp
movss 0x2c(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
jne 0x260d3
movss 0x8(%r14), %xmm0
movss %xmm0, (%rbp)
movss 0xc(%r14), %xmm0
movss %xmm0, 0x4(%rbp)
movss 0x10(%r14), %xmm0
movss %xmm0, 0x8(%rbp)
leaq 0x30(%rsp), %rdi
callq 0xc244
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x262cb
jmp 0x262cb
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xc244
movq %rbx, %rdi
callq 0x4580