// File: FreeSlave[P]vhlt/hlbsp/brink.cpp
// Function: FixBrinks_r_r(bclipnode_s const*, bpartition_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_r (const bclipnode_t *clipnode, const bpartition_t *p, bbrinklevel_e level, int &headnode_out, dclipnode_t *begin, dclipnode_t *end, dclipnode_t *&current
#ifdef HLBSP_MERGECLIPNODE
					, clipnodemap_t *outputmap
#endif
					)
{
	while (p && p->type > level)
	{
		p = p->next;
	}
	if (p == NULL)
	{
		headnode_out = clipnode->content;
		return true;
	}
	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 = p->planenum;
	cn->children[p->planeside] = p->content;
	int r;
	if (!FixBrinks_r_r (clipnode, p->next, level, r, begin, end, current
#ifdef HLBSP_MERGECLIPNODE
		, outputmap
#endif
		))
	{
		return false;
	}
	cn->children[!p->planeside] = 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 the existing clipnode
	}
#else
	headnode_out = cn - begin;
#endif
	return true;
}

// Assembly:
subq $0xe8, %rsp
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rax
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movl %edx, 0xcc(%rsp)
movq %rcx, 0xc0(%rsp)
movq %r8, 0xb8(%rsp)
movq %r9, 0xb0(%rsp)
xorl %eax, %eax
cmpq $0x0, 0xd0(%rsp)
movb %al, 0x37(%rsp)
je 0x191a0
movq 0xd0(%rsp), %rax
movl 0xc(%rax), %eax
cmpl 0xcc(%rsp), %eax
setg %al
movb %al, 0x37(%rsp)
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x191aa
jmp 0x191c0
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xd0(%rsp)
jmp 0x19176
cmpq $0x0, 0xd0(%rsp)
jne 0x191ed
movq 0xd8(%rsp), %rax
movl 0x20(%rax), %ecx
movq 0xc0(%rsp), %rax
movl %ecx, (%rax)
movb $0x1, 0xe7(%rsp)
jmp 0x194cf
leaq 0xa0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rcx
addq $0x8, %rcx
movq %rcx, (%rax)
movq 0xd0(%rsp), %rax
movl (%rax), %ecx
movq 0xa8(%rsp), %rax
movl %ecx, (%rax)
movq 0xd0(%rsp), %rax
movl 0x8(%rax), %eax
movw %ax, %dx
movq 0xa8(%rsp), %rax
movq 0xd0(%rsp), %rcx
movb 0x4(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
movw %dx, 0x4(%rax,%rcx,2)
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rsi
movl 0xcc(%rsp), %edx
movq 0xb8(%rsp), %r8
movq 0xb0(%rsp), %r9
movq 0xf0(%rsp), %r10
movq 0xf8(%rsp), %rax
leaq 0x94(%rsp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x19130
testb $0x1, %al
jne 0x192c4
movb $0x0, 0xe7(%rsp)
jmp 0x194cf
movw 0x94(%rsp), %dx
movq 0xa8(%rsp), %rax
movq 0xd0(%rsp), %rcx
movb 0x4(%rcx), %cl
notb %cl
movzbl %cl, %ecx
andl $0x1, %ecx
movw %dx, 0x4(%rax,%rcx,2)
leaq 0x88(%rsp), %rdi
callq 0x1a620
movq 0xf8(%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 0x20d66c3(%rip), %rcx   # 0x20efa21
testb $0x1, (%rcx)
movb %al, 0x36(%rsp)
jne 0x1938f
movq 0xf8(%rsp), %rdi
callq 0x1a700
movq %rax, 0x58(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1a6e0
movb %al, 0x36(%rsp)
movb 0x36(%rsp), %al
testb $0x1, %al
jne 0x1939c
jmp 0x1945b
movq 0x98(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jb 0x193bb
movb $0x0, 0xe7(%rsp)
jmp 0x194cf
movq 0xa8(%rsp), %rcx
movq 0x98(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, (%rax)
movq 0x98(%rsp), %rax
movq 0xb8(%rsp), %rcx
subq %rcx, %rax
shrq $0x3, %rax
movl %eax, 0x24(%rsp)
movq 0xf8(%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 0xb8(%rsp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl %eax, %ecx
movq 0xc0(%rsp), %rax
movl %ecx, (%rax)
jmp 0x194c7
leaq 0x4a5273e(%rip), %rax   # 0x4a6bba0
movl (%rax), %ecx
addl $0x1, %ecx
leaq 0x4a52732(%rip), %rax   # 0x4a6bba0
movl %ecx, (%rax)
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq 0x98(%rsp), %rcx
addq $0x8, %rcx
cmpq %rcx, %rax
je 0x1949a
leaq 0x20af7(%rip), %rdi     # 0x39f8a
movb $0x0, %al
callq 0xb510
movq 0x98(%rsp), %rcx
movq 0xf0(%rsp), %rax
movq %rcx, (%rax)
leaq 0x88(%rsp), %rdi
callq 0x1a820
movl 0xc(%rax), %ecx
movq 0xc0(%rsp), %rax
movl %ecx, (%rax)
movb $0x1, 0xe7(%rsp)
movb 0xe7(%rsp), %al
andb $0x1, %al
addq $0xe8, %rsp
retq