// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlrad/mathutil.cpp
// Function: CalcSightArea(float const*, float const*, Winding const*, int, float, float)
vec_t CalcSightArea (const vec3_t receiver_origin, const vec3_t receiver_normal, const Winding *emitter_winding, int skylevel
	#ifdef HLRAD_DIVERSE_LIGHTING
					, vec_t lighting_power, vec_t lighting_scale
	#endif
					)
{
	// maybe there are faster ways in calculating the weighted area, but at least this way is not bad.
	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;
		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
			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 $0x18, %rsp
movss %xmm1, 0xc(%rsp)
movss %xmm0, 0x8(%rsp)
movl %ecx, %ebp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movslq 0x8(%rdx), %r13
leaq (,%r13,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 %r13, %r13
jle 0x25a10
movq 0x10(%r12), %rax
movss (%r15), %xmm0
movsd 0x4(%r15), %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 (,%r13,2), %rcx
addq %r13, %rcx
leaq -0xc(,%rcx,4), %rcx
movl $0x1, %edx
xorl %esi, %esi
movsd 0x1183c(%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 0x259ae
movq $0x0, (%rbx,%rsi)
movl $0x0, 0x8(%rbx,%rsi)
movb $0x1, %r8b
cmpq %rsi, %rcx
jne 0x259f4
jmp 0x25b52
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 0x25a03
addq $0xc, %rsi
incq %rdx
movl %r8d, %edi
jmp 0x258e1
xorps %xmm0, %xmm0
testb $0x1, %r8b
jne 0x25b55
movslq %ebp, %rsi
leaq 0x4f4bec6(%rip), %rax   # 0x4f718e0
movl (%rax,%rsi,4), %eax
testl %eax, %eax
jle 0x25b52
leaq 0x4f4bf34(%rip), %rcx   # 0x4f71960
movq (%rcx,%rsi,8), %rbp
leaq 0x4f4bed9(%rip), %rcx   # 0x4f71910
movq (%rcx,%rsi,8), %r12
xorpd %xmm7, %xmm7
xorl %r15d, %r15d
xorps %xmm8, %xmm8
movss 0x8(%rsp), %xmm1
movq %rbx, 0x10(%rsp)
movss (%r12), %xmm6
movss 0x4(%r12), %xmm2
movss 0x4(%r14), %xmm0
mulss %xmm2, %xmm0
movss (%r14), %xmm4
mulss %xmm6, %xmm4
addss %xmm0, %xmm4
movss 0x8(%r12), %xmm3
movss 0x8(%r14), %xmm0
mulss %xmm3, %xmm0
addss %xmm4, %xmm0
ucomiss %xmm0, %xmm7
jae 0x25b1f
testl %r13d, %r13d
jle 0x25acd
movl %r13d, %ecx
movq %rbx, %rdx
movss 0x4(%rdx), %xmm4
mulss %xmm2, %xmm4
movss (%rdx), %xmm5
mulss %xmm6, %xmm5
addss %xmm4, %xmm5
movss 0x8(%rdx), %xmm4
mulss %xmm3, %xmm4
addss %xmm5, %xmm4
ucomiss %xmm4, %xmm7
jae 0x25b1f
addq $0xc, %rdx
decl %ecx
jne 0x25a9e
ucomiss 0x115d0(%rip), %xmm1    # 0x370a4
jne 0x25ad8
jnp 0x25b15
movq %r14, %rbx
movq %r13, %r14
movq %rsi, %r13
movss %xmm8, 0x4(%rsp)
callq 0x4500
movss 0x4(%rsp), %xmm8
xorps %xmm7, %xmm7
movq %r13, %rsi
movq %r14, %r13
movq %rbx, %r14
movq 0x10(%rsp), %rbx
movss 0x8(%rsp), %xmm1
leaq 0x4f4bdce(%rip), %rax   # 0x4f718e0
movl (%rax,%rsi,4), %eax
mulss (%rbp), %xmm0
addss %xmm0, %xmm8
incl %r15d
addq $0xc, %r12
addq $0x4, %rbp
cmpl %eax, %r15d
jl 0x25a51
mulss 0x15ffc(%rip), %xmm8    # 0x3bb38
xorps %xmm0, %xmm0
cvtss2sd %xmm8, %xmm0
mulsd 0x115d4(%rip), %xmm0    # 0x37120
cvtsd2ss %xmm0, %xmm0
jmp 0x25b55
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movq %rbx, %rdi
callq 0x4370
movss 0x4(%rsp), %xmm0
mulss 0xc(%rsp), %xmm0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq