// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: CalcPoints(lightinfo_t*)
static void		CalcPoints(lightinfo_t* l)
{
	const int       facenum = l->surfnum;
	const dface_t*  f = g_dfaces + facenum;
	const dplane_t* p = getPlaneFromFace (f);
	const vec_t*    face_delta = g_face_offset[facenum];
	const eModelLightmodes lightmode = g_face_lightmode[facenum];
	const int       h = l->texsize[1] + 1;
	const int       w = l->texsize[0] + 1;
	const vec_t     starts = l->texmins[0] * TEXTURE_STEP;
	const vec_t     startt = l->texmins[1] * TEXTURE_STEP;
	light_flag_t    LuxelFlags[MAX_SINGLEMAP];
	light_flag_t*   pLuxelFlags;
	vec_t           us, ut;
	vec_t*          surf;
	int             s, t;
	l->numsurfpt = w * h;
	for (t = 0; t < h; t++)
	{
		for (s = 0; s < w; s++)
		{
			surf = l->surfpt[s+w*t];
			pLuxelFlags = &LuxelFlags[s+w*t];
			us = starts + s * TEXTURE_STEP;
			ut = startt + t * TEXTURE_STEP;
#ifdef HLRAD_BLUR_MINIMALSQUARE
			vec_t square[2][2];
			square[0][0] = us - TEXTURE_STEP;
			square[0][1] = ut - TEXTURE_STEP;
			square[1][0] = us + TEXTURE_STEP;
			square[1][1] = ut + TEXTURE_STEP;
#endif
#ifdef HLRAD_AVOIDWALLBLEED
			bool nudged;
#endif
			*pLuxelFlags = SetSampleFromST (surf,
#ifdef HLRAD_GROWSAMPLE
											l->surfpt_position[s+w*t], &l->surfpt_surface[s+w*t],
#endif
#ifdef HLRAD_AVOIDWALLBLEED
											&nudged,
#endif
											l, us, ut,
#ifdef HLRAD_GROWSAMPLE
#ifdef HLRAD_BLUR_MINIMALSQUARE
											square,
#else
											TEXTURE_STEP,
#endif
#endif
											lightmode);
		}
	}
    {
		int i, n;
		int s_other, t_other;
		light_flag_t* pLuxelFlags_other;
		vec_t* surf_other;
		bool adjusted;
		for (i = 0; i < h + w; i++)
		{ // propagate valid light samples
			adjusted = false;
			for (t = 0; t < h; t++)
			{
				for (s = 0; s < w; s++)
				{
					surf = l->surfpt[s+w*t];
					pLuxelFlags = &LuxelFlags[s+w*t];
					if (*pLuxelFlags != LightOutside)
						continue;
					for (n = 0; n < 4; n++)
					{
						switch (n)
						{
						case 0: s_other = s + 1; t_other = t; break;
						case 1: s_other = s - 1; t_other = t; break;
						case 2: s_other = s; t_other = t + 1; break;
						case 3: s_other = s; t_other = t - 1; break;
						}
						if (t_other < 0 || t_other >= h || s_other < 0 || s_other >= w)
							continue;
						surf_other = l->surfpt[s_other+w*t_other];
						pLuxelFlags_other = &LuxelFlags[s_other+w*t_other];
						if (*pLuxelFlags_other != LightOutside && *pLuxelFlags_other != LightShifted)
						{
							*pLuxelFlags = LightShifted;
							VectorCopy (surf_other, surf);
	#ifdef HLRAD_GROWSAMPLE
							VectorCopy (l->surfpt_position[s_other+w*t_other], l->surfpt_position[s+w*t]);
							l->surfpt_surface[s+w*t] = l->surfpt_surface[s_other+w*t_other];
	#endif
							adjusted = true;
							break;
						}
					}
				}
			}
			for (t = 0; t < h; t++)
			{
				for (s = 0; s < w; s++)
				{
					pLuxelFlags = &LuxelFlags[s+w*t];
					if (*pLuxelFlags == LightShifted)
					{
						*pLuxelFlags = LightShiftedInside;
					}
				}
			}
			if (!adjusted)
				break;
		}
	}
	for (int i = 0; i < MAX_SINGLEMAP; i++)
	{
		l->surfpt_lightoutside[i] = (LuxelFlags[i] == LightOutside);
	}
}

