// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: ReduceLightmap()
void ReduceLightmap ()
{
	byte *oldlightdata = (byte *)malloc (g_lightdatasize);
	hlassume (oldlightdata != NULL, assume_NoMemory);
	memcpy (oldlightdata, g_dlightdata, g_lightdatasize);
#ifdef ZHLT_XASH
	if (g_dlitdatasize != g_lightdatasize)
	{
		Error ("g_dlitdatasize != g_lightdatasize");
	}
	byte *olddlitdata = (byte *)malloc (g_dlitdatasize);
	hlassume (olddlitdata != NULL, assume_NoMemory);
	memcpy (olddlitdata, g_ddlitdata, g_dlitdatasize);
	g_dlitdatasize = 0;
#endif
	g_lightdatasize = 0;

	int facenum;
	for (facenum = 0; facenum < g_numfaces; facenum++)
	{
		dface_t *f = &g_dfaces[facenum];
		facelight_t *fl = &facelight[facenum];
		if (g_texinfo[f->texinfo].flags & TEX_SPECIAL)
		{
			continue;                                      // non-lit texture
		}
#ifdef HLRAD_ENTSTRIPRAD
		// just need to zero the lightmap so that it won't contribute to lightdata size
		if (IntForKey (g_face_entity[facenum], "zhlt_striprad"))
		{
			f->lightofs = g_lightdatasize;
			for (int k = 0; k < MAXLIGHTMAPS; k++)
			{
				f->styles[k] = 255;
			}
			continue;
		}
#endif
#if 0 //debug. --vluzacn
		const char *lightmapcolor = ValueForKey (g_face_entity[facenum], "zhlt_rad");
		if (*lightmapcolor)
		{
			hlassume (MAXLIGHTMAPS == 4, assume_first);
			int styles[4], values[4][3];
			if (sscanf (lightmapcolor, "%d=%d,%d,%d %d=%d,%d,%d %d=%d,%d,%d %d=%d,%d,%d"
					, &styles[0], &values[0][0], &values[0][1], &values[0][2]
					, &styles[1], &values[1][0], &values[1][1], &values[1][2]
					, &styles[2], &values[2][0], &values[2][1], &values[2][2]
					, &styles[3], &values[3][0], &values[3][1], &values[3][2]
				) != 16)
			{
				Error ("Bad value for 'zhlt_rad'.");
			}
			f->lightofs = g_lightdatasize;
			int i, k;
			for (k = 0; k < 4; k++)
			{
				f->styles[k] = 255;
			}
			for (k = 0; k < 4 && styles[k] != 255; k++)
			{
				f->styles[k] = styles[k];
				hlassume (g_lightdatasize + fl->numsamples * 3 <= g_max_map_lightdata, assume_MAX_MAP_LIGHTING);
		#ifdef ZHLT_XASH
				hlassume (g_dlitdatasize + fl->numsamples * 3 <= g_max_map_dlitdata, assume_MAX_MAP_LIGHTING);
		#endif
				for (i = 0; i < fl->numsamples; i++)
				{
					VectorCopy (values[k], (byte *)&g_dlightdata[g_lightdatasize + i * 3]);
		#ifdef ZHLT_XASH
					VectorFill ((byte *)&g_ddlitdata[g_lightdatasize + i * 3], 128);
		#endif
				}
				g_lightdatasize += fl->numsamples * 3;
		#ifdef ZHLT_XASH
				g_dlitdatasize += fl->numsamples * 3;
		#endif
			}
			continue;
		}
#endif
		if (f->lightofs == -1)
		{
			continue;
		}

		int i, k;
		int oldofs;
		unsigned char oldstyles[MAXLIGHTMAPS];
		oldofs = f->lightofs;
		f->lightofs = g_lightdatasize;
		for (k = 0; k < MAXLIGHTMAPS; k++)
		{
			oldstyles[k] = f->styles[k];
			f->styles[k] = 255;
		}
		int numstyles = 0;
		for (k = 0; k < MAXLIGHTMAPS && oldstyles[k] != 255; k++)
		{
			unsigned char maxb = 0;
			for (i = 0; i < fl->numsamples; i++)
			{
				unsigned char *v = &oldlightdata[oldofs + fl->numsamples * 3 * k + i * 3];
				maxb = qmax (maxb, VectorMaximum (v));
			}
			if (maxb <= 0) // black
			{
				continue;
			}
			f->styles[numstyles] = oldstyles[k];
			hlassume (g_lightdatasize + fl->numsamples * 3 * (numstyles + 1) <= g_max_map_lightdata, assume_MAX_MAP_LIGHTING);
			memcpy (&g_dlightdata[f->lightofs + fl->numsamples * 3 * numstyles], &oldlightdata[oldofs + fl->numsamples * 3 * k], fl->numsamples * 3);
#ifdef ZHLT_XASH
			hlassume (g_dlitdatasize + fl->numsamples * 3 * (numstyles + 1) <= g_max_map_dlitdata, assume_MAX_MAP_LIGHTING);
			memcpy (&g_ddlitdata[f->lightofs + fl->numsamples * 3 * numstyles], &olddlitdata[oldofs + fl->numsamples * 3 * k], fl->numsamples * 3);
#endif
			numstyles++;
		}
		g_lightdatasize += fl->numsamples * 3 * numstyles;
#ifdef ZHLT_XASH
		g_dlitdatasize += fl->numsamples * 3 * numstyles;
#endif
	}
	free (oldlightdata);
#ifdef ZHLT_XASH
	free (olddlitdata);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x830762(%rip), %r14    # 0x851484
movslq (%r14), %rdi
callq 0x4410
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
leaq 0x830741(%rip), %rax    # 0x851488
movq (%rax), %rsi
movslq (%r14), %rdx
movq %rbx, %rdi
callq 0x4250
movl $0x0, (%r14)
leaq 0x1b90691(%rip), %r15   # 0x1bb13f4
cmpl $0x0, (%r15)
jle 0x20f59
xorl %ebp, %ebp
leaq 0x1b9068a(%rip), %r12   # 0x1bb1400
leaq (,%rbp,4), %rax
leaq (%rax,%rax,4), %r14
movswq 0xa(%r12,%r14), %rax
leaq (%rax,%rax,4), %rax
leaq 0x119065d(%rip), %rcx   # 0x11b13f0
testb $0x1, 0x24(%rcx,%rax,8)
jne 0x20f4a
leaq (%r12,%r14), %r13
leaq 0x52d7c37(%rip), %rax   # 0x52f89e0
movq (%rax,%rbp,8), %rdi
leaq 0x1b524(%rip), %rsi     # 0x3c2d8
callq 0x76a7
testl %eax, %eax
je 0x20dd8
leaq 0x8306c0(%rip), %rax    # 0x851484
movl (%rax), %eax
movl %eax, 0x10(%r13)
movl $0xffffffff, 0xc(%r12,%r14) # imm = 0xFFFFFFFF
jmp 0x20f4a
movl 0x10(%r13), %eax
movl %eax, 0x8(%rsp)
cmpl $-0x1, %eax
je 0x20f4a
leaq (,%rbp,4), %rax
addq %rbp, %rax
leaq 0x4f51bb5(%rip), %rcx   # 0x4f729b0
leaq (%rcx,%rax,8), %r14
leaq 0x83067e(%rip), %rax    # 0x851484
movl (%rax), %eax
movl %eax, 0x10(%r13)
movl 0xc(%r13), %eax
movl %eax, 0xc(%rsp)
movq %r13, 0x18(%rsp)
movl $0xffffffff, 0xc(%r13)  # imm = 0xFFFFFFFF
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r11d, %r11d
movl 0x8(%rsp), %r13d
movq %r14, 0x10(%rsp)
movb 0xc(%rsp,%r12), %cl
cmpb $-0x1, %cl
je 0x20f2b
movslq (%r14), %rax
testq %rax, %rax
jle 0x20f1a
movl %eax, %edx
imull %r15d, %edx
addl %r13d, %edx
xorl %esi, %esi
movq %rax, %rdi
movslq %edx, %rdx
movzbl (%rbx,%rdx), %r8d
movzbl 0x1(%rbx,%rdx), %r9d
movzbl 0x2(%rbx,%rdx), %r10d
cmpb %r10b, %r9b
cmoval %r9d, %r10d
cmpb %r10b, %r8b
cmoval %r8d, %r10d
cmpb %r10b, %sil
movzbl %sil, %esi
cmovbel %r10d, %esi
addl $0x3, %edx
decq %rdi
jne 0x20e5c
testb %sil, %sil
je 0x20f1a
movslq %r11d, %r13
movq 0x18(%rsp), %r14
movb %cl, 0xc(%r14,%r13)
leal 0x1(%r13), %ecx
movq %rcx, 0x20(%rsp)
imull %ecx, %eax
leal (%rax,%rax,2), %eax
leaq 0x8305c7(%rip), %rcx    # 0x851484
addl (%rcx), %eax
xorl %edi, %edi
leaq 0x2746c(%rip), %rcx     # 0x48334
cmpl (%rcx), %eax
setle %dil
movl $0x38, %esi
callq 0xa7c9
movslq 0x10(%r14), %rax
movq 0x10(%rsp), %r14
movslq (%r14), %rcx
leaq (%rcx,%rcx,2), %rdx
imull %edx, %r13d
movslq %r13d, %rdi
movl 0x8(%rsp), %r13d
addq %rax, %rdi
leaq 0x83058a(%rip), %rax    # 0x851488
addq (%rax), %rdi
movl %r12d, %eax
imull %edx, %eax
addl %r13d, %eax
movslq %eax, %rsi
addq %rbx, %rsi
callq 0x4250
movq 0x20(%rsp), %r11
incq %r12
addl $0x3, %r15d
cmpq $0x4, %r12
jne 0x20e34
imull (%r14), %r11d
leal (%r11,%r11,2), %eax
leaq 0x83054a(%rip), %rcx    # 0x851484
addl %eax, (%rcx)
leaq 0x1b904b1(%rip), %r15   # 0x1bb13f4
leaq 0x1b904b6(%rip), %r12   # 0x1bb1400
incq %rbp
movslq (%r15), %rax
cmpq %rax, %rbp
jl 0x20d76
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4380