// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movslq %edi, %rax
leaq (%rax,%rax,4), %rax
leaq 0x1b8b797(%rip), %rcx   # 0x1b91420
leaq (%rcx,%rax,4), %r14
movsd 0x163ab(%rip), %xmm0    # 0x1c040
movsd %xmm0, 0x10(%rsp)
movsd 0x163ad(%rip), %xmm0    # 0x1c050
movsd %xmm0, 0x8(%rsp)
movswq 0xa(%r14), %rbx
leaq (%rbx,%rbx,4), %rax
leaq 0x118b757(%rip), %r15   # 0x1191410
movl 0x20(%r15,%rax,8), %edi
cmpl $-0x1, %edi
je 0x5cce
callq 0x6c59
cmpl $-0x1, %eax
cmovnel %eax, %ebx
movswq 0x8(%r14), %rax
movq %rax, 0x40(%rsp)
testq %rax, %rax
jle 0x5da7
leaq 0x14(%rsp), %r12
movslq %ebx, %rax
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rbx
movslq 0x4(%r14), %rax
movq %rax, 0x38(%rsp)
xorl %ecx, %ecx
movq 0x38(%rsp), %rax
movq %rcx, 0x48(%rsp)
addq %rcx, %rax
leaq 0x1e25730(%rip), %rcx   # 0x1e2b440
movslq (%rcx,%rax,4), %rax
movl %eax, %ecx
negl %ecx
testq %rax, %rax
leaq 0x1d0b6fe(%rip), %rdx   # 0x1d11420
leaq (%rdx,%rax,4), %rax
leaq 0x2(%rdx,%rcx,4), %rcx
cmovnsq %rax, %rcx
movzwl (%rcx), %eax
leaq (%rax,%rax,2), %rax
leaq 0x100b6e3(%rip), %rcx   # 0x1011420
leaq (%rcx,%rax,4), %r15
movb $0x1, %r14b
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
xorl %esi, %esi
shlq $0x4, %rsi
addq %rbx, %rsi
movq %r15, %rdi
callq 0x5b56
movss (%rbp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x5d6e
movss %xmm0, (%rbp)
ucomiss (%r13), %xmm0
jbe 0x5d7b
movss %xmm0, (%r13)
movl $0x1, %esi
leaq 0xc(%rsp), %r13
movq %r12, %rbp
testb $0x1, %r14b
movl $0x0, %r14d
jne 0x5d50
movq 0x48(%rsp), %rcx
incq %rcx
cmpq 0x40(%rsp), %rcx
jne 0x5cfc
movss 0x10(%rsp), %xmm0
mulss 0x162eb(%rip), %xmm0    # 0x1c0a0
callq 0x4160
cvttss2si %xmm0, %ebx
movl %ebx, 0x54(%rsp)
movss 0x8(%rsp), %xmm0
mulss 0x162d0(%rip), %xmm0    # 0x1c0a0
callq 0x41d0
cvttss2si %xmm0, %eax
movl %eax, 0x20(%rsp)
movss 0x14(%rsp), %xmm0
mulss 0x162b5(%rip), %xmm0    # 0x1c0a0
callq 0x4160
cvttss2si %xmm0, %eax
movl %eax, 0x24(%rsp)
movss 0x162a0(%rip), %xmm0    # 0x1c0a0
mulss 0xc(%rsp), %xmm0
callq 0x41d0
cvttss2si %xmm0, %eax
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rdx
movl %ebx, (%rdx)
movl 0x20(%rsp), %eax
movq 0x30(%rsp), %rcx
movl %eax, (%rcx)
movl 0x24(%rsp), %eax
movl %eax, 0x4(%rdx)
movl 0x1c(%rsp), %eax
movl %eax, 0x4(%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq