// File: FreeSlave[P]vhlt/hlbsp/solidbsp.cpp
// Function: MakeLeaf(node_s*)
static void MakeLeaf (node_t *leafnode)
{
	int				nummarkfaces;
	face_t			*markfaces[MAX_LEAF_FACES + 1];
	surface_t		*surf;
	face_t			*f;	

    leafnode->planenum = -1;

	leafnode->iscontentsdetail = leafnode->detailbrushes != NULL;
	FreeLeafBrushes (leafnode);
	leafnode->detailbrushes = NULL;
#ifdef HLBSP_DETAILBRUSH_CULL
	if (leafnode->boundsbrush)
	{
		FreeBrush (leafnode->boundsbrush);
	}
	leafnode->boundsbrush = NULL;
#endif

	if (!(leafnode->isportalleaf && leafnode->contents == CONTENTS_SOLID))
	{
		nummarkfaces = 0;
		for (surf = leafnode->surfaces; surf; surf = surf->next)
		{
			if (!surf->onnode)
			{
				continue;
			}
#ifdef HLCSG_HLBSP_SOLIDHINT
			if (!surf->onnode)
			{
				continue;
			}
#endif
			for (f = surf->faces; f; f = f->next)
			{
				if (f->original == NULL)
				{ // because it is not on node or its content is solid
					continue;
				}
#ifdef HLCSG_HLBSP_SOLIDHINT
				if (f->original == NULL)
				{
					continue;
				}
#endif
				hlassume(nummarkfaces < MAX_LEAF_FACES, assume_MAX_LEAF_FACES);

				markfaces[nummarkfaces++] = f->original;
			}
		}
		markfaces[nummarkfaces] = NULL;                    // end marker
		nummarkfaces++;

		leafnode->markfaces = (face_t**)malloc(nummarkfaces * sizeof(*leafnode->markfaces));
		memcpy(leafnode->markfaces, markfaces, nummarkfaces * sizeof(*leafnode->markfaces));
	}

	FreeLeafSurfs(leafnode);
	leafnode->surfaces = NULL;

}

// Assembly:
subq $0x20028, %rsp          # imm = 0x20028
movq %rdi, 0x20020(%rsp)
movq 0x20020(%rsp), %rax
movl $0xffffffff, 0x80(%rax) # imm = 0xFFFFFFFF
movq 0x20020(%rsp), %rax
cmpq $0x0, 0x8(%rax)
setne %cl
movq 0x20020(%rsp), %rax
andb $0x1, %cl
movb %cl, 0x4a(%rax)
movq 0x20020(%rsp), %rdi
callq 0x2dfc0
movq 0x20020(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x20020(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2c27c
movq 0x20020(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x250e0
movq 0x20020(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x20020(%rsp), %rax
testb $0x1, 0x49(%rax)
je 0x2c2af
movq 0x20020(%rsp), %rax
cmpl $-0x2, 0xa0(%rax)
je 0x2c3ee
movl $0x0, 0x2001c(%rsp)
movq 0x20020(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x2c384
movq 0x8(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x2c2e7
jmp 0x2c372
movq 0x8(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x2c2f5
jmp 0x2c372
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
je 0x2c370
movq (%rsp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x2c316
jmp 0x2c363
movq (%rsp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x2c323
jmp 0x2c363
cmpl $0x4000, 0x2001c(%rsp) # imm = 0x4000
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x12, %esi
callq 0xbe90
movq (%rsp), %rax
movq 0x20(%rax), %rcx
movl 0x2001c(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x2001c(%rsp)
cltq
movq %rcx, 0x10(%rsp,%rax,8)
movq (%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
jmp 0x2c302
jmp 0x2c372
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x2c2ca
movslq 0x2001c(%rsp), %rax
movq $0x0, 0x10(%rsp,%rax,8)
movl 0x2001c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2001c(%rsp)
movslq 0x2001c(%rsp), %rdi
shlq $0x3, %rdi
callq 0x43e0
movq %rax, %rcx
movq 0x20020(%rsp), %rax
movq %rcx, 0xa8(%rax)
movq 0x20020(%rsp), %rax
movq 0xa8(%rax), %rdi
leaq 0x10(%rsp), %rsi
movslq 0x2001c(%rsp), %rdx
shlq $0x3, %rdx
callq 0x4260
movq 0x20020(%rsp), %rdi
callq 0x2e020
movq 0x20020(%rsp), %rax
movq $0x0, (%rax)
addq $0x20028, %rsp          # imm = 0x20028
retq
nopw %cs:(%rax,%rax)