// File: /workspace/github_cpp_makefile_star2_O1/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, 0x24(%rsp)
movss %xmm2, 0x14(%rsp)
movl %r8d, %ebp
movss %xmm1, 0xc(%rsp)
movss %xmm0, 0x10(%rsp)
movq %rcx, 0x50(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, %r13
movq %rdx, (%rsp)
movslq 0x8(%rdx), %r14
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4410
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
movq %r14, 0x18(%rsp)
testq %r14, %r14
jle 0x26ba2
movl %ebp, 0x20(%rsp)
movq 0x18(%rsp), %rbp
negq %rbp
movl $0x8, %r14d
movl $0x1, %r12d
xorl %r15d, %r15d
xorpd %xmm6, %xmm6
movsd 0x116e1(%rip), %xmm7    # 0x38110
movq (%rsp), %rax
movq 0x10(%rax), %rax
movss -0x8(%rax,%r14), %xmm0
movss (%r13), %xmm3
subss %xmm3, %xmm0
movq %rbp, %rcx
addq %r12, %rcx
movl $0x0, %ecx
cmovneq %r12, %rcx
leaq (%rcx,%rcx,2), %rcx
movss 0x4(%rax,%rcx,4), %xmm8
movsd -0x4(%rax,%r14), %xmm1
movsd 0x4(%r13), %xmm4
subps %xmm4, %xmm1
subss %xmm4, %xmm8
movss (%rax,%rcx,4), %xmm5
movss 0x8(%rax,%rcx,4), %xmm2
unpcklps %xmm5, %xmm2            # xmm2 = xmm2[0],xmm5[0],xmm2[1],xmm5[1]
shufps $0xd4, %xmm4, %xmm3     # xmm3 = xmm3[0,1],xmm4[1,3]
shufps $0x52, %xmm4, %xmm3     # xmm3 = xmm3[2,0],xmm4[1,1]
subps %xmm3, %xmm2
movaps %xmm2, %xmm3
shufps $0x0, %xmm2, %xmm3      # xmm3 = xmm3[0,0],xmm2[0,0]
movss %xmm8, %xmm3            # xmm3 = xmm8[0],xmm3[1,2,3]
mulss %xmm0, %xmm8
shufps $0xd4, %xmm1, %xmm0     # xmm0 = xmm0[0,1],xmm1[1,3]
shufps $0x52, %xmm1, %xmm0     # xmm0 = xmm0[2,0],xmm1[1,1]
mulps %xmm0, %xmm3
movaps %xmm1, %xmm4
mulps %xmm2, %xmm4
subps %xmm3, %xmm4
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulss %xmm1, %xmm2
subss %xmm2, %xmm8
movaps %xmm4, %xmm0
mulps %xmm4, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm4, %xmm1
mulss %xmm4, %xmm1
addss %xmm0, %xmm1
movaps %xmm8, %xmm0
mulss %xmm8, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm6, %xmm0
jb 0x26af4
sqrtsd %xmm0, %xmm0
jmp 0x26b1b
movaps %xmm8, 0x40(%rsp)
movaps %xmm4, 0x30(%rsp)
callq 0x4240
movaps 0x30(%rsp), %xmm4
movaps 0x40(%rsp), %xmm8
movsd 0x115f9(%rip), %xmm7    # 0x38110
xorpd %xmm6, %xmm6
ucomisd %xmm0, %xmm7
jbe 0x26b2c
xorps %xmm1, %xmm1
movb $0x1, %r15b
xorps %xmm2, %xmm2
jmp 0x26b6c
cvtps2pd %xmm4, %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
divpd %xmm2, %xmm1
cvtpd2ps %xmm1, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm8, %xmm2
divsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
cvtsd2ss %xmm0, %xmm0
ucomiss 0x115ea(%rip), %xmm0    # 0x38144
movzbl %r15b, %eax
movl $0x1, %r15d
cmovnel %eax, %r15d
cmovpl %eax, %r15d
movlpd %xmm1, -0x8(%rbx,%r14)
movss %xmm2, (%rbx,%r14)
addq $0xc, %r14
leaq (%r12,%rbp), %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x26a2f
xorps %xmm0, %xmm0
testb $0x1, %r15b
movl 0x20(%rsp), %ebp
jne 0x26d5e
movslq %ebp, %r12
leaq 0x4f4bd34(%rip), %r13   # 0x4f728e0
cmpl $0x0, (%r13,%r12,4)
jle 0x26d5b
leaq 0x4f4bda1(%rip), %rax   # 0x4f72960
movq (%rax,%r12,8), %rbp
leaq 0x4f4bd46(%rip), %rax   # 0x4f72910
movss 0xc(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
addsd 0x11531(%rip), %xmm1    # 0x38110
movsd %xmm1, 0x40(%rsp)
movq (%rax,%r12,8), %r14
movss 0x10(%rsp), %xmm1
subss %xmm0, %xmm1
movss %xmm1, 0x30(%rsp)
xorl %r15d, %r15d
xorps %xmm8, %xmm8
xorpd %xmm7, %xmm7
movss 0x14(%rsp), %xmm1
movq 0x28(%rsp), %rdx
movss (%r14), %xmm6
movss 0x4(%r14), %xmm2
movss 0x4(%rdx), %xmm0
mulss %xmm2, %xmm0
movss (%rdx), %xmm4
mulss %xmm6, %xmm4
addss %xmm0, %xmm4
movss 0x8(%r14), %xmm3
movss 0x8(%rdx), %xmm0
mulss %xmm3, %xmm0
addss %xmm4, %xmm0
ucomiss %xmm0, %xmm8
jae 0x26d28
cmpl $0x0, 0x18(%rsp)
jle 0x26c8f
movq 0x18(%rsp), %rax
movq %rbx, %rcx
movss 0x4(%rcx), %xmm4
mulss %xmm2, %xmm4
movss (%rcx), %xmm5
mulss %xmm6, %xmm5
addss %xmm4, %xmm5
movss 0x8(%rcx), %xmm4
mulss %xmm3, %xmm4
addss %xmm5, %xmm4
ucomiss %xmm4, %xmm8
jae 0x26d28
addq $0xc, %rcx
decl %eax
jne 0x26c5b
ucomiss 0x114a2(%rip), %xmm1    # 0x38138
jne 0x26c9a
jnp 0x26cb8
movss %xmm7, (%rsp)
callq 0x4510
xorps %xmm8, %xmm8
movss (%rsp), %xmm7
movq 0x28(%rsp), %rdx
movss 0x14(%rsp), %xmm1
movss (%r14), %xmm2
movss 0x4(%r14), %xmm3
movq 0x50(%rsp), %rax
mulss 0x4(%rax), %xmm3
mulss (%rax), %xmm2
addss %xmm3, %xmm2
movss 0x8(%r14), %xmm4
mulss 0x8(%rax), %xmm4
addss %xmm2, %xmm4
xorps 0x113a5(%rip), %xmm4    # 0x38090
xorps %xmm3, %xmm3
cvtss2sd %xmm4, %xmm3
xorps %xmm2, %xmm2
movsd 0x40(%rsp), %xmm5
ucomisd %xmm3, %xmm5
jae 0x26d1f
movss 0x10(%rsp), %xmm2
ucomiss %xmm4, %xmm2
jbe 0x26d1c
subss 0xc(%rsp), %xmm4
mulss %xmm4, %xmm0
divss 0x30(%rsp), %xmm0
movaps %xmm0, %xmm2
mulss (%rbp), %xmm2
addss %xmm2, %xmm7
incl %r15d
addq $0xc, %r14
addq $0x4, %rbp
cmpl (%r13,%r12,4), %r15d
jl 0x26c0f
mulss 0x15eb2(%rip), %xmm7    # 0x3cbf8
xorps %xmm0, %xmm0
cvtss2sd %xmm7, %xmm0
mulsd 0x113c3(%rip), %xmm0    # 0x38118
cvtsd2ss %xmm0, %xmm0
jmp 0x26d5e
xorps %xmm0, %xmm0
movss %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x4380
movss (%rsp), %xmm0
mulss 0x24(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq