// File: FreeSlave[P]vhlt/hlbsp/solidbsp.cpp
// Function: LinkLeafFaces(surface_s*, node_s*)
static void     LinkLeafFaces(surface_t* planelist, node_t* leafnode)
{
    face_t*         f;
    surface_t*      surf;
    int             rank, r;
#ifndef ZHLT_DETAILBRUSH
    int             nummarkfaces;
    face_t*         markfaces[MAX_LEAF_FACES];

    leafnode->faces = NULL;
    leafnode->planenum = -1;
#endif

    rank = -1;
    for (surf = planelist; surf; surf = surf->next)
    {
#ifdef ZHLT_DETAILBRUSH
		if (!surf->onnode)
		{
			continue;
		}
#endif
        for (f = surf->faces; f; f = f->next)
        {
            if ((f->contents == CONTENTS_HINT))
            {
                f->contents = CONTENTS_EMPTY;
            }
#ifdef ZHLT_DETAILBRUSH
			if (f->detaillevel)
			{
				continue;
			}
#endif
            r = RankForContents(f->contents);
            if (r > rank)
            {
                rank = r;
            }
        }
    }
#ifdef HLBSP_WARNMIXEDCONTENTS
	for (surf = planelist; surf; surf = surf->next)
	{
#ifdef ZHLT_DETAILBRUSH
		if (!surf->onnode)
		{
			continue;
		}
#endif
		for (f = surf->faces; f; f = f->next)
		{
#ifdef ZHLT_DETAILBRUSH
			if (f->detaillevel)
			{
				continue;
			}
#endif
			r = RankForContents(f->contents);
			if (r != rank)
				break;
		}
		if (f)
			break;
	}
	if (surf)
	{
		entity_t *ent = EntityForModel (g_nummodels - 1);
		if (g_nummodels - 1 != 0 && ent == &g_entities[0])
		{
			ent = NULL;
		}
		Warning ("Ambiguous leafnode content ( %s and %s ) at (%.0f,%.0f,%.0f)-(%.0f,%.0f,%.0f) in hull %d of model %d (entity: classname \"%s\", origin \"%s\", targetname \"%s\")", 
			ContentsToString (ContentsForRank(r)), ContentsToString (ContentsForRank(rank)), 
			leafnode->mins[0], leafnode->mins[1], leafnode->mins[2], leafnode->maxs[0], leafnode->maxs[1], leafnode->maxs[2], 
			g_hullnum, g_nummodels - 1, 
			(ent? ValueForKey (ent, "classname"): "unknown"), 
			(ent? ValueForKey (ent, "origin"): "unknown"), 
			(ent? ValueForKey (ent, "targetname"): "unknown"));
		for (surface_t *surf2 = planelist; surf2; surf2 = surf2->next)
		{
			for (face_t *f2 = surf2->faces; f2; f2 = f2->next)
			{
				Developer (DEVELOPER_LEVEL_SPAM, "content = %d plane = %d normal = (%g,%g,%g)\n", f2->contents, f2->planenum, 
					g_dplanes[f2->planenum].normal[0], g_dplanes[f2->planenum].normal[1], g_dplanes[f2->planenum].normal[2]);
				for (int i = 0; i < f2->numpoints; i++)
				{
					Developer (DEVELOPER_LEVEL_SPAM, "(%g,%g,%g)\n", f2->pts[i][0], f2->pts[i][1], f2->pts[i][2]);
				}
			}
		}
	}
#endif

    leafnode->contents = ContentsForRank(rank);

#ifndef ZHLT_DETAILBRUSH
    if (leafnode->contents != CONTENTS_SOLID)
    {
        nummarkfaces = 0;
        for (surf = leafnode->surfaces; surf; surf = surf->next)
        {
            for (f = surf->faces; f; f = f->next)
            {
#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;
#endif
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
movq 0xc0(%rsp), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
je 0x2bd3a
movq 0xa8(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x2bc85
jmp 0x2bd22
movq 0xa8(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x2bd20
movq 0xb0(%rsp), %rax
cmpl $-0x10, 0x10(%rax)
jne 0x2bcc1
movq 0xb0(%rsp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movq 0xb0(%rsp), %rax
cmpl $0x0, 0x14(%rax)
je 0x2bcd1
jmp 0x2bd08
movq 0xb0(%rsp), %rax
movl 0x10(%rax), %edi
callq 0x2ddf0
movl %eax, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jle 0x2bd06
movl 0xa0(%rsp), %eax
movl %eax, 0xa4(%rsp)
jmp 0x2bd08
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
jmp 0x2bc99
jmp 0x2bd22
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
jmp 0x2bc62
movq 0xc0(%rsp), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
je 0x2be00
movq 0xa8(%rsp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x2bd6a
jmp 0x2bde8
movq 0xa8(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x2bdd9
movq 0xb0(%rsp), %rax
cmpl $0x0, 0x14(%rax)
je 0x2bd99
jmp 0x2bdc4
movq 0xb0(%rsp), %rax
movl 0x10(%rax), %edi
callq 0x2ddf0
movl %eax, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0xa4(%rsp), %eax
je 0x2bdc2
jmp 0x2bdd9
jmp 0x2bdc4
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
jmp 0x2bd7e
cmpq $0x0, 0xb0(%rsp)
je 0x2bde6
jmp 0x2be00
jmp 0x2bde8
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
jmp 0x2bd4a
cmpq $0x0, 0xa8(%rsp)
je 0x2c1d0
leaq 0x1b61a(%rip), %rax     # 0x47430
movl (%rax), %edi
subl $0x1, %edi
callq 0x8ce0
movq %rax, 0x98(%rsp)
leaq 0x1b601(%rip), %rax     # 0x47430
movl (%rax), %eax
subl $0x1, %eax
cmpl $0x0, %eax
je 0x2be56
leaq 0x20115c0(%rip), %rax   # 0x203d400
cmpq %rax, 0x98(%rsp)
jne 0x2be56
movq $0x0, 0x98(%rsp)
movl 0xa0(%rsp), %edi
callq 0x2ded0
movl %eax, %edi
callq 0x2aed0
movq %rax, 0x38(%rsp)
movl 0xa4(%rsp), %edi
callq 0x2ded0
movl %eax, %edi
callq 0x2aed0
movq %rax, 0x40(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x50(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x68(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x70(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
leaq 0x20beb11(%rip), %rax   # 0x20eaa10
movl (%rax), %eax
movl %eax, 0x7c(%rsp)
leaq 0x1b524(%rip), %rax     # 0x47430
movl (%rax), %eax
subl $0x1, %eax
movl %eax, 0x80(%rsp)
cmpq $0x0, 0x98(%rsp)
je 0x2bf3e
movq 0x98(%rsp), %rdi
leaq 0xa613(%rip), %rsi      # 0x36545
callq 0x7cd0
movq %rax, 0x30(%rsp)
jmp 0x2bf4c
leaq 0xf65c(%rip), %rax      # 0x3b5a1
movq %rax, 0x30(%rsp)
jmp 0x2bf4c
movq 0x30(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x98(%rsp)
je 0x2bf7c
movq 0x98(%rsp), %rdi
leaq 0xa6c2(%rip), %rsi      # 0x36632
callq 0x7cd0
movq %rax, 0x20(%rsp)
jmp 0x2bf8a
leaq 0xf61e(%rip), %rax      # 0x3b5a1
movq %rax, 0x20(%rsp)
jmp 0x2bf8a
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x98(%rsp)
je 0x2bfba
movq 0x98(%rsp), %rdi
leaq 0xa712(%rip), %rsi      # 0x366c0
callq 0x7cd0
movq %rax, 0x10(%rsp)
jmp 0x2bfc8
leaq 0xf5e0(%rip), %rax      # 0x3b5a1
movq %rax, 0x10(%rsp)
jmp 0x2bfc8
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r9
movl 0x80(%rsp), %r8d
movl 0x7c(%rsp), %ecx
movsd 0x70(%rsp), %xmm5
movsd 0x68(%rsp), %xmm4
movsd 0x60(%rsp), %xmm3
movsd 0x58(%rsp), %xmm2
movsd 0x50(%rsp), %xmm1
movsd 0x48(%rsp), %xmm0
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
movq 0x10(%rsp), %rax
leaq 0xfb4b(%rip), %rdi      # 0x3bb63
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x6, %al
callq 0xb980
movq 0xc0(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x2c1ce
movq 0x90(%rsp), %rax
movq 0x48(%rax), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
je 0x2c1b4
movq 0x88(%rsp), %rax
movl 0x10(%rax), %edx
movq 0x88(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x88(%rsp), %rax
movslq 0x8(%rax), %rsi
leaq 0x20c399d(%rip), %rax   # 0x20efa30
shlq $0x6, %rsi
addq %rsi, %rax
movsd (%rax), %xmm0
movq 0x88(%rsp), %rax
movslq 0x8(%rax), %rsi
leaq 0x20c397f(%rip), %rax   # 0x20efa30
shlq $0x6, %rsi
addq %rsi, %rax
movsd 0x8(%rax), %xmm1
movq 0x88(%rsp), %rax
movslq 0x8(%rax), %rsi
leaq 0x20c3960(%rip), %rax   # 0x20efa30
shlq $0x6, %rsi
addq %rsi, %rax
movsd 0x10(%rax), %xmm2
movl $0x5, %edi
leaq 0xfb17(%rip), %rsi      # 0x3bbff
movb $0x3, %al
callq 0xbb80
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movq 0x88(%rsp), %rcx
cmpl 0x2c(%rcx), %eax
jge 0x2c19a
movq 0x88(%rsp), %rax
addq $0x38, %rax
movslq 0x84(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd (%rax), %xmm0
movq 0x88(%rsp), %rax
addq $0x38, %rax
movslq 0x84(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm1
movq 0x88(%rsp), %rax
addq $0x38, %rax
movslq 0x84(%rsp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm2
movl $0x5, %edi
leaq 0xfaa3(%rip), %rsi      # 0x3bc20
movb $0x3, %al
callq 0xbb80
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x2c0fa
jmp 0x2c19c
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x88(%rsp)
jmp 0x2c05b
jmp 0x2c1b6
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x90(%rsp)
jmp 0x2c038
jmp 0x2c1d0
movl 0xa4(%rsp), %edi
callq 0x2ded0
movl %eax, %ecx
movq 0xb8(%rsp), %rax
movl %ecx, 0xa0(%rax)
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)