// File: FreeSlave[P]vhlt/hlbsp/solidbsp.cpp
// Function: BuildBspTree_r(node_s*)
static void     BuildBspTree_r(node_t* node)
{
    surface_t*      split;
    bool            midsplit;
    surface_t*      allsurfs;
#ifdef HLBSP_MAXNODESIZE_SKYBOX
	vec3_t			validmins, validmaxs;
#endif

    midsplit = CalcNodeBounds(node
#ifdef HLBSP_MAXNODESIZE_SKYBOX
		, validmins, validmaxs
#endif
		);
#ifdef HLBSP_DETAILBRUSH_CULL
	if (node->boundsbrush)
	{
		CalcBrushBounds (node->boundsbrush, node->loosemins, node->loosemaxs);
	}
	else
	{
		VectorFill (node->loosemins, BOGUS_RANGE);
		VectorFill (node->loosemaxs, -BOGUS_RANGE);
	}
#endif

#ifdef ZHLT_DETAILBRUSH
	int splitdetaillevel = CalcSplitDetaillevel (node);
#ifdef HLCSG_HLBSP_SOLIDHINT
	FixDetaillevelForDiscardable (node, splitdetaillevel);
#endif
#endif
    split = SelectPartition(node->surfaces, node, midsplit
#ifdef ZHLT_DETAILBRUSH
		, splitdetaillevel
#endif
#ifdef HLBSP_MAXNODESIZE_SKYBOX
		, validmins, validmaxs
#endif
		);
#ifdef ZHLT_DETAILBRUSH
	if (!node->isdetail && (!split || split->detaillevel > 0))
	{
		node->isportalleaf = true;
		LinkLeafFaces (node->surfaces, node); // set contents
		if (node->contents == CONTENTS_SOLID)
		{
			split = NULL;
		}
	}
	else
	{
		node->isportalleaf = false;
	}
#endif
    if (!split)
    {                                                      // this is a leaf node
#ifdef ZHLT_DETAILBRUSH
		MakeLeaf (node);
#else
        node->planenum = PLANENUM_LEAF;
        LinkLeafFaces(node->surfaces, node);
#endif
        return;
    }

    // these are final polygons
    split->onnode = node;                                  // can't use again
    allsurfs = node->surfaces;
    node->planenum = split->planenum;
    node->faces = NULL;
    CopyFacesToNode(node, split);

    node->children[0] = AllocNode();
    node->children[1] = AllocNode();
#ifdef ZHLT_DETAILBRUSH
	node->children[0]->isdetail = split->detaillevel > 0;
	node->children[1]->isdetail = split->detaillevel > 0;
#endif

    // split all the polysurfaces into front and back lists
    SplitNodeSurfaces(allsurfs, node);
#ifdef ZHLT_DETAILBRUSH
	SplitNodeBrushes (node->detailbrushes, node);
#ifdef HLBSP_DETAILBRUSH_CULL
	if (node->boundsbrush)
	{
		for (int k = 0; k < 2; k++)
		{
			dplane_t p;
			brush_t *copy, *front, *back;
			if (k == 0)
			{ // front child
				VectorCopy (g_dplanes[split->planenum].normal, p.normal);
				p.dist = g_dplanes[split->planenum].dist - BOUNDS_EXPANSION;
			}
			else
			{ // back child
				VecSubtractVector (0, g_dplanes[split->planenum].normal, p.normal);
				p.dist = -g_dplanes[split->planenum].dist - BOUNDS_EXPANSION;
			}
			copy = NewBrushFromBrush (node->boundsbrush);
			SplitBrush (copy, &p, &front, &back);
			if (back)
			{
				FreeBrush (back);
			}
			if (!front)
			{
				Warning ("BuildBspTree_r: bounds was clipped away at (%f,%f,%f)-(%f,%f,%f).", node->loosemins[0], node->loosemins[1], node->loosemins[2], node->loosemaxs[0], node->loosemaxs[1], node->loosemaxs[2]);
			}
			node->children[k]->boundsbrush = front;
		}
		FreeBrush (node->boundsbrush);
	}
	node->boundsbrush = NULL;
#endif
#endif

#ifdef ZHLT_DETAILBRUSH
	if (!split->detaillevel)
	{
		MakeNodePortal (node);
		SplitNodePortals (node);
	}
#else
    // create the portal that seperates the two children
    MakeNodePortal(node);

    // carve the portals on the boundaries of the node
    SplitNodePortals(node);
#endif

    // recursively do the children
    BuildBspTree_r(node->children[0]);
    BuildBspTree_r(node->children[1]);
	UpdateStatus();
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x2b780
andb $0x1, %al
movb %al, 0xa7(%rsp)
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2b21c
movq 0xb0(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xb0(%rsp), %rsi
addq $0x18, %rsi
movq 0xb0(%rsp), %rdx
addq $0x30, %rdx
callq 0x25920
jmp 0x2b29a
movq 0xb0(%rsp), %rax
movsd 0xe3dc(%rip), %xmm0     # 0x39608
movsd %xmm0, 0x18(%rax)
movq 0xb0(%rsp), %rax
movsd 0xe3c7(%rip), %xmm0     # 0x39608
movsd %xmm0, 0x20(%rax)
movq 0xb0(%rsp), %rax
movsd 0xe3b2(%rip), %xmm0     # 0x39608
movsd %xmm0, 0x28(%rax)
movq 0xb0(%rsp), %rax
movsd 0xf3a5(%rip), %xmm0     # 0x3a610
movsd %xmm0, 0x30(%rax)
movq 0xb0(%rsp), %rax
movsd 0xf390(%rip), %xmm0     # 0x3a610
movsd %xmm0, 0x38(%rax)
movq 0xb0(%rsp), %rax
movsd 0xf37b(%rip), %xmm0     # 0x3a610
movsd %xmm0, 0x40(%rax)
movq 0xb0(%rsp), %rdi
callq 0x2aaf0
movl %eax, 0x5c(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0x2ab90
movq 0xb0(%rsp), %rax
movq (%rax), %rdi
movq 0xb0(%rsp), %rsi
movb 0xa7(%rsp), %al
movl 0x5c(%rsp), %ecx
leaq 0x80(%rsp), %r8
leaq 0x60(%rsp), %r9
andb $0x1, %al
movzbl %al, %edx
callq 0x2bb90
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
testb $0x1, 0x48(%rax)
jne 0x2b363
cmpq $0x0, 0xa8(%rsp)
je 0x2b320
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x50(%rax)
jle 0x2b363
movq 0xb0(%rsp), %rax
movb $0x1, 0x49(%rax)
movq 0xb0(%rsp), %rax
movq (%rax), %rdi
movq 0xb0(%rsp), %rsi
callq 0x2bc30
movq 0xb0(%rsp), %rax
cmpl $-0x2, 0xa0(%rax)
jne 0x2b361
movq $0x0, 0xa8(%rsp)
jmp 0x2b36f
movq 0xb0(%rsp), %rax
movb $0x0, 0x49(%rax)
cmpq $0x0, 0xa8(%rsp)
jne 0x2b38c
movq 0xb0(%rsp), %rdi
callq 0x2c200
jmp 0x2b771
movq 0xb0(%rsp), %rcx
movq 0xa8(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xa8(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0xb0(%rsp), %rax
movl %ecx, 0x80(%rax)
movq 0xb0(%rsp), %rax
movq $0x0, 0x98(%rax)
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x2c420
callq 0x25b80
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x88(%rax)
callq 0x25b80
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x90(%rax)
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x50(%rax)
setg %cl
movq 0xb0(%rsp), %rax
movq 0x88(%rax), %rax
andb $0x1, %cl
movb %cl, 0x48(%rax)
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x50(%rax)
setg %cl
movq 0xb0(%rsp), %rax
movq 0x90(%rax), %rax
andb $0x1, %cl
movb %cl, 0x48(%rax)
movq 0x98(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x2c540
movq 0xb0(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0xb0(%rsp), %rsi
callq 0x2c670
movq 0xb0(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2b70c
movl $0x0, 0x58(%rsp)
cmpl $0x2, 0x58(%rsp)
jge 0x2b6fb
cmpl $0x0, 0x58(%rsp)
jne 0x2b56d
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c4554(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c4530(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c450b(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c44e6(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movsd 0x30(%rax), %xmm0
movsd 0xdbea(%rip), %xmm1     # 0x39148
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x2b62c
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c44b0(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c4489(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
subsd 0x8(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c4461(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
subsd 0x10(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x8(%rax), %rcx
leaq 0x20c4439(%rip), %rax   # 0x20efa30
shlq $0x6, %rcx
addq %rcx, %rax
movsd 0x30(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0xdb26(%rip), %xmm1     # 0x39148
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0xb0(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x25140
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
callq 0x253b0
cmpq $0x0, (%rsp)
je 0x2b669
movq (%rsp), %rdi
callq 0x250e0
cmpq $0x0, 0x8(%rsp)
jne 0x2b6cd
movq 0xb0(%rsp), %rax
movsd 0x18(%rax), %xmm0
movq 0xb0(%rsp), %rax
movsd 0x20(%rax), %xmm1
movq 0xb0(%rsp), %rax
movsd 0x28(%rax), %xmm2
movq 0xb0(%rsp), %rax
movsd 0x30(%rax), %xmm3
movq 0xb0(%rsp), %rax
movsd 0x38(%rax), %xmm4
movq 0xb0(%rsp), %rax
movsd 0x40(%rax), %xmm5
leaq 0x10415(%rip), %rdi     # 0x3badb
movb $0x6, %al
callq 0xb980
movq 0x8(%rsp), %rcx
movq 0xb0(%rsp), %rax
movslq 0x58(%rsp), %rdx
movq 0x88(%rax,%rdx,8), %rax
movq %rcx, 0x10(%rax)
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x2b4b3
movq 0xb0(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x250e0
movq 0xb0(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x2b744
movq 0xb0(%rsp), %rdi
callq 0x2c760
movq 0xb0(%rsp), %rdi
callq 0x2ca00
movq 0xb0(%rsp), %rax
movq 0x88(%rax), %rdi
callq 0x2b1b0
movq 0xb0(%rsp), %rax
movq 0x90(%rax), %rdi
callq 0x2b1b0
callq 0x2cca0
addq $0xb8, %rsp
retq
nopl (%rax)