// Assembly:
subq $0x538, %rsp            # imm = 0x538
movq %rdi, 0x530(%rsp)
movq 0x530(%rsp), %rax
movl 0x135c(%rax), %eax
movl %eax, 0x52c(%rsp)
movslq 0x52c(%rsp), %rcx
leaq 0x1ba8ecd(%rip), %rax   # 0x1be6410
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x520(%rsp)
movq 0x520(%rsp), %rdi
callq 0x5cfa0
movq %rax, 0x518(%rsp)
movslq 0x52c(%rsp), %rcx
leaq 0x543049a(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x510(%rsp)
movslq 0x52c(%rsp), %rcx
leaq 0x537046c(%rip), %rax   # 0x53ada00
movl (%rax,%rcx,4), %eax
movl %eax, 0x50c(%rsp)
movq 0x530(%rsp), %rax
movl 0xf58(%rax), %eax
addl $0x1, %eax
movl %eax, 0x508(%rsp)
movq 0x530(%rsp), %rax
movl 0xf54(%rax), %eax
addl $0x1, %eax
movl %eax, 0x504(%rsp)
movq 0x530(%rsp), %rax
movl 0xf4c(%rax), %eax
shll $0x4, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x500(%rsp)
movq 0x530(%rsp), %rax
movl 0xf50(%rax), %eax
shll $0x4, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x4fc(%rsp)
movl 0x504(%rsp), %ecx
imull 0x508(%rsp), %ecx
movq 0x530(%rsp), %rax
movl %ecx, 0x2c(%rax)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x508(%rsp), %eax
jge 0x3d7ee
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x504(%rsp), %eax
jge 0x3d7dc
movq 0x530(%rsp), %rax
addq $0x30, %rax
movl 0x54(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x50(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movl 0x54(%rsp), %eax
movl 0x504(%rsp), %ecx
imull 0x50(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x70(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movss 0x500(%rsp), %xmm0
movl 0x54(%rsp), %eax
shll $0x4, %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0x64(%rsp)
movss 0x4fc(%rsp), %xmm0
movl 0x50(%rsp), %eax
shll $0x4, %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0x60(%rsp)
movss 0x64(%rsp), %xmm0
movss 0x2c951(%rip), %xmm1    # 0x6a044
subss %xmm1, %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x60(%rsp), %xmm0
movss 0x2c939(%rip), %xmm1    # 0x6a044
subss %xmm1, %xmm0
movss %xmm0, 0x44(%rsp)
movss 0x2c927(%rip), %xmm0    # 0x6a044
addss 0x64(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
movss 0x2c913(%rip), %xmm0    # 0x6a044
addss 0x60(%rsp), %xmm0
movss %xmm0, 0x4c(%rsp)
movq 0x58(%rsp), %rdi
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rsi
movl 0x54(%rsp), %eax
movl 0x504(%rsp), %ecx
imull 0x50(%rsp), %ecx
addl %ecx, %eax
cltq
imulq $0xc, %rax, %rax
addq %rax, %rsi
movq 0x530(%rsp), %rax
movq 0xdc8(%rax), %rdx
movl 0x54(%rsp), %eax
movl 0x504(%rsp), %ecx
imull 0x50(%rsp), %ecx
addl %ecx, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdx
movq 0x530(%rsp), %r8
movss 0x64(%rsp), %xmm0
movss 0x60(%rsp), %xmm1
leaq 0x40(%rsp), %r9
movl 0x50c(%rsp), %eax
leaq 0x3f(%rsp), %rcx
movl %eax, (%rsp)
callq 0x34dd0
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, (%rax)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3d645
jmp 0x3d7de
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x3d62c
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
movl 0x508(%rsp), %ecx
addl 0x504(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3dc5a
movb $0x0, 0x17(%rsp)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x508(%rsp), %eax
jge 0x3dbbc
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x504(%rsp), %eax
jge 0x3dbaa
movq 0x530(%rsp), %rax
addq $0x30, %rax
movl 0x54(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x50(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movl 0x54(%rsp), %eax
movl 0x504(%rsp), %ecx
imull 0x50(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x70(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
cmpl $0x0, (%rax)
je 0x3d8a9
jmp 0x3db9a
movl $0x0, 0x34(%rsp)
cmpl $0x4, 0x34(%rsp)
jge 0x3db98
movl 0x34(%rsp), %eax
movq %rax, 0x8(%rsp)
subq $0x3, %rax
ja 0x3d932
movq 0x8(%rsp), %rax
leaq 0x30379(%rip), %rcx     # 0x6dc50
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x3d932
movl 0x54(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x30(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x3d932
movl 0x54(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x3d932
movl 0x54(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x50(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jl 0x3d95a
movl 0x2c(%rsp), %eax
cmpl 0x508(%rsp), %eax
jge 0x3d95a
cmpl $0x0, 0x30(%rsp)
jl 0x3d95a
movl 0x30(%rsp), %eax
cmpl 0x504(%rsp), %eax
jl 0x3d95f
jmp 0x3db88
movq 0x530(%rsp), %rax
addq $0x30, %rax
movl 0x30(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x2c(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl 0x30(%rsp), %eax
movl 0x504(%rsp), %ecx
imull 0x2c(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x70(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x0, (%rax)
je 0x3db86
movq 0x20(%rsp), %rax
cmpl $0x1, (%rax)
je 0x3db86
movq 0x68(%rsp), %rax
movl $0x1, (%rax)
movq 0x18(%rsp), %rax
movss (%rax), %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, (%rax)
movq 0x18(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, 0x4(%rax)
movq 0x18(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, 0x8(%rax)
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rax
movl 0x30(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x2c(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rax
movl 0x54(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x50(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rax
movl 0x30(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x2c(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rax
movl 0x54(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x50(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rax
movl 0x30(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x2c(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x530(%rsp), %rax
movq 0xdc0(%rax), %rax
movl 0x54(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x50(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movq 0x530(%rsp), %rax
movq 0xdc8(%rax), %rax
movl 0x30(%rsp), %ecx
movl 0x504(%rsp), %edx
imull 0x2c(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x530(%rsp), %rax
movq 0xdc8(%rax), %rax
movl 0x54(%rsp), %ecx
movl 0x504(%rsp), %esi
imull 0x50(%rsp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movb $0x1, 0x17(%rsp)
jmp 0x3db98
jmp 0x3db88
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x3d8b1
jmp 0x3db9a
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3d836
jmp 0x3dbac
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x3d81d
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x508(%rsp), %eax
jge 0x3dc3f
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x504(%rsp), %eax
jge 0x3dc30
movl 0x54(%rsp), %eax
movl 0x504(%rsp), %ecx
imull 0x50(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rcx
leaq 0x70(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x3dc21
movq 0x68(%rsp), %rax
movl $0x2, (%rax)
jmp 0x3dc23
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3dbd9
jmp 0x3dc32
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x3dbc4
testb $0x1, 0x17(%rsp)
jne 0x3dc48
jmp 0x3dc5a
jmp 0x3dc4a
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x3d7f6
movl $0x0, 0x10(%rsp)
cmpl $0x121, 0x10(%rsp)      # imm = 0x121
jge 0x3dc9d
movslq 0x10(%rsp), %rax
cmpl $0x0, 0x70(%rsp,%rax,4)
sete %dl
movq 0x530(%rsp), %rax
movslq 0x10(%rsp), %rcx
andb $0x1, %dl
movb %dl, 0xdd0(%rax,%rcx)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x3dc62
addq $0x538, %rsp            # imm = 0x538
retq
nopw %cs:(%rax,%rax)