// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlbsp/outside.cpp
// Function: FillOutside(node_s*, bool, unsigned int)
node_t*         FillOutside(node_t* node, const bool leakfile, const unsigned hullnum)
{
    int             s;
    int             i;
    bool            inside;
    bool            ret;
    vec3_t          origin;
    const char*     cl;

    Verbose("----- FillOutside ----\n");

    if (g_nofill)
    {
        Log("skipped\n");
        return node;
    }
#ifdef HLBSP_REMOVEHULL2
	if (hullnum == 2 && g_nohull2)
		return node;
#endif

    //
    // place markers for all entities so
    // we know if we leak inside
    //
    inside = false;
    for (i = 1; i < g_numentities; i++)
    {
        GetVectorForKey(&g_entities[i], "origin", origin);
        cl = ValueForKey(&g_entities[i], "classname");
        if (!isClassnameAllowableOutside(cl))
        {
            /*if (!VectorCompare(origin, vec3_origin))
			*/ if (*ValueForKey(&g_entities[i], "origin")) //--vluzacn
            {
                origin[2] += 1;                            // so objects on floor are ok

                // nudge playerstart around if needed so clipping hulls allways
                // have a vlaid point
                if (!strcmp(cl, "info_player_start"))
                {
                    int             x, y;

                    for (x = -16; x <= 16; x += 16)
                    {
                        for (y = -16; y <= 16; y += 16)
                        {
                            origin[0] += x;
                            origin[1] += y;
                            if (PlaceOccupant(i, origin, node))
                            {
                                inside = true;
                                goto gotit;
                            }
                            origin[0] -= x;
                            origin[1] -= y;
                        }
                    }
                  gotit:;
                }
                else
                {
                    if (PlaceOccupant(i, origin, node))
                        inside = true;
                }
            }
        }
    }

    if (!inside)
    {
        Warning("No entities exist in hull %i, no filling performed for this hull", hullnum);
        return node;
    }

	if(!g_outside_node.portals)
	{
		Warning("No outside node portal found in hull %i, no filling performed for this hull",hullnum);
		return node;
	}

    s = !(g_outside_node.portals->nodes[1] == &g_outside_node);

    // first check to see if an occupied leaf is hit
    outleafs = 0;
    valid++;

    prevleaknode = NULL;

    if (leakfile)
    {
        pointfile = fopen(g_pointfilename, "w");
        if (!pointfile)
        {
            Error("Couldn't open pointfile %s\n", g_pointfilename);
        }

        linefile = fopen(g_linefilename, "w");
        if (!linefile)
        {
            Error("Couldn't open linefile %s\n", g_linefilename);
        }
    }

    ret = RecursiveFillOutside(g_outside_node.portals->nodes[s], false);

    if (leakfile)
    {
        fclose(pointfile);
        fclose(linefile);
    }

    if (ret)
    {
        GetVectorForKey(&g_entities[hit_occupied], "origin", origin);


        {
            Warning("=== LEAK in hull %i ===\nEntity %s @ (%4.0f,%4.0f,%4.0f)",
                 hullnum, ValueForKey(&g_entities[hit_occupied], "classname"), origin[0], origin[1], origin[2]);
            PrintOnce(
                "\n  A LEAK is a hole in the map, where the inside of it is exposed to the\n"
                "(unwanted) outside region.  The entity listed in the error is just a helpful\n"
                "indication of where the beginning of the leak pointfile starts, so the\n"
                "beginning of the line can be quickly found and traced to until reaching the\n"
                "outside. Unless this entity is accidentally on the outside of the map, it\n"
                "probably should not be deleted.  Some complex rotating objects entities need\n"
                "their origins outside the map.  To deal with these, just enclose the origin\n"
                "brush with a solid world brush\n");
        }

        if (!g_bLeaked)
        {
            // First leak spits this out
            Log("Leak pointfile generated\n\n");
        }

        if (g_bLeakOnly)
        {
            Error("Stopped by leak.");
        }

        g_bLeaked = true;
            
        return node;
    }
#ifdef HLBSP_DELETELEAKFILE
	if (leakfile && !ret)
	{
		unlink(g_linefilename);
		unlink(g_pointfilename);
	}
#endif

    // now go back and fill things in
    valid++;
    RecursiveFillOutside(g_outside_node.portals->nodes[s], true);

    // remove faces and nodes from filled in leafs  
    c_falsenodes = 0;
    c_free_faces = 0;
    c_keep_faces = 0;
    node = ClearOutFaces_r(node);

    Verbose("%5i outleafs\n", outleafs);
    Verbose("%5i freed faces\n", c_free_faces);
    Verbose("%5i keep faces\n", c_keep_faces);
    Verbose("%5i falsenodes\n", c_falsenodes);

    // save portal file for vis tracing
    if ((hullnum == 0) && leakfile)
    {
        WritePortalfile(node);
    }

    return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r13d
movl %esi, %ebp
movq %rdi, %rbx
leaq 0xe04e(%rip), %rdi      # 0x20ecf
xorl %eax, %eax
callq 0xa2ef
leaq 0x20c0b73(%rip), %rax   # 0x20d3a02
cmpb $0x1, (%rax)
jne 0x12ea7
leaq 0xe04c(%rip), %rdi      # 0x20ee7
xorl %eax, %eax
callq 0x9bae
jmp 0x13210
cmpl $0x2, %r13d
jne 0x12ebd
leaq 0x20c0b56(%rip), %rax   # 0x20d3a0a
cmpb $0x0, (%rax)
jne 0x13210
leaq 0x200e520(%rip), %rax   # 0x20213e4
cmpl $0x2, (%rax)
jl 0x131ff
movl %ebp, 0x4(%rsp)
movl %r13d, 0xc(%rsp)
movl $0x1, %r13d
movl $0x0, 0x8(%rsp)
leaq 0xa7ef(%rip), %r15      # 0x1d6da
leaq 0x20c0b1e(%rip), %r12   # 0x20d3a10
leaq (,%r13,4), %rbp
addq %r13, %rbp
shlq $0x3, %rbp
addq 0x170d0(%rip), %rbp     # 0x29fd8
movq %rbp, %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
callq 0x7fe0
movq %rbp, %rdi
leaq 0xa6cb(%rip), %rsi      # 0x1d5ed
callq 0x7482
movq %rax, %r14
movq %rax, %rdi
callq 0x12ca4
testb %al, %al
jne 0x13001
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7482
cmpb $0x0, (%rax)
je 0x13001
movsd 0x20(%rsp), %xmm0
addsd 0xd16c(%rip), %xmm0     # 0x200c8
movsd %xmm0, 0x30(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
leaq 0xdf7e(%rip), %rsi      # 0x20ef0
callq 0x4420
movb 0x49(%rbx), %cl
testl %eax, %eax
je 0x1301c
movq %rbx, %rdx
testb %cl, %cl
movsd 0x30(%rsp), %xmm4
jne 0x12feb
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movq %rbx, %rdx
movslq 0x80(%rdx), %rax
shlq $0x6, %rax
movsd 0x8(%r12,%rax), %xmm2
mulsd %xmm1, %xmm2
movsd (%r12,%rax), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
movsd 0x10(%r12,%rax), %xmm2
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
xorl %ecx, %ecx
ucomisd 0x30(%r12,%rax), %xmm2
setbe %cl
movq 0x88(%rdx,%rcx,8), %rdx
cmpb $0x1, 0x49(%rdx)
jne 0x12f9e
cmpl $-0x2, 0xa0(%rdx)
je 0x13001
movl %r13d, 0xc0(%rdx)
movb $0x1, %al
movl %eax, 0x8(%rsp)
incq %r13
leaq 0x200e3d9(%rip), %rax   # 0x20213e4
movslq (%rax), %rax
cmpq %rax, %r13
jl 0x12ef2
jmp 0x130e9
movl $0xfffffff0, %eax       # imm = 0xFFFFFFF0
movapd 0x10(%rsp), %xmm0
movsd 0x30(%rsp), %xmm6
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movl $0xfffffff0, %esi       # imm = 0xFFFFFFF0
xorps %xmm3, %xmm3
cvtsi2sd %esi, %xmm3
movapd %xmm1, %xmm2
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
addpd %xmm2, %xmm0
movapd %xmm0, 0x10(%rsp)
movq %rbx, %rdx
testb %cl, %cl
jne 0x130b3
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
movq %rbx, %rdx
movslq 0x80(%rdx), %rdi
shlq $0x6, %rdi
movsd 0x8(%r12,%rdi), %xmm4
mulsd %xmm3, %xmm4
movsd (%r12,%rdi), %xmm5
mulsd %xmm0, %xmm5
addsd %xmm4, %xmm5
movsd 0x10(%r12,%rdi), %xmm4
mulsd %xmm6, %xmm4
addsd %xmm5, %xmm4
xorl %r8d, %r8d
ucomisd 0x30(%r12,%rdi), %xmm4
setbe %r8b
movq 0x88(%rdx,%r8,8), %rdx
cmpb $0x1, 0x49(%rdx)
jne 0x13064
cmpl $-0x2, 0xa0(%rdx)
jne 0x12ff4
subpd %xmm2, %xmm0
movapd %xmm0, 0x10(%rsp)
leal 0x10(%rsi), %edx
testl %esi, %esi
movl %edx, %esi
jle 0x13039
leal 0x10(%rax), %edx
testl %eax, %eax
movl %edx, %eax
jle 0x1302d
jmp 0x13001
testb $0x1, 0x8(%rsp)
movl 0xc(%rsp), %r13d
movl 0x4(%rsp), %ecx
je 0x131ff
leaq 0x20bb80c(%rip), %r14   # 0x20ce910
movq 0xb0(%r14), %rax
testq %rax, %rax
je 0x13222
movq 0x50(%rax), %r15
incl 0x20bb7be(%rip)         # 0x20ce8dc
movl $0x0, 0x20bb7b0(%rip)   # 0x20ce8d8
movq $0x0, 0x20bb7ad(%rip) # 0x20ce8e0
testb %cl, %cl
je 0x1319f
leaq 0x20bc8c2(%rip), %rdi   # 0x20cfa00
leaq 0xf022(%rip), %rsi      # 0x22167
callq 0x4370
movq %rax, 0x20bb797(%rip)   # 0x20ce8e8
testq %rax, %rax
jne 0x1316b
leaq 0xde32(%rip), %rdi      # 0x20f8f
leaq 0x20bc89c(%rip), %rsi   # 0x20cfa00
xorl %eax, %eax
callq 0x9e9c
leaq 0x20bd88e(%rip), %rdi   # 0x20d0a00
leaq 0xefee(%rip), %rsi      # 0x22167
callq 0x4370
movq %rax, 0x20bb76b(%rip)   # 0x20ce8f0
testq %rax, %rax
jne 0x1319f
leaq 0xde1a(%rip), %rdi      # 0x20fab
leaq 0x20bd868(%rip), %rsi   # 0x20d0a00
xorl %eax, %eax
callq 0x9e9c
xorl %r12d, %r12d
cmpq %r14, %r15
setne %r12b
movq 0xb0(%r14), %rax
movq 0x48(%rax,%r12,8), %rdi
xorl %esi, %esi
callq 0x13397
movl %eax, %ebp
cmpb $0x0, 0x4(%rsp)
je 0x1322b
movq 0x20bb71c(%rip), %rdi   # 0x20ce8e8
callq 0x42e0
movq 0x20bb718(%rip), %rdi   # 0x20ce8f0
callq 0x42e0
testb %bpl, %bpl
jne 0x13234
leaq 0x20bd817(%rip), %rdi   # 0x20d0a00
callq 0x4150
leaq 0x20bc80b(%rip), %rdi   # 0x20cfa00
callq 0x4150
jmp 0x132ea
leaq 0xdcfc(%rip), %rdi      # 0x20f02
movl %r13d, %esi
xorl %eax, %eax
callq 0xa20e
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdd1a(%rip), %rdi      # 0x20f43
jmp 0x13206
testb %bpl, %bpl
je 0x132ea
movslq 0x20bb6bd(%rip), %rax   # 0x20ce8f8
leaq (%rax,%rax,4), %rax
leaq 0x200e1aa(%rip), %r15   # 0x20213f0
leaq (%r15,%rax,8), %rdi
leaq 0xa489(%rip), %rsi      # 0x1d6da
leaq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x7fe0
movslq 0x20bb693(%rip), %rax   # 0x20ce8f8
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rdi
leaq 0xa379(%rip), %rsi      # 0x1d5ed
callq 0x7482
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0xdd35(%rip), %rdi      # 0x20fc6
movl %r13d, %esi
movq %rax, %rdx
movb $0x3, %al
callq 0xa20e
leaq 0xdd59(%rip), %rdi      # 0x20ffe
xorl %eax, %eax
callq 0xa10c
leaq 0x20c0756(%rip), %r14   # 0x20d3a09
cmpb $0x0, (%r14)
jne 0x132c7
leaq 0xdf6a(%rip), %rdi      # 0x2122a
xorl %eax, %eax
callq 0x9bae
leaq 0x20c073a(%rip), %rax   # 0x20d3a08
cmpb $0x1, (%rax)
jne 0x132e1
leaq 0xdf6b(%rip), %rdi      # 0x21245
xorl %eax, %eax
callq 0x9e9c
movb $0x1, (%r14)
jmp 0x13210
incl 0x20bb5ec(%rip)         # 0x20ce8dc
movq 0xb0(%r14), %rax
movq 0x48(%rax,%r12,8), %rdi
movl $0x1, %esi
callq 0x13397
xorl %eax, %eax
movl %eax, 0x20bb5ee(%rip)   # 0x20ce8fc
movl %eax, 0x20bb5ec(%rip)   # 0x20ce900
movl %eax, 0x20bb5ea(%rip)   # 0x20ce904
movq %rbx, %rdi
callq 0x13472
movq %rax, %rbx
movl 0x20bb5ad(%rip), %esi   # 0x20ce8d8
leaq 0xdf24(%rip), %rdi      # 0x21256
xorl %eax, %eax
callq 0xa2ef
movl 0x20bb5c1(%rip), %esi   # 0x20ce900
leaq 0xdf1e(%rip), %rdi      # 0x21264
xorl %eax, %eax
callq 0xa2ef
movl 0x20bb5b1(%rip), %esi   # 0x20ce904
leaq 0xdf1b(%rip), %rdi      # 0x21275
xorl %eax, %eax
callq 0xa2ef
movl 0x20bb595(%rip), %esi   # 0x20ce8fc
leaq 0xdf17(%rip), %rdi      # 0x21285
xorl %eax, %eax
callq 0xa2ef
testl %r13d, %r13d
setne %al
movl 0x4(%rsp), %ecx
xorb $0x1, %cl
orb %al, %cl
jne 0x13210
movq %rbx, %rdi
callq 0x13de2
jmp 0x13210