// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/common/bspfile.cpp
// Function: CountBlocks()
int CountBlocks ()
{
#ifdef ZHLT_64BIT_FIX
#if !defined (PLATFORM_CAN_CALC_EXTENT) && !defined (HLRAD)
	return -1; // otherwise GetFaceExtents will error
#endif
#endif
	lightmapblock_t *blocks;
	blocks = (lightmapblock_t *)malloc (sizeof (lightmapblock_t));
	hlassume (blocks != NULL, assume_NoMemory);
	memset (blocks, 0, sizeof (lightmapblock_t));
	int k;
	for (k = 0; k < g_numfaces; k++)
	{
		dface_t *f = &g_dfaces[k];
#ifdef ZHLT_EMBEDLIGHTMAP
		const char *texname =  GetTextureByNumber (ParseTexinfoForFace (f));
#else
		const char *texname =  GetTextureByNumber (f->texinfo);
#endif
		if (!strncmp (texname, "sky", 3) //sky, no lightmap allocation.
			|| !strncmp (texname, "!", 1) || !strncasecmp (texname, "water", 5) || !strncasecmp (texname, "laser", 5) //water, no lightmap allocation.
#ifdef ZHLT_EMBEDLIGHTMAP
			|| (g_texinfo[ParseTexinfoForFace (f)].flags & TEX_SPECIAL) //aaatrigger, I don't know.
#else
			|| (g_texinfo[f->texinfo].flags & TEX_SPECIAL) //aaatrigger, I don't know.
#endif
			)
		{
			continue;
		}
		int extents[2];
		vec3_t point;
		{
#ifdef ZHLT_64BIT_FIX
			int bmins[2];
			int bmaxs[2];
			int i;
			GetFaceExtents (k, bmins, bmaxs);
			for (i = 0; i < 2; i++)
			{
				extents[i] = (bmaxs[i] - bmins[i]) * TEXTURE_STEP;
			}

			VectorClear (point);
			if (f->numedges > 0)
			{
				int e = g_dsurfedges[f->firstedge];
				dvertex_t *v = &g_dvertexes[g_dedges[abs (e)].v[e >= 0? 0: 1]];
				VectorCopy (v->point, point);
			}
#else
			float mins[2], maxs[2];
			int bmins[2], bmaxs[2];
			texinfo_t *tex;
			tex = &g_texinfo[f->texinfo];
			mins[0] = mins[1] = 999999;
			maxs[0] = maxs[1] = -99999;
			VectorClear (point);
			int i;
			for (i = 0; i < f->numedges; i++)
			{
				int e;
				dvertex_t *v;
				int j;
				e = g_dsurfedges[f->firstedge + i];
				if (e >= 0)
				{
					v = &g_dvertexes[g_dedges[e].v[0]];
				}
				else
				{
					v = &g_dvertexes[g_dedges[-e].v[1]];
				}
				if (i == 0)
				{
					VectorCopy (v->point, point);
				}
				for (j = 0; j < 2; j++)
				{
					float 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++)
			{
				bmins[i] = floor (mins[i] / TEXTURE_STEP);
				bmaxs[i] = ceil (maxs[i] / TEXTURE_STEP);
				extents[i] = (bmaxs[i] - bmins[i]) * TEXTURE_STEP;
			}
#endif
		}
		if (extents[0] < 0 || extents[1] < 0 || extents[0] > qmax (512, MAX_SURFACE_EXTENT * TEXTURE_STEP) || extents[1] > qmax (512, MAX_SURFACE_EXTENT * TEXTURE_STEP))
			// the default restriction from the engine is 512, but place 'max (512, MAX_SURFACE_EXTENT * TEXTURE_STEP)' here in case someone raise the limit
		{
			Warning ("Bad surface extents %d/%d at position (%.0f,%.0f,%.0f)", extents[0], extents[1], point[0], point[1], point[2]);
			continue;
		}
		DoAllocBlock (blocks, (extents[0] / TEXTURE_STEP) + 1, (extents[1] / TEXTURE_STEP) + 1);
	}
	int count = 0;
	lightmapblock_t *next;
	for (; blocks; blocks = next)
	{
		if (blocks->used)
		{
			count++;
		}
		next = blocks->next;
		free (blocks);
	}
	return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0x210, %edi            # imm = 0x210
callq 0x4430
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
xorl %ebp, %ebp
movl $0x210, %edx            # imm = 0x210
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41c0
cmpl $0x0, 0x1b8b294(%rip)   # 0x1b91424
jle 0x636b
leaq 0x1b8b29d(%rip), %r12   # 0x1b9143a
xorl %r14d, %r14d
movswq (%r12), %r15
leaq (%r15,%r15,4), %rax
leaq 0x118b270(%rip), %rcx   # 0x1191420
movl 0x20(%rcx,%rax,8), %edi
cmpl $-0x1, %edi
je 0x61c5
callq 0x6ccd
cmpl $-0x1, %eax
cmovnel %eax, %r15d
leaq 0x20b9294(%rip), %r13   # 0x20bf460
cmpl $-0x1, %r15d
je 0x61f4
movslq %r15d, %rax
movq 0x82b2ec(%rip), %rcx    # 0x8314c8
leaq (%rax,%rax,4), %rax
leaq 0x118b239(%rip), %rdx   # 0x1191420
movslq 0x20(%rdx,%rax,8), %rax
movslq 0x4(%rcx,%rax,4), %r13
addq %rcx, %r13
movl $0x3, %edx
movq %r13, %rdi
leaq 0x1994a(%rip), %rsi     # 0x1fb4d
callq 0x41a0
testl %eax, %eax
je 0x6354
cmpb $0x21, (%r13)
je 0x6354
movl $0x5, %edx
movq %r13, %rdi
leaq 0x1b805(%rip), %rsi     # 0x21a2f
callq 0x4410
testl %eax, %eax
je 0x6354
movl $0x5, %edx
movq %r13, %rdi
leaq 0x15fc8(%rip), %rsi     # 0x1c20e
callq 0x4410
testl %eax, %eax
je 0x6354
movswq (%r12), %r15
leaq (%r15,%r15,4), %rax
leaq 0x118b1bd(%rip), %rcx   # 0x1191420
movl 0x20(%rcx,%rax,8), %edi
cmpl $-0x1, %edi
je 0x6278
callq 0x6ccd
cmpl $-0x1, %eax
cmovnel %eax, %r15d
movslq %r15d, %rax
leaq (%rax,%rax,4), %rax
leaq 0x118b19a(%rip), %rcx   # 0x1191420
testb $0x1, 0x24(%rcx,%rax,8)
jne 0x6354
movl %r14d, %edi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x5c7a
cmpw $0x0, -0x2(%r12)
jle 0x62eb
movslq -0x6(%r12), %rax
leaq 0x1e25198(%rip), %rcx   # 0x1e2b450
movl (%rcx,%rax,4), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
leaq 0x1d0b167(%rip), %rdx   # 0x1d11430
leaq (%rdx,%rcx,4), %rcx
shrl $0x1f, %eax
movzwl (%rcx,%rax,2), %eax
leaq (%rax,%rax,2), %rax
leaq 0x100b151(%rip), %rcx   # 0x1011430
cvtps2pd (%rcx,%rax,4), %xmm0
cvtss2sd 0x8(%rcx,%rax,4), %xmm2
jmp 0x62f2
pxor %xmm0, %xmm0
xorps %xmm2, %xmm2
movq 0x8(%rsp), %xmm1
movq 0x10(%rsp), %xmm3
psubd %xmm3, %xmm1
pslld $0x4, %xmm1
movd %xmm1, %esi
pshufd $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movd %xmm1, %edx
movl $0x201, %ecx            # imm = 0x201
cmpl %ecx, %esi
setae %al
cmpl %ecx, %edx
setae %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x6342
movdqa %xmm0, %xmm1
punpckhqdq %xmm0, %xmm1    # xmm1 = xmm1[1],xmm0[1]
leaq 0x15edb(%rip), %rdi     # 0x1c214
movb $0x3, %al
callq 0xa612
jmp 0x6354
shrl $0x4, %esi
incl %esi
shrl $0x4, %edx
incl %edx
movq %rbx, %rdi
callq 0x5f1e
incq %r14
movslq 0x1b8b0c6(%rip), %rax   # 0x1b91424
addq $0x14, %r12
cmpq %rax, %r14
jl 0x61a0
testq %rbx, %rbx
je 0x638b
xorl %ebp, %ebp
movzbl 0x8(%rbx), %eax
addl %eax, %ebp
movq (%rbx), %r14
movq %rbx, %rdi
callq 0x43a0
movq %r14, %rbx
testq %r14, %r14
jne 0x6372
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq