// File: FreeSlave[P]vhlt/hlbsp/solidbsp.cpp
// Function: SolidBSP(surfchain_t const*, brush_s*, bool)
node_t*         SolidBSP(const surfchain_t* const surfhead, 
#ifdef ZHLT_DETAILBRUSH
						 brush_t *detailbrushes, 
#endif
						 bool report_progress)
{
    node_t*         headnode;

	ResetStatus(report_progress);
	double start_time = I_FloatTime();
	if(report_progress)
	{
		Log("SolidBSP [hull %d] ",g_hullnum);
	}
	else
	{
	    Verbose("----- SolidBSP -----\n");
	}

    headnode = AllocNode();
    headnode->surfaces = surfhead->surfaces;
#ifdef ZHLT_DETAILBRUSH
	headnode->detailbrushes = detailbrushes;
	headnode->isdetail = false;
#ifdef HLBSP_DETAILBRUSH_CULL
	vec3_t brushmins, brushmaxs;
	VectorAddVec (surfhead->mins, -SIDESPACE, brushmins);
	VectorAddVec (surfhead->maxs, SIDESPACE, brushmaxs);
	headnode->boundsbrush = BrushFromBox (brushmins, brushmaxs);
#endif
#endif

#ifndef HLCSG_HLBSP_ALLOWEMPTYENTITY
    if (!surfhead->surfaces)
    {
        // nothing at all to build
#ifdef ZHLT_DETAILBRUSH
		headnode->isportalleaf = true;
		headnode->iscontentsdetail = (headnode->detailbrushes != NULL);
		FreeLeafBrushes (headnode->detailbrushes);
#endif
        headnode->planenum = -1;
        headnode->contents = CONTENTS_EMPTY;
        return headnode;
    }
#endif

    // generate six portals that enclose the entire world
    MakeHeadnodePortals(headnode, surfhead->mins, surfhead->maxs);

    // recursively partition everything
    BuildBspTree_r(headnode);

	double end_time = I_FloatTime();
	if(report_progress)
	{
		Log("%d (%.2f seconds)\n",++g_numProcessed,(end_time - start_time));
	}

    return headnode;
}

// Assembly:
subq $0x78, %rsp
movb %dl, %al
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
andb $0x1, %al
movb %al, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
movzbl %al, %edi
callq 0x2b180
callq 0x8e50
movsd %xmm0, 0x50(%rsp)
testb $0x1, 0x67(%rsp)
je 0x2b02f
leaq 0x20bf9f3(%rip), %rax   # 0x20eaa10
movl (%rax), %esi
leaq 0x10a6b(%rip), %rdi     # 0x3ba91
movb $0x0, %al
callq 0xb060
jmp 0x2b03d
leaq 0x10a6f(%rip), %rdi     # 0x3baa5
movb $0x0, %al
callq 0xba90
callq 0x25b80
movq %rax, 0x58(%rsp)
movq 0x70(%rsp), %rax
movq 0x30(%rax), %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
movq 0x68(%rsp), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x58(%rsp), %rax
movb $0x0, 0x48(%rax)
movq 0x70(%rsp), %rax
movsd 0x1084c(%rip), %xmm0    # 0x3b8c8
addsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x70(%rsp), %rax
movsd 0x10835(%rip), %xmm0    # 0x3b8c8
addsd 0x8(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x70(%rsp), %rax
movsd 0x1081d(%rip), %xmm0    # 0x3b8c8
addsd 0x10(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x70(%rsp), %rax
movsd 0xf3b5(%rip), %xmm0     # 0x3a478
addsd 0x18(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x70(%rsp), %rax
movsd 0xf39d(%rip), %xmm0     # 0x3a478
addsd 0x20(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x70(%rsp), %rax
movsd 0xf385(%rip), %xmm0     # 0x3a478
addsd 0x28(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x25670
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x58(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x18, %rdx
callq 0x22c00
movq 0x58(%rsp), %rdi
callq 0x2b1b0
callq 0x8e50
movsd %xmm0, 0x8(%rsp)
testb $0x1, 0x67(%rsp)
je 0x2b176
movl 0x32c491d(%rip), %esi   # 0x32efa70
addl $0x1, %esi
movl %esi, 0x32c4914(%rip)   # 0x32efa70
movsd 0x8(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
leaq 0x1094c(%rip), %rdi     # 0x3babb
movb $0x1, %al
callq 0xb060
movq 0x58(%rsp), %rax
addq $0x78, %rsp
retq