// File: 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:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movl %ecx, 0x8c(%rsp)
movss %xmm0, 0x88(%rsp)
movss %xmm1, 0x84(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x80(%rsp)
movq 0x90(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x7c(%rsp)
movslq 0x7c(%rsp), %rax
imulq $0xc, %rax, %rdi
callq 0x4430
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movb $0x0, 0x6f(%rsp)
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0x4c105
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0xa0(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x5c(%rsp)
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0xa0(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x60(%rsp)
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0xa0(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x64(%rsp)
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, (%rsp)
movl 0x68(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x7c(%rsp)
movq (%rsp), %rax
movslq %edx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0xa0(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x50(%rsp)
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movl 0x68(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x7c(%rsp)
movq 0x8(%rsp), %rax
movslq %edx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0xa0(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x54(%rsp)
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x68(%rsp), %eax
addl $0x1, %eax
cltd
idivl 0x7c(%rsp)
movq 0x10(%rsp), %rax
movslq %edx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0xa0(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x58(%rsp)
movss 0x60(%rsp), %xmm0
movss 0x58(%rsp), %xmm2
movss 0x64(%rsp), %xmm1
mulss 0x54(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x44(%rsp)
movss 0x64(%rsp), %xmm0
movss 0x50(%rsp), %xmm2
movss 0x5c(%rsp), %xmm1
mulss 0x58(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x48(%rsp)
movss 0x5c(%rsp), %xmm0
movss 0x54(%rsp), %xmm2
movss 0x60(%rsp), %xmm1
mulss 0x50(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4c(%rsp)
leaq 0x44(%rsp), %rdi
callq 0x11980
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x4c0a2
jp 0x4c0a2
movb $0x1, 0x6f(%rsp)
movss 0x44(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movss 0x48(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movss 0x4c(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x4be8c
testb $0x1, 0x6f(%rsp)
jne 0x4c321
movl $0x0, 0x40(%rsp)
movslq 0x8c(%rsp), %rcx
leaq 0x4f5b809(%rip), %rax   # 0x4fa7930
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movslq 0x8c(%rsp), %rcx
leaq 0x4f5b841(%rip), %rax   # 0x4fa7980
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
movl 0x40(%rsp), %eax
movslq 0x8c(%rsp), %rdx
leaq 0x4f5b7a5(%rip), %rcx   # 0x4fa7900
cmpl (%rcx,%rdx,4), %eax
jge 0x4c2f3
movq 0x30(%rsp), %rax
movss (%rax), %xmm1
movq 0x98(%rsp), %rax
movss (%rax), %xmm2
movq 0x30(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x98(%rsp), %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x30(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x98(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
xorps %xmm0, %xmm0
ucomiss 0x24(%rsp), %xmm0
jb 0x4c1cc
jmp 0x4c2c7
movl $0x0, 0x3c(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0x4c259
movq 0x30(%rsp), %rax
movss (%rax), %xmm0
movq 0x18(%rsp), %rax
movss (%rax), %xmm2
movq 0x30(%rsp), %rax
movss 0x4(%rax), %xmm1
movq 0x18(%rsp), %rax
mulss 0x4(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x30(%rsp), %rax
movss 0x8(%rax), %xmm1
movq 0x18(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
jb 0x4c23c
jmp 0x4c259
jmp 0x4c23e
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x18(%rsp), %rax
addq $0xc, %rax
movq %rax, 0x18(%rsp)
jmp 0x4c1de
movl 0x3c(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0x4c265
jmp 0x4c2c7
movss 0x88(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x20f6e(%rip), %xmm1    # 0x6d1e8
ucomisd %xmm1, %xmm0
jne 0x4c284
jp 0x4c284
jmp 0x4c29e
movss 0x24(%rsp), %xmm0
movss 0x88(%rsp), %xmm1
callq 0x44140
movss %xmm0, 0x24(%rsp)
movss 0x24(%rsp), %xmm0
movq 0x28(%rsp), %rax
movss (%rax), %xmm2
movss 0x80(%rsp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x80(%rsp)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
movq 0x30(%rsp), %rax
addq $0xc, %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
addq $0x4, %rax
movq %rax, 0x28(%rsp)
jmp 0x4c148
movss 0x22835(%rip), %xmm0    # 0x6eb30
mulss 0x80(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1dd70(%rip), %xmm1    # 0x6a080
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x80(%rsp)
movq 0x70(%rsp), %rdi
callq 0x43a0
movss 0x84(%rsp), %xmm0
mulss 0x80(%rsp), %xmm0
movss %xmm0, 0x80(%rsp)
movss 0x80(%rsp), %xmm0
addq $0xa8, %rsp
retq
nopw (%rax,%rax)