// File: FreeSlave[P]vhlt/hlbsp/portals.cpp
// Function: WritePortalFile_r(node_s const*)
static void     WritePortalFile_r(const node_t* const node)
{
    int             i;
    portal_t*       p;
    Winding*        w;
    dplane_t        plane2;

#ifdef ZHLT_DETAILBRUSH
	if (!node->isportalleaf)
#else
    if (!node->contents)
#endif
    {
        WritePortalFile_r(node->children[0]);
        WritePortalFile_r(node->children[1]);
        return;
    }

    if (node->contents == CONTENTS_SOLID)
    {
        return;
    }

    for (p = node->portals; p;)
    {
        w = p->winding;
        if (w && p->nodes[0] == node)
        {
            if (p->nodes[0]->contents == p->nodes[1]->contents)
            {
                // write out to the file

                // sometimes planes get turned around when they are very near
                // the changeover point between different axis.  interpret the
                // plane the same way vis will, and flip the side orders if needed
                w->getPlane(plane2);
                if (DotProduct(p->plane.normal, plane2.normal) < 1.0 - ON_EPSILON)
                {                                          // backwards...
#ifdef ZHLT_WINDING_FIX
					if (DotProduct(p->plane.normal, plane2.normal) > -1.0 + ON_EPSILON)
					{
						Warning ("Colinear portal @");
						w->Print ();
					}
					else
					{
						Warning ("Backward portal @");
						w->Print ();
					}
#endif
                    fprintf(pf, "%u %i %i ", w->m_NumPoints, p->nodes[1]->visleafnum, p->nodes[0]->visleafnum);
                }
                else
                {
                    fprintf(pf, "%u %i %i ", w->m_NumPoints, p->nodes[0]->visleafnum, p->nodes[1]->visleafnum);
                }

                for (i = 0; i < w->m_NumPoints; i++)
                {
                    fprintf(pf, "(%f %f %f) ", w->m_Points[i][0], w->m_Points[i][1], w->m_Points[i][2]);
                }
                fprintf(pf, "\n");
#ifdef HLBSP_VIEWPORTAL
				if (g_viewportal)
				{
					vec3_t center, center1, center2;
					vec3_t from = {0.0, 0.0, -65536};
					w->getCenter (center);
					VectorMA (center, 0.5, p->plane.normal, center1);
					VectorMA (center, -0.5, p->plane.normal, center2);
					fprintf (pf_view, "%5.2f %5.2f %5.2f\n", from[0], from[1], from[2]);
					fprintf (pf_view, "%5.2f %5.2f %5.2f\n", center1[0], center1[1], center1[2]);
					for (i = 0; i < w->m_NumPoints; i++)
					{
						vec_t *p1, *p2;
						p1 = w->m_Points[i];
						p2 = w->m_Points[(i+1)%w->m_NumPoints];
						fprintf (pf_view, "%5.2f %5.2f %5.2f\n", p1[0], p1[1], p1[2]);
						fprintf (pf_view, "%5.2f %5.2f %5.2f\n", p2[0], p2[1], p2[2]);
						fprintf (pf_view, "%5.2f %5.2f %5.2f\n", center2[0], center2[1], center2[2]);
						fprintf (pf_view, "%5.2f %5.2f %5.2f\n", center1[0], center1[1], center1[2]);
					}
				}
#endif
            }
        }

        if (p->nodes[0] == node)
        {
            p = p->next[0];
        }
        else
        {
            p = p->next[1];
        }
    }

}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
testb $0x1, 0x49(%rax)
jne 0x232da
movq 0xf0(%rsp), %rax
movq 0x88(%rax), %rdi
callq 0x23290
movq 0xf0(%rsp), %rax
movq 0x90(%rax), %rdi
callq 0x23290
jmp 0x23908
movq 0xf0(%rsp), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x232f0
jmp 0x23908
movq 0xf0(%rsp), %rax
movq 0xb0(%rax), %rax
movq %rax, 0xe0(%rsp)
cmpq $0x0, 0xe0(%rsp)
je 0x23908
movq 0xe0(%rsp), %rax
movq 0x68(%rax), %rax
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
je 0x238c3
movq 0xe0(%rsp), %rax
movq 0x48(%rax), %rax
cmpq 0xf0(%rsp), %rax
jne 0x238c3
movq 0xe0(%rsp), %rax
movq 0x48(%rax), %rax
movl 0xa0(%rax), %eax
movq 0xe0(%rsp), %rcx
movq 0x50(%rcx), %rcx
cmpl 0xa0(%rcx), %eax
jne 0x238c1
movq 0xd8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xd810
movq 0xe0(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x98(%rsp), %xmm2
movq 0xe0(%rsp), %rax
movsd 0x8(%rax), %xmm1
mulsd 0xa0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0xe0(%rsp), %rax
movsd 0x10(%rax), %xmm1
movsd 0xa8(%rsp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x17095(%rip), %xmm0    # 0x3a480
ucomisd %xmm1, %xmm0
jbe 0x234d3
movq 0xe0(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x98(%rsp), %xmm2
movq 0xe0(%rsp), %rax
movsd 0x8(%rax), %xmm0
mulsd 0xa0(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movq 0xe0(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0xa8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x1703a(%rip), %xmm1    # 0x3a488
ucomisd %xmm1, %xmm0
jbe 0x23471
leaq 0x1713b(%rip), %rdi     # 0x3a596
movb $0x0, %al
callq 0xb980
movq 0xd8(%rsp), %rdi
callq 0xd780
jmp 0x2348c
leaq 0x17130(%rip), %rdi     # 0x3a5a8
movb $0x0, %al
callq 0xb980
movq 0xd8(%rsp), %rdi
callq 0xd780
movq 0x20c756d(%rip), %rdi   # 0x20eaa00
movq 0xd8(%rsp), %rax
movl 0x8(%rax), %edx
movq 0xe0(%rsp), %rax
movq 0x50(%rax), %rax
movl 0xb8(%rax), %ecx
movq 0xe0(%rsp), %rax
movq 0x48(%rax), %rax
movl 0xb8(%rax), %r8d
leaq 0x170f0(%rip), %rsi     # 0x3a5ba
movb $0x0, %al
callq 0x4420
jmp 0x23518
movq 0x20c7526(%rip), %rdi   # 0x20eaa00
movq 0xd8(%rsp), %rax
movl 0x8(%rax), %edx
movq 0xe0(%rsp), %rax
movq 0x48(%rax), %rax
movl 0xb8(%rax), %ecx
movq 0xe0(%rsp), %rax
movq 0x50(%rax), %rax
movl 0xb8(%rax), %r8d
leaq 0x170a9(%rip), %rsi     # 0x3a5ba
movb $0x0, %al
callq 0x4420
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movq 0xd8(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x235c5
movq 0x20c74be(%rip), %rdi   # 0x20eaa00
movq 0xd8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xec(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq 0xd8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xec(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq 0xd8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xec(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm2
leaq 0x1701c(%rip), %rsi     # 0x3a5c4
movb $0x3, %al
callq 0x4420
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x23523
movq 0x20c7434(%rip), %rdi   # 0x20eaa00
leaq 0x14725(%rip), %rsi     # 0x37cf8
movb $0x0, %al
callq 0x4420
leaq 0x20cc44a(%rip), %rax   # 0x20efa2b
testb $0x1, (%rax)
je 0x238bf
movq 0x16fff(%rip), %rax     # 0x3a5f0
movq %rax, 0x20(%rsp)
movq 0x16ffb(%rip), %rax     # 0x3a5f8
movq %rax, 0x28(%rsp)
movq 0x16ff7(%rip), %rax     # 0x3a600
movq %rax, 0x30(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xe030
movsd 0x80(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x15df8(%rip), %xmm2    # 0x39438
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x88(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd 0x15dcc(%rip), %xmm2    # 0x39438
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x90(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0x15da0(%rip), %xmm2    # 0x39438
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x80(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x16dcd(%rip), %xmm2    # 0x3a490
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x88(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd 0x16da1(%rip), %xmm2    # 0x3a490
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x90(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0x16d75(%rip), %xmm2    # 0x3a490
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x20c72d8(%rip), %rdi   # 0x20eaa08
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x16e87(%rip), %rsi     # 0x3a5d0
movb $0x3, %al
callq 0x4420
movq 0x20c72b1(%rip), %rdi   # 0x20eaa08
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
leaq 0x16e60(%rip), %rsi     # 0x3a5d0
movb $0x3, %al
callq 0x4420
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movq 0xd8(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x238bd
movq 0xd8(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0xec(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0xd8(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movq 0xd8(%rsp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movq 0x8(%rsp), %rax
movl %edx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x20c720c(%rip), %rdi   # 0x20eaa08
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movq 0x18(%rsp), %rax
movsd 0x8(%rax), %xmm1
movq 0x18(%rsp), %rax
movsd 0x10(%rax), %xmm2
leaq 0x16db0(%rip), %rsi     # 0x3a5d0
movb $0x3, %al
callq 0x4420
movq 0x20c71da(%rip), %rdi   # 0x20eaa08
movq 0x10(%rsp), %rax
movsd (%rax), %xmm0
movq 0x10(%rsp), %rax
movsd 0x8(%rax), %xmm1
movq 0x10(%rsp), %rax
movsd 0x10(%rax), %xmm2
leaq 0x16d7e(%rip), %rsi     # 0x3a5d0
movb $0x3, %al
callq 0x4420
movq 0x20c71a8(%rip), %rdi   # 0x20eaa08
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
leaq 0x16d57(%rip), %rsi     # 0x3a5d0
movb $0x3, %al
callq 0x4420
movq 0x20c7181(%rip), %rdi   # 0x20eaa08
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
leaq 0x16d30(%rip), %rsi     # 0x3a5d0
movb $0x3, %al
callq 0x4420
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x23782
jmp 0x238bf
jmp 0x238c1
jmp 0x238c3
movq 0xe0(%rsp), %rax
movq 0x48(%rax), %rax
cmpq 0xf0(%rsp), %rax
jne 0x238ef
movq 0xe0(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0xe0(%rsp)
jmp 0x23903
movq 0xe0(%rsp), %rax
movq 0x60(%rax), %rax
movq %rax, 0xe0(%rsp)
jmp 0x23307
addq $0xf8, %rsp
retq