// File: FreeSlave[P]vhlt/hlrad/qrad.cpp
// Function: getScale(patch_s const*)
static vec_t    getScale(const patch_t* const patch)
{
    dface_t*        f = g_dfaces + patch->faceNumber;
    texinfo_t*      tx = &g_texinfo[f->texinfo];

    if (g_texscale)
    {
#ifdef ZHLT_FREETEXTUREAXIS
		const dplane_t*	faceplane = getPlaneFromFace (f);
		vec3_t			vecs_perpendicular[2];
		vec_t			scale[2];
		vec_t			dot;
		
		// snap texture "vecs" to faceplane without affecting texture alignment
		for (int x = 0; x < 2; x++)
		{
			dot = DotProduct (faceplane->normal, tx->vecs[x]);
			VectorMA (tx->vecs[x], -dot, faceplane->normal, vecs_perpendicular[x]);
		}
		
		scale[0] = 1 / qmax (NORMAL_EPSILON, VectorLength (vecs_perpendicular[0]));
		scale[1] = 1 / qmax (NORMAL_EPSILON, VectorLength (vecs_perpendicular[1]));

		// don't care about the angle between vecs[0] and vecs[1] (given the length of "vecs", smaller angle = larger texel area), because gridplanes will have the same angle (also smaller angle = larger patch area)

		return sqrt (scale[0] * scale[1]);
#else
        vec_t           scale[2];

        scale[0] = 0.0;
        scale[1] = 0.0;

        scale[0] += tx->vecs[0][0] * tx->vecs[0][0];
        scale[0] += tx->vecs[0][1] * tx->vecs[0][1];
        scale[0] += tx->vecs[0][2] * tx->vecs[0][2];

        scale[1] += tx->vecs[1][0] * tx->vecs[1][0];
        scale[1] += tx->vecs[1][1] * tx->vecs[1][1];
        scale[1] += tx->vecs[1][2] * tx->vecs[1][2];

        scale[0] = sqrt(scale[0]);
        scale[1] = sqrt(scale[1]);

        return 2.0 / ((scale[0] + scale[1]));
#endif
    }
    else
    {
        return 1.0;
    }
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq 0x58(%rsp), %rax
movslq 0x58(%rax), %rcx
leaq 0x1b8f0e7(%rip), %rax   # 0x1be6410
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movswq 0xa(%rax), %rcx
leaq 0x118f0ba(%rip), %rax   # 0x11e6400
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
testb $0x1, 0x2708f(%rip)     # 0x7e3e8
je 0x57685
movq 0x50(%rsp), %rdi
callq 0x5cfa0
movq %rax, 0x40(%rsp)
movl $0x0, 0x10(%rsp)
cmpl $0x2, 0x10(%rsp)
jge 0x574f5
movq 0x40(%rsp), %rax
movss (%rax), %xmm1
movq 0x48(%rsp), %rax
movslq 0x10(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss (%rax), %xmm2
movq 0x40(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x48(%rsp), %rax
movslq 0x10(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x40(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x48(%rsp), %rax
movslq 0x10(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x14(%rsp)
movq 0x48(%rsp), %rax
movslq 0x10(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss (%rax), %xmm1
movss 0x14(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x40(%rsp), %rax
movss (%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movslq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0x48(%rsp), %rax
movslq 0x10(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm1
movss 0x14(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x40(%rsp), %rax
movss 0x4(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movslq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0x48(%rsp), %rax
movslq 0x10(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm1
movss 0x14(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x40(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movslq 0x10(%rsp), %rcx
leaq 0x20(%rsp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x57376
movss 0x20(%rsp), %xmm0
mulss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x24(%rsp), %xmm1
mulss 0x24(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x28(%rsp), %xmm1
mulss 0x28(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movaps %xmm0, %xmm1
movsd 0x12b2b(%rip), %xmm0    # 0x6a068
ucomisd %xmm1, %xmm0
jbe 0x57553
movsd 0x12b1d(%rip), %xmm0    # 0x6a068
movsd %xmm0, 0x8(%rsp)
jmp 0x57596
movss 0x20(%rsp), %xmm0
mulss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x24(%rsp), %xmm1
mulss 0x24(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x28(%rsp), %xmm1
mulss 0x28(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm1
movsd 0x15c44(%rip), %xmm0    # 0x6d1e8
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x2c(%rsp), %xmm0
mulss 0x2c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x30(%rsp), %xmm1
mulss 0x30(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x34(%rsp), %xmm1
mulss 0x34(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movaps %xmm0, %xmm1
movsd 0x12a6e(%rip), %xmm0    # 0x6a068
ucomisd %xmm1, %xmm0
jbe 0x5760f
movsd 0x12a60(%rip), %xmm0    # 0x6a068
movsd %xmm0, (%rsp)
jmp 0x57651
movss 0x2c(%rsp), %xmm0
mulss 0x2c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x30(%rsp), %xmm1
mulss 0x30(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x34(%rsp), %xmm1
mulss 0x34(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movsd %xmm0, (%rsp)
movsd (%rsp), %xmm1
movsd 0x15b8a(%rip), %xmm0    # 0x6d1e8
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x18(%rsp), %xmm0
mulss 0x1c(%rsp), %xmm0
callq 0x8e40
movss %xmm0, 0x64(%rsp)
jmp 0x57693
movss 0x129bf(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x64(%rsp)
movss 0x64(%rsp), %xmm0
addq $0x68, %rsp
retq
nop