// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
leaq 0x28(%rsp), %rdi
callq 0xc22a
movl 0x58(%r14), %edi
callq 0x3031e
movq %rax, %r15
movslq 0x58(%r14), %rbx
movsd (%r13), %xmm0
movsd %xmm0, 0x48(%rsp)
movss 0x8(%r13), %xmm1
movss %xmm1, 0x50(%rsp)
movss (%r12), %xmm2
mulss %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulss 0x4(%r12), %xmm0
mulss 0x8(%r12), %xmm1
addss %xmm0, %xmm2
addss %xmm2, %xmm1
movss %xmm1, 0x54(%rsp)
movq 0x28(%r14), %rsi
leaq 0x28(%rsp), %rdi
callq 0xc318
leaq 0x48(%rsp), %rsi
movss 0xc(%rsi), %xmm0
leaq 0x28(%rsp), %rdi
movss 0x14c64(%rip), %xmm1    # 0x3ba84
callq 0xd02a
cmpl $-0x2, %eax
jne 0x26f2c
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movss 0x14c44(%rip), %xmm0    # 0x3ba84
xorl %edx, %edx
callq 0xd0d2
cmpl $0x0, 0x30(%rsp)
je 0x26f2c
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xbcc8
leaq (%rbx,%rbx,2), %r12
shlq $0x2, %r12
addq 0x21108(%rip), %r12     # 0x47f78
cvtps2pd 0x10(%rsp), %xmm0
cvtps2pd (%r15), %xmm1
mulpd 0x1573f(%rip), %xmm1    # 0x3c5c0
addpd %xmm0, %xmm1
cvtpd2ps %xmm1, %xmm0
movq %rsp, %rdi
movlpd %xmm0, (%rdi)
xorps %xmm0, %xmm0
cvtss2sd 0x18(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8(%r15), %xmm1
mulsd 0x14456(%rip), %xmm1    # 0x3b300
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x8(%rdi)
movss 0x11276(%rip), %xmm0    # 0x38138
movss 0x14bde(%rip), %xmm1    # 0x3baa8
movq %r12, %rsi
movq %r15, %rdx
movl $0x2, %ecx
callq 0x304d4
testq %rax, %rax
movq %rbp, 0x20(%rsp)
je 0x26f52
movss 0x8(%rsp), %xmm0
subss 0x18(%rsp), %xmm0
movsd (%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
subps %xmm2, %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
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x26f64
sqrtsd %xmm0, %xmm0
jmp 0x26f69
movss 0x8(%r14), %xmm0
movss %xmm0, (%rbp)
movss 0xc(%r14), %xmm0
movss %xmm0, 0x4(%rbp)
movss 0x10(%r14), %xmm0
movss %xmm0, 0x8(%rbp)
jmp 0x2717a
movss 0x14416(%rip), %xmm0    # 0x3b370
movss %xmm0, 0xc(%rsp)
xorl %ebp, %ebp
jmp 0x26f86
callq 0x4240
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movsd (%rsp), %xmm0
movaps %xmm0, 0x60(%rsp)
movb $0x1, %bpl
movss 0x8(%rsp), %xmm0
testb %bpl, %bpl
movss %xmm0, 0x1c(%rsp)
jne 0x2713a
movl 0x30(%rsp), %eax
testl %eax, %eax
je 0x2713a
movl $0x8, %r13d
movl $0x1, %ebx
movq 0x38(%rsp), %rcx
movl %eax, %eax
cmpq %rax, %rbx
movq %rbx, %rax
movl $0x0, %edx
cmoveq %rdx, %rax
leaq (%rax,%rax,2), %rax
movss -0x8(%rcx,%r13), %xmm0
addss (%rcx,%rax,4), %xmm0
movss %xmm0, (%rsp)
movss -0x4(%rcx,%r13), %xmm1
addss 0x4(%rcx,%rax,4), %xmm1
movss %xmm1, 0x4(%rsp)
movss (%rcx,%r13), %xmm2
addss 0x8(%rcx,%rax,4), %xmm2
movsd 0x10(%rsp), %xmm3
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
addps %xmm3, %xmm0
addss 0x18(%rsp), %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
mulsd 0x15bd9(%rip), %xmm1    # 0x3cbf0
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0x8(%rsp)
cvtps2pd %xmm0, %xmm0
mulpd 0x15bb4(%rip), %xmm0    # 0x3cbe0
cvtpd2ps %xmm0, %xmm0
cvtps2pd %xmm0, %xmm2
movlpd %xmm0, (%rsp)
cvtps2pd (%r15), %xmm0
mulpd 0x1557c(%rip), %xmm0    # 0x3c5c0
addpd %xmm2, %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, (%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8(%r15), %xmm1
mulsd 0x14297(%rip), %xmm1    # 0x3b300
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movq %rsp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl $0x1, %ecx
movss 0x14a15(%rip), %xmm1    # 0x3baa8
callq 0x304d4
testq %rax, %rax
je 0x27122
movss 0x8(%rsp), %xmm0
subss 0x18(%rsp), %xmm0
movsd (%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
subps %xmm2, %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 0x14227(%rip), %xmm0    # 0x3b308
jb 0x270e9
sqrtsd %xmm0, %xmm0
jmp 0x270ee
callq 0x4240
cvtsd2ss %xmm0, %xmm0
testb $0x1, %bpl
je 0x27103
movss 0xc(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x27122
movsd (%rsp), %xmm1
movaps %xmm1, 0x60(%rsp)
movb $0x1, %bpl
movss 0x8(%rsp), %xmm1
movss %xmm1, 0x1c(%rsp)
movss %xmm0, 0xc(%rsp)
movl 0x30(%rsp), %eax
addq $0xc, %r13
leaq 0x1(%rbx), %rcx
cmpq %rax, %rbx
movq %rcx, %rbx
jb 0x26fac
testb $0x1, %bpl
je 0x27155
movq 0x20(%rsp), %rax
movaps 0x60(%rsp), %xmm0
movlps %xmm0, (%rax)
movss 0x1c(%rsp), %xmm0
jmp 0x27175
movss 0x8(%r14), %xmm0
movq 0x20(%rsp), %rax
movss %xmm0, (%rax)
movss 0xc(%r14), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x10(%r14), %xmm0
movss %xmm0, 0x8(%rax)
leaq 0x28(%rsp), %rdi
callq 0xc410
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27197
jmp 0x27197
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xc410
movq %rbx, %rdi
callq 0x4590