// File: FreeSlave[P]vhlt/hlbsp/brink.cpp
// Function: FixBrinks_r(bclipnode_s const*, bbrinklevel_e, int&, dclipnode_t*, dclipnode_t*, dclipnode_t*&, std::map<std::pair<int, std::pair<int, int>>, int, std::less<std::pair<int, std::pair<int, int>>>, std::allocator<std::pair<std::pair<int, std::pair<int, int>> const, int>>>*)
bool FixBrinks_r (const bclipnode_t *clipnode, bbrinklevel_e level, int &headnode_out, dclipnode_t *begin, dclipnode_t *end, dclipnode_t *&current
#ifdef HLBSP_MERGECLIPNODE
				, clipnodemap_t *outputmap
#endif
				)
{
	if (clipnode->isleaf)
	{
		return FixBrinks_r_r (clipnode, clipnode->partitions, level, headnode_out, begin, end, current
#ifdef HLBSP_MERGECLIPNODE
							, outputmap
#endif
							);
	}
	else
	{
		dclipnode_t *cn;
#ifdef HLBSP_MERGECLIPNODE
		dclipnode_t tmpclipnode;
		cn = &tmpclipnode;
		dclipnode_t *c = current;
		current++;
#else
		if (current >= end)
		{
			return false;
		}
		cn = current;
		current++;
#endif
		cn->planenum = clipnode->planenum;
		for (int k = 0; k < 2; k++)
		{
			int r;
			if (!FixBrinks_r (clipnode->children[k], level, r, begin, end, current
#ifdef HLBSP_MERGECLIPNODE
				, outputmap
#endif
				))
			{
				return false;
			}
			cn->children[k] = r;
		}
#ifdef HLBSP_MERGECLIPNODE
		clipnodemap_t::iterator output;
		output = outputmap->find (MakeKey (*cn));
		if (g_noclipnodemerge || output == outputmap->end ())
		{
			if (c >= end)
			{
				return false;
			}
			*c = *cn;
			(*outputmap)[MakeKey (*cn)] = c - begin;
			headnode_out = c - begin;
		}
		else
		{
			count_mergedclipnodes++;
			if (current != c + 1)
			{
				Error ("Merge clipnodes: internal error");
			}
			current = c;
			headnode_out = output->second; // use existing clipnode
		}
#else
		headnode_out = cn - begin;
#endif
		return true;
	}
}

// Assembly:
subq $0xe8, %rsp
movq 0xf0(%rsp), %rax
movq %rdi, 0xd8(%rsp)
movl %esi, 0xd4(%rsp)
movq %rdx, 0xc8(%rsp)
movq %rcx, 0xc0(%rsp)
movq %r8, 0xb8(%rsp)
movq %r9, 0xb0(%rsp)
movq 0xd8(%rsp), %rax
testb $0x1, (%rax)
je 0x1958a
movq 0xd8(%rsp), %rdi
movq 0xd8(%rsp), %rax
movq 0x28(%rax), %rsi
movl 0xd4(%rsp), %edx
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %r8
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r10
movq 0xf0(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x19130
andb $0x1, %al
movb %al, 0xe7(%rsp)
jmp 0x19863
leaq 0xa0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rcx
addq $0x8, %rcx
movq %rcx, (%rax)
movq 0xd8(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0xa8(%rsp), %rax
movl %ecx, (%rax)
movl $0x0, 0x94(%rsp)
cmpl $0x2, 0x94(%rsp)
jge 0x19680
movq 0xd8(%rsp), %rax
movslq 0x94(%rsp), %rcx
movq 0x10(%rax,%rcx,8), %rdi
movl 0xd4(%rsp), %esi
movq 0xc0(%rsp), %rcx
movq 0xb8(%rsp), %r8
movq 0xb0(%rsp), %r9
movq 0xf0(%rsp), %rax
leaq 0x90(%rsp), %rdx
movq %rax, (%rsp)
callq 0x194e0
testb $0x1, %al
jne 0x1964b
movb $0x0, 0xe7(%rsp)
jmp 0x19863
movl 0x90(%rsp), %eax
movw %ax, %dx
movq 0xa8(%rsp), %rax
movslq 0x94(%rsp), %rcx
movw %dx, 0x4(%rax,%rcx,2)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x195df
leaq 0x88(%rsp), %rdi
callq 0x1a620
movq 0xf0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a670
movq 0x28(%rsp), %rdi
movl %edx, 0x68(%rsp)
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x74(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x7c(%rsp)
leaq 0x74(%rsp), %rsi
callq 0x1a640
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x88(%rsp)
movb $0x1, %al
leaq 0x20d632f(%rip), %rcx   # 0x20efa21
testb $0x1, (%rcx)
movb %al, 0x37(%rsp)
jne 0x19723
movq 0xf0(%rsp), %rdi
callq 0x1a700
movq %rax, 0x58(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1a6e0
movb %al, 0x37(%rsp)
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x19730
jmp 0x197ef
movq 0x98(%rsp), %rax
cmpq 0xb8(%rsp), %rax
jb 0x1974f
movb $0x0, 0xe7(%rsp)
jmp 0x19863
movq 0xa8(%rsp), %rcx
movq 0x98(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, (%rax)
movq 0x98(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq %rcx, %rax
shrq $0x3, %rax
movl %eax, 0x24(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a670
movq 0x18(%rsp), %rdi
movl %edx, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x4c(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x54(%rsp)
leaq 0x4c(%rsp), %rsi
callq 0x1a730
movl 0x24(%rsp), %ecx
movl %ecx, (%rax)
movq 0x98(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl %eax, %ecx
movq 0xc8(%rsp), %rax
movl %ecx, (%rax)
jmp 0x1985b
leaq 0x4a523aa(%rip), %rax   # 0x4a6bba0
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x4a5239e(%rip), %rax   # 0x4a6bba0
movl %ecx, (%rax)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq 0x98(%rsp), %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
je 0x1982e
leaq 0x20763(%rip), %rdi     # 0x39f8a
movb $0x0, %al
callq 0xb510
movq 0x98(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, (%rax)
leaq 0x88(%rsp), %rdi
callq 0x1a820
movl 0xc(%rax), %ecx
movq 0xc8(%rsp), %rax
movl %ecx, (%rax)
movb $0x1, 0xe7(%rsp)
movb 0xe7(%rsp), %al
andb $0x1, %al
addq $0xe8, %rsp
retq
nopw %cs:(%rax,%rax)