// File: /workspace/github_cpp_makefile_star2_O1/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 0x1b8f955(%rip), %rdx   # 0x1bb1400
movswq 0xa(%rdx,%rcx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq 0x118f934(%rip), %rdx   # 0x11b13f0
testb $0x1, 0x24(%rdx,%rcx,8)
jne 0x21ca5
leaq 0x51d0ec2(%rip), %rcx   # 0x51f2990
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x21ca5
movq %rax, 0x18(%rsp)
movslq (%rax), %rax
leaq (%rax,%rax,4), %rcx
leaq 0x4f50ec2(%rip), %rax   # 0x4f729b0
leaq (%rax,%rcx,8), %r13
shlq $0x2, %rcx
movq %rcx, 0x20(%rsp)
xorl %r12d, %r12d
leaq 0x1b8f8fb(%rip), %rax   # 0x1bb1400
movq 0x20(%rsp), %rcx
addq %rcx, %rax
cmpb $-0x1, 0xc(%r12,%rax)
je 0x21c97
cmpl $0x0, (%r13)
jle 0x21c8a
leaq (%r12,%rax), %r14
addq $0xc, %r14
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x8(%r13,%r12,8), %rbx
movl 0x10(%rsp), %eax
cmpl %eax, 0x18(%rbx,%rbp)
jne 0x21c76
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 0xdf80
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 0x54d8eac(%rip), %rcx   # 0x54faa60
ucomiss (%rcx,%rax,4), %xmm1
jae 0x21c58
leaq 0x5518f8b(%rip), %rax   # 0x553ab50
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
cvtss2sd %xmm1, %xmm1
addsd 0x16538(%rip), %xmm0    # 0x38110
ucomisd %xmm0, %xmm1
jbe 0x21c76
callq 0xb411
movss 0x8(%rsp), %xmm1
maxss 0xc(%rsp), %xmm1
movss 0x4(%rsp), %xmm0
maxss %xmm1, %xmm0
leaq 0x5518f4c(%rip), %rax   # 0x553ab50
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
addsd 0x164f6(%rip), %xmm1    # 0x38110
ucomisd %xmm1, %xmm2
jbe 0x21c51
leaq 0x5518f29(%rip), %rax   # 0x553ab50
movss %xmm0, (%rax)
movss (%rbx,%rbp), %xmm0
leaq 0x5518f1d(%rip), %rax   # 0x553ab54
movss %xmm0, (%rax)
movss 0x4(%rbx,%rbp), %xmm0
movss %xmm0, 0x4(%rax)
movss 0x8(%rbx,%rbp), %xmm0
movss %xmm0, 0x8(%rax)
callq 0xb422
jmp 0x21c76
movss %xmm0, 0xc(%rbx,%rbp)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x10(%rbx,%rbp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x14(%rbx,%rbp)
incq %r15
movslq (%r13), %rax
addq $0x1c, %rbp
cmpq %rax, %r15
jl 0x21b31
incq %r12
cmpq $0x4, %r12
jne 0x21afe
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
jmp 0x21ad2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq