// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlcsg/textures.cpp
// Function: TexinfoForBrushTexture(plane_t const*, brush_texture_t*, double const*)
int             TexinfoForBrushTexture(const plane_t* const plane, brush_texture_t* bt, const vec3_t origin
#ifdef ZHLT_HIDDENSOUNDTEXTURE
					, bool shouldhide
#endif
					)
{
    vec3_t          vecs[2];
    int             sv, tv;
    vec_t           ang, sinv, cosv;
    vec_t           ns, nt;
    texinfo_t       tx;
    texinfo_t*      tc;
    int             i, j, k;

#ifdef HLCSG_HLBSP_VOIDTEXINFO
	if (!strncasecmp(bt->name, "NULL", 4))
	{
		return -1;
	}
#endif
    memset(&tx, 0, sizeof(tx));
#ifndef HLCSG_CUSTOMHULL
#ifdef HLCSG_PRECISIONCLIP
	if(!strncmp(bt->name,"BEVEL",5))
	{
		tx.flags |= TEX_BEVEL;
		safe_strncpy(bt->name,"NULL",5);
	}
#endif
#endif
#ifndef HLCSG_AUTOWAD_NEW
#ifdef HLCSG_AUTOWAD_TEXTURELIST_FIX
	ThreadLock ();
	autowad_PushName (bt->name);
	ThreadUnlock ();
#endif
#endif
#ifdef HLCSG_TEXMAP64_FIX
	FindMiptex (bt->name);
#else
    tx.miptex = FindMiptex(bt->name);

    // Note: FindMiptex() still needs to be called here to add it to the global miptex array

    // Very Sleazy Hack 104 - since the tx.miptex index will be bogus after we sort the miptex array later
    // Put the string name of the miptex in this "index" until after we are done sorting it in WriteMiptex().
    tx.miptex = texmap64_store(strdup(bt->name));
#endif

    // set the special flag
    if (bt->name[0] == '*'
        || !strncasecmp(bt->name, "sky", 3)

// =====================================================================================
//Cpt_Andrew - Env_Sky Check
// =====================================================================================
        || !strncasecmp(bt->name, "env_sky", 5)
// =====================================================================================

#ifndef HLCSG_CUSTOMHULL
        || !strncasecmp(bt->name, "clip", 4)
#endif
        || !strncasecmp(bt->name, "origin", 6)
#ifdef ZHLT_NULLTEX // AJM
        || !strncasecmp(bt->name, "null", 4)
#endif
        || !strncasecmp(bt->name, "aaatrigger", 10)
       )
    {
		// actually only 'sky' and 'aaatrigger' needs this. --vluzacn
        tx.flags |= TEX_SPECIAL;
    }
#ifdef ZHLT_HIDDENSOUNDTEXTURE
	if (shouldhide)
	{
		tx.flags |= TEX_SHOULDHIDE;
	}
#endif

    if (bt->txcommand)
    {
        memcpy(tx.vecs, bt->vects.quark.vects, sizeof(tx.vecs));
        if (origin[0] || origin[1] || origin[2])
        {
            tx.vecs[0][3] += DotProduct(origin, tx.vecs[0]);
            tx.vecs[1][3] += DotProduct(origin, tx.vecs[1]);
        }
    }
    else
    {
        if (g_nMapFileVersion < 220)
        {
            TextureAxisFromPlane(plane, vecs[0], vecs[1]);
        }

        if (!bt->vects.valve.scale[0])
        {
            bt->vects.valve.scale[0] = 1;
        }
        if (!bt->vects.valve.scale[1])
        {
            bt->vects.valve.scale[1] = 1;
        }

        if (g_nMapFileVersion < 220)
        {
            // rotate axis
            if (bt->vects.valve.rotate == 0)
            {
                sinv = 0;
                cosv = 1;
            }
            else if (bt->vects.valve.rotate == 90)
            {
                sinv = 1;
                cosv = 0;
            }
            else if (bt->vects.valve.rotate == 180)
            {
                sinv = 0;
                cosv = -1;
            }
            else if (bt->vects.valve.rotate == 270)
            {
                sinv = -1;
                cosv = 0;
            }
            else
            {
                ang = bt->vects.valve.rotate / 180 * Q_PI;
                sinv = sin(ang);
                cosv = cos(ang);
            }

            if (vecs[0][0])
            {
                sv = 0;
            }
            else if (vecs[0][1])
            {
                sv = 1;
            }
            else
            {
                sv = 2;
            }

            if (vecs[1][0])
            {
                tv = 0;
            }
            else if (vecs[1][1])
            {
                tv = 1;
            }
            else
            {
                tv = 2;
            }

            for (i = 0; i < 2; i++)
            {
                ns = cosv * vecs[i][sv] - sinv * vecs[i][tv];
                nt = sinv * vecs[i][sv] + cosv * vecs[i][tv];
                vecs[i][sv] = ns;
                vecs[i][tv] = nt;
            }

            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    tx.vecs[i][j] = vecs[i][j] / bt->vects.valve.scale[i];
                }
            }
        }
        else
        {
            vec_t           scale;

            scale = 1 / bt->vects.valve.scale[0];
            VectorScale(bt->vects.valve.UAxis, scale, tx.vecs[0]);

            scale = 1 / bt->vects.valve.scale[1];
            VectorScale(bt->vects.valve.VAxis, scale, tx.vecs[1]);
        }

        tx.vecs[0][3] = bt->vects.valve.shift[0] + DotProduct(origin, tx.vecs[0]);
        tx.vecs[1][3] = bt->vects.valve.shift[1] + DotProduct(origin, tx.vecs[1]);
    }

    //
    // find the g_texinfo
    //
    ThreadLock();
    tc = g_texinfo;
    for (i = 0; i < g_numtexinfo; i++, tc++)
    {
        // Sleazy hack 104, Pt 3 - Use strcmp on names to avoid dups
#ifdef HLCSG_TEXMAP64_FIX
		if (strcmp (texmap_retrieve (tc->miptex), bt->name) != 0)
#else
        if (strcmp(texmap64_retrieve((tc->miptex)), texmap64_retrieve((tx.miptex))) != 0)
#endif
        {
            continue;
        }
        if (tc->flags != tx.flags)
        {
            continue;
        }
        for (j = 0; j < 2; j++)
        {
            for (k = 0; k < 4; k++)
            {
                if (tc->vecs[j][k] != tx.vecs[j][k])
                {
                    goto skip;
                }
            }
        }
        ThreadUnlock();
        return i;
skip:;
    }

