// File: FreeSlave[P]vhlt/hlbsp/portals.cpp
// Function: MakeHeadnodePortals(node_s*, double const*, double const*)
void            MakeHeadnodePortals(node_t* node, const vec3_t mins, const vec3_t maxs)
{
    vec3_t          bounds[2];
    int             i, j, n;
    portal_t*       p;
    portal_t*       portals[6];
    dplane_t        bplanes[6];
    dplane_t*       pl;

    // pad with some space so there will never be null volume leafs
    for (i = 0; i < 3; i++)
    {
        bounds[0][i] = mins[i] - SIDESPACE;
        bounds[1][i] = maxs[i] + SIDESPACE;
    }

    g_outside_node.contents = CONTENTS_SOLID;
    g_outside_node.portals = NULL;

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 2; j++)
        {
            n = j * 3 + i;

            p = AllocPortal();
            portals[n] = p;

            pl = &bplanes[n];
            memset(pl, 0, sizeof(*pl));
            if (j)
            {
                pl->normal[i] = -1;
                pl->dist = -bounds[j][i];
            }
            else
            {
                pl->normal[i] = 1;
                pl->dist = bounds[j][i];
            }
            p->plane = *pl;
            p->winding = new Winding(*pl);
            AddPortalToNodes(p, node, &g_outside_node);
        }
    }

    // clip the basewindings by all the other planes
    for (i = 0; i < 6; i++)
    {
        for (j = 0; j < 6; j++)
        {
            if (j == i)
            {
                continue;
            }
            portals[i]->winding->Clip(bplanes[j], true);
        }
    }
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
movq %rdx, 0x220(%rsp)
movl $0x0, 0x1ec(%rsp)
cmpl $0x3, 0x1ec(%rsp)
jge 0x22ca7
movq 0x228(%rsp), %rax
movslq 0x1ec(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x17827(%rip), %xmm1    # 0x3a478
subsd %xmm1, %xmm0
movslq 0x1ec(%rsp), %rax
movsd %xmm0, 0x1f0(%rsp,%rax,8)
movq 0x220(%rsp), %rax
movslq 0x1ec(%rsp), %rcx
movsd 0x177fa(%rip), %xmm0    # 0x3a478
addsd (%rax,%rcx,8), %xmm0
movslq 0x1ec(%rsp), %rax
movsd %xmm0, 0x208(%rsp,%rax,8)
movl 0x1ec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ec(%rsp)
jmp 0x22c2a
movl $0xfffffffe, 0x20c7d1f(%rip) # imm = 0xFFFFFFFE
movq $0x0, 0x20c7d24(%rip) # 0x20ea9e0
movl $0x0, 0x1ec(%rsp)
cmpl $0x3, 0x1ec(%rsp)
jge 0x22ecc
movl $0x0, 0x1e8(%rsp)
cmpl $0x2, 0x1e8(%rsp)
jge 0x22eb4
imull $0x3, 0x1e8(%rsp), %eax
addl 0x1ec(%rsp), %eax
movl %eax, 0x1e4(%rsp)
callq 0x24f50
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rcx
movslq 0x1e4(%rsp), %rax
movq %rcx, 0x1a0(%rsp,%rax,8)
movslq 0x1e4(%rsp), %rcx
leaq 0x20(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x4180
cmpl $0x0, 0x1e8(%rsp)
je 0x22dbe
movq 0x18(%rsp), %rax
movslq 0x1ec(%rsp), %rcx
movsd 0x166ee(%rip), %xmm0    # 0x39460
movsd %xmm0, (%rax,%rcx,8)
movslq 0x1e8(%rsp), %rcx
leaq 0x1f0(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq 0x1ec(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq 0x18(%rsp), %rax
movsd %xmm0, 0x30(%rax)
jmp 0x22e06
movq 0x18(%rsp), %rax
movslq 0x1ec(%rsp), %rcx
movsd 0x16375(%rip), %xmm0    # 0x39148
movsd %xmm0, (%rax,%rcx,8)
movslq 0x1e8(%rsp), %rcx
leaq 0x1f0(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq 0x1ec(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x18(%rsp), %rax
movsd %xmm0, 0x30(%rax)
movq 0x18(%rsp), %rcx
movq 0x1d8(%rsp), %rax
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups 0x30(%rcx), %xmm3
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movl $0x20, %edi
callq 0x42f0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rsi
callq 0xf740
jmp 0x22e51
movq (%rsp), %rcx
movq 0x1d8(%rsp), %rax
movq %rcx, 0x68(%rax)
movq 0x1d8(%rsp), %rdi
movq 0x230(%rsp), %rsi
leaq 0x20c7ab8(%rip), %rdx   # 0x20ea930
callq 0x22a40
movl 0x1e8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1e8(%rsp)
jmp 0x22ce0
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl $0x20, %esi
callq 0x4300
jmp 0x22f79
jmp 0x22eb6
movl 0x1ec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ec(%rsp)
jmp 0x22cc7
movl $0x0, 0x1ec(%rsp)
cmpl $0x6, 0x1ec(%rsp)
jge 0x22f71
movl $0x0, 0x1e8(%rsp)
cmpl $0x6, 0x1e8(%rsp)
jge 0x22f59
movl 0x1e8(%rsp), %eax
cmpl 0x1ec(%rsp), %eax
jne 0x22f0c
jmp 0x22f46
movslq 0x1ec(%rsp), %rax
movq 0x1a0(%rsp,%rax,8), %rax
movq 0x68(%rax), %rdi
movslq 0x1e8(%rsp), %rax
leaq 0x20(%rsp), %rsi
shlq $0x6, %rax
addq %rax, %rsi
movl $0x1, %edx
movsd 0x166bf(%rip), %xmm0    # 0x39600
callq 0x105e0
movl 0x1e8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1e8(%rsp)
jmp 0x22ef0
jmp 0x22f5b
movl 0x1ec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ec(%rsp)
jmp 0x22ed7
addq $0x238, %rsp            # imm = 0x238
retq
movq 0x10(%rsp), %rdi
callq 0x4520
nopw %cs:(%rax,%rax)