// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: AddSamplesToPatches(sample_t const**, unsigned char const*, int, lightinfo_t const*)
static void AddSamplesToPatches (const sample_t **samples, const unsigned char *styles, int facenum, const lightinfo_t *l)
{
#ifndef HLRAD_GatherPatchLight
    if (g_numbounce == 0)
    {
        return;
    }
#endif
	patch_t *patch;
	int i, j, m, k;
	int numtexwindings;
	Winding **texwindings;

	numtexwindings = 0;
	for (patch = g_face_patches[facenum]; patch; patch = patch->next)
	{
		numtexwindings++;
	}
	texwindings = (Winding **)malloc (numtexwindings * sizeof (Winding *));
	hlassume (texwindings != NULL, assume_NoMemory);

	// translate world winding into winding in s,t plane
	for (j = 0, patch = g_face_patches[facenum]; j < numtexwindings; j++, patch = patch->next)
	{
		Winding *w = new Winding (patch->winding->m_NumPoints);
		for (int x = 0; x < w->m_NumPoints; x++)
		{
			vec_t s, t;
			SetSTFromSurf (l, patch->winding->m_Points[x], s, t);
			w->m_Points[x][0] = s;
			w->m_Points[x][1] = t;
			w->m_Points[x][2] = 0.0;
		}
		w->RemoveColinearPoints ();
		texwindings[j] = w;
	}
	
	for (i = 0; i < l->numsurfpt; i++)
	{
		// prepare clip planes
		vec_t s_vec, t_vec;
		s_vec = l->texmins[0] * TEXTURE_STEP + (i % (l->texsize[0] + 1)) * TEXTURE_STEP;
		t_vec = l->texmins[1] * TEXTURE_STEP + (i / (l->texsize[0] + 1)) * TEXTURE_STEP;

		dplane_t clipplanes[4];
		VectorClear (clipplanes[0].normal);
		clipplanes[0].normal[0] = 1;
		clipplanes[0].dist = s_vec - 0.5 * TEXTURE_STEP;
		VectorClear (clipplanes[1].normal);
		clipplanes[1].normal[0] = -1;
		clipplanes[1].dist = -(s_vec + 0.5 * TEXTURE_STEP);
		VectorClear (clipplanes[2].normal);
		clipplanes[2].normal[1] = 1;
		clipplanes[2].dist = t_vec - 0.5 * TEXTURE_STEP;
		VectorClear (clipplanes[3].normal);
		clipplanes[3].normal[1] = -1;
		clipplanes[3].dist = -(t_vec + 0.5 * TEXTURE_STEP);

		// clip each patch
		for (j = 0, patch = g_face_patches[facenum]; j < numtexwindings; j++, patch = patch->next)
		{
			Winding *w = new Winding (*texwindings[j]);
			for (k = 0; k < 4; k++)
			{
				if (w->m_NumPoints)
				{
					w->Clip (clipplanes[k], false);
				}
			}
			if (w->m_NumPoints)
			{
				// add sample to patch
				vec_t area = w->getArea () / (TEXTURE_STEP * TEXTURE_STEP);
				patch->samples += area;
				for (m = 0; m < ALLSTYLES && styles[m] != 255; m++)
				{
					int style = styles[m];
					const sample_t *s = &samples[m][i];
					for (k = 0; k < ALLSTYLES && patch->totalstyle_all[k] != 255; k++)
					{
						if (patch->totalstyle_all[k] == style)
						{
							break;
						}
					}
					if (k == ALLSTYLES)
					{
			#ifdef HLRAD_READABLE_EXCEEDSTYLEWARNING
						if (++stylewarningcount >= stylewarningnext)
						{
							stylewarningnext = stylewarningcount * 2;
							Warning("Too many direct light styles on a face(?,?,?)\n");
							Warning(" total %d warnings for too many styles", stylewarningcount);
						}
			#else
						Warning("Too many direct light styles on a face(?,?,?)\n");
			#endif
					}
					else
					{
						if (patch->totalstyle_all[k] == 255)
						{
							patch->totalstyle_all[k] = style;
						}
						VectorMA (patch->samplelight_all[k], area, s->light, patch->samplelight_all[k]);
			#ifdef ZHLT_XASH
						VectorMA (patch->samplelight_all_direction[k], area, s->light_direction, patch->samplelight_all_direction[k]);
			#endif
					}
				}
			}
			delete w;
		}
	}

	for (j = 0; j < numtexwindings; j++)
	{
		delete texwindings[j];
	}
	free (texwindings);
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movl %edx, 0x104(%rsp)
movq %rcx, 0xf8(%rsp)
movl $0x0, 0xdc(%rsp)
movslq 0x104(%rsp), %rcx
leaq 0x526fd10(%rip), %rax   # 0x52ada00
movq (%rax,%rcx,8), %rax
movq %rax, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
je 0x3dd2d
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
jmp 0x3dcfc
movslq 0xdc(%rsp), %rdi
shlq $0x3, %rdi
callq 0x4430
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movl $0x0, 0xe8(%rsp)
movslq 0x104(%rsp), %rcx
leaq 0x526fc84(%rip), %rax   # 0x52ada00
movq (%rax,%rcx,8), %rax
movq %rax, 0xf0(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x3df3f
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x30(%rsp)
movq 0xf0(%rsp), %rax
movq 0x28(%rax), %rax
movl 0x8(%rax), %esi
callq 0xe980
jmp 0x3ddc7
movq 0x30(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
movq 0xc8(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x3dee5
movq 0xf8(%rsp), %rdi
movq 0xf0(%rsp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rsi
movslq 0xb8(%rsp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rsi
leaq 0xb4(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x43f70
movss 0xb4(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xb8(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movss 0xb0(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xb8(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0xc8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xb8(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rax)
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x3dddf
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x3e65c
movq 0xc8(%rsp), %rdi
movss 0x2fc9b(%rip), %xmm0    # 0x6db90
callq 0xf2b0
movq 0xc8(%rsp), %rdx
movq 0xd0(%rsp), %rax
movslq 0xe8(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
jmp 0x3dd88
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movq 0xf8(%rsp), %rcx
cmpl 0x2c(%rcx), %eax
jge 0x3e5ed
movq 0xf8(%rsp), %rax
movl 0xf4c(%rax), %eax
shll $0x4, %eax
movl %eax, 0x28(%rsp)
movl 0xec(%rsp), %eax
movq 0xf8(%rsp), %rcx
movl 0xf54(%rcx), %ecx
addl $0x1, %ecx
cltd
idivl %ecx
movl 0x28(%rsp), %eax
shll $0x4, %edx
addl %edx, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0xac(%rsp)
movq 0xf8(%rsp), %rax
movl 0xf50(%rax), %eax
shll $0x4, %eax
movl %eax, 0x2c(%rsp)
movl 0xec(%rsp), %eax
movq 0xf8(%rsp), %rcx
movl 0xf54(%rcx), %ecx
addl $0x1, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x2c(%rsp), %eax
shll $0x4, %ecx
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0xa8(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x58(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x54(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x50(%rsp)
movss 0x2c039(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x50(%rsp)
movss 0xac(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2fc1a(%rip), %xmm1    # 0x6dc48
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x5c(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x6c(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x68(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x64(%rsp)
movss 0x2f4b5(%rip), %xmm0    # 0x6d514
movss %xmm0, 0x64(%rsp)
movss 0xac(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2fbce(%rip), %xmm1    # 0x6dc48
addsd %xmm1, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x7c(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x78(%rsp)
movss 0x2bf87(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x7c(%rsp)
movss 0xa8(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2fb68(%rip), %xmm1    # 0x6dc48
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x84(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x94(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x90(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x8c(%rsp)
movss 0x2f3f7(%rip), %xmm0    # 0x6d514
movss %xmm0, 0x90(%rsp)
movss 0xa8(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2fb0d(%rip), %xmm1    # 0x6dc48
addsd %xmm1, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x98(%rsp)
movl $0x0, 0xe8(%rsp)
movslq 0x104(%rsp), %rcx
leaq 0x526f883(%rip), %rax   # 0x52ada00
movq (%rax,%rcx,8), %rax
movq %rax, 0xf0(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x3e5d5
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq 0xd0(%rsp), %rax
movslq 0xe8(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0xea00
jmp 0x3e1cd
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0xe0(%rsp)
cmpl $0x4, 0xe0(%rsp)
jge 0x3e25e
movq 0x48(%rsp), %rax
cmpl $0x0, 0x8(%rax)
je 0x3e249
movq 0x48(%rsp), %rdi
movslq 0xe0(%rsp), %rax
leaq 0x50(%rsp), %rsi
imulq $0x14, %rax, %rax
addq %rax, %rsi
xorl %edx, %edx
movss 0x2f976(%rip), %xmm0    # 0x6db90
callq 0x10450
jmp 0x3e249
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x3e65c
jmp 0x3e24b
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x3e1e2
movq 0x48(%rsp), %rax
cmpl $0x0, 0x8(%rax)
je 0x3e58f
movq 0x48(%rsp), %rdi
callq 0xdcb0
movss 0x2f945(%rip), %xmm1    # 0x6dbc4
divss %xmm1, %xmm0
movss %xmm0, 0x44(%rsp)
movss 0x44(%rsp), %xmm0
movq 0xf0(%rsp), %rax
addss 0x108(%rax), %xmm0
movss %xmm0, 0x108(%rax)
movl $0x0, 0xe4(%rsp)
xorl %eax, %eax
cmpl $0x40, 0xe4(%rsp)
movb %al, 0x1f(%rsp)
jge 0x3e2e2
movq 0x108(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
setne %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x3e2ef
jmp 0x3e58d
movq 0x108(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x40(%rsp)
movq 0x110(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0xec(%rsp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0xe0(%rsp)
xorl %eax, %eax
cmpl $0x40, 0xe0(%rsp)
movb %al, 0x1e(%rsp)
jge 0x3e371
movq 0xf0(%rsp), %rax
movq 0x118(%rax), %rax
movslq 0xe0(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
setne %al
movb %al, 0x1e(%rsp)
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x3e37b
jmp 0x3e3b3
movq 0xf0(%rsp), %rax
movq 0x118(%rax), %rax
movslq 0xe0(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl 0x40(%rsp), %eax
jne 0x3e39e
jmp 0x3e3b3
jmp 0x3e3a0
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x3e33a
cmpl $0x40, 0xe0(%rsp)
jne 0x3e41b
leaq 0x55317a8(%rip), %rax   # 0x556fb6c
movl (%rax), %eax
addl $0x1, %eax
leaq 0x553179c(%rip), %rcx   # 0x556fb6c
movl %eax, (%rcx)
leaq 0x4006f(%rip), %rcx     # 0x7e448
cmpl (%rcx), %eax
jl 0x3e416
leaq 0x5531788(%rip), %rax   # 0x556fb6c
movl (%rax), %ecx
shll %ecx
leaq 0x40059(%rip), %rax     # 0x7e448
movl %ecx, (%rax)
leaq 0x30135(%rip), %rdi     # 0x6e52d
movb $0x0, %al
callq 0xb9f0
leaq 0x5531766(%rip), %rax   # 0x556fb6c
movl (%rax), %esi
leaq 0x2fe66(%rip), %rdi     # 0x6e275
movb $0x0, %al
callq 0xb9f0
jmp 0x3e575
movq 0xf0(%rsp), %rax
movq 0x118(%rax), %rax
movslq 0xe0(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
jne 0x3e45d
movl 0x40(%rsp), %eax
movb %al, %dl
movq 0xf0(%rsp), %rax
movq 0x118(%rax), %rax
movslq 0xe0(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0xf0(%rsp), %rax
movq 0x110(%rax), %rax
movslq 0xe0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm1
movss 0x44(%rsp), %xmm0
movq 0x38(%rsp), %rax
movss 0xc(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xf0(%rsp), %rax
movq 0x110(%rax), %rax
movslq 0xe0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0xf0(%rsp), %rax
movq 0x110(%rax), %rax
movslq 0xe0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm1
movss 0x44(%rsp), %xmm0
movq 0x38(%rsp), %rax
movss 0x10(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xf0(%rsp), %rax
movq 0x110(%rax), %rax
movslq 0xe0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0xf0(%rsp), %rax
movq 0x110(%rax), %rax
movslq 0xe0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm1
movss 0x44(%rsp), %xmm0
movq 0x38(%rsp), %rax
movss 0x14(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0xf0(%rsp), %rax
movq 0x110(%rax), %rax
movslq 0xe0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
jmp 0x3e577
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x3e2b2
jmp 0x3e58f
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3e5aa
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x3e5ac
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
jmp 0x3e189
jmp 0x3e5d7
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3df4a
movl $0x0, 0xe8(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x3e647
movq 0xd0(%rsp), %rax
movslq 0xe8(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3e632
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x3e634
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x3e5f8
movq 0xd0(%rsp), %rdi
callq 0x43a0
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xc0(%rsp), %rdi
callq 0x45c0
nopl (%rax)