// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movss %xmm1, 0x24(%rsp)
movss %xmm0, 0x14(%rsp)
movl %ecx, %ebp
movq %rsi, 0x28(%rsp)
movq %rdi, %r12
movq %rdx, 0x8(%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 0x26848
movl %ebp, 0x20(%rsp)
movq 0x18(%rsp), %r13
negq %r13
movl $0x8, %ebp
movl $0x1, %r15d
xorl %r14d, %r14d
xorpd %xmm6, %xmm6
movsd 0x11a39(%rip), %xmm7    # 0x38110
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movss -0x8(%rax,%rbp), %xmm0
movss (%r12), %xmm3
subss %xmm3, %xmm0
movq %r13, %rcx
addq %r15, %rcx
movl $0x0, %ecx
cmovneq %r15, %rcx
leaq (%rcx,%rcx,2), %rcx
movss 0x4(%rax,%rcx,4), %xmm8
movsd -0x4(%rax,%rbp), %xmm1
movsd 0x4(%r12), %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 0x2679c
sqrtsd %xmm0, %xmm0
jmp 0x267c3
movaps %xmm8, 0x40(%rsp)
movaps %xmm4, 0x30(%rsp)
callq 0x4240
movaps 0x30(%rsp), %xmm4
movaps 0x40(%rsp), %xmm8
movsd 0x11951(%rip), %xmm7    # 0x38110
xorpd %xmm6, %xmm6
ucomisd %xmm0, %xmm7
jbe 0x267d4
xorps %xmm1, %xmm1
movb $0x1, %r14b
xorps %xmm2, %xmm2
jmp 0x26814
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 0x11942(%rip), %xmm0    # 0x38144
movzbl %r14b, %eax
movl $0x1, %r14d
cmovnel %eax, %r14d
cmovpl %eax, %r14d
movlpd %xmm1, -0x8(%rbx,%rbp)
movss %xmm2, (%rbx,%rbp)
addq $0xc, %rbp
leaq (%r15,%r13), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x266d7
xorps %xmm0, %xmm0
testb $0x1, %r14b
movl 0x20(%rsp), %ebp
jne 0x2696c
movslq %ebp, %r14
leaq 0x4f4c08e(%rip), %r15   # 0x4f728e0
cmpl $0x0, (%r15,%r14,4)
jle 0x26969
leaq 0x4f4c0fc(%rip), %rax   # 0x4f72960
movq (%rax,%r14,8), %r12
leaq 0x4f4c0a1(%rip), %rax   # 0x4f72910
movq (%rax,%r14,8), %r13
xorpd %xmm7, %xmm7
xorl %ebp, %ebp
xorps %xmm8, %xmm8
movss 0x14(%rsp), %xmm1
movq 0x28(%rsp), %rdx
movss (%r13), %xmm6
movss 0x4(%r13), %xmm2
movss 0x4(%rdx), %xmm0
mulss %xmm2, %xmm0
movss (%rdx), %xmm4
mulss %xmm6, %xmm4
addss %xmm0, %xmm4
movss 0x8(%r13), %xmm3
movss 0x8(%rdx), %xmm0
mulss %xmm3, %xmm0
addss %xmm4, %xmm0
ucomiss %xmm0, %xmm7
jae 0x26936
cmpl $0x0, 0x18(%rsp)
jle 0x268ff
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, %xmm7
jae 0x26936
addq $0xc, %rcx
decl %eax
jne 0x268d0
ucomiss 0x11832(%rip), %xmm1    # 0x38138
jne 0x2690a
jnp 0x2692b
movss %xmm8, 0x8(%rsp)
callq 0x4510
movss 0x8(%rsp), %xmm8
xorps %xmm7, %xmm7
movq 0x28(%rsp), %rdx
movss 0x14(%rsp), %xmm1
mulss (%r12), %xmm0
addss %xmm0, %xmm8
incl %ebp
addq $0xc, %r13
addq $0x4, %r12
cmpl (%r15,%r14,4), %ebp
jl 0x26888
mulss 0x162a5(%rip), %xmm8    # 0x3cbf8
xorps %xmm0, %xmm0
cvtss2sd %xmm8, %xmm0
mulsd 0x117b5(%rip), %xmm0    # 0x38118
cvtsd2ss %xmm0, %xmm0
jmp 0x2696c
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x4380
movss 0x8(%rsp), %xmm0
mulss 0x24(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq