// File: /workspace/github_cpp_makefile_star2_O3/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 0x830425(%rip), %r14    # 0x850484
movslq (%r14), %rdi
callq 0x4400
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
leaq 0x830404(%rip), %rax    # 0x850488
movq (%rax), %rsi
movslq (%r14), %rdx
movq %rbx, %rdi
callq 0x4240
movl $0x0, (%r14)
leaq 0x1b90354(%rip), %rdx   # 0x1bb03f4
cmpl $0x0, (%rdx)
jle 0x202c2
xorl %ecx, %ecx
leaq 0x1b9034e(%rip), %r12   # 0x1bb0400
leaq 0x1190337(%rip), %r13   # 0x11b03f0
leaq (,%rcx,4), %rax
leaq (%rax,%rax,4), %r14
movswq 0xa(%r12,%r14), %rax
leaq (%rax,%rax,4), %rax
testb $0x1, 0x24(%r13,%rax,8)
jne 0x202b3
movq %rdx, %r13
leaq (%r12,%r14), %r15
leaq 0x52d78f7(%rip), %rax   # 0x52f79e0
movq %rcx, (%rsp)
movq (%rax,%rcx,8), %rdi
leaq 0x1b160(%rip), %rsi     # 0x3b258
callq 0x7663
testl %eax, %eax
je 0x2012a
leaq 0x83037c(%rip), %rax    # 0x850484
movl (%rax), %eax
movl %eax, 0x10(%r15)
movl $0xffffffff, 0xc(%r12,%r14) # imm = 0xFFFFFFFF
movq (%rsp), %rcx
movq %r13, %rdx
leaq 0x11902cb(%rip), %r13   # 0x11b03f0
jmp 0x202b3
movl 0x10(%r15), %ebp
cmpl $-0x1, %ebp
movq (%rsp), %rcx
movq %r13, %rdx
leaq 0x11902b1(%rip), %r13   # 0x11b03f0
je 0x202b3
leaq (%rcx,%rcx,4), %rax
leaq 0x4f51860(%rip), %rcx   # 0x4f719b0
leaq (%rcx,%rax,8), %rcx
leaq 0x830329(%rip), %rax    # 0x850484
movl (%rax), %eax
movl %eax, 0x10(%r15)
movl 0xc(%r15), %eax
movl %eax, 0x14(%rsp)
movq %r15, 0x20(%rsp)
movl $0xffffffff, 0xc(%r15)  # imm = 0xFFFFFFFF
movq %rcx, 0x18(%rsp)
movl (%rcx), %r13d
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r10d, %r10d
movl %ebp, 0xc(%rsp)
movb 0x14(%rsp,%r15), %al
cmpb $-0x1, %al
je 0x20288
testl %r13d, %r13d
jle 0x20277
movl %r13d, %ecx
movl %r13d, %edx
imull %r14d, %edx
addl %ebp, %edx
xorl %esi, %esi
movslq %edx, %rdx
movzbl (%rbx,%rdx), %edi
movzbl 0x1(%rbx,%rdx), %r8d
movzbl %sil, %esi
movzbl 0x2(%rbx,%rdx), %r9d
cmpb %r9b, %r8b
cmoval %r8d, %r9d
cmpb %r9b, %dil
cmoval %edi, %r9d
cmpb %sil, %r9b
cmovbel %esi, %r9d
movl %r9d, %esi
addl $0x3, %edx
decq %rcx
jne 0x201af
testb %sil, %sil
je 0x20277
movslq %r10d, %r12
movq 0x20(%rsp), %rbp
movb %al, 0xc(%rbp,%r12)
leal 0x1(%r12), %eax
movl %eax, 0x10(%rsp)
imull %eax, %r13d
leal (%r13,%r13,2), %eax
leaq 0x83026f(%rip), %rcx    # 0x850484
addl (%rcx), %eax
xorl %edi, %edi
leaq 0x27114(%rip), %rcx     # 0x47334
cmpl (%rcx), %eax
setle %dil
movl $0x38, %esi
callq 0xa6ed
movslq 0x10(%rbp), %rax
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %rcx
movslq (%rcx), %r13
leaq (,%r13,2), %rdx
addq %r13, %rdx
imull %edx, %r12d
movslq %r12d, %rdi
addq %rax, %rdi
leaq 0x83022c(%rip), %rax    # 0x850488
addq (%rax), %rdi
movl %r15d, %eax
imull %edx, %eax
addl %ebp, %eax
movslq %eax, %rsi
addq %rbx, %rsi
callq 0x4240
movl 0x10(%rsp), %r10d
incq %r15
addl $0x3, %r14d
cmpq $0x4, %r15
jne 0x2018b
imull %r10d, %r13d
leal (%r13,%r13,2), %eax
leaq 0x8301ec(%rip), %rcx    # 0x850484
addl %eax, (%rcx)
leaq 0x1b90153(%rip), %rdx   # 0x1bb03f4
leaq 0x1b90158(%rip), %r12   # 0x1bb0400
leaq 0x1190141(%rip), %r13   # 0x11b03f0
movq (%rsp), %rcx
incq %rcx
movslq (%rdx), %rax
cmpq %rax, %rcx
jl 0x200b9
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4370