// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: CalcFaceExtents(lightinfo_t*)
static void     CalcFaceExtents(lightinfo_t* l)
{
    const int       facenum = l->surfnum;
    dface_t*        s;
    float           mins[2], maxs[2], val; //vec_t           mins[2], maxs[2], val; //vluzacn
    int             i, j, e;
    dvertex_t*      v;
    texinfo_t*      tex;

    s = l->face;

#ifdef ZHLT_64BIT_FIX
	mins[0] = mins[1] = 99999999;
	maxs[0] = maxs[1] = -99999999;
#else
    mins[0] = mins[1] = 999999;
    maxs[0] = maxs[1] = -99999; // a little small, but same with Goldsrc. --vluzacn
#endif

    tex = &g_texinfo[s->texinfo];

    for (i = 0; i < s->numedges; i++)
    {
        e = g_dsurfedges[s->firstedge + i];
        if (e >= 0)
        {
            v = g_dvertexes + g_dedges[e].v[0];
        }
        else
        {
            v = g_dvertexes + g_dedges[-e].v[1];
        }

        for (j = 0; j < 2; j++)
        {
            val = v->point[0] * tex->vecs[j][0] +
                v->point[1] * tex->vecs[j][1] + v->point[2] * tex->vecs[j][2] + tex->vecs[j][3];
            if (val < mins[j])
            {
                mins[j] = val;
            }
            if (val > maxs[j])
            {
                maxs[j] = val;
            }
        }
    }

    for (i = 0; i < 2; i++)
    {
        l->exactmins[i] = mins[i];
        l->exactmaxs[i] = maxs[i];

#ifndef ZHLT_64BIT_FIX
        mins[i] = floor(mins[i] / TEXTURE_STEP); //mins[i] = floor(mins[i] / 16.0); //--vluzacn
        maxs[i] = ceil(maxs[i] / TEXTURE_STEP); //maxs[i] = ceil(maxs[i] / 16.0); //--vluzacn

		l->texmins[i] = mins[i];
        l->texsize[i] = maxs[i] - mins[i];
#endif
	}
#ifdef ZHLT_64BIT_FIX
	int bmins[2];
	int bmaxs[2];
	GetFaceExtents (l->surfnum, bmins, bmaxs);
	for (i = 0; i < 2; i++)
	{
		mins[i] = bmins[i];
		maxs[i] = bmaxs[i];
		l->texmins[i] = bmins[i];
		l->texsize[i] = bmaxs[i] - bmins[i];
	}
#endif

	if (!(tex->flags & TEX_SPECIAL))
	{
		if ((l->texsize[0] > MAX_SURFACE_EXTENT) || (l->texsize[1] > MAX_SURFACE_EXTENT)
			|| l->texsize[0] < 0 || l->texsize[1] < 0 //--vluzacn
			)
		{
			ThreadLock();
			PrintOnce("\nfor Face %d (texture %s) at ", s - g_dfaces, TextureNameFromFace(s));

			for (i = 0; i < s->numedges; i++)
			{
				e = g_dsurfedges[s->firstedge + i];
				if (e >= 0)
                {
					v = g_dvertexes + g_dedges[e].v[0];
                }
				else
                {
					v = g_dvertexes + g_dedges[-e].v[1];
                }
#ifdef HLRAD_OVERWRITEVERTEX_FIX
				vec3_t pos;
				VectorAdd (v->point, g_face_offset[facenum], pos);
				Log ("(%4.3f %4.3f %4.3f) ", pos[0], pos[1], pos[2]);
#else
                VectorAdd(v->point, g_face_offset[facenum], v->point);
				Log("(%4.3f %4.3f %4.3f) ", v->point[0], v->point[1], v->point[2]);
#endif
			}
			Log("\n");

			Error( "Bad surface extents (%d x %d)\nCheck the file ZHLTProblems.html for a detailed explanation of this problem", l->texsize[0], l->texsize[1]);
		}
	}
#ifdef HLRAD_BLUR
	// allocate sample light cache
	{
		if (g_extra
	#ifdef HLRAD_FASTMODE
			&& !g_fastmode
	#endif
			)
		{
			l->lmcache_density = 3;
		}
		else
		{
			l->lmcache_density = 1;
		}
		l->lmcache_side = (int)ceil ((0.5 * g_blur * l->lmcache_density - 0.5) * (1 - NORMAL_EPSILON));
		l->lmcache_offset = l->lmcache_side;
		l->lmcachewidth = l->texsize[0] * l->lmcache_density + 1 + 2 * l->lmcache_side;
		l->lmcacheheight = l->texsize[1] * l->lmcache_density + 1 + 2 * l->lmcache_side;
	#ifdef HLRAD_AUTOCORING
		l->lmcache = (vec3_t (*)[ALLSTYLES])malloc (l->lmcachewidth * l->lmcacheheight * sizeof (vec3_t [ALLSTYLES]));
#ifdef ZHLT_XASH
		l->lmcache_direction = (vec3_t (*)[ALLSTYLES])malloc (l->lmcachewidth * l->lmcacheheight * sizeof (vec3_t [ALLSTYLES]));
#endif
	#else
		l->lmcache = (vec3_t (*)[MAXLIGHTMAPS])malloc (l->lmcachewidth * l->lmcacheheight * sizeof (vec3_t [MAXLIGHTMAPS]));
	#endif
		hlassume (l->lmcache != NULL, assume_NoMemory);
#ifdef ZHLT_XASH
		hlassume (l->lmcache_direction != NULL, assume_NoMemory);
#endif
#ifdef HLRAD_AVOIDNORMALFLIP
		l->lmcache_normal = (vec3_t *)malloc (l->lmcachewidth * l->lmcacheheight * sizeof (vec3_t));
		hlassume (l->lmcache_normal != NULL, assume_NoMemory);
#endif
#ifdef HLRAD_AVOIDWALLBLEED
		l->lmcache_wallflags = (int *)malloc (l->lmcachewidth * l->lmcacheheight * sizeof (int));
		hlassume (l->lmcache_wallflags != NULL, assume_NoMemory);
#endif
#ifdef HLRAD_GROWSAMPLE
		l->surfpt_position = (vec3_t *)malloc (MAX_SINGLEMAP * sizeof (vec3_t));
		l->surfpt_surface = (int *)malloc (MAX_SINGLEMAP * sizeof (int));
		hlassume (l->surfpt_position != NULL && l->surfpt_surface != NULL, assume_NoMemory);
#endif
	}
#endif
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq 0x70(%rsp), %rax
movl 0x135c(%rax), %eax
movl %eax, 0x6c(%rsp)
movq 0x70(%rsp), %rax
movq 0x1360(%rax), %rax
movq %rax, 0x60(%rsp)
movss 0x30e1f(%rip), %xmm0    # 0x6dbc0
movss %xmm0, 0x5c(%rsp)
movss 0x30e11(%rip), %xmm0    # 0x6dbc0
movss %xmm0, 0x58(%rsp)
movss 0x30dff(%rip), %xmm0    # 0x6dbbc
movss %xmm0, 0x54(%rsp)
movss 0x30df1(%rip), %xmm0    # 0x6dbbc
movss %xmm0, 0x50(%rsp)
movq 0x60(%rsp), %rax
movswq 0xa(%rax), %rcx
leaq 0x11a961e(%rip), %rax   # 0x11e6400
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
movq 0x60(%rsp), %rcx
movswl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3cf8d
movq 0x60(%rsp), %rax
movl 0x4(%rax), %eax
addl 0x48(%rsp), %eax
movslq %eax, %rcx
leaq 0x1e4360f(%rip), %rax   # 0x1e80430
movl (%rax,%rcx,4), %eax
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x40(%rsp)
jl 0x3ce57
movslq 0x40(%rsp), %rcx
leaq 0x1d295d5(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movslq %eax, %rcx
leaq 0x10295c7(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0x3ce82
xorl %eax, %eax
subl 0x40(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d295a9(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movslq %eax, %rcx
leaq 0x102959a(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movl $0x0, 0x44(%rsp)
cmpl $0x2, 0x44(%rsp)
jge 0x3cf7b
movq 0x38(%rsp), %rax
movss (%rax), %xmm1
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss (%rax), %xmm2
movq 0x38(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x38(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
addss 0xc(%rax), %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x4c(%rsp), %xmm1
movslq 0x44(%rsp), %rax
movss 0x58(%rsp,%rax,4), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x3cf46
movss 0x4c(%rsp), %xmm0
movslq 0x44(%rsp), %rax
movss %xmm0, 0x58(%rsp,%rax,4)
movss 0x4c(%rsp), %xmm0
movslq 0x44(%rsp), %rax
ucomiss 0x50(%rsp,%rax,4), %xmm0
jbe 0x3cf69
movss 0x4c(%rsp), %xmm0
movslq 0x44(%rsp), %rax
movss %xmm0, 0x50(%rsp,%rax,4)
jmp 0x3cf6b
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x3ce8a
jmp 0x3cf7d
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3cdf6
movl $0x0, 0x48(%rsp)
cmpl $0x2, 0x48(%rsp)
jge 0x3cfe5
movslq 0x48(%rsp), %rax
movss 0x58(%rsp,%rax,4), %xmm0
movq 0x70(%rsp), %rax
movslq 0x48(%rsp), %rcx
movss %xmm0, 0xf3c(%rax,%rcx,4)
movslq 0x48(%rsp), %rax
movss 0x50(%rsp,%rax,4), %xmm0
movq 0x70(%rsp), %rax
movslq 0x48(%rsp), %rcx
movss %xmm0, 0xf44(%rax,%rcx,4)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3cf95
movq 0x70(%rsp), %rax
movl 0x135c(%rax), %edi
leaq 0x28(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x6100
movl $0x0, 0x48(%rsp)
cmpl $0x2, 0x48(%rsp)
jge 0x3d084
movslq 0x48(%rsp), %rax
cvtsi2ssl 0x28(%rsp,%rax,4), %xmm0
movslq 0x48(%rsp), %rax
movss %xmm0, 0x58(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
cvtsi2ssl 0x20(%rsp,%rax,4), %xmm0
movslq 0x48(%rsp), %rax
movss %xmm0, 0x50(%rsp,%rax,4)
movslq 0x48(%rsp), %rax
movl 0x28(%rsp,%rax,4), %edx
movq 0x70(%rsp), %rax
movslq 0x48(%rsp), %rcx
movl %edx, 0xf4c(%rax,%rcx,4)
movslq 0x48(%rsp), %rax
movl 0x20(%rsp,%rax,4), %edx
movslq 0x48(%rsp), %rax
subl 0x28(%rsp,%rax,4), %edx
movq 0x70(%rsp), %rax
movslq 0x48(%rsp), %rcx
movl %edx, 0xf54(%rax,%rcx,4)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3d007
movq 0x30(%rsp), %rax
movl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x3d291
movq 0x70(%rsp), %rax
cmpl $0x10, 0xf54(%rax)
jg 0x3d0d4
movq 0x70(%rsp), %rax
cmpl $0x10, 0xf58(%rax)
jg 0x3d0d4
movq 0x70(%rsp), %rax
cmpl $0x0, 0xf54(%rax)
jl 0x3d0d4
movq 0x70(%rsp), %rax
cmpl $0x0, 0xf58(%rax)
jge 0x3d28f
callq 0xd220
movq 0x60(%rsp), %rax
leaq 0x1ba932b(%rip), %rcx   # 0x1be6410
subq %rcx, %rax
movl $0x14, %ecx
cqto
idivq %rcx
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
callq 0x43f10
movq 0x8(%rsp), %rsi
movq %rax, %rdx
leaq 0x31380(%rip), %rdi     # 0x6e490
movb $0x0, %al
callq 0xb8b0
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
movq 0x60(%rsp), %rcx
movswl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3d25d
movq 0x60(%rsp), %rax
movl 0x4(%rax), %eax
addl 0x48(%rsp), %eax
movslq %eax, %rcx
leaq 0x1e432e6(%rip), %rax   # 0x1e80430
movl (%rax,%rcx,4), %eax
movl %eax, 0x40(%rsp)
cmpl $0x0, 0x40(%rsp)
jl 0x3d180
movslq 0x40(%rsp), %rcx
leaq 0x1d292ac(%rip), %rax   # 0x1d66410
movzwl (%rax,%rcx,4), %eax
movslq %eax, %rcx
leaq 0x102929e(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0x3d1ab
xorl %eax, %eax
subl 0x40(%rsp), %eax
movslq %eax, %rcx
leaq 0x1d29280(%rip), %rax   # 0x1d66410
movzwl 0x2(%rax,%rcx,4), %eax
movslq %eax, %rcx
leaq 0x1029271(%rip), %rax   # 0x1066410
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movss (%rax), %xmm0
movslq 0x6c(%rsp), %rcx
leaq 0x5430850(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss (%rax), %xmm0
movss %xmm0, 0x14(%rsp)
movq 0x38(%rsp), %rax
movss 0x4(%rax), %xmm0
movslq 0x6c(%rsp), %rcx
leaq 0x5430829(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x4(%rax), %xmm0
movss %xmm0, 0x18(%rsp)
movq 0x38(%rsp), %rax
movss 0x8(%rax), %xmm0
movslq 0x6c(%rsp), %rcx
leaq 0x5430801(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x8(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x14(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x18(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x1c(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x31268(%rip), %rdi     # 0x6e4ae
movb $0x3, %al
callq 0xb0d0
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3d11f
leaq 0x2ea94(%rip), %rdi     # 0x6bcf8
movb $0x0, %al
callq 0xb0d0
movq 0x70(%rsp), %rax
movl 0xf54(%rax), %esi
movq 0x70(%rsp), %rax
movl 0xf58(%rax), %edx
leaq 0x3123b(%rip), %rdi     # 0x6e4c3
movb $0x0, %al
callq 0xb580
jmp 0x3d291
leaq 0x54f2789(%rip), %rax   # 0x552fa21
testb $0x1, (%rax)
je 0x3d2ba
leaq 0x527074c(%rip), %rax   # 0x52ad9f0
testb $0x1, (%rax)
jne 0x3d2ba
movq 0x70(%rsp), %rax
movl $0x3, 0x1368(%rax)
jmp 0x3d2c9
movq 0x70(%rsp), %rax
movl $0x1, 0x1368(%rax)
leaq 0x41174(%rip), %rax     # 0x7e444
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x30210(%rip), %xmm0    # 0x6d4f0
mulsd %xmm1, %xmm0
movq 0x70(%rsp), %rax
cvtsi2sdl 0x1368(%rax), %xmm1
mulsd %xmm1, %xmm0
movsd 0x30503(%rip), %xmm1    # 0x6d800
addsd %xmm1, %xmm0
movsd 0x308cf(%rip), %xmm1    # 0x6dbd8
mulsd %xmm1, %xmm0
callq 0x4090
cvttsd2si %xmm0, %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x1370(%rax)
movq 0x70(%rsp), %rax
movl 0x1370(%rax), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x136c(%rax)
movq 0x70(%rsp), %rax
movl 0xf54(%rax), %ecx
movq 0x70(%rsp), %rax
imull 0x1368(%rax), %ecx
addl $0x1, %ecx
movq 0x70(%rsp), %rax
movl 0x1370(%rax), %eax
shll %eax
addl %eax, %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x1390(%rax)
movq 0x70(%rsp), %rax
movl 0xf58(%rax), %ecx
movq 0x70(%rsp), %rax
imull 0x1368(%rax), %ecx
addl $0x1, %ecx
movq 0x70(%rsp), %rax
movl 0x1370(%rax), %eax
shll %eax
addl %eax, %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x1394(%rax)
movq 0x70(%rsp), %rax
movl 0x1390(%rax), %eax
movq 0x70(%rsp), %rcx
imull 0x1394(%rcx), %eax
cltq
imulq $0x300, %rax, %rdi      # imm = 0x300
callq 0x4430
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x1378(%rax)
movq 0x70(%rsp), %rax
cmpq $0x0, 0x1378(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x70(%rsp), %rax
movl 0x1390(%rax), %eax
movq 0x70(%rsp), %rcx
imull 0x1394(%rcx), %eax
cltq
imulq $0xc, %rax, %rdi
callq 0x4430
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x1380(%rax)
movq 0x70(%rsp), %rax
cmpq $0x0, 0x1380(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x70(%rsp), %rax
movl 0x1390(%rax), %eax
movq 0x70(%rsp), %rcx
imull 0x1394(%rcx), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x4430
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x1388(%rax)
movq 0x70(%rsp), %rax
cmpq $0x0, 0x1388(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movl $0xd8c, %edi            # imm = 0xD8C
callq 0x4430
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0xdc0(%rax)
movl $0x484, %edi            # imm = 0x484
callq 0x4430
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0xdc8(%rax)
movq 0x70(%rsp), %rcx
xorl %eax, %eax
cmpq $0x0, 0xdc0(%rcx)
movb %al, 0x7(%rsp)
je 0x3d4f1
movq 0x70(%rsp), %rax
cmpq $0x0, 0xdc8(%rax)
setne %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
addq $0x78, %rsp
retq
nopw (%rax,%rax)