// File: 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:
subq $0x68, %rsp
leaq 0x846d39(%rip), %rax    # 0x886494
movslq (%rax), %rdi
callq 0x4430
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x60(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x60(%rsp), %rdi
leaq 0x846d0b(%rip), %rax    # 0x886498
movq (%rax), %rsi
leaq 0x846cfd(%rip), %rax    # 0x886494
movslq (%rax), %rdx
callq 0x4280
leaq 0x846cee(%rip), %rax    # 0x886494
movl $0x0, (%rax)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
leaq 0x1ba6c45(%rip), %rcx   # 0x1be6404
cmpl (%rcx), %eax
jge 0x3fbd9
movslq 0x5c(%rsp), %rcx
leaq 0x1ba6c3d(%rip), %rax   # 0x1be6410
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movslq 0x5c(%rsp), %rcx
leaq 0x4f681e5(%rip), %rax   # 0x4fa79d0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rax
movswq 0xa(%rax), %rcx
leaq 0x11a6bf8(%rip), %rax   # 0x11e6400
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3f81f
jmp 0x3fbc9
movslq 0x5c(%rsp), %rcx
leaq 0x52ee1d5(%rip), %rax   # 0x532da00
movq (%rax,%rcx,8), %rdi
leaq 0x2ea66(%rip), %rsi     # 0x6e29c
callq 0x7f70
cmpl $0x0, %eax
je 0x3f881
leaq 0x846c4d(%rip), %rax    # 0x886494
movl (%rax), %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x10(%rax)
movl $0x0, 0x44(%rsp)
cmpl $0x4, 0x44(%rsp)
jge 0x3f87c
movq 0x50(%rsp), %rax
movslq 0x44(%rsp), %rcx
movb $-0x1, 0xc(%rax,%rcx)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x3f859
jmp 0x3fbc9
movq 0x50(%rsp), %rax
cmpl $-0x1, 0x10(%rax)
jne 0x3f891
jmp 0x3fbc9
movq 0x50(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x38(%rsp)
leaq 0x846bf0(%rip), %rax    # 0x886494
movl (%rax), %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x10(%rax)
movl $0x0, 0x3c(%rsp)
cmpl $0x4, 0x3c(%rsp)
jge 0x3f8f0
movq 0x50(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movb 0xc(%rax,%rcx), %cl
movslq 0x3c(%rsp), %rax
movb %cl, 0x34(%rsp,%rax)
movq 0x50(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movb $-0x1, 0xc(%rax,%rcx)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x3f8b6
movl $0x0, 0x30(%rsp)
movl $0x0, 0x3c(%rsp)
xorl %eax, %eax
cmpl $0x4, 0x3c(%rsp)
movb %al, 0x1f(%rsp)
jge 0x3f923
movslq 0x3c(%rsp), %rax
movzbl 0x34(%rsp,%rax), %eax
cmpl $0xff, %eax
setne %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x3f930
jmp 0x3fbaa
movb $0x0, 0x2f(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x3fadd
movq 0x60(%rsp), %rax
movl 0x38(%rsp), %ecx
movq 0x48(%rsp), %rdx
imull $0x3, (%rdx), %edx
imull 0x3c(%rsp), %edx
addl %edx, %ecx
imull $0x3, 0x40(%rsp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movzbl 0x2f(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rax
movzbl (%rax), %eax
movl %eax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movzbl 0x1(%rax), %eax
movq 0x20(%rsp), %rcx
movzbl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jle 0x3f9b1
movq 0x20(%rsp), %rax
movb 0x1(%rax), %al
movb %al, 0x13(%rsp)
jmp 0x3f9bd
movq 0x20(%rsp), %rax
movb 0x2(%rax), %al
movb %al, 0x13(%rsp)
movl 0x18(%rsp), %eax
movb 0x13(%rsp), %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jle 0x3f9d9
movq 0x20(%rsp), %rax
movb (%rax), %al
movb %al, 0x12(%rsp)
jmp 0x3fa11
movq 0x20(%rsp), %rax
movzbl 0x1(%rax), %eax
movq 0x20(%rsp), %rcx
movzbl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jle 0x3f9fd
movq 0x20(%rsp), %rax
movb 0x1(%rax), %al
movb %al, 0x11(%rsp)
jmp 0x3fa09
movq 0x20(%rsp), %rax
movb 0x2(%rax), %al
movb %al, 0x11(%rsp)
movb 0x11(%rsp), %al
movb %al, 0x12(%rsp)
movl 0x14(%rsp), %eax
movb 0x12(%rsp), %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jle 0x3fa2d
movb 0x2f(%rsp), %al
movb %al, 0x10(%rsp)
jmp 0x3fac5
movq 0x20(%rsp), %rax
movzbl (%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %rax
movzbl 0x1(%rax), %eax
movq 0x20(%rsp), %rcx
movzbl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jle 0x3fa5d
movq 0x20(%rsp), %rax
movb 0x1(%rax), %al
movb %al, 0xb(%rsp)
jmp 0x3fa69
movq 0x20(%rsp), %rax
movb 0x2(%rax), %al
movb %al, 0xb(%rsp)
movl 0xc(%rsp), %eax
movb 0xb(%rsp), %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jle 0x3fa85
movq 0x20(%rsp), %rax
movb (%rax), %al
movb %al, 0xa(%rsp)
jmp 0x3fabd
movq 0x20(%rsp), %rax
movzbl 0x1(%rax), %eax
movq 0x20(%rsp), %rcx
movzbl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jle 0x3faa9
movq 0x20(%rsp), %rax
movb 0x1(%rax), %al
movb %al, 0x9(%rsp)
jmp 0x3fab5
movq 0x20(%rsp), %rax
movb 0x2(%rax), %al
movb %al, 0x9(%rsp)
movb 0x9(%rsp), %al
movb %al, 0xa(%rsp)
movb 0xa(%rsp), %al
movb %al, 0x10(%rsp)
movb 0x10(%rsp), %al
movb %al, 0x2f(%rsp)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x3f93d
movzbl 0x2f(%rsp), %eax
cmpl $0x0, %eax
jg 0x3faec
jmp 0x3fb9a
movslq 0x3c(%rsp), %rax
movb 0x34(%rsp,%rax), %dl
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
movb %dl, 0xc(%rax,%rcx)
leaq 0x84698a(%rip), %rax    # 0x886494
movl (%rax), %eax
movq 0x48(%rsp), %rcx
imull $0x3, (%rcx), %ecx
movl 0x30(%rsp), %edx
addl $0x1, %edx
imull %edx, %ecx
addl %ecx, %eax
leaq 0x3d81d(%rip), %rcx     # 0x7d344
cmpl (%rcx), %eax
setle %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x38, %esi
callq 0xbf00
leaq 0x846955(%rip), %rax    # 0x886498
movq (%rax), %rdi
movq 0x50(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x48(%rsp), %rcx
imull $0x3, (%rcx), %ecx
imull 0x30(%rsp), %ecx
addl %ecx, %eax
cltq
addq %rax, %rdi
movq 0x60(%rsp), %rsi
movl 0x38(%rsp), %eax
movq 0x48(%rsp), %rcx
imull $0x3, (%rcx), %ecx
imull 0x3c(%rsp), %ecx
addl %ecx, %eax
cltq
addq %rax, %rsi
movq 0x48(%rsp), %rax
imull $0x3, (%rax), %eax
movslq %eax, %rdx
callq 0x4280
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x3f900
movq 0x48(%rsp), %rax
imull $0x3, (%rax), %ecx
imull 0x30(%rsp), %ecx
leaq 0x8468d6(%rip), %rax    # 0x886494
addl (%rax), %ecx
leaq 0x8468cd(%rip), %rax    # 0x886494
movl %ecx, (%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3f7b4
movq 0x60(%rsp), %rdi
callq 0x43a0
addq $0x68, %rsp
retq
nopl (%rax,%rax)