// File: 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:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movss %xmm0, 0x94(%rsp)
movss %xmm1, 0x90(%rsp)
movl %r8d, 0x8c(%rsp)
movss %xmm2, 0x88(%rsp)
movss %xmm3, 0x84(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x80(%rsp)
movq 0xa0(%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 0x4c680
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0xb0(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x5c(%rsp)
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0xb0(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x60(%rsp)
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x68(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0xb0(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x64(%rsp)
movq 0xa0(%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 0xb0(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x50(%rsp)
movq 0xa0(%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 0xb0(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x54(%rsp)
movq 0xa0(%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 0xb0(%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 0x4c61d
jp 0x4c61d
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 0x4c407
testb $0x1, 0x6f(%rsp)
jne 0x4c984
movl $0x0, 0x40(%rsp)
movslq 0x8c(%rsp), %rcx
leaq 0x4f5b28e(%rip), %rax   # 0x4fa7930
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movslq 0x8c(%rsp), %rcx
leaq 0x4f5b2c6(%rip), %rax   # 0x4fa7980
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
movl 0x40(%rsp), %eax
movslq 0x8c(%rsp), %rdx
leaq 0x4f5b22a(%rip), %rcx   # 0x4fa7900
cmpl (%rcx,%rdx,4), %eax
jge 0x4c956
movq 0x30(%rsp), %rax
movss (%rax), %xmm1
movq 0xa8(%rsp), %rax
movss (%rax), %xmm2
movq 0x30(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0xa8(%rsp), %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x30(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0xa8(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
xorps %xmm0, %xmm0
ucomiss 0x24(%rsp), %xmm0
jb 0x4c747
jmp 0x4c92a
movl $0x0, 0x3c(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0x4c7d4
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 0x4c7b7
jmp 0x4c7d4
jmp 0x4c7b9
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x18(%rsp), %rax
addq $0xc, %rax
movq %rax, 0x18(%rsp)
jmp 0x4c759
movl 0x3c(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jge 0x4c7e3
jmp 0x4c92a
movss 0x88(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x209f0(%rip), %xmm1    # 0x6d1e8
ucomisd %xmm1, %xmm0
jne 0x4c802
jp 0x4c802
jmp 0x4c81c
movss 0x24(%rsp), %xmm0
movss 0x88(%rsp), %xmm1
callq 0x44140
movss %xmm0, 0x24(%rsp)
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
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movss 0x90(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1d7c7(%rip), %xmm2    # 0x6a068
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x4c8b6
xorps %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
jmp 0x4c901
movss 0x20(%rsp), %xmm1
movss 0x94(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x4c8ff
movss 0x24(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
subss 0x90(%rsp), %xmm1
mulss %xmm1, %xmm0
movss 0x94(%rsp), %xmm1
subss 0x90(%rsp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
jmp 0x4c901
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 0x4c6c3
movss 0x221d2(%rip), %xmm0    # 0x6eb30
mulss 0x80(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1d70d(%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 $0xb8, %rsp
retq
nopw (%rax,%rax)