// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/lerp.cpp
// Function: CalcAdaptedSpot(localtriangulation_t const*, float const*, int, float*)
static bool CalcAdaptedSpot (const localtriangulation_t *lt, const vec3_t position, int surface, vec3_t spot)
	// If the surface formed by the face and its neighbor faces is not flat, the surface should be unfolded onto the face plane
	// CalcAdaptedSpot calculates the coordinate of the unfolded spot on the face plane from the original position on the surface
	// CalcAdaptedSpot(center) = {0,0,0}
	// CalcAdaptedSpot(position on the face plane) = position - center
	// Param position: must include g_face_offset
{
	int i;
	vec_t dot;
	vec3_t surfacespot;
	vec_t dist;
	vec_t dist2;
	vec3_t phongnormal;
	vec_t frac;
	vec3_t middle;
	vec3_t v;
	
	for (i = 0; i < (int)lt->neighborfaces.size (); i++)
	{
		if (lt->neighborfaces[i] == surface)
		{
			break;
		}
	}
	if (i == (int)lt->neighborfaces.size ())
	{
		VectorClear (spot);
		return false;
	}

	VectorSubtract (position, lt->center, surfacespot);
	dot = DotProduct (surfacespot, lt->normal);
	VectorMA (surfacespot, -dot, lt->normal, spot);

	// use phong normal instead of face normal, because phong normal is a continuous function
	GetPhongNormal (surface, position, phongnormal);
	dot = DotProduct (spot, phongnormal);
	if (fabs (dot) > ON_EPSILON)
	{
		frac = DotProduct (surfacespot, phongnormal) / dot;
		frac = qmax (0, qmin (frac, 1)); // to correct some extreme cases
	}
	else
	{
		frac = 0;
	}
	VectorScale (spot, frac, middle);

	dist = VectorLength (spot);
	VectorSubtract (surfacespot, middle, v);
	dist2 = VectorLength (middle) + VectorLength (v);

	if (dist > ON_EPSILON && fabs (dist2 - dist) > ON_EPSILON)
	{
		VectorScale (spot, dist2 / dist, spot);
	}
	return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq 0x58(%rdi), %rax
movq 0x60(%rdi), %r15
subq %rax, %r15
shrq $0x2, %r15
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %r12d
cmovgl %r15d, %r12d
cmpq %rcx, %r12
je 0xd20c
cmpl %edx, (%rax,%rcx,4)
je 0xd209
incq %rcx
jmp 0xd1fa
movl %ecx, %r12d
cmpl %r15d, %r12d
je 0xd426
movss (%rsi), %xmm5
movss 0x4(%rsi), %xmm3
subss 0x38(%rdi), %xmm5
subss 0x3c(%rdi), %xmm3
movss 0x8(%rsi), %xmm4
subss 0x40(%rdi), %xmm4
movss 0x44(%rdi), %xmm0
movss 0x48(%rdi), %xmm1
mulss %xmm3, %xmm1
movaps %xmm5, %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
movss 0x4c(%rdi), %xmm1
mulss %xmm4, %xmm1
addss %xmm2, %xmm1
mulss %xmm1, %xmm0
movss %xmm5, (%rsp)
movaps %xmm5, %xmm2
subss %xmm0, %xmm2
movss %xmm2, (%rbx)
movss 0x48(%rdi), %xmm0
mulss %xmm1, %xmm0
movss %xmm3, 0x8(%rsp)
movaps %xmm3, %xmm2
subss %xmm0, %xmm2
movss %xmm2, 0x4(%rbx)
mulss 0x4c(%rdi), %xmm1
movss %xmm4, 0x4(%rsp)
movaps %xmm4, %xmm0
subss %xmm1, %xmm0
movss %xmm0, 0x8(%rbx)
leaq 0xc(%rsp), %r14
movl %edx, %edi
movq %r14, %rdx
callq 0x17782
movss (%r14), %xmm3
movss 0x4(%r14), %xmm5
movsd (%rbx), %xmm0
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movaps %xmm2, %xmm1
mulss %xmm5, %xmm1
movaps %xmm0, %xmm4
mulss %xmm3, %xmm4
addss %xmm1, %xmm4
movss 0x8(%rbx), %xmm1
movss 0x8(%r14), %xmm6
movaps %xmm1, %xmm7
mulss %xmm6, %xmm7
addss %xmm4, %xmm7
movaps 0x27fae(%rip), %xmm4    # 0x352a0
andps %xmm7, %xmm4
cvtss2sd %xmm4, %xmm8
xorps %xmm4, %xmm4
ucomisd 0x282a2(%rip), %xmm8    # 0x355a8
jbe 0xd331
mulss 0x8(%rsp), %xmm5
mulss (%rsp), %xmm3
addss %xmm5, %xmm3
mulss 0x4(%rsp), %xmm6
addss %xmm3, %xmm6
divss %xmm7, %xmm6
minss 0x24d37(%rip), %xmm6    # 0x32064
maxss %xmm6, %xmm4
movaps %xmm0, %xmm3
mulps %xmm0, %xmm3
cvtps2pd %xmm3, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
addsd %xmm3, %xmm5
movaps %xmm1, %xmm3
mulss %xmm1, %xmm3
cvtss2sd %xmm3, %xmm3
addsd %xmm5, %xmm3
sqrtsd %xmm3, %xmm3
cvtsd2ss %xmm3, %xmm3
cvtss2sd %xmm3, %xmm5
ucomisd 0x2823f(%rip), %xmm5    # 0x355a8
jbe 0xd42e
movaps %xmm0, %xmm5
mulss %xmm4, %xmm5
mulss %xmm4, %xmm2
mulss %xmm1, %xmm4
movss (%rsp), %xmm8
subss %xmm5, %xmm8
movss 0x8(%rsp), %xmm6
subss %xmm2, %xmm6
movss 0x4(%rsp), %xmm7
subss %xmm4, %xmm7
mulss %xmm5, %xmm5
cvtss2sd %xmm5, %xmm5
mulss %xmm2, %xmm2
cvtss2sd %xmm2, %xmm2
addsd %xmm5, %xmm2
mulss %xmm4, %xmm4
cvtss2sd %xmm4, %xmm4
addsd %xmm2, %xmm4
sqrtsd %xmm4, %xmm2
mulss %xmm8, %xmm8
cvtss2sd %xmm8, %xmm4
mulss %xmm6, %xmm6
cvtss2sd %xmm6, %xmm5
addsd %xmm4, %xmm5
mulss %xmm7, %xmm7
cvtss2sd %xmm7, %xmm4
addsd %xmm5, %xmm4
sqrtsd %xmm4, %xmm4
addsd %xmm2, %xmm4
cvtsd2ss %xmm4, %xmm2
movaps %xmm2, %xmm4
subss %xmm3, %xmm4
andps 0x27ea3(%rip), %xmm4    # 0x352a0
cvtss2sd %xmm4, %xmm4
ucomisd 0x2819f(%rip), %xmm4    # 0x355a8
jbe 0xd42e
divss %xmm3, %xmm2
movaps %xmm2, %xmm3
unpcklps %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
mulps %xmm0, %xmm3
movlps %xmm3, (%rbx)
mulss %xmm1, %xmm2
movss %xmm2, 0x8(%rbx)
jmp 0xd42e
andl $0x0, 0x8(%rbx)
andq $0x0, (%rbx)
cmpl %r15d, %r12d
setne %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq