// File: FreeSlave[P]vhlt/hlrad/qrad.cpp
// Function: FindTexlightEntity(int)
static entity_t *FindTexlightEntity (int facenum)
{
	dface_t *face = &g_dfaces[facenum];
	const dplane_t *dplane = getPlaneFromFace (face);
	const char *texname = GetTextureByNumber (face->texinfo);
	entity_t *faceent = g_face_entity[facenum];
	vec3_t centroid;
	Winding *w = new Winding (*face);
	w->getCenter (centroid);
	delete w;
	VectorAdd (centroid, g_face_offset[facenum], centroid);

	entity_t *found = NULL;
	vec_t bestdist = -1;
	for (int i = 0; i < g_numentities; i++)
	{
		entity_t *ent = &g_entities[i];
		if (strcmp (ValueForKey (ent, "classname"), "light_surface"))
			continue;
		if (strcasecmp (ValueForKey (ent, "_tex"), texname))
			continue;
		vec3_t delta;
		GetVectorForKey (ent, "origin", delta);
		VectorSubtract (delta, centroid, delta);
		vec_t dist = VectorLength (delta);
		if (*ValueForKey (ent, "_frange"))
		{
			if (dist > FloatForKey (ent, "_frange"))
				continue;
		}
		if (*ValueForKey (ent, "_fdist"))
		{
			if (fabs (DotProduct (delta, dplane->normal)) > FloatForKey (ent, "_fdist"))
				continue;
		}
		if (*ValueForKey (ent, "_fclass"))
		{
			if (strcmp (ValueForKey (faceent, "classname"), ValueForKey (ent, "_fclass")))
				continue;
		}
		if (*ValueForKey (ent, "_fname"))
		{
			if (strcmp (ValueForKey (faceent, "targetname"), ValueForKey (ent, "_fname")))
				continue;
		}
		if (bestdist >= 0 && dist > bestdist)
			continue;
		found = ent;
		bestdist = dist;
	}
	return found;
}

// Assembly:
subq $0xa8, %rsp
movl %edi, 0xa4(%rsp)
movslq 0xa4(%rsp), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x1b90d0f(%rip), %rax   # 0x1be6410
leaq (%rax,%rcx,4), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x5cfa0
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rax
movswl 0xa(%rax), %edi
callq 0x69d0
movq %rax, 0x88(%rsp)
movslq 0xa4(%rsp), %rcx
leaq 0x52d82b6(%rip), %rax   # 0x532da00
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq 0x98(%rsp), %rsi
movss 0x18415(%rip), %xmm0    # 0x6db90
callq 0xf130
jmp 0x55782
movq 0x28(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x74(%rsp), %rsi
callq 0xe060
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x557b6
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movss 0x74(%rsp), %xmm0
movslq 0xa4(%rsp), %rcx
leaq 0x5418245(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss (%rax), %xmm0
movss %xmm0, 0x74(%rsp)
movss 0x78(%rsp), %xmm0
movslq 0xa4(%rsp), %rcx
leaq 0x541821f(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x4(%rax), %xmm0
movss %xmm0, 0x78(%rsp)
movss 0x7c(%rsp), %xmm0
movslq 0xa4(%rsp), %rcx
leaq 0x54181f8(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x8(%rax), %xmm0
movss %xmm0, 0x7c(%rsp)
movq $0x0, 0x50(%rsp)
movss 0x17cd9(%rip), %xmm0    # 0x6d514
movss %xmm0, 0x4c(%rsp)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
leaq 0x201ebe0(%rip), %rcx   # 0x2074434
cmpl (%rcx), %eax
jge 0x55b47
movslq 0x48(%rsp), %rcx
leaq 0x201ebd8(%rip), %rax   # 0x2074440
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x14cd7(%rip), %rsi     # 0x6a557
callq 0x7d50
movq %rax, %rdi
leaq 0x14d3e(%rip), %rsi     # 0x6a5cd
callq 0x4480
cmpl $0x0, %eax
je 0x558c0
jmp 0x55b37
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x55b54
movq 0x40(%rsp), %rdi
leaq 0x14cf2(%rip), %rsi     # 0x6a5be
callq 0x7d50
movq %rax, %rdi
movq 0x88(%rsp), %rsi
callq 0x4350
cmpl $0x0, %eax
je 0x558eb
jmp 0x55b37
movq 0x40(%rsp), %rdi
leaq 0x34(%rsp), %rdx
leaq 0x1bcb8(%rip), %rsi     # 0x715b4
callq 0x8c10
movss 0x34(%rsp), %xmm0
subss 0x74(%rsp), %xmm0
movss %xmm0, 0x34(%rsp)
movss 0x38(%rsp), %xmm0
subss 0x78(%rsp), %xmm0
movss %xmm0, 0x38(%rsp)
movss 0x3c(%rsp), %xmm0
subss 0x7c(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0x34(%rsp), %xmm0
mulss 0x34(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x38(%rsp), %xmm1
mulss 0x38(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x3c(%rsp), %xmm1
mulss 0x3c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x1bd12(%rip), %rsi     # 0x7169c
callq 0x7d50
cmpb $0x0, (%rax)
je 0x559c6
movss 0x30(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x1bcf0(%rip), %rsi     # 0x7169c
callq 0x8b40
movaps %xmm0, %xmm1
movss 0x1c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x559c4
jmp 0x55b37
jmp 0x559c6
movq 0x40(%rsp), %rdi
leaq 0x1bcd2(%rip), %rsi     # 0x716a4
callq 0x7d50
cmpb $0x0, (%rax)
je 0x55a55
movss 0x34(%rsp), %xmm1
movq 0x90(%rsp), %rax
movss (%rax), %xmm2
movss 0x38(%rsp), %xmm0
movq 0x90(%rsp), %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x3c(%rsp), %xmm0
movq 0x90(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
callq 0x11ce0
movss %xmm0, 0x18(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x1bc69(%rip), %rsi     # 0x716a4
callq 0x8b40
movaps %xmm0, %xmm1
movss 0x18(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x55a53
jmp 0x55b37
jmp 0x55a55
movq 0x40(%rsp), %rdi
leaq 0x1bc4a(%rip), %rsi     # 0x716ab
callq 0x7d50
cmpb $0x0, (%rax)
je 0x55aae
movq 0x80(%rsp), %rdi
leaq 0x14add(%rip), %rsi     # 0x6a557
callq 0x7d50
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x1bc1b(%rip), %rsi     # 0x716ab
callq 0x7d50
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x4480
cmpl $0x0, %eax
je 0x55aac
jmp 0x55b37
jmp 0x55aae
movq 0x40(%rsp), %rdi
leaq 0x1bbf9(%rip), %rsi     # 0x716b3
callq 0x7d50
cmpb $0x0, (%rax)
je 0x55b04
movq 0x80(%rsp), %rdi
leaq 0x14be8(%rip), %rsi     # 0x6a6bb
callq 0x7d50
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x1bbca(%rip), %rsi     # 0x716b3
callq 0x7d50
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x4480
cmpl $0x0, %eax
je 0x55b02
jmp 0x55b37
jmp 0x55b04
movss 0x4c(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x55b21
movss 0x30(%rsp), %xmm0
ucomiss 0x4c(%rsp), %xmm0
jbe 0x55b21
jmp 0x55b37
movq 0x40(%rsp), %rax
movq %rax, 0x50(%rsp)
movss 0x30(%rsp), %xmm0
movss %xmm0, 0x4c(%rsp)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x55849
movq 0x50(%rsp), %rax
addq $0xa8, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x45c0
nop