// File: FreeSlave[P]vhlt/hlbsp/solidbsp.cpp
// Function: SplitNodePortals(node_s*)
static void     SplitNodePortals(node_t *node)
{
    portal_t*       p;
    portal_t*       next_portal;
    portal_t*       new_portal;
    node_t*         f;
    node_t*         b;
    node_t*         other_node;
    int             side = 0;
    dplane_t*       plane;
    Winding*        frontwinding;
    Winding*        backwinding;

    plane = &g_dplanes[node->planenum];
    f = node->children[0];
    b = node->children[1];

    for (p = node->portals; p; p = next_portal)
    {
        if (p->nodes[0] == node)
        {
            side = 0;
        }
        else if (p->nodes[1] == node)
        {
            side = 1;
        }
        else
        {
            Error("SplitNodePortals: mislinked portal");
        }
        next_portal = p->next[side];

        other_node = p->nodes[!side];
        RemovePortalFromNode(p, p->nodes[0]);
        RemovePortalFromNode(p, p->nodes[1]);

        // cut the portal into two portals, one on each side of the cut plane
        p->winding->Divide(*plane, &frontwinding, &backwinding);

#ifdef ZHLT_WINDING_RemoveColinearPoints_VL
		if (!frontwinding && !backwinding)
		{
			continue;
		}
#endif
        if (!frontwinding)
        {
            if (side == 0)
            {
                AddPortalToNodes(p, b, other_node);
            }
            else
            {
                AddPortalToNodes(p, other_node, b);
            }
            continue;
        }
        if (!backwinding)
        {
            if (side == 0)
            {
                AddPortalToNodes(p, f, other_node);
            }
            else
            {
                AddPortalToNodes(p, other_node, f);
            }
            continue;
        }

        // the winding is split
        new_portal = AllocPortal();
        *new_portal = *p;
        new_portal->winding = backwinding;
        delete p->winding;
        p->winding = frontwinding;

        if (side == 0)
        {
            AddPortalToNodes(p, f, other_node);
            AddPortalToNodes(new_portal, b, other_node);
        }
        else
        {
            AddPortalToNodes(p, other_node, f);
            AddPortalToNodes(new_portal, other_node, b);
        }
    }

    node->portals = NULL;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x60(%rsp), %rax
movslq 0x80(%rax), %rcx
leaq 0x20c300c(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rax
movq 0x90(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x60(%rsp), %rax
movq 0xb0(%rax), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0x2cc86
movq 0x58(%rsp), %rax
movq 0x48(%rax), %rax
cmpq 0x60(%rsp), %rax
jne 0x2ca89
movl $0x0, 0x2c(%rsp)
jmp 0x2cab3
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
cmpq 0x60(%rsp), %rax
jne 0x2caa3
movl $0x1, 0x2c(%rsp)
jmp 0x2cab1
leaq 0xf24e(%rip), %rdi      # 0x3bcf8
movb $0x0, %al
callq 0xb510
jmp 0x2cab3
movq 0x58(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq 0x58(%rax,%rcx,8), %rax
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rax
cmpl $0x0, 0x2c(%rsp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movq 0x48(%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rdi
movq 0x58(%rsp), %rax
movq 0x48(%rax), %rsi
callq 0x22af0
movq 0x58(%rsp), %rdi
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rsi
callq 0x22af0
movq 0x58(%rsp), %rax
movq 0x68(%rax), %rdi
movq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movsd 0xcad3(%rip), %xmm0     # 0x39600
callq 0x10c90
cmpq $0x0, 0x18(%rsp)
jne 0x2cb47
cmpq $0x0, 0x10(%rsp)
jne 0x2cb47
jmp 0x2cc77
cmpq $0x0, 0x18(%rsp)
jne 0x2cb85
cmpl $0x0, 0x2c(%rsp)
jne 0x2cb6c
movq 0x58(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x22a40
jmp 0x2cb80
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x22a40
jmp 0x2cc77
cmpq $0x0, 0x10(%rsp)
jne 0x2cbc3
cmpl $0x0, 0x2c(%rsp)
jne 0x2cbaa
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x22a40
jmp 0x2cbbe
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x22a40
jmp 0x2cc77
callq 0x24f50
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rdi
movl $0x70, %edx
callq 0x4260
movq 0x10(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x68(%rax)
movq 0x58(%rsp), %rax
movq 0x68(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2cc0e
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x18(%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x68(%rax)
cmpl $0x0, 0x2c(%rsp)
jne 0x2cc4d
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x22a40
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x22a40
jmp 0x2cc75
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x22a40
movq 0x48(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x22a40
jmp 0x2cc77
movq 0x50(%rsp), %rax
movq %rax, 0x58(%rsp)
jmp 0x2ca63
movq 0x60(%rsp), %rax
movq $0x0, 0xb0(%rax)
addq $0x68, %rsp
retq
nopl (%rax,%rax)