// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %rbx
movq %r8, %r15
movl %edx, 0x4(%rsp)
movq %rsi, %r12
movq %rdi, %r14
movslq 0x48(%rdi), %rax
testq %rax, %rax
jle 0xdecb
movq 0x50(%r14), %rdx
movq (%rdx), %rdx
addq $0x20, %rdx
xorl %esi, %esi
xorl %ebp, %ebp
cmpq %rcx, (%rdx)
sete %r8b
andb -0x20(%rdx), %r8b
movzbl %r8b, %r8d
addl %r8d, %ebp
testb %r8b, %r8b
cmovnel %esi, %edi
incq %rsi
addq $0x28, %rdx
cmpq %rsi, %rax
jne 0xdea2
movslq %edi, %r13
jmp 0xded0
xorl %ebp, %ebp
xorl %r13d, %r13d
testl %ebp, %ebp
jne 0xdeeb
leaq 0x13678(%rip), %rdi     # 0x21553
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
cmpl $0x2, %ebp
jl 0xdf07
leaq 0x13697(%rip), %rdi     # 0x2158e
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
cmpq %rbx, %r15
jne 0xdf23
leaq 0x136bc(%rip), %rdi     # 0x215cf
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
movq 0x50(%r14), %rdi
movslq 0x48(%r14), %rsi
addq $0x2, %rsi
callq 0x119ec
movq 0x50(%r14), %rax
movq (%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
movslq 0x48(%r14), %rdx
leaq (%rdx,%rdx,4), %rdx
movq $0x0, 0x20(%rax,%rcx,8)
movb $0x0, (%rax,%rcx,8)
movq %r12, 0x8(%rax,%rcx,8)
movl 0x4(%rsp), %esi
movl %esi, 0x4(%rax,%rcx,8)
movl 0x48(%r14), %esi
movl %esi, 0x10(%rax,%rcx,8)
movl 0x48(%r14), %esi
incl %esi
movl %esi, 0x14(%rax,%rcx,8)
movb $0x1, %cl
movb %cl, (%rax,%rdx,8)
movl 0x20(%r15), %esi
movl %esi, 0x18(%rax,%rdx,8)
movq %r15, 0x20(%rax,%rdx,8)
movb %cl, 0x28(%rax,%rdx,8)
movl 0x20(%rbx), %ecx
movl %ecx, 0x40(%rax,%rdx,8)
movq %rbx, 0x48(%rax,%rdx,8)
addl $0x2, 0x48(%r14)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq