// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: AddPatchLights(int)
void AddPatchLights (int facenum)
{
	dface_t *f;
#ifndef HLRAD_LOCALTRIANGULATION
	lerpTriangulation_t *trian;
#endif
	facelightlist_t *item;
	dface_t *f_other;
	facelight_t *fl_other;
	int k;
	int i;
	sample_t *samp;

	f = &g_dfaces[facenum];

	if (g_texinfo[f->texinfo].flags & TEX_SPECIAL)
	{
		return;
	}

#ifndef HLRAD_LOCALTRIANGULATION
	trian = CreateTriangulation (facenum);
#endif
	
	for (item = g_dependentfacelights[facenum]; item != NULL; item = item->next)
	{
		f_other = &g_dfaces[item->facenum];
		fl_other = &facelight[item->facenum];
		for (k = 0; k < MAXLIGHTMAPS && f_other->styles[k] != 255; k++)
		{
			for (i = 0; i < fl_other->numsamples; i++)
			{
				samp = &fl_other->samples[k][i];
				if (samp->surface != facenum)
				{ // the sample is not in this surface
					continue;
				}

				{
					vec3_t v;
		#ifdef ZHLT_XASH
					vec3_t v_direction;
		#endif

#ifdef HLRAD_LOCALTRIANGULATION
					int style = f_other->styles[k];
					InterpolateSampleLight (samp->pos, samp->surface, 1, &style, &v
		#ifdef ZHLT_XASH
											, &v_direction
		#endif
											);
#else
					SampleTriangulation (trian, samp->pos, v, 
		#ifdef ZHLT_XASH
										v_direction, 
		#endif
										f_other->styles[k]); //LRC
#endif

	#ifdef HLRAD_STYLE_CORING
					VectorAdd (samp->light, v, v);
		#ifdef ZHLT_XASH
					VectorAdd (samp->light_direction, v_direction, v_direction);
		#endif
					if (VectorMaximum (v) >= g_corings[f_other->styles[k]])
					{
						VectorCopy (v, samp->light);
		#ifdef ZHLT_XASH
						VectorCopy (v_direction, samp->light_direction);
		#endif
					}
					else
					{
		#ifdef HLRAD_AUTOCORING
						if (VectorMaximum (v) > g_maxdiscardedlight + NORMAL_EPSILON)
						{
							ThreadLock ();
							if (VectorMaximum (v) > g_maxdiscardedlight + NORMAL_EPSILON)
							{
								g_maxdiscardedlight = VectorMaximum (v);
								VectorCopy (samp->pos, g_maxdiscardedpos);
							}
							ThreadUnlock ();
						}
		#endif
					}
	#else
					VectorAdd (samp->light, v, samp->light);
	#endif
				}
			} // loop samples
		}
	}

#ifndef HLRAD_LOCALTRIANGULATION
	FreeTriangulation (trian);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, 0x10(%rsp)
movslq %edi, %rax
leaq (%rax,%rax,4), %rcx
leaq 0x1b8f5e0(%rip), %rdx   # 0x1bb0400
movswq 0xa(%rdx,%rcx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq 0x118f5bf(%rip), %rdx   # 0x11b03f0
testb $0x1, 0x24(%rdx,%rcx,8)
jne 0x2101e
leaq 0x51d0b4d(%rip), %rcx   # 0x51f1990
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x2101e
movq %rax, 0x18(%rsp)
movslq (%rax), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x4f50b4d(%rip), %rax   # 0x4f719b0
leaq (%rax,%rcx,8), %r13
shlq $0x2, %rcx
movq %rcx, 0x20(%rsp)
xorl %r12d, %r12d
leaq 0x1b8f586(%rip), %rax   # 0x1bb0400
movq 0x20(%rsp), %rcx
addq %rax, %rcx
cmpb $-0x1, 0xc(%r12,%rcx)
je 0x21010
movl (%r13), %eax
testl %eax, %eax
jle 0x21003
leaq (%r12,%rcx), %r14
addq $0xc, %r14
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x8(%r13,%r12,8), %rbx
movl 0x10(%rsp), %ecx
cmpl %ecx, 0x18(%rbx,%rbp)
jne 0x20ff0
leaq (%rbx,%rbp), %rdi
movzbl (%r14), %eax
movl %eax, 0x14(%rsp)
movl 0x18(%rbx,%rbp), %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rcx
leaq 0x4(%rsp), %r8
callq 0xdad0
movss 0xc(%rbx,%rbp), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
movss 0x10(%rbx,%rbp), %xmm2
addss 0x8(%rsp), %xmm2
movss %xmm2, 0x8(%rsp)
movss 0x14(%rbx,%rbp), %xmm1
addss 0xc(%rsp), %xmm1
movss %xmm1, 0xc(%rsp)
maxss %xmm1, %xmm2
movaps %xmm0, %xmm1
maxss %xmm2, %xmm1
movzbl (%r14), %eax
leaq 0x54d8b36(%rip), %rcx   # 0x54f9a60
ucomiss (%rcx,%rax,4), %xmm1
jae 0x20fce
leaq 0x5518c15(%rip), %rax   # 0x5539b50
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
cvtss2sd %xmm1, %xmm1
addsd 0x161ca(%rip), %xmm0    # 0x37118
ucomisd %xmm0, %xmm1
jbe 0x20fec
callq 0xb321
movss 0x8(%rsp), %xmm1
maxss 0xc(%rsp), %xmm1
movss 0x4(%rsp), %xmm0
maxss %xmm1, %xmm0
leaq 0x5518bd6(%rip), %rax   # 0x5539b50
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
addsd 0x16188(%rip), %xmm1    # 0x37118
ucomisd %xmm1, %xmm2
jbe 0x20fc7
leaq 0x5518bb3(%rip), %rax   # 0x5539b50
movss %xmm0, (%rax)
movss (%rbx,%rbp), %xmm0
leaq 0x5518ba7(%rip), %rax   # 0x5539b54
movss %xmm0, (%rax)
movss 0x4(%rbx,%rbp), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x8(%rbx,%rbp), %xmm0
movss %xmm0, 0x8(%rax)
callq 0xb332
jmp 0x20fec
movss %xmm0, 0xc(%rbx,%rbp)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x10(%rbx,%rbp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x14(%rbx,%rbp)
movl (%r13), %eax
incq %r15
movslq %eax, %rcx
addq $0x1c, %rbp
cmpq %rcx, %r15
jl 0x20ea7
incq %r12
cmpq $0x4, %r12
jne 0x20e73
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
jmp 0x20e47
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq