// File: FreeSlave[P]vhlt/hlrad/lightmap.cpp
// Function: CalcFaceVectors(lightinfo_t*)
static void     CalcFaceVectors(lightinfo_t* l)
{
    texinfo_t*      tex;
    int             i, j;
    vec3_t          texnormal;
    vec_t           distscale;
    vec_t           dist, len;

    tex = &g_texinfo[l->face->texinfo];

    // convert from float to double
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 3; j++)
        {
            l->worldtotex[i][j] = tex->vecs[i][j];
        }
    }

    // calculate a normal to the texture axis.  points can be moved along this
    // without changing their S/T
    CrossProduct(tex->vecs[1], tex->vecs[0], texnormal);
    VectorNormalize(texnormal);

    // flip it towards plane normal
    distscale = DotProduct(texnormal, l->facenormal);
    if (distscale == 0.0)
    {
        const unsigned facenum = l->face - g_dfaces;
    
        ThreadLock();
        Log("Malformed face (%d) normal @ \n", facenum);
        Winding* w = new Winding(*l->face);
        {
            const unsigned numpoints = w->m_NumPoints;
            unsigned x;
            for (x=0; x<numpoints; x++)
            {
                VectorAdd(w->m_Points[x], g_face_offset[facenum], w->m_Points[x]);
            }
        }
        w->Print();
        delete w;
        ThreadUnlock();

        hlassume(false, assume_MalformedTextureFace);
    }

    if (distscale < 0)
    {
        distscale = -distscale;
        VectorSubtract(vec3_origin, texnormal, texnormal);
    }

    // distscale is the ratio of the distance along the texture normal to
    // the distance along the plane normal
    distscale = 1.0 / distscale;

#ifdef ZHLT_FREETEXTUREAXIS
	for (i = 0; i < 2; i++)
	{
		CrossProduct (l->worldtotex[!i], l->facenormal, l->textoworld[i]);
		len = DotProduct (l->textoworld[i], l->worldtotex[i]);
		VectorScale (l->textoworld[i], 1 / len, l->textoworld[i]);
	}
#else
    for (i = 0; i < 2; i++)
    {
        len = (float)VectorLength(l->worldtotex[i]);
        dist = DotProduct(l->worldtotex[i], l->facenormal);
        dist *= distscale;
        VectorMA(l->worldtotex[i], -dist, texnormal, l->textoworld[i]);
        VectorScale(l->textoworld[i], (1 / len) * (1 / len), l->textoworld[i]);
    }
#endif

    // calculate texorg on the texture plane
    for (i = 0; i < 3; i++)
    {
        l->texorg[i] = -tex->vecs[0][3] * l->textoworld[0][i] - tex->vecs[1][3] * l->textoworld[1][i];
    }

    // project back to the face plane
#ifdef HLRAD_GROWSAMPLE
    dist = DotProduct(l->texorg, l->facenormal) - l->facedist;
#else
    dist = DotProduct(l->texorg, l->facenormal) - l->facedist - DEFAULT_HUNT_OFFSET;
#endif
    dist *= distscale;
    VectorMA(l->texorg, -dist, texnormal, l->texorg);
#ifdef HLRAD_CalcPoints_NEW
	VectorCopy (texnormal, l->texnormal);
#endif

}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq 0x1360(%rax), %rax
movswq 0xa(%rax), %rcx
leaq 0x11aa02f(%rip), %rax   # 0x11e6400
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
cmpl $0x2, 0x54(%rsp)
jge 0x3c453
movl $0x0, 0x50(%rsp)
cmpl $0x3, 0x50(%rsp)
jge 0x3c444
movq 0x58(%rsp), %rax
movslq 0x54(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movslq 0x50(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq 0x50(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x3c3f4
jmp 0x3c446
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3c3e5
movq 0x58(%rsp), %rax
movss 0x14(%rax), %xmm0
movq 0x58(%rsp), %rax
movss 0x8(%rax), %xmm2
movq 0x58(%rsp), %rax
movss 0x18(%rax), %xmm1
movq 0x58(%rsp), %rax
mulss 0x4(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x44(%rsp)
movq 0x58(%rsp), %rax
movss 0x18(%rax), %xmm0
movq 0x58(%rsp), %rax
movss (%rax), %xmm2
movq 0x58(%rsp), %rax
movss 0x10(%rax), %xmm1
movq 0x58(%rsp), %rax
mulss 0x8(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x48(%rsp)
movq 0x58(%rsp), %rax
movss 0x10(%rax), %xmm0
movq 0x58(%rsp), %rax
movss 0x4(%rax), %xmm2
movq 0x58(%rsp), %rax
movss 0x14(%rax), %xmm1
movq 0x58(%rsp), %rax
mulss (%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4c(%rsp)
leaq 0x44(%rsp), %rdi
callq 0x11980
movss 0x44(%rsp), %xmm1
movq 0x60(%rsp), %rax
movss 0xc(%rax), %xmm2
movss 0x48(%rsp), %xmm0
movq 0x60(%rsp), %rax
mulss 0x10(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x4c(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss 0x14(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x40(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3c762
jp 0x3c762
movq 0x60(%rsp), %rax
movq 0x1360(%rax), %rax
leaq 0x1ba9e76(%rip), %rcx   # 0x1be6410
subq %rcx, %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
movl %eax, 0x34(%rsp)
callq 0xd220
movl 0x34(%rsp), %esi
leaq 0x31e55(%rip), %rdi     # 0x6e410
xorl %eax, %eax
callq 0xb0d0
movl $0x20, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rsp), %rax
movq 0x1360(%rax), %rsi
movss 0x315a5(%rip), %xmm0    # 0x6db90
callq 0xf130
jmp 0x3c5f2
movq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x18(%rsp), %eax
jae 0x3c72e
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movl 0x34(%rsp), %eax
movl %eax, %ecx
leaq 0x54313cd(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss (%rax), %xmm0
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movl 0x34(%rsp), %eax
movl %eax, %ecx
leaq 0x5431384(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x4(%rax), %xmm0
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movl 0x34(%rsp), %eax
movl %eax, %ecx
leaq 0x5431339(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
addss 0x8(%rax), %xmm0
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x3c610
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movl $0x20, %esi
callq 0x4330
jmp 0x3cd57
movq 0x28(%rsp), %rdi
callq 0xd7f0
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x3c751
movq (%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
callq 0xd240
xorl %edi, %edi
movl $0x1e, %esi
callq 0xbf00
xorps %xmm0, %xmm0
ucomiss 0x40(%rsp), %xmm0
jbe 0x3c7cc
movss 0x40(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, 0x40(%rsp)
leaq 0x2f6c4(%rip), %rax     # 0x6be50
movss (%rax), %xmm0
subss 0x44(%rsp), %xmm0
movss %xmm0, 0x44(%rsp)
leaq 0x2f6ad(%rip), %rax     # 0x6be50
movss 0x4(%rax), %xmm0
subss 0x48(%rsp), %xmm0
movss %xmm0, 0x48(%rsp)
leaq 0x2f695(%rip), %rax     # 0x6be50
movss 0x8(%rax), %xmm0
subss 0x4c(%rsp), %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x40(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x30a0a(%rip), %xmm0    # 0x6d1e8
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
movl $0x0, 0x54(%rsp)
cmpl $0x2, 0x54(%rsp)
jge 0x3cb63
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
cmpl $0x0, 0x54(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x60(%rsp), %rax
movss 0x14(%rax), %xmm2
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
cmpl $0x0, 0x54(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm1
movq 0x60(%rsp), %rax
mulss 0x10(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
cmpl $0x0, 0x54(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x60(%rsp), %rax
movss 0xc(%rax), %xmm2
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
cmpl $0x0, 0x54(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm1
movq 0x60(%rsp), %rax
mulss 0x14(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
cmpl $0x0, 0x54(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movq 0x60(%rsp), %rax
movss 0x10(%rax), %xmm2
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
cmpl $0x0, 0x54(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm1
movq 0x60(%rsp), %rax
mulss 0xc(%rax), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm1
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm2
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x60(%rsp), %rax
addq $0xf00, %rax            # imm = 0xF00
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
movss 0x2d5b2(%rip), %xmm1    # 0x6a04c
divss 0x38(%rsp), %xmm1
mulss %xmm1, %xmm0
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movss 0x2d569(%rip), %xmm1    # 0x6a04c
divss 0x38(%rsp), %xmm1
mulss %xmm1, %xmm0
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x4(%rax)
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movss 0x2d51f(%rip), %xmm1    # 0x6a04c
divss 0x38(%rsp), %xmm1
mulss %xmm1, %xmm0
movq 0x60(%rsp), %rax
addq $0xf18, %rax            # imm = 0xF18
movslq 0x54(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3c7f4
movl $0x0, 0x54(%rsp)
cmpl $0x3, 0x54(%rsp)
jge 0x3cbf1
movq 0x58(%rsp), %rax
movss 0xc(%rax), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x60(%rsp), %rax
movslq 0x54(%rsp), %rcx
movss 0xf18(%rax,%rcx,4), %xmm2
movq 0x58(%rsp), %rax
movss 0x1c(%rax), %xmm1
movq 0x60(%rsp), %rax
movslq 0x54(%rsp), %rcx
mulss 0xf24(%rax,%rcx,4), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movslq 0x54(%rsp), %rcx
movss %xmm0, 0xef4(%rax,%rcx,4)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3cb6b
movq 0x60(%rsp), %rax
movss 0xef4(%rax), %xmm1
movq 0x60(%rsp), %rax
movss 0xc(%rax), %xmm2
movq 0x60(%rsp), %rax
movss 0xef8(%rax), %xmm0
movq 0x60(%rsp), %rax
mulss 0x10(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x60(%rsp), %rax
movss 0xefc(%rax), %xmm0
movq 0x60(%rsp), %rax
movss 0x14(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0x40(%rsp), %xmm0
mulss 0x3c(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x60(%rsp), %rax
movss 0xef4(%rax), %xmm1
movss 0x3c(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss 0x44(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xef4(%rax)
movq 0x60(%rsp), %rax
movss 0xef8(%rax), %xmm1
movss 0x3c(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss 0x48(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xef8(%rax)
movq 0x60(%rsp), %rax
movss 0xefc(%rax), %xmm1
movss 0x3c(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss 0x4c(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xefc(%rax)
movss 0x44(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xf30(%rax)
movss 0x48(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xf34(%rax)
movss 0x4c(%rsp), %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0xf38(%rax)
addq $0x68, %rsp
retq
movq 0x20(%rsp), %rdi
callq 0x45c0
nopw %cs:(%rax,%rax)