// File: /workspace/github_cpp_makefile_star2_O1/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 0x5470(%rip), %rsi      # 0x1fc79
movl $0x4, %edx
movq %rbp, %rdi
callq 0x4410
testl %eax, %eax
je 0x1a9a7
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm0, (%rsp)
movq $0x0, 0x20(%rsp)
movq %rbp, %rdi
callq 0x1a056
cmpb $0x2a, (%rbp)
je 0x1a8bc
leaq 0x5302(%rip), %rsi      # 0x1fb4d
movl $0x3, %edx
movq %rbp, %rdi
callq 0x4410
testl %eax, %eax
je 0x1a8bc
leaq 0x52e6(%rip), %rsi      # 0x1fb49
movl $0x5, %edx
movq %rbp, %rdi
callq 0x4410
testl %eax, %eax
je 0x1a8bc
leaq 0x727d(%rip), %rsi      # 0x21af8
movl $0x6, %edx
movq %rbp, %rdi
callq 0x4410
testl %eax, %eax
je 0x1a8bc
leaq 0x5329(%rip), %rsi      # 0x1fbbc
movl $0x4, %edx
movq %rbp, %rdi
callq 0x4410
testl %eax, %eax
je 0x1a8bc
leaq 0x76e3(%rip), %rsi      # 0x21f8e
movl $0xa, %edx
movq %rbp, %rdi
callq 0x4410
testl %eax, %eax
jne 0x1a8c1
orb $0x1, 0x24(%rsp)
cmpb $0x0, (%r15)
je 0x1a9b2
movups 0x8(%r15), %xmm0
movupd 0x18(%r15), %xmm1
movapd %xmm1, 0x10(%rsp)
movaps %xmm0, (%rsp)
movsd (%r14), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a915
jp 0x1a915
movsd 0x8(%r14), %xmm2
ucomisd %xmm1, %xmm2
jne 0x1a915
jp 0x1a915
movsd 0x10(%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0x1a915
jnp 0x1ac47
xorps %xmm1, %xmm1
cvtss2sd (%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x4(%rsp), %xmm2
movsd 0x8(%r14), %xmm3
movsd 0x10(%r14), %xmm4
mulsd %xmm3, %xmm2
mulsd %xmm0, %xmm1
cvtss2sd 0x8(%rsp), %xmm5
addsd %xmm2, %xmm1
mulsd %xmm4, %xmm5
xorps %xmm2, %xmm2
cvtss2sd 0xc(%rsp), %xmm2
addsd %xmm1, %xmm5
addsd %xmm5, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
movss %xmm1, 0xc(%rsp)
xorps %xmm1, %xmm1
cvtss2sd 0x10(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x14(%rsp), %xmm2
mulsd %xmm3, %xmm2
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
xorps %xmm0, %xmm0
cvtss2sd 0x18(%rsp), %xmm0
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rsp), %xmm1
addsd %xmm0, %xmm1
jmp 0x1ac3a
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x1ad84
leaq 0x61b2c37(%rip), %r13   # 0x61cd5f0
cmpl $0xdb, (%r13)
jg 0x1a9d5
leaq 0x48(%rsp), %rdx
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x12f2b
movsd 0x50(%r15), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x1a9f5
jp 0x1a9f5
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x50(%r15)
movsd 0x58(%r15), %xmm1
ucomisd %xmm0, %xmm1
jne 0x1aa11
jp 0x1aa11
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x58(%r15)
cmpl $0xdb, (%r13)
jg 0x1aa46
movsd 0x48(%r15), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0x1aaba
jp 0x1aaba
movsd 0x46bb(%rip), %xmm0     # 0x1f0f8
xorpd %xmm6, %xmm6
jmp 0x1aada
movsd 0x46aa(%rip), %xmm0     # 0x1f0f8
movapd %xmm0, %xmm1
divsd 0x50(%r15), %xmm1
movupd 0x8(%r15), %xmm2
movapd %xmm1, %xmm3
unpcklpd %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
mulpd %xmm2, %xmm3
cvtpd2ps %xmm3, %xmm2
movlpd %xmm2, (%rsp)
mulsd 0x18(%r15), %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0x8(%rsp)
divsd 0x58(%r15), %xmm0
movupd 0x20(%r15), %xmm1
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
cvtpd2ps %xmm2, %xmm1
movlpd %xmm1, 0x10(%rsp)
mulsd 0x30(%r15), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
jmp 0x1abb3
ucomisd 0x71ce(%rip), %xmm0     # 0x21c90
jne 0x1ad96
jp 0x1ad96
xorpd %xmm0, %xmm0
movsd 0x461e(%rip), %xmm6     # 0x1f0f8
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
ucomisd 0x48(%rsp), %xmm2
movapd 0x47e4(%rip), %xmm1     # 0x1f300
xorpd %xmm6, %xmm1
cmovnel %eax, %edx
leaq 0x30(%rsp,%rcx,8), %rcx
cmovpl %eax, %edx
leaq (%rsp,%rdx,8), %rdx
addq $0x30, %rdx
movsd (%rcx,%rax), %xmm2
movsd (%rdx,%rax), %xmm3
movapd %xmm3, %xmm4
mulsd %xmm1, %xmm4
movapd %xmm0, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm4, %xmm5
mulsd %xmm0, %xmm3
mulsd %xmm6, %xmm2
addsd %xmm3, %xmm2
movsd %xmm5, (%rcx,%rax)
movsd %xmm2, (%rdx,%rax)
addq $0x18, %rax
cmpq $0x18, %rax
je 0x1ab33
leaq 0x30(%rsp), %rax
movq %rsp, %rcx
xorl %edx, %edx
movsd 0x50(%r15,%rdx,8), %xmm0
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rcx,%rsi,4)
incq %rsi
cmpq $0x3, %rsi
jne 0x1ab84
leaq 0x1(%rdx), %rsi
addq $0x18, %rax
addq $0x10, %rcx
testq %rdx, %rdx
movq %rsi, %rdx
je 0x1ab7b
xorps %xmm0, %xmm0
cvtss2sd (%rsp), %xmm0
movsd (%r14), %xmm1
movsd 0x8(%r14), %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x4(%rsp), %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm1, %xmm0
addsd %xmm3, %xmm0
movsd 0x10(%r14), %xmm3
xorps %xmm4, %xmm4
cvtss2sd 0x8(%rsp), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm0, %xmm4
addsd 0x38(%r15), %xmm4
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
movss %xmm0, 0xc(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
xorps %xmm4, %xmm4
cvtss2sd 0x14(%rsp), %xmm4
mulsd %xmm2, %xmm4
mulsd %xmm1, %xmm0
addsd %xmm4, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x18(%rsp), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
addsd 0x40(%r15), %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
callq 0xb695
leaq 0x11767c9(%rip), %r15   # 0x119141c
cmpl $0x0, (%r15)
jle 0x1ad02
leaq 0x11767bc(%rip), %r13   # 0x1191420
xorl %r14d, %r14d
movq %rsp, %rax
movl 0x24(%rax), %r12d
movl 0x20(%r13), %ebx
testl %ebx, %ebx
setns %al
cmpl %ebx, 0x61d72f3(%rip)   # 0x61f1f70
setg %cl
andb %al, %cl
movzbl %cl, %edi
xorl %esi, %esi
callq 0xaa4d
movslq %ebx, %rax
leaq 0x61d72ea(%rip), %rcx   # 0x61f1f80
movq (%rcx,%rax,8), %rdi
movq %rbp, %rsi
callq 0x4450
testl %eax, %eax
jne 0x1ace5
cmpl %r12d, 0x24(%r13)
jne 0x1ace5
movq %rsp, %rax
movq %r13, %rcx
xorl %edx, %edx
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm0
ucomiss (%rax,%rsi,4), %xmm0
jne 0x1ace5
jp 0x1ace5
incq %rsi
cmpq $0x4, %rsi
jne 0x1acb6
leaq 0x1(%rdx), %rsi
addq $0x10, %rcx
addq $0x10, %rax
testq %rdx, %rdx
movq %rsi, %rdx
je 0x1acb4
jmp 0x1ad7f
incl %r14d
addq $0x28, %r13
movl (%r15), %eax
cmpl %eax, %r14d
jl 0x1ac6e
cmpl $0x40000, %eax          # imm = 0x40000
setl %al
jmp 0x1ad0e
movb $0x1, %al
xorl %r14d, %r14d
leaq 0x1176712(%rip), %r13   # 0x1191420
movzbl %al, %edi
movl $0x26, %esi
callq 0xaa4d
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, 0x61d7229(%rip) # imm = 0x40000
setl %dil
movl $0x26, %esi
callq 0xaa4d
movslq 0x61d7214(%rip), %rbx   # 0x61f1f70
movq %rbp, %rdi
callq 0x45c0
leaq 0x61d7215(%rip), %rcx   # 0x61f1f80
movq %rax, (%rcx,%rbx,8)
leal 0x1(%rbx), %eax
movl %eax, 0x61d71f8(%rip)   # 0x61f1f70
movl %ebx, 0x20(%r13)
incl (%r15)
callq 0xb6a6
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd 0x1382(%rip), %xmm0     # 0x1c120
jne 0x1adaf
jp 0x1adaf
movsd 0x4516(%rip), %xmm0     # 0x1f2c0
jmp 0x1aa3d
ucomisd 0x6ee1(%rip), %xmm0     # 0x21c98
jne 0x1adcc
jp 0x1adcc
xorpd %xmm0, %xmm0
movsd 0x44f9(%rip), %xmm6     # 0x1f2c0
jmp 0x1aada
divsd 0x134c(%rip), %xmm0     # 0x1c120
mulsd 0x133c(%rip), %xmm0     # 0x1c118
movsd %xmm0, 0x28(%rsp)
callq 0x40e0
movapd %xmm0, 0x60(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x4610
xorpd %xmm2, %xmm2
movapd 0x60(%rsp), %xmm6
jmp 0x1aada