// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/common/bspfile.cpp
// Function: GetFaceExtents(int, int*, int*)
void GetFaceExtents (int facenum, int mins_out[2], int maxs_out[2])
{
	CorrectFPUPrecision ();

	dface_t *f;
	float mins[2], maxs[2], val;
	int i, j, e;
	dvertex_t *v;
	texinfo_t *tex;
	int bmins[2], bmaxs[2];

	f = &g_dfaces[facenum];

	mins[0] = mins[1] = 999999;
	maxs[0] = maxs[1] = -99999;

#ifdef ZHLT_EMBEDLIGHTMAP
	tex = &g_texinfo[ParseTexinfoForFace (f)];
#else
	tex = &g_texinfo[f->texinfo];
#endif

	for (i = 0; i < f->numedges; i++)
	{
		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]];
		}
		for (j = 0; j < 2; j++)
		{
			// The old code: 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];
			//   was meant to be compiled for x86 under MSVC (prior to VS 11), so the intermediate values were stored as 64-bit double by default.
			// The new code will produce the same result as the old code, but it's portable for different platforms.
			// See this article for details: Intermediate Floating-Point Precision by Bruce-Dawson http://www.altdevblogaday.com/2012/03/22/intermediate-floating-point-precision/

			// The essential reason for having this ugly code is to get exactly the same value as the counterpart of game engine.
			// The counterpart of game engine is the function CalcFaceExtents in HLSDK.
			// So we must also know how Valve compiles HLSDK. I think Valve compiles HLSDK with VC6.0 in the past.
			val = CalculatePointVecsProduct (v->point, tex->vecs[j]);
			if (val < mins[j])
			{
				mins[j] = val;
			}
			if (val > maxs[j])
			{
				maxs[j] = val;
			}
		}
	}

	for (i = 0; i < 2; i++)
	{
		bmins[i] = (int)floor (mins[i] / TEXTURE_STEP);
		bmaxs[i] = (int)ceil (maxs[i] / TEXTURE_STEP);
	}

	for (i = 0; i < 2; i++)
	{
		mins_out[i] = bmins[i];
		maxs_out[i] = bmaxs[i];
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movslq %edi, %rax
leaq (%rax,%rax,4), %rax
leaq 0x1b8b790(%rip), %rcx   # 0x1b91430
leaq (%rcx,%rax,4), %rbx
movsd 0x16394(%rip), %xmm0    # 0x1c040
movsd %xmm0, 0x8(%rsp)
movsd 0x16396(%rip), %xmm0    # 0x1c050
movsd %xmm0, (%rsp)
movswq 0xa(%rbx), %r14
leaq (%r14,%r14,4), %rax
leaq 0x118b751(%rip), %r15   # 0x1191420
movl 0x20(%r15,%rax,8), %edi
cmpl $-0x1, %edi
je 0x5ce5
callq 0x6ccd
cmpl $-0x1, %eax
cmovnel %eax, %r14d
movswq 0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
jle 0x5d91
movslq %r14d, %rax
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rax
movq %rax, 0x10(%rsp)
movslq 0x4(%rbx), %rbx
xorl %r14d, %r14d
leaq 0x1d0b71a(%rip), %r13   # 0x1d11430
leaq (%r14,%rbx), %rax
leaq 0x1e2572f(%rip), %rcx   # 0x1e2b450
movslq (%rcx,%rax,4), %rax
movl %eax, %ecx
negl %ecx
testq %rax, %rax
leaq (%r13,%rax,4), %rax
leaq 0x2(%r13,%rcx,4), %rcx
cmovnsq %rax, %rcx
movzwl (%rcx), %eax
leaq (%rax,%rax,2), %rax
leaq 0x100b6e8(%rip), %rcx   # 0x1011430
leaq (%rcx,%rax,4), %r15
movq 0x10(%rsp), %r12
xorl %ebp, %ebp
movq %r15, %rdi
movq %r12, %rsi
callq 0x5b6d
movss 0x8(%rsp,%rbp,4), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x5d6f
movss %xmm0, 0x8(%rsp,%rbp,4)
ucomiss (%rsp,%rbp,4), %xmm0
jbe 0x5d7a
movss %xmm0, (%rsp,%rbp,4)
incq %rbp
addq $0x10, %r12
cmpq $0x1, %rbp
je 0x5d53
incq %r14
cmpq 0x20(%rsp), %r14
jne 0x5d16
movsd 0x8(%rsp), %xmm0
mulps 0x162c2(%rip), %xmm0    # 0x1c060
movaps %xmm0, 0x10(%rsp)
callq 0x4160
movaps %xmm0, 0x20(%rsp)
movaps 0x10(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
callq 0x4160
movaps 0x20(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvttps2dq %xmm1, %xmm0
movlps %xmm0, 0x50(%rsp)
movsd (%rsp), %xmm0
mulps 0x16288(%rip), %xmm0    # 0x1c060
movaps %xmm0, 0x10(%rsp)
callq 0x41d0
movaps %xmm0, 0x20(%rsp)
movaps 0x10(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
callq 0x41d0
movaps 0x20(%rsp), %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvttps2dq %xmm1, %xmm0
movlps %xmm0, 0x48(%rsp)
xorl %eax, %eax
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
movl 0x50(%rsp,%rax,4), %ecx
movl %ecx, (%rsi,%rax,4)
movl 0x48(%rsp,%rax,4), %ecx
movl %ecx, (%rdx,%rax,4)
incq %rax
cmpq $0x1, %rax
je 0x5e12
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq