// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1, %edi
movl $0x210, %esi            # imm = 0x210
callq 0x4200
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
cmpl $0x0, 0x1b8b27f(%rip)   # 0x1b91414
jle 0x635e
leaq 0x1b8b288(%rip), %rbp   # 0x1b9142a
xorl %r14d, %r14d
leaq 0x118b264(%rip), %r12   # 0x1191410
movswq (%rbp), %r15
leaq (%r15,%r15,4), %rax
movl 0x20(%r12,%rax,8), %edi
cmpl $-0x1, %edi
je 0x61cb
callq 0x6c59
cmpl $-0x1, %eax
cmovnel %eax, %r15d
leaq 0x20b927e(%rip), %r13   # 0x20bf450
cmpl $-0x1, %r15d
je 0x61f3
movslq %r15d, %rax
movq 0x82b2d6(%rip), %rcx    # 0x8314b8
leaq (%rax,%rax,4), %rax
movslq 0x20(%r12,%rax,8), %rax
movslq 0x4(%rcx,%rax,4), %r13
addq %rcx, %r13
movzbl (%r13), %eax
cmpl $0x21, %eax
je 0x6342
cmpl $0x73, %eax
jne 0x6218
cmpb $0x6b, 0x1(%r13)
jne 0x6218
cmpb $0x79, 0x2(%r13)
je 0x6342
movl $0x5, %edx
movq %r13, %rdi
leaq 0x1b82d(%rip), %rsi     # 0x21a54
callq 0x4400
testl %eax, %eax
je 0x6342
movl $0x5, %edx
movq %r13, %rdi
leaq 0x15fc3(%rip), %rsi     # 0x1c206
callq 0x4400
testl %eax, %eax
je 0x6342
movswq (%rbp), %r15
leaq (%r15,%r15,4), %rax
movl 0x20(%r12,%rax,8), %edi
cmpl $-0x1, %edi
je 0x626f
callq 0x6c59
cmpl $-0x1, %eax
cmovnel %eax, %r15d
movslq %r15d, %rax
leaq (%rax,%rax,4), %rax
testb $0x1, 0x24(%r12,%rax,8)
jne 0x6342
movl %r14d, %edi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x5c63
cmpw $0x0, -0x2(%rbp)
jle 0x62d9
movslq -0x6(%rbp), %rax
leaq 0x1e2519a(%rip), %rcx   # 0x1e2b440
movl (%rcx,%rax,4), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
leaq 0x1d0b169(%rip), %rdx   # 0x1d11420
leaq (%rdx,%rcx,4), %rcx
shrl $0x1f, %eax
movzwl (%rcx,%rax,2), %eax
leaq (%rax,%rax,2), %rax
leaq 0x100b153(%rip), %rcx   # 0x1011420
cvtps2pd (%rcx,%rax,4), %xmm0
cvtss2sd 0x8(%rcx,%rax,4), %xmm2
jmp 0x62e0
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 0x6330
movdqa %xmm0, %xmm1
punpckhqdq %xmm0, %xmm1    # xmm1 = xmm1[1],xmm0[1]
leaq 0x15ee5(%rip), %rdi     # 0x1c20c
movb $0x3, %al
callq 0xa4d6
jmp 0x6342
shrl $0x4, %esi
incl %esi
shrl $0x4, %edx
incl %edx
movq %rbx, %rdi
callq 0x5f28
incq %r14
movslq 0x1b8b0c8(%rip), %rax   # 0x1b91414
addq $0x14, %rbp
cmpq %rax, %r14
jl 0x61ac
testq %rbx, %rbx
je 0x637b
xorl %ebp, %ebp
movzbl 0x8(%rbx), %eax
addl %eax, %ebp
movq (%rbx), %r14
movq %rbx, %rdi
callq 0x4390
movq %r14, %rbx
testq %r14, %r14
jne 0x6360
jmp 0x637d
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq