// File: /workspace/github_cpp_makefile_star2_O2/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 0xa95e
movl 0x58(%r14), %edi
callq 0x2b600
movq %rax, %r15
movslq 0x58(%r14), %rbx
movsd (%r13), %xmm0
movsd %xmm0, 0x70(%rsp)
movss 0x8(%r13), %xmm2
movss %xmm2, 0x78(%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, 0x50(%rsp)
movss %xmm2, 0x7c(%rsp)
movq 0x28(%r14), %rsi
leaq 0x30(%rsp), %rdi
callq 0xaa22
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movss 0x12d5a(%rip), %xmm1    # 0x359c4
movss 0x50(%rsp), %xmm0
callq 0xb5a6
cmpl $-0x2, %eax
jne 0x22d39
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movss 0x12d34(%rip), %xmm0    # 0x359c4
xorl %edx, %edx
callq 0xb632
cmpl $0x0, 0x38(%rsp)
je 0x22d39
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xa53c
imulq $0xc, %rbx, %r12
addq 0x212bc(%rip), %r12     # 0x43f78
cvtps2pd 0x18(%rsp), %xmm0
cvtps2pd (%r15), %xmm1
mulpd 0x13863(%rip), %xmm1    # 0x36530
addpd %xmm0, %xmm1
cvtpd2ps %xmm1, %xmm0
leaq 0x8(%rsp), %rdi
movlpd %xmm0, (%rdi)
cvtss2sd 0x20(%rsp), %xmm0
cvtss2sd 0x8(%r15), %xmm1
mulsd 0x1256e(%rip), %xmm1    # 0x35260
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x8(%rdi)
pushq $0x2
popq %rcx
movss 0xf35a(%rip), %xmm0     # 0x32064
movss 0x12cd6(%rip), %xmm1    # 0x359e8
movq %r12, %rsi
movq %r15, %rdx
callq 0x2b7fc
testq %rax, %rax
je 0x22d5a
movsd 0x8(%rsp), %xmm1
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
jmp 0x22efe
movss 0x8(%r14), %xmm0
movss %xmm0, (%rbp)
movss 0xc(%r14), %xmm0
movss %xmm0, 0x4(%rbp)
movss 0x10(%r14), %xmm0
jmp 0x22f08
movq %rbp, 0x68(%rsp)
movss 0x12561(%rip), %xmm0    # 0x352c8
movss %xmm0, 0x14(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x28(%rsp)
imulq $0xc, %r13, %rbp
addq $0x8, %rbp
movq %r13, %rbx
movaps %xmm1, 0x50(%rsp)
movl 0x38(%rsp), %eax
cmpq %rax, %rbx
jae 0x22ef2
movq 0x40(%rsp), %rcx
leaq 0x1(%rbx), %r13
cmpq %rax, %r13
movl $0x0, %eax
cmovneq %r13, %rax
imulq $0xc, %rax, %rax
movss -0x8(%rcx,%rbp), %xmm0
addss (%rcx,%rax), %xmm0
movss %xmm0, 0x8(%rsp)
movss -0x4(%rcx,%rbp), %xmm1
addss 0x4(%rcx,%rax), %xmm1
movss %xmm1, 0xc(%rsp)
movss (%rcx,%rbp), %xmm2
addss 0x8(%rcx,%rax), %xmm2
movsd 0x18(%rsp), %xmm3
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
addss 0x20(%rsp), %xmm2
addps %xmm3, %xmm0
cvtps2pd %xmm0, %xmm0
mulpd 0x13ce6(%rip), %xmm0    # 0x36ae0
cvtpd2ps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm1
mulsd 0x13cee(%rip), %xmm1    # 0x36af8
cvtsd2ss %xmm1, %xmm1
cvtps2pd %xmm0, %xmm0
cvtps2pd (%r15), %xmm2
mulpd 0x13713(%rip), %xmm2    # 0x36530
addpd %xmm0, %xmm2
cvtpd2ps %xmm2, %xmm0
movlpd %xmm0, 0x8(%rsp)
cvtss2sd %xmm1, %xmm0
cvtss2sd 0x8(%r15), %xmm1
mulsd 0x12423(%rip), %xmm1    # 0x35260
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
pushq $0x1
popq %rcx
movss 0x12b84(%rip), %xmm1    # 0x359e8
callq 0x2b7fc
testq %rax, %rax
je 0x22ec9
movss 0x10(%rsp), %xmm0
movaps %xmm0, %xmm2
subss 0x20(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
movsd 0x18(%rsp), %xmm3
movaps %xmm1, %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
movss 0x14(%rsp), %xmm3
ucomiss %xmm2, %xmm3
setbe %al
andb 0x28(%rsp), %al
cmpb $0x1, %al
jne 0x22eda
addq $0xc, %rbp
incq %rbx
movaps 0x50(%rsp), %xmm1
jmp 0x22d89
movb $0x1, %al
movq %rax, 0x28(%rsp)
movss %xmm0, 0x4(%rsp)
movss %xmm2, 0x14(%rsp)
jmp 0x22d79
testb $0x1, 0x28(%rsp)
movq 0x68(%rsp), %rbp
je 0x22f29
movlps %xmm1, (%rbp)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x8(%rbp)
leaq 0x30(%rsp), %rdi
callq 0xab02
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movss 0x8(%r14), %xmm0
movss %xmm0, (%rbp)
movss 0xc(%r14), %xmm0
movss %xmm0, 0x4(%rbp)
movss 0x10(%r14), %xmm0
movss %xmm0, 0x4(%rsp)
jmp 0x22f02
jmp 0x22f51
jmp 0x22f51
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xab02
movq %rbx, %rdi
callq 0x45a0
nop