// File: FreeSlave[P]vhlt/hlbsp/outside.cpp
// Function: ClearOutFaces_r(node_s*)
static node_t*  ClearOutFaces_r(node_t* node)
{
    face_t*         f;
    face_t*         fnext;
#ifndef ZHLT_DETAILBRUSH
    face_t**        fp;
#endif
    portal_t*       p;

    // mark the node and all it's faces, so they
    // can be removed if no children use them

    node->valid = 0;                                       // will be set if any children touch it
    for (f = node->faces; f; f = f->next)
    {
        f->outputnumber = -1;
    }

    // go down the children
#ifdef ZHLT_DETAILBRUSH
	if (!node->isportalleaf)
#else
    if (node->planenum != -1)
#endif
    {
        //
        // decision node
        //
        node->children[0] = ClearOutFaces_r(node->children[0]);
        node->children[1] = ClearOutFaces_r(node->children[1]);

        // free any faces not in open child leafs
        f = node->faces;
        node->faces = NULL;

        for (; f; f = fnext)
        {
            fnext = f->next;
            if (f->outputnumber == -1)
            {                                              // never referenced, so free it
                c_free_faces++;
                FreeFace(f);
            }
            else
            {
                c_keep_faces++;
                f->next = node->faces;
                node->faces = f;
            }
        }

        if (!node->valid)
        {
			// Here leaks memory. --vluzacn
            // this node does not touch any interior leafs

            // if both children are solid, just make this node solid
            if (node->children[0]->contents == CONTENTS_SOLID && node->children[1]->contents == CONTENTS_SOLID)
            {
                node->contents = CONTENTS_SOLID;
                node->planenum = -1;
#ifdef ZHLT_DETAILBRUSH
				node->isportalleaf = true;
#endif
                return node;
            }

            // if one child is solid, shortcut down the other side
            if (node->children[0]->contents == CONTENTS_SOLID)
            {
                return node->children[1];
            }
            if (node->children[1]->contents == CONTENTS_SOLID)
            {
                return node->children[0];
            }

            c_falsenodes++;
        }
        return node;
    }

    //
    // leaf node
    //
    if (node->contents != CONTENTS_SOLID)
    {
        // this node is still inside

        // mark all the nodes used as portals
        for (p = node->portals; p;)
        {
            if (p->onnode)
            {
                p->onnode->valid = 1;
            }
            if (p->nodes[0] == node)                       // only write out from first leaf
            {
                p = p->next[0];
            }
            else
            {
                p = p->next[1];
            }
        }

#ifdef ZHLT_DETAILBRUSH
		MarkFacesInside_r (node);
#else
        // mark all of the faces to be drawn
        for (fp = node->markfaces; *fp; fp++)
        {
            (*fp)->outputnumber = 0;
        }
#endif

        return node;
    }

#ifndef ZHLT_DETAILBRUSH
    // this was a filled in node, so free the markfaces
    if (node->planenum != -1)
    {
        free(node->markfaces);
    }
#endif

    return node;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq 0x18(%rsp), %rax
movl $0x0, 0xbc(%rax)
movq 0x18(%rsp), %rax
movq 0x98(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x2203c
movq 0x10(%rsp), %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x22019
movq 0x18(%rsp), %rax
testb $0x1, 0x49(%rax)
jne 0x2220f
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rdi
callq 0x21ff0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x88(%rax)
movq 0x18(%rsp), %rax
movq 0x90(%rax), %rdi
callq 0x21ff0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x90(%rax)
movq 0x18(%rsp), %rax
movq 0x98(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq $0x0, 0x98(%rax)
cmpq $0x0, 0x10(%rsp)
je 0x22129
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
cmpl $-0x1, 0x28(%rax)
jne 0x220e7
movl 0x20c884e(%rip), %eax   # 0x20ea920
addl $0x1, %eax
movl %eax, 0x20c8845(%rip)   # 0x20ea920
movq 0x10(%rsp), %rdi
callq 0x24f00
jmp 0x2211b
movl 0x20c8837(%rip), %eax   # 0x20ea924
addl $0x1, %eax
movl %eax, 0x20c882e(%rip)   # 0x20ea924
movq 0x18(%rsp), %rax
movq 0x98(%rax), %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x98(%rax)
jmp 0x2211d
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x220ac
movq 0x18(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
jne 0x22200
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x2219b
movq 0x18(%rsp), %rax
movq 0x90(%rax), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x2219b
movq 0x18(%rsp), %rax
movl $0xfffffffe, 0xa0(%rax) # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rax
movl $0xffffffff, 0x80(%rax) # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rax
movb $0x1, 0x49(%rax)
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x2229c
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x221c6
movq 0x18(%rsp), %rax
movq 0x90(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x2229c
movq 0x18(%rsp), %rax
movq 0x90(%rax), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x221f1
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x2229c
movl 0x20c8725(%rip), %eax   # 0x20ea91c
addl $0x1, %eax
movl %eax, 0x20c871c(%rip)   # 0x20ea91c
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x2229c
movq 0x18(%rsp), %rax
cmpl $-0x2, 0xa0(%rax)
je 0x22292
movq 0x18(%rsp), %rax
movq 0xb0(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
je 0x2227c
movq (%rsp), %rax
cmpq $0x0, 0x40(%rax)
je 0x22251
movq (%rsp), %rax
movq 0x40(%rax), %rax
movl $0x1, 0xbc(%rax)
movq (%rsp), %rax
movq 0x48(%rax), %rax
cmpq 0x18(%rsp), %rax
jne 0x2226e
movq (%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, (%rsp)
jmp 0x2227a
movq (%rsp), %rax
movq 0x60(%rax), %rax
movq %rax, (%rsp)
jmp 0x2222d
movq 0x18(%rsp), %rdi
callq 0x228b0
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x2229c
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)