// File: /workspace/github_cpp_makefile_star2_O2/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), %rbx
leaq 0x50e8(%rip), %rsi      # 0x1cc09
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
je 0x17c98
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
andq $0x0, 0x20(%rsp)
movq %rbx, %rdi
callq 0x1739c
cmpb $0x2a, (%rbx)
je 0x17bc3
leaq 0x4f53(%rip), %rsi      # 0x1caad
pushq $0x3
popq %rdx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
je 0x17bc3
leaq 0x4f39(%rip), %rsi      # 0x1caa9
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
je 0x17bc3
leaq 0x6e87(%rip), %rsi      # 0x1ea0d
pushq $0x6
popq %rdx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
je 0x17bc3
leaq 0x4f80(%rip), %rsi      # 0x1cb1c
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
je 0x17bc3
leaq 0x7308(%rip), %rsi      # 0x1eeba
pushq $0xa
popq %rdx
movq %rbx, %rdi
callq 0x4420
xorl %ebp, %ebp
testl %eax, %eax
jne 0x17bce
movl $0x1, 0x24(%rsp)
pushq $0x1
popq %rbp
cmpb $0x0, (%r15)
je 0x17ca1
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 0x17c20
jp 0x17c20
ucomisd %xmm2, %xmm1
jne 0x17c20
jp 0x17c20
movsd 0x10(%r14), %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm2
jne 0x17c20
jnp 0x17f5b
cvtss2sd (%rsp), %xmm2
cvtss2sd 0x4(%rsp), %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
cvtss2sd 0x8(%rsp), %xmm3
movsd 0x10(%r14), %xmm4
mulsd %xmm4, %xmm3
cvtss2sd 0xc(%rsp), %xmm5
addsd %xmm2, %xmm3
addsd %xmm3, %xmm5
cvtsd2ss %xmm5, %xmm2
movss %xmm2, 0xc(%rsp)
cvtss2sd 0x10(%rsp), %xmm2
cvtss2sd 0x14(%rsp), %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
cvtss2sd 0x18(%rsp), %xmm0
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
cvtss2sd 0x1c(%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
jmp 0x17f55
pushq $-0x1
popq %r14
jmp 0x1805b
leaq 0x61b3948(%rip), %r13   # 0x61cb5f0
cmpl $0xdb, (%r13)
jg 0x17cc4
leaq 0x48(%rsp), %rdx
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x10769
movsd 0x50(%r15), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
jne 0x17cec
jp 0x17cec
movq %rax, 0x50(%r15)
movsd 0x436c(%rip), %xmm0     # 0x1c058
movsd 0x58(%r15), %xmm1
ucomisd %xmm2, %xmm1
jne 0x17d06
jp 0x17d06
movq %rax, 0x58(%r15)
movsd 0x4352(%rip), %xmm1     # 0x1c058
cmpl $0xdb, (%r13)
jg 0x17d37
movsd 0x48(%r15), %xmm0
ucomisd %xmm2, %xmm0
jne 0x17db9
jp 0x17db9
movsd 0x432a(%rip), %xmm0     # 0x1c058
xorpd %xmm6, %xmm6
jmp 0x17dd9
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x6e7d(%rip), %xmm0     # 0x1ebc0
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 0x17ed8
ucomisd 0x6de7(%rip), %xmm0     # 0x1eba8
jne 0x1806d
jp 0x1806d
xorpd %xmm0, %xmm0
movsd 0x427f(%rip), %xmm6     # 0x1c058
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 0x4425(%rip), %xmm1     # 0x1c240
xorpd %xmm6, %xmm1
cmovnel %eax, %edx
leaq 0x30(%rsp,%rcx,8), %rcx
cmovpl %eax, %edx
leaq (%rsp,%rdx,8), %rdx
addq $0x30, %rdx
cmpq $0x30, %rax
je 0x17e72
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
jmp 0x17e32
leaq 0x30(%rsp), %rax
movq %rsp, %rcx
xorl %edx, %edx
cmpq $0x2, %rdx
je 0x17eb5
movsd 0x50(%r15,%rdx,8), %xmm0
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x17ea8
movsd (%rax,%rsi,8), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rcx,%rsi,4)
incq %rsi
jmp 0x17e8b
incq %rdx
addq $0x18, %rax
addq $0x10, %rcx
jmp 0x17e7c
movss (%rsp), %xmm1
movss 0x4(%rsp), %xmm2
movups 0x14(%rsp), %xmm3
movl 0x24(%rsp), %ebp
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
cvtss2sd %xmm0, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm1, %xmm5
addsd 0x38(%r15), %xmm5
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 0xa058
xorl %r14d, %r14d
leaq 0x11774b6(%rip), %r12   # 0x118f420
leaq 0x11774ab(%rip), %r15   # 0x118f41c
movq %rsp, %r13
movl (%r15), %eax
cmpl %eax, %r14d
jge 0x17fdf
movl 0x20(%r12), %edi
callq 0x17ad2
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
testl %eax, %eax
jne 0x17fd6
cmpl %ebp, 0x24(%r12)
jne 0x17fd6
movq %r13, %rax
movq %r12, %rcx
xorl %edx, %edx
cmpq $0x2, %rdx
je 0x18056
xorl %esi, %esi
cmpq $0x4, %rsi
je 0x17fc9
movss (%rcx,%rsi,4), %xmm0
ucomiss (%rax,%rsi,4), %xmm0
leaq 0x1(%rsi), %rsi
jne 0x17fd6
jnp 0x17fb0
jmp 0x17fd6
incq %rdx
addq $0x10, %rcx
addq $0x10, %rax
jmp 0x17fa4
incl %r14d
addq $0x28, %r12
jmp 0x17f74
movl $0x40000, %r13d         # imm = 0x40000
xorl %edi, %edi
cmpl %r13d, %eax
setl %dil
pushq $0x26
popq %rbp
movl %ebp, %esi
callq 0x9517
movq 0x20(%rsp), %rax
movq %rax, 0x20(%r12)
movaps (%rsp), %xmm0
movaps 0x10(%rsp), %xmm1
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
xorl %edi, %edi
cmpl %r13d, 0x61d7f51(%rip)  # 0x61eff70
setl %dil
movl %ebp, %esi
callq 0x9517
movslq 0x61d7f3f(%rip), %r13   # 0x61eff70
movq %rbx, %rdi
callq 0x45d0
leaq 0x61d7f40(%rip), %rcx   # 0x61eff80
movq %rax, (%rcx,%r13,8)
leal 0x1(%r13), %eax
movl %eax, 0x61d7f22(%rip)   # 0x61eff70
movl %r13d, 0x20(%r12)
incl (%r15)
callq 0xa069
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd 0x101b(%rip), %xmm0     # 0x19090
jne 0x18086
jp 0x18086
movsd 0x4187(%rip), %xmm0     # 0x1c208
jmp 0x17d2e
ucomisd 0x6b22(%rip), %xmm0     # 0x1ebb0
jne 0x180a3
jp 0x180a3
xorpd %xmm0, %xmm0
movsd 0x416a(%rip), %xmm6     # 0x1c208
jmp 0x17dd9
divsd 0xfe5(%rip), %xmm0      # 0x19090
mulsd 0xfd5(%rip), %xmm0      # 0x19088
movsd %xmm0, 0x28(%rsp)
callq 0x40f0
movapd %xmm0, 0x60(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x4610
xorpd %xmm2, %xmm2
movapd 0x60(%rsp), %xmm6
jmp 0x17dd9