// File: FreeSlave[P]vhlt/hlbsp/brink.cpp
// Function: BrinkSplitClipnode(bbrink_t*, plane_t const*, int, bclipnode_s*, bclipnode_s*, bclipnode_s*)
void BrinkSplitClipnode (bbrink_t *b, const dplane_t *plane, int planenum, bclipnode_t *prev, bclipnode_t *n0, bclipnode_t *n1)
{
	int found;
	int numfound = 0;
	for (int i = 0; i < b->numnodes; i++)
	{
		bbrinknode_t *node = &(*b->nodes)[i];
		if (node->isleaf && node->clipnode == prev)
		{
			found = i;
			numfound++;
		}
	}
	if (numfound == 0)
	{
		PrintOnce ("BrinkSplitClipnode: internal error: couldn't find clipnode");
		hlassume (false, assume_first);
	}
	if (numfound > 1)
	{
		PrintOnce ("BrinkSplitClipnode: internal error: found more than one clipnode");
		hlassume (false, assume_first);
	}
	if (n0 == n1)
	{
		PrintOnce ("BrinkSplitClipnode: internal error: n0 == n1");
		hlassume (false, assume_first);
	}
	b->nodes->resize (b->numnodes + 2);
	bbrinknode_t *node = &(*b->nodes)[found];
	bbrinknode_t *front = &(*b->nodes)[b->numnodes];
	bbrinknode_t *back = &(*b->nodes)[b->numnodes + 1];
	
	node->clipnode = NULL;
	node->isleaf = false;
	node->plane = plane;
	node->planenum = planenum;
	node->children[0] = b->numnodes;
	node->children[1] = b->numnodes + 1;
	
	front->isleaf = true;
	front->content = n0->content;
	front->clipnode = n0;

	back->isleaf = true;
	back->content = n1->content;
	back->clipnode = n1;

	b->numnodes += 2;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movq %rcx, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x60(%rsp), %rcx
cmpl 0x48(%rcx), %eax
jge 0x123f3
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x19c00
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
testb $0x1, (%rax)
je 0x123e4
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
cmpq 0x48(%rsp), %rax
jne 0x123e4
movl 0x2c(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x123e6
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x12391
cmpl $0x0, 0x30(%rsp)
jne 0x12411
leaq 0x272ee(%rip), %rdi     # 0x396ef
movb $0x0, %al
callq 0xb840
xorl %esi, %esi
movl %esi, %edi
callq 0xbe90
cmpl $0x1, 0x30(%rsp)
jle 0x1242f
leaq 0x2730b(%rip), %rdi     # 0x3972a
movb $0x0, %al
callq 0xb840
xorl %esi, %esi
movl %esi, %edi
callq 0xbe90
movq 0x40(%rsp), %rax
cmpq 0x38(%rsp), %rax
jne 0x12452
leaq 0x27329(%rip), %rdi     # 0x3976b
movb $0x0, %al
callq 0xb840
xorl %esi, %esi
movl %esi, %edi
callq 0xbe90
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x60(%rsp), %rax
movl 0x48(%rax), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x19c20
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movslq 0x34(%rsp), %rsi
callq 0x19c00
movq %rax, 0x18(%rsp)
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x60(%rsp), %rax
movslq 0x48(%rax), %rsi
callq 0x19c00
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rdi
movq 0x60(%rsp), %rax
movl 0x48(%rax), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x19c00
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rax
movq $0x0, 0x20(%rax)
movq 0x18(%rsp), %rax
movb $0x0, (%rax)
movq 0x58(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x8(%rax)
movl 0x54(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x60(%rsp), %rax
movl 0x48(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x60(%rsp), %rax
movl 0x48(%rax), %ecx
addl $0x1, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x10(%rsp), %rax
movb $0x1, (%rax)
movq 0x40(%rsp), %rax
movl 0x20(%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x40(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x8(%rsp), %rax
movb $0x1, (%rax)
movq 0x38(%rsp), %rax
movl 0x20(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x60(%rsp), %rax
movl 0x48(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x48(%rax)
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)