// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %rbx
leaq 0xdb55(%rip), %rdi      # 0x1ee2f
xorl %eax, %eax
callq 0x8f76
leaq 0x20c172a(%rip), %rax   # 0x20d2a12
cmpb $0x1, (%rax)
jne 0x11300
leaq 0xdb53(%rip), %rdi      # 0x1ee47
xorl %eax, %eax
callq 0x8780
jmp 0x11581
cmpl $0x2, %ebp
jne 0x11315
leaq 0x20c170e(%rip), %rax   # 0x20d2a1a
cmpb $0x0, (%rax)
jne 0x11581
movl %r14d, 0xc(%rsp)
movl %ebp, 0x4(%rsp)
pushq $0x1
popq %r15
movl $0x0, 0x8(%rsp)
leaq 0xa311(%rip), %r12      # 0x1b642
leaq 0x10(%rsp), %r13
leaq 0x200f0b7(%rip), %rax   # 0x20203f4
movslq (%rax), %rax
cmpq %rax, %r15
jge 0x11457
imulq $0x28, %r15, %rbp
addq 0x17c84(%rip), %rbp     # 0x28fd8
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x7190
movq %rbp, %rdi
leaq 0xa1e9(%rip), %rsi      # 0x1b555
callq 0x691b
movq %rax, %r14
movq %rax, %rdi
callq 0x11114
testb %al, %al
jne 0x1144f
movq %rbp, %rdi
movq %r12, %rsi
callq 0x691b
cmpb $0x0, (%rax)
je 0x1144f
movsd 0x20(%rsp), %xmm0
addsd 0xcc82(%rip), %xmm0     # 0x1e028
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
leaq 0xda9a(%rip), %rsi      # 0x1ee50
callq 0x4430
testl %eax, %eax
je 0x113d9
movl %r15d, %edi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x116fc
movl 0x8(%rsp), %ecx
orb %al, %cl
movl %ecx, 0x8(%rsp)
jmp 0x1144f
pushq $-0x10
popq %rbp
cmpl $0x10, %ebp
jg 0x1144f
cvtsi2sd %ebp, %xmm0
movapd %xmm0, 0x30(%rsp)
pushq $-0x10
popq %r14
cmpl $0x10, %r14d
jg 0x11444
cvtsi2sd %r14d, %xmm0
movapd 0x30(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x10(%rsp), %xmm0
movapd %xmm1, 0x40(%rsp)
addpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movl %r15d, %edi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x116fc
testb %al, %al
jne 0x11449
movapd 0x10(%rsp), %xmm0
subpd 0x40(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
addl $0x10, %r14d
jmp 0x113ef
addl $0x10, %ebp
jmp 0x113dc
movb $0x1, %al
movl %eax, 0x8(%rsp)
incq %r15
jmp 0x11336
testb $0x1, 0x8(%rsp)
je 0x11566
leaq 0x20bc4b7(%rip), %r14   # 0x20cd920
movq 0xb0(%r14), %rax
testq %rax, %rax
je 0x1156f
movq 0x50(%rax), %r15
andl $0x0, 0x20bc464(%rip)   # 0x20cd8e8
incl 0x20bc462(%rip)         # 0x20cd8ec
andq $0x0, 0x20bc45e(%rip)   # 0x20cd8f0
movl 0xc(%rsp), %r13d
testb %r13b, %r13b
je 0x11504
leaq 0x20bd56d(%rip), %rdi   # 0x20cea10
leaq 0xec1d(%rip), %rsi      # 0x200c7
callq 0x4380
movq %rax, 0x20bc442(%rip)   # 0x20cd8f8
testq %rax, %rax
jne 0x114d0
leaq 0xda2d(%rip), %rdi      # 0x1eeef
leaq 0x20bd547(%rip), %rsi   # 0x20cea10
xorl %eax, %eax
callq 0x8b1f
leaq 0x20be539(%rip), %rdi   # 0x20cfa10
leaq 0xebe9(%rip), %rsi      # 0x200c7
callq 0x4380
movq %rax, 0x20bc416(%rip)   # 0x20cd900
testq %rax, %rax
jne 0x11504
leaq 0xda15(%rip), %rdi      # 0x1ef0b
leaq 0x20be513(%rip), %rsi   # 0x20cfa10
xorl %eax, %eax
callq 0x8b1f
xorl %r12d, %r12d
cmpq %r14, %r15
setne %r12b
movq 0xb0(%r14), %rax
movq 0x48(%rax,%r12,8), %rdi
xorl %esi, %esi
callq 0x11720
movl %eax, %ebp
testb %r13b, %r13b
je 0x11593
movq 0x20bc3c9(%rip), %rdi   # 0x20cd8f8
callq 0x42e0
movq 0x20bc3c5(%rip), %rdi   # 0x20cd900
callq 0x42e0
testb %bpl, %bpl
movl 0x4(%rsp), %ebp
jne 0x115a0
leaq 0x20be4c0(%rip), %rdi   # 0x20cfa10
callq 0x4150
leaq 0x20bd4b4(%rip), %rdi   # 0x20cea10
callq 0x4150
jmp 0x11653
leaq 0xd8f5(%rip), %rdi      # 0x1ee62
jmp 0x11576
leaq 0xd92d(%rip), %rdi      # 0x1eea3
movl 0x4(%rsp), %esi
xorl %eax, %eax
callq 0x8e95
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %bpl, %bpl
movl 0x4(%rsp), %ebp
je 0x11653
movslq 0x20bc361(%rip), %rax   # 0x20cd908
imulq $0x28, %rax, %rdi
leaq 0x200ee4e(%rip), %r15   # 0x2020400
addq %r15, %rdi
leaq 0xa086(%rip), %rsi      # 0x1b642
leaq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x7190
movslq 0x20bc338(%rip), %rax   # 0x20cd908
imulq $0x28, %rax, %rdi
addq %r15, %rdi
leaq 0x9f77(%rip), %rsi      # 0x1b555
callq 0x691b
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0xd92b(%rip), %rdi      # 0x1ef26
movl %ebp, %esi
movq %rax, %rdx
movb $0x3, %al
callq 0x8e95
leaq 0xd950(%rip), %rdi      # 0x1ef5e
xorl %eax, %eax
callq 0x8d93
leaq 0x20c13fd(%rip), %r14   # 0x20d2a19
cmpb $0x0, (%r14)
jne 0x11630
leaq 0xdb61(%rip), %rdi      # 0x1f18a
xorl %eax, %eax
callq 0x8780
leaq 0x20c13e1(%rip), %rax   # 0x20d2a18
cmpb $0x1, (%rax)
jne 0x1164a
leaq 0xdb62(%rip), %rdi      # 0x1f1a5
xorl %eax, %eax
callq 0x8b1f
movb $0x1, (%r14)
jmp 0x11581
incl 0x20bc293(%rip)         # 0x20cd8ec
movq 0xb0(%r14), %rax
movq 0x48(%rax,%r12,8), %rdi
pushq $0x1
popq %rsi
callq 0x11720
andl $0x0, 0x20bc298(%rip)   # 0x20cd90c
andl $0x0, 0x20bc295(%rip)   # 0x20cd910
andl $0x0, 0x20bc292(%rip)   # 0x20cd914
movq %rbx, %rdi
callq 0x1194f
movq %rax, %rbx
movl 0x20bc255(%rip), %esi   # 0x20cd8e8
leaq 0xdb1c(%rip), %rdi      # 0x1f1b6
xorl %eax, %eax
callq 0x8f76
movl 0x20bc269(%rip), %esi   # 0x20cd910
leaq 0xdb16(%rip), %rdi      # 0x1f1c4
xorl %eax, %eax
callq 0x8f76
movl 0x20bc259(%rip), %esi   # 0x20cd914
leaq 0xdb13(%rip), %rdi      # 0x1f1d5
xorl %eax, %eax
callq 0x8f76
movl 0x20bc23d(%rip), %esi   # 0x20cd90c
leaq 0xdb0f(%rip), %rdi      # 0x1f1e5
xorl %eax, %eax
callq 0x8f76
testl %ebp, %ebp
setne %al
xorb $0x1, %r13b
orb %al, %r13b
jne 0x11581
movq %rbx, %rdi
callq 0x1208b
jmp 0x11581