// File: /workspace/github_cpp_makefile_star2_O2/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 0x83032e(%rip), %r14    # 0x84d494
movslq (%r14), %rdi
callq 0x4420
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x92cb
leaq 0x83030f(%rip), %rax    # 0x84d498
movq (%rax), %rsi
movslq (%r14), %rdx
movq %rbx, %rdi
callq 0x4240
andl $0x0, (%r14)
leaq 0x1b9026e(%rip), %r15   # 0x1bad410
leaq 0xc(%r15), %r14
xorl %r12d, %r12d
leaq 0x1b90254(%rip), %rax   # 0x1bad404
movslq (%rax), %rax
cmpq %rax, %r12
jge 0x1d3b0
imulq $0x14, %r12, %r13
movswq 0xa(%r15,%r13), %rax
imulq $0x28, %rax, %rax
leaq 0x119022f(%rip), %rcx   # 0x11ad400
testb $0x1, 0x24(%rcx,%rax)
jne 0x1d3a4
addq %r15, %r13
leaq 0x52d780a(%rip), %rax   # 0x52f49f0
movq (%rax,%r12,8), %rdi
leaq 0x19027(%rip), %rsi     # 0x36218
callq 0x6a5d
testl %eax, %eax
je 0x1d21d
leaq 0x830293(%rip), %rax    # 0x84d494
movl (%rax), %eax
movl %eax, 0x10(%r13)
xorl %eax, %eax
cmpq $0x4, %rax
je 0x1d3a4
movb $-0x1, (%r14,%rax)
incq %rax
jmp 0x1d209
movq %r13, %rcx
movl 0x10(%r13), %r13d
cmpl $-0x1, %r13d
je 0x1d3a4
imulq $0x28, %r12, %rdx
leaq 0x4f51787(%rip), %rax   # 0x4f6e9c0
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
leaq 0x83024c(%rip), %rax    # 0x84d494
movl (%rax), %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x10(%rcx)
xorl %eax, %eax
cmpq $0x4, %rax
je 0x1d26c
movb (%r14,%rax), %cl
movb %cl, 0x4(%rsp,%rax)
movb $-0x1, (%r14,%rax)
incq %rax
jmp 0x1d254
movq %r12, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r11d, %r11d
movq 0x8(%rsp), %r12
movl %r13d, (%rsp)
cmpq $0x4, %r15
je 0x1d383
movb 0x4(%rsp,%r15), %cl
movl (%r12), %eax
cmpb $-0x1, %cl
je 0x1d387
xorl %edi, %edi
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
movl %eax, %esi
imull %ebp, %esi
addl %r13d, %esi
subq $0x1, %rdx
jb 0x1d2ed
movslq %esi, %rsi
movzbl (%rbx,%rsi), %r8d
movzbl 0x1(%rbx,%rsi), %r9d
movzbl %dil, %edi
movzbl 0x2(%rbx,%rsi), %r10d
cmpb %r10b, %r9b
cmoval %r9d, %r10d
cmpb %r10b, %r8b
cmoval %r8d, %r10d
cmpb %dil, %r10b
cmovbel %edi, %r10d
addl $0x3, %esi
movl %r10d, %edi
jmp 0x1d2b2
testb %dil, %dil
je 0x1d378
movslq %r11d, %r13
movq 0x18(%rsp), %r12
movb %cl, 0xc(%r12,%r13)
movq %rbp, 0x20(%rsp)
leal 0x1(%r13), %ebp
imull %ebp, %eax
leal (%rax,%rax,2), %eax
leaq 0x83017b(%rip), %rcx    # 0x84d494
addl (%rcx), %eax
xorl %edi, %edi
leaq 0x27020(%rip), %rcx     # 0x44344
cmpl (%rcx), %eax
setle %dil
pushq $0x38
popq %rsi
callq 0x92cb
movslq 0x10(%r12), %rax
movq 0x8(%rsp), %r12
movslq (%r12), %rcx
leaq (%rcx,%rcx,2), %rdx
imull %edx, %r13d
movslq %r13d, %rdi
movl (%rsp), %r13d
addq %rax, %rdi
leaq 0x83013f(%rip), %rax    # 0x84d498
addq (%rax), %rdi
movl %r15d, %eax
imull %edx, %eax
addl %r13d, %eax
movslq %eax, %rsi
addq %rbx, %rsi
callq 0x4240
movq %rbp, %r11
movq 0x20(%rsp), %rbp
incq %r15
addl $0x3, %ebp
jmp 0x1d282
movl (%r12), %eax
imull %eax, %r11d
leal (%r11,%r11,2), %eax
leaq 0x8300fe(%rip), %rcx    # 0x84d494
addl %eax, (%rcx)
leaq 0x1b90071(%rip), %r15   # 0x1bad410
movq 0x10(%rsp), %r12
incq %r12
addq $0x14, %r14
jmp 0x1d1a9
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4380