#ifdef HLCSG_HLBSP_REDUCETEXTURE
    hlassume(g_numtexinfo < MAX_INTERNAL_MAP_TEXINFO, assume_MAX_MAP_TEXINFO);
#else
    hlassume(g_numtexinfo < MAX_MAP_TEXINFO, assume_MAX_MAP_TEXINFO);
#endif

    *tc = tx;
#ifdef HLCSG_TEXMAP64_FIX
	tc->miptex = texmap_store (bt->name, false);
#endif
    g_numtexinfo++;
    ThreadUnlock();
    return i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x60(%rsi), %rbp
leaq 0x5a26(%rip), %rsi      # 0x1fc99
movl $0x4, %edx
movq %rbp, %rdi
callq 0x4400
testl %eax, %eax
je 0x1a410
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movq $0x0, 0x20(%rsp)
movq %rbp, %rdi
callq 0x19abd
cmpb $0x2a, (%rbp)
je 0x1a323
leaq 0x588b(%rip), %rsi      # 0x1fb3d
movl $0x3, %edx
movq %rbp, %rdi
callq 0x4400
testl %eax, %eax
je 0x1a323
leaq 0x586f(%rip), %rsi      # 0x1fb39
movl $0x5, %edx
movq %rbp, %rdi
callq 0x4400
testl %eax, %eax
je 0x1a323
leaq 0x783b(%rip), %rsi      # 0x21b1d
movl $0x6, %edx
movq %rbp, %rdi
callq 0x4400
testl %eax, %eax
je 0x1a323
leaq 0x58b2(%rip), %rsi      # 0x1fbac
movl $0x4, %edx
movq %rbp, %rdi
callq 0x4400
testl %eax, %eax
je 0x1a323
leaq 0x7cb8(%rip), %rsi      # 0x21fca
movl $0xa, %edx
movq %rbp, %rdi
callq 0x4400
testl %eax, %eax
jne 0x1a32b
movl $0x1, 0x24(%rsp)
cmpb $0x0, (%r15)
je 0x1a41b
movups 0x8(%r15), %xmm0
movups 0x18(%r15), %xmm1
movaps %xmm1, 0x10(%rsp)
movaps %xmm0, (%rsp)
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0x1a37d
jp 0x1a37d
ucomisd %xmm2, %xmm1
jne 0x1a37d
jp 0x1a37d
movsd 0x10(%r14), %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm2
jne 0x1a37d
jnp 0x1a6ff
xorps %xmm2, %xmm2
cvtss2sd (%rsp), %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x4(%rsp), %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x8(%rsp), %xmm3
movsd 0x10(%r14), %xmm4
mulsd %xmm4, %xmm3
cvtss2sd 0xc(%rsp), %xmm5
addsd %xmm2, %xmm3
addsd %xmm3, %xmm5
xorps %xmm2, %xmm2
cvtsd2ss %xmm5, %xmm2
movss %xmm2, 0xc(%rsp)
xorps %xmm2, %xmm2
cvtss2sd 0x10(%rsp), %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x14(%rsp), %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
xorps %xmm0, %xmm0
cvtss2sd 0x18(%rsp), %xmm0
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rsp), %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
jmp 0x1a6f9
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x1a83b
leaq 0x61b31be(%rip), %r13   # 0x61cd5e0
cmpl $0xdb, (%r13)
jg 0x1a43e
leaq 0x48(%rsp), %rdx
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x129f1
movsd 0x50(%r15), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0x1a466
jp 0x1a466
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x50(%r15)
movsd 0x4c82(%rip), %xmm0     # 0x1f0e8
movsd 0x58(%r15), %xmm1
ucomisd %xmm2, %xmm1
jne 0x1a48a
jp 0x1a48a
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x58(%r15)
movsd 0x4c5e(%rip), %xmm1     # 0x1f0e8
cmpl $0xdb, (%r13)
jg 0x1a4bb
movsd 0x48(%r15), %xmm0
ucomisd %xmm2, %xmm0
jne 0x1a53d
jp 0x1a53d
movsd 0x4c36(%rip), %xmm0     # 0x1f0e8
xorpd %xmm5, %xmm5
jmp 0x1a55d
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x7809(%rip), %xmm0     # 0x21cd0
divpd %xmm1, %xmm0
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd %xmm0, %xmm3
movupd 0x8(%r15), %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
mulpd %xmm1, %xmm0
cvtpd2ps %xmm0, %xmm1
shufpd $0x1, %xmm3, %xmm3      # xmm3 = xmm3[1,0]
movlpd %xmm1, (%rsp)
movupd 0x18(%r15), %xmm0
mulpd %xmm3, %xmm0
movupd 0x28(%r15), %xmm3
mulpd %xmm2, %xmm3
cvtpd2ps %xmm3, %xmm2
cvtpd2ps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
unpcklpd %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
movapd %xmm0, %xmm3
shufps $0xe9, %xmm0, %xmm3     # xmm3 = xmm3[1,2],xmm0[2,3]
movlps %xmm3, 0x10(%rsp)
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movss %xmm2, 0x18(%rsp)
movapd %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
jmp 0x1a676
ucomisd 0x7773(%rip), %xmm0     # 0x21cb8
jne 0x1a84d
jp 0x1a84d
xorpd %xmm0, %xmm0
movsd 0x4b8b(%rip), %xmm5     # 0x1f0e8
movsd 0x38(%rsp), %xmm1
cmpneqsd %xmm2, %xmm1
movq %xmm1, %rcx
addl $0x2, %ecx
movsd 0x50(%rsp), %xmm1
cmpneqsd %xmm2, %xmm1
movq %xmm1, %rdx
addl $0x2, %edx
xorl %eax, %eax
ucomisd 0x30(%rsp), %xmm2
cmovnel %eax, %ecx
cmovpl %eax, %ecx
shll $0x3, %ecx
ucomisd 0x48(%rsp), %xmm2
cmovnel %eax, %edx
movsd 0x30(%rsp,%rcx), %xmm1
cmovpl %eax, %edx
shll $0x3, %edx
movsd 0x30(%rsp,%rdx), %xmm2
movapd %xmm2, %xmm3
mulsd %xmm5, %xmm3
movapd %xmm0, %xmm4
mulsd %xmm1, %xmm4
subsd %xmm3, %xmm4
mulsd %xmm0, %xmm2
mulsd %xmm5, %xmm1
addsd %xmm2, %xmm1
movsd %xmm4, 0x30(%rsp,%rcx)
movsd %xmm1, 0x30(%rsp,%rdx)
movsd 0x48(%rsp,%rcx), %xmm1
movsd 0x48(%rsp,%rdx), %xmm2
movapd %xmm0, %xmm3
mulsd %xmm2, %xmm0
mulsd %xmm5, %xmm2
mulsd %xmm1, %xmm3
subsd %xmm2, %xmm3
mulsd %xmm1, %xmm5
addsd %xmm0, %xmm5
movsd %xmm3, 0x48(%rsp,%rcx)
movsd %xmm5, 0x48(%rsp,%rdx)
movb $0x1, %cl
movsd 0x50(%r15,%rax,8), %xmm0
leaq (%rax,%rax,2), %rdx
leaq (%rsp,%rdx,8), %rdx
addq $0x30, %rdx
shlq $0x4, %rax
addq %rsp, %rax
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rax,%rsi,4)
incq %rsi
cmpq $0x3, %rsi
jne 0x1a62d
movl $0x1, %eax
testb $0x1, %cl
movl $0x0, %ecx
jne 0x1a611
movss (%rsp), %xmm1
movss 0x4(%rsp), %xmm2
movups 0x14(%rsp), %xmm3
movss 0x10(%rsp), %xmm0
movhps 0x8(%rsp), %xmm0        # xmm0 = xmm0[0,1],mem[0,1]
shufps $0x42, %xmm3, %xmm0     # xmm0 = xmm0[2,0],xmm3[0,1]
cvtss2sd %xmm1, %xmm1
movsd (%r14), %xmm3
movsd 0x8(%r14), %xmm4
cvtss2sd %xmm2, %xmm2
mulsd %xmm4, %xmm2
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
movsd 0x10(%r14), %xmm2
xorps %xmm5, %xmm5
cvtss2sd %xmm0, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm1, %xmm5
addsd 0x38(%r15), %xmm5
xorps %xmm1, %xmm1
cvtsd2ss %xmm5, %xmm1
movss %xmm1, 0xc(%rsp)
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
cvtss2sd %xmm1, %xmm1
movaps %xmm0, %xmm5
unpckhpd %xmm0, %xmm5            # xmm5 = xmm5[1],xmm0[1]
cvtss2sd %xmm5, %xmm5
mulsd %xmm4, %xmm5
mulsd %xmm3, %xmm1
addsd %xmm5, %xmm1
shufps $0xff, %xmm0, %xmm0     # xmm0 = xmm0[3,3,3,3]
cvtss2sd %xmm0, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addsd 0x40(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x1c(%rsp)
callq 0xb551
leaq 0x1176d01(%rip), %r15   # 0x119140c
cmpl $0x0, (%r15)
jle 0x1a7b9
leaq 0x1176cf4(%rip), %r13   # 0x1191410
xorl %r14d, %r14d
movl 0x24(%rsp), %r12d
movl 0x20(%r13), %ebx
testl %ebx, %ebx
setns %al
cmpl %ebx, 0x61d782d(%rip)   # 0x61f1f60
setg %cl
andb %al, %cl
movzbl %cl, %edi
xorl %esi, %esi
callq 0xa91d
movslq %ebx, %rax
leaq 0x61d7824(%rip), %rcx   # 0x61f1f70
movq (%rcx,%rax,8), %rdi
movq %rbp, %rsi
callq 0x4440
testl %eax, %eax
jne 0x1a79c
cmpl %r12d, 0x24(%r13)
jne 0x1a79c
movb $0x1, %al
xorl %edx, %edx
shlq $0x4, %rdx
leaq (%rdx,%r13), %rcx
addq %rsp, %rdx
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm0
ucomiss (%rdx,%rsi,4), %xmm0
jne 0x1a79c
jp 0x1a79c
incq %rsi
cmpq $0x4, %rsi
jne 0x1a773
movl $0x1, %edx
testb $0x1, %al
movl $0x0, %eax
jne 0x1a766
jmp 0x1a836
incl %r14d
addq $0x28, %r13
movl (%r15), %eax
cmpl %eax, %r14d
jl 0x1a724
cmpl $0x40000, %eax          # imm = 0x40000
setl %al
jmp 0x1a7c5
movb $0x1, %al
xorl %r14d, %r14d
leaq 0x1176c4b(%rip), %r13   # 0x1191410
movzbl %al, %edi
movl $0x26, %esi
callq 0xa91d
movq 0x20(%rsp), %rax
movq %rax, 0x20(%r13)
movapd (%rsp), %xmm0
movapd 0x10(%rsp), %xmm1
movupd %xmm1, 0x10(%r13)
movupd %xmm0, (%r13)
xorl %edi, %edi
cmpl $0x40000, 0x61d7762(%rip) # imm = 0x40000
setl %dil
movl $0x26, %esi
callq 0xa91d
movslq 0x61d774d(%rip), %rbx   # 0x61f1f60
movq %rbp, %rdi
callq 0x45a0
leaq 0x61d774e(%rip), %rcx   # 0x61f1f70
movq %rax, (%rcx,%rbx,8)
leal 0x1(%rbx), %eax
movl %eax, 0x61d7731(%rip)   # 0x61f1f60
movl %ebx, 0x20(%r13)
incl (%r15)
callq 0xb562
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd 0x18d3(%rip), %xmm0     # 0x1c128
jne 0x1a866
jp 0x1a866
movsd 0x4a47(%rip), %xmm0     # 0x1f2a8
jmp 0x1a4b2
ucomisd 0x7452(%rip), %xmm0     # 0x21cc0
jne 0x1a883
jp 0x1a883
xorpd %xmm0, %xmm0
movsd 0x4a2a(%rip), %xmm5     # 0x1f2a8
jmp 0x1a55d
divsd 0x189d(%rip), %xmm0     # 0x1c128
mulsd 0x188d(%rip), %xmm0     # 0x1c120
movsd %xmm0, 0x70(%rsp)
callq 0x40e0
movsd %xmm0, 0x68(%rsp)
movsd 0x70(%rsp), %xmm0
callq 0x45f0
xorpd %xmm2, %xmm2
movsd 0x68(%rsp), %xmm5
jmp 0x1a55d