// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/mathutil.cpp
// Function: CalcSightArea_SpotLight(float const*, float const*, Winding const*, float const*, float, float, int, float, float)
vec_t CalcSightArea_SpotLight (const vec3_t receiver_origin, const vec3_t receiver_normal, const Winding *emitter_winding, const vec3_t emitter_normal, vec_t emitter_stopdot, vec_t emitter_stopdot2, int skylevel
	#ifdef HLRAD_DIVERSE_LIGHTING
					, vec_t lighting_power, vec_t lighting_scale
	#endif
					)
{
	// stopdot = cos (cone)
	// stopdot2 = cos (cone2)
	// stopdot >= stopdot2 >= 0
	// ratio = 1.0 , when dot2 >= stopdot
	// ratio = (dot - stopdot2) / (stopdot - stopdot2) , when stopdot > dot2 > stopdot2
	// ratio = 0.0 , when stopdot2 >= dot2
	vec_t area = 0.0;

	int numedges = emitter_winding->m_NumPoints;
	vec3_t *edges = (vec3_t *)malloc (numedges * sizeof (vec3_t));
	hlassume (edges != NULL, assume_NoMemory);
	bool error = false;
	for (int x = 0; x < numedges; x++)
	{
		vec3_t v1, v2, normal;
		VectorSubtract (emitter_winding->m_Points[x], receiver_origin, v1);
		VectorSubtract (emitter_winding->m_Points[(x + 1) % numedges], receiver_origin, v2);
		CrossProduct (v1, v2, normal); // pointing inward
		if (!VectorNormalize (normal))
		{
			error = true;
		}
		VectorCopy (normal, edges[x]);
	}
	if (!error)
	{
		int i, j;
		vec3_t *pnormal;
		vec_t *psize;
		vec_t dot;
		vec_t dot2;
		vec3_t *pedge;
		for (i = 0, pnormal = g_skynormals[skylevel], psize = g_skynormalsizes[skylevel]; i < g_numskynormals[skylevel]; i++, pnormal++, psize++)
		{
			dot = DotProduct (*pnormal, receiver_normal);
			if (dot <= 0)
				continue;
			for (j = 0, pedge = edges; j < numedges; j++, pedge++)
			{
				if (DotProduct (*pnormal, *pedge) <= 0)
				{
					break;
				}
			}
			if (j < numedges)
			{
				continue;
			}
	#ifdef HLRAD_DIVERSE_LIGHTING
			if (lighting_power != 1.0)
			{
				dot = pow (dot, lighting_power);
			}
	#endif
			dot2 = -DotProduct (*pnormal, emitter_normal);
			if (dot2 <= emitter_stopdot2 + NORMAL_EPSILON)
			{
				dot = 0;
			}
			else if (dot2 < emitter_stopdot)
			{
				dot = dot * (dot2 - emitter_stopdot2) / (emitter_stopdot - emitter_stopdot2);
			}
			area += dot * (*psize);
		}
		area = area * 4 * Q_PI; // convert to absolute sphere area
	}
	free (edges);
#ifdef HLRAD_DIVERSE_LIGHTING
	area *= lighting_scale;
#endif
	return area;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movss %xmm3, 0x2c(%rsp)
movss %xmm2, 0x14(%rsp)
movl %r8d, 0x18(%rsp)
movss %xmm1, 0xc(%rsp)
movss %xmm0, 0x10(%rsp)
movq %rcx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r13
movq %rdx, (%rsp)
movslq 0x8(%rdx), %r14
imulq $0xc, %r14, %rdi
callq 0x4420
xorl %edi, %edi
movq %rax, 0x20(%rsp)
testq %rax, %rax
setne %dil
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0x92cb
xorl %r15d, %r15d
testl %r14d, %r14d
movl $0x0, %eax
cmovgl %r14d, %eax
movq %rax, 0x50(%rsp)
imulq $0xc, %rax, %rbx
xorl %r12d, %r12d
cmpq %r15, %rbx
je 0x229eb
movq (%rsp), %rax
movq 0x10(%rax), %rcx
movss (%rcx,%r15), %xmm0
movss (%r13), %xmm2
subss %xmm2, %xmm0
movl %ebp, %eax
cltd
idivl %r14d
imulq $0xc, %rdx, %rax
movss 0x4(%rcx,%rax), %xmm3
movsd 0x4(%rcx,%r15), %xmm1
movsd 0x4(%r13), %xmm4
subps %xmm4, %xmm1
subss %xmm4, %xmm3
movss (%rcx,%rax), %xmm5
movss 0x8(%rcx,%rax), %xmm6
unpcklps %xmm5, %xmm6            # xmm6 = xmm6[0],xmm5[0],xmm6[1],xmm5[1]
shufps $0xd4, %xmm4, %xmm2     # xmm2 = xmm2[0,1],xmm4[1,3]
shufps $0x52, %xmm4, %xmm2     # xmm2 = xmm2[2,0],xmm4[1,1]
subps %xmm2, %xmm6
movaps %xmm0, %xmm2
shufps $0xd4, %xmm1, %xmm2     # xmm2 = xmm2[0,1],xmm1[1,3]
shufps $0x52, %xmm1, %xmm2     # xmm2 = xmm2[2,0],xmm1[1,1]
mulss %xmm3, %xmm0
unpcklps %xmm6, %xmm3            # xmm3 = xmm3[0],xmm6[0],xmm3[1],xmm6[1]
mulps %xmm2, %xmm3
movaps %xmm1, %xmm2
mulps %xmm6, %xmm2
subps %xmm3, %xmm2
movlps %xmm2, 0x30(%rsp)
shufps $0x55, %xmm6, %xmm6     # xmm6 = xmm6[1,1,1,1]
mulss %xmm1, %xmm6
subss %xmm6, %xmm0
movss %xmm0, 0x38(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xc021
ucomiss 0xf6b9(%rip), %xmm0     # 0x32070
setnp %al
sete %cl
andb %al, %cl
orb %cl, %r12b
movsd 0x30(%rsp), %xmm0
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax,%r15)
movss 0x38(%rsp), %xmm0
movss %xmm0, 0x8(%rax,%r15)
addq $0xc, %r15
incl %ebp
jmp 0x22913
xorps %xmm0, %xmm0
testb $0x1, %r12b
jne 0x22ba2
movslq 0x18(%rsp), %r14
leaq 0x4f4bf1c(%rip), %rax   # 0x4f6e920
movq (%rax,%r14,8), %r12
leaq 0x4f4bf61(%rip), %rax   # 0x4f6e970
movq (%rax,%r14,8), %r13
movss 0xc(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
addsd 0xf65b(%rip), %xmm1     # 0x32080
movsd %xmm1, 0x18(%rsp)
movss 0x10(%rsp), %xmm1
subss %xmm0, %xmm1
movss %xmm1, 0x28(%rsp)
xorps %xmm7, %xmm7
xorl %ebp, %ebp
leaq 0x4f4bea9(%rip), %rbx   # 0x4f6e8f0
xorps %xmm8, %xmm8
movss 0x14(%rsp), %xmm1
movq 0x40(%rsp), %r15
cmpl (%rbx,%r14,4), %ebp
jge 0x22b88
movss (%r12), %xmm2
movss 0x4(%r12), %xmm3
movss 0x4(%r15), %xmm0
mulss %xmm3, %xmm0
movss (%r15), %xmm4
mulss %xmm2, %xmm4
addss %xmm0, %xmm4
movss 0x8(%r12), %xmm6
movss 0x8(%r15), %xmm0
mulss %xmm6, %xmm0
addss %xmm4, %xmm0
ucomiss %xmm0, %xmm7
jae 0x22b79
movq 0x50(%rsp), %rax
movq 0x20(%rsp), %rcx
subl $0x1, %eax
jb 0x22ae2
movss 0x4(%rcx), %xmm4
mulss %xmm3, %xmm4
movss (%rcx), %xmm5
mulss %xmm2, %xmm5
addss %xmm4, %xmm5
movss 0x8(%rcx), %xmm4
mulss %xmm6, %xmm4
addss %xmm5, %xmm4
ucomiss %xmm4, %xmm7
jae 0x22b79
addq $0xc, %rcx
jmp 0x22aac
ucomiss 0xf57b(%rip), %xmm1     # 0x32064
jne 0x22aed
jnp 0x22b1b
movss %xmm8, (%rsp)
callq 0x4520
movss (%rsp), %xmm8
xorps %xmm7, %xmm7
movss 0x14(%rsp), %xmm1
movss (%r12), %xmm2
movss 0x4(%r12), %xmm3
movss 0x8(%r12), %xmm6
movq 0x48(%rsp), %rax
mulss 0x4(%rax), %xmm3
mulss (%rax), %xmm2
addss %xmm3, %xmm2
mulss 0x8(%rax), %xmm6
addss %xmm2, %xmm6
xorps 0x12753(%rip), %xmm6    # 0x35290
cvtss2sd %xmm6, %xmm3
xorps %xmm2, %xmm2
movsd 0x18(%rsp), %xmm4
ucomisd %xmm3, %xmm4
jae 0x22b6e
movss 0x10(%rsp), %xmm2
ucomiss %xmm6, %xmm2
jbe 0x22b6b
subss 0xc(%rsp), %xmm6
mulss %xmm6, %xmm0
divss 0x28(%rsp), %xmm0
movaps %xmm0, %xmm2
mulss (%r13), %xmm2
addss %xmm2, %xmm8
incl %ebp
addq $0xc, %r12
addq $0x4, %r13
jmp 0x22a56
mulss 0x13f5f(%rip), %xmm8    # 0x36af0
cvtss2sd %xmm8, %xmm0
mulsd 0xf4ea(%rip), %xmm0     # 0x32088
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
callq 0x4380
movss (%rsp), %xmm0
mulss 0x2c(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq