// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movss %xmm3, 0x20(%rsp)
movss %xmm2, 0xc(%rsp)
movl %r8d, %r14d
movss %xmm1, 0x4(%rsp)
movss %xmm0, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r12
movslq 0x8(%rdx), %rbp
leaq (,%rbp,4), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4400
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
testq %rbp, %rbp
jle 0x25d5b
movq 0x10(%r13), %rax
movss (%r12), %xmm0
movsd 0x4(%r12), %xmm1
movaps %xmm0, %xmm2
shufps $0xd4, %xmm1, %xmm2     # xmm2 = xmm2[0,1],xmm1[1,3]
shufps $0xe2, %xmm1, %xmm2     # xmm2 = xmm2[2,0],xmm1[2,3]
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
leaq -0xc(,%rcx,4), %rcx
movl $0x1, %edx
xorl %esi, %esi
movsd 0x114f1(%rip), %xmm3    # 0x37118
xorps %xmm4, %xmm4
xorl %edi, %edi
movss (%rax,%rsi), %xmm6
subss %xmm0, %xmm6
cmpq %rsi, %rcx
movl %edx, %r8d
movl $0x0, %r9d
cmovneq %r8, %r9
leaq (%r9,%r9,2), %r8
movss 0x4(%rax,%r8,4), %xmm5
subss %xmm1, %xmm5
movsd 0x4(%rax,%rsi), %xmm7
subps %xmm1, %xmm7
movss (%rax,%r8,4), %xmm9
movss 0x8(%rax,%r8,4), %xmm8
unpcklps %xmm9, %xmm8            # xmm8 = xmm8[0],xmm9[0],xmm8[1],xmm9[1]
subps %xmm2, %xmm8
movaps %xmm8, %xmm9
shufps $0x0, %xmm8, %xmm9      # xmm9 = xmm9[0,0],xmm8[0,0]
movss %xmm5, %xmm9            # xmm9 = xmm5[0],xmm9[1,2,3]
mulss %xmm6, %xmm5
shufps $0xd4, %xmm7, %xmm6     # xmm6 = xmm6[0,1],xmm7[1,3]
shufps $0x52, %xmm7, %xmm6     # xmm6 = xmm6[2,0],xmm7[1,1]
mulps %xmm6, %xmm9
movaps %xmm7, %xmm6
mulps %xmm8, %xmm6
subps %xmm9, %xmm6
shufps $0x55, %xmm8, %xmm8     # xmm8 = xmm8[1,1,1,1]
mulss %xmm7, %xmm8
subss %xmm8, %xmm5
movaps %xmm6, %xmm7
mulps %xmm6, %xmm7
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
movaps %xmm6, %xmm8
mulss %xmm6, %xmm8
addss %xmm7, %xmm8
movaps %xmm5, %xmm7
mulss %xmm5, %xmm7
addss %xmm8, %xmm7
cvtss2sd %xmm7, %xmm7
sqrtsd %xmm7, %xmm7
ucomisd %xmm7, %xmm3
jbe 0x25cf9
movq $0x0, (%rbx,%rsi)
movl $0x0, 0x8(%rbx,%rsi)
movb $0x1, %r8b
cmpq %rsi, %rcx
jne 0x25d3f
jmp 0x25f35
cvtps2pd %xmm6, %xmm6
cvtss2sd %xmm5, %xmm5
divsd %xmm7, %xmm5
cvtsd2ss %xmm5, %xmm5
xorps %xmm8, %xmm8
cvtsd2ss %xmm7, %xmm8
ucomiss %xmm4, %xmm8
setnp %r9b
sete %r8b
andb %r9b, %r8b
unpcklpd %xmm7, %xmm7            # xmm7 = xmm7[0,0]
divpd %xmm7, %xmm6
cvtpd2ps %xmm6, %xmm6
orb %dil, %r8b
movlpd %xmm6, (%rbx,%rsi)
movss %xmm5, 0x8(%rbx,%rsi)
cmpq %rsi, %rcx
je 0x25d4e
addq $0xc, %rsi
incq %rdx
movl %r8d, %edi
jmp 0x25c2c
xorps %xmm0, %xmm0
testb $0x1, %r8b
jne 0x25f38
movslq %r14d, %rsi
leaq 0x4f4bb7b(%rip), %rax   # 0x4f718e0
movl (%rax,%rsi,4), %eax
testl %eax, %eax
jle 0x25f35
leaq 0x4f4bbe9(%rip), %rcx   # 0x4f71960
movq (%rcx,%rsi,8), %r13
leaq 0x4f4bb8e(%rip), %rcx   # 0x4f71910
movss 0x4(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
addsd 0x11381(%rip), %xmm1    # 0x37118
movsd %xmm1, 0x30(%rsp)
movq (%rcx,%rsi,8), %r12
movss 0x8(%rsp), %xmm1
subss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
xorl %r14d, %r14d
xorpd %xmm7, %xmm7
xorps %xmm8, %xmm8
movss 0xc(%rsp), %xmm1
movq %rbx, 0x28(%rsp)
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 0x25f02
testl %ebp, %ebp
jle 0x25e45
movl %ebp, %ecx
movq %rbx, %rdx
movss 0x4(%rdx), %xmm4
mulss %xmm3, %xmm4
movss (%rdx), %xmm5
mulss %xmm2, %xmm5
addss %xmm4, %xmm5
movss 0x8(%rdx), %xmm4
mulss %xmm6, %xmm4
addss %xmm5, %xmm4
ucomiss %xmm4, %xmm7
jae 0x25f02
addq $0xc, %rdx
decl %ecx
jne 0x25e12
ucomiss 0x11258(%rip), %xmm1    # 0x370a4
jne 0x25e50
jnp 0x25e97
movq %r15, %rbx
movq %rbp, %r15
movq %rsi, %rbp
movss %xmm8, 0x24(%rsp)
callq 0x4500
movss 0x24(%rsp), %xmm8
xorps %xmm7, %xmm7
movq %rbp, %rsi
movq %r15, %rbp
movq %rbx, %r15
movq 0x28(%rsp), %rbx
movss 0xc(%rsp), %xmm1
movss (%r12), %xmm2
movss 0x4(%r12), %xmm3
movss 0x8(%r12), %xmm6
movq 0x10(%rsp), %rax
mulss 0x4(%rax), %xmm3
mulss (%rax), %xmm2
addss %xmm3, %xmm2
mulss 0x8(%rax), %xmm6
addss %xmm2, %xmm6
xorps 0x111c7(%rip), %xmm6    # 0x37080
xorps %xmm3, %xmm3
cvtss2sd %xmm6, %xmm3
xorps %xmm2, %xmm2
movsd 0x30(%rsp), %xmm4
ucomisd %xmm3, %xmm4
jae 0x25eed
movss 0x8(%rsp), %xmm2
ucomiss %xmm6, %xmm2
jbe 0x25eea
subss 0x4(%rsp), %xmm6
mulss %xmm6, %xmm0
divss 0x1c(%rsp), %xmm0
movaps %xmm0, %xmm2
mulss (%r13), %xmm2
addss %xmm2, %xmm8
leaq 0x4f4b9e1(%rip), %rax   # 0x4f718e0
movl (%rax,%rsi,4), %eax
incl %r14d
addq $0xc, %r12
addq $0x4, %r13
cmpl %eax, %r14d
jl 0x25dc7
mulss 0x15c19(%rip), %xmm8    # 0x3bb38
xorps %xmm0, %xmm0
cvtss2sd %xmm8, %xmm0
mulsd 0x111f1(%rip), %xmm0    # 0x37120
cvtsd2ss %xmm0, %xmm0
jmp 0x25f38
xorps %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
movq %rbx, %rdi
callq 0x4370
movss 0x10(%rsp), %xmm0
mulss 0x20(%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq