// File: /workspace/github_cpp_makefile_star2_O1/FreeSlave[P]vhlt/hlbsp/brink.cpp
// Function: AnalyzeBrinks(bbrinkinfo_s*)
void AnalyzeBrinks (bbrinkinfo_t *info)
{
	int countgood = 0;
	int countinvalid = 0;
	int countskipped = 0;
	int countfixed = 0;
	int i, j, side;
	for (i = 0; i < info->numbrinks; i++)
	{
		bbrink_t *b = info->brinks[i];
		if (b->numnodes <= 5) // quickly reject the most trivial brinks
		{
#ifdef HLBSP_BRINKNOTUSEDBYLEAF_FIX
			if (b->numnodes != 3 && b->numnodes != 5)
			{
				PrintOnce ("AnalyzeBrinks: internal error 1");
				hlassume (false, assume_first);
			}
			// because a brink won't necessarily be split twice after its creation
			if (b->numnodes == 3)
			{
				if (g_developer >= DEVELOPER_LEVEL_FLUFF)
				{
					Developer (DEVELOPER_LEVEL_FLUFF, "Brink wasn't split by the second plane:\n");
					PrintBrink (b);
				}
				countinvalid++;
			}
			else
			{
				countgood++;
			}
			continue;
#else
			hlassume (b->numnodes == 5, assume_first);
			countgood++;
			continue;
#endif
		}
		
		if (b->numnodes > 2 * MAXBRINKWEDGES - 1)
		{
			if (g_developer >= DEVELOPER_LEVEL_MEGASPAM)
			{
				Developer (DEVELOPER_LEVEL_MEGASPAM, "Skipping complicated brink:\n");
				PrintBrink (b);
			}
			countskipped++;
			continue;
		}
		bcircle_t c;
		// build the circle to find out the planes a player may move along
		if (!CalculateCircle (b, &c))
		{
			if (g_developer >= DEVELOPER_LEVEL_FLUFF)
			{
				Developer (DEVELOPER_LEVEL_FLUFF, "CalculateCircle failed for brink:\n");
				PrintBrink (b);
			}
			countinvalid++;
			continue;
		}
		
		int transitionfound[2];
		bsurface_t *transitionpos[2];
		bool transitionside[2];
		for (side = 0; side < 2; side++)
		{
			transitionfound[side] = 0;
			for (j = 1; j < c.numwedges[side]; j++) // we will later consider the surfaces on the first split
			{
				bsurface_t *s = &c.surfaces[side][j];
				if ((s->prev->content == CONTENTS_SOLID) != (s->next->content == CONTENTS_SOLID))
				{
					transitionfound[side]++;
					transitionpos[side] = s;
					transitionside[side] = (s->prev->content == CONTENTS_SOLID);
				}
			}
		}

		if (transitionfound[0] == 0 || transitionfound[1] == 0)
		{
			// at least one side of the first split is completely SOLID or EMPTY. no bugs in this case
			countgood++;
			continue;
		}

		if (transitionfound[0] > 1 || transitionfound[1] > 1 ||
			(c.surfaces[0][0].prev->content == CONTENTS_SOLID) != (c.surfaces[0][0].next->content == CONTENTS_SOLID) ||
			(c.surfaces[1][0].prev->content == CONTENTS_SOLID) != (c.surfaces[1][0].next->content == CONTENTS_SOLID))
		{
			// there must at least 3 transition surfaces now, which is too complicated. just leave it unfixed
			if (g_developer >= DEVELOPER_LEVEL_MEGASPAM)
			{
				Developer (DEVELOPER_LEVEL_MEGASPAM, "Skipping complicated brink:\n");
				PrintBrink (b);
				PrintCircle (&c);
			}
			countskipped++;
			continue;
		}
		
		if (transitionside[1] != !transitionside[0])
		{
			PrintOnce ("AnalyzeBrinks: internal error 2");
			hlassume (false, assume_first);
		}
		bool bfix = false;
		bool berror = false;
		vec3_t vup = {0, 0, 1};
		bool isfloor;
		bool onfloor;
		bool blocking;
		{
			isfloor = false;
			for (int side2 = 0; side2 < 2; side2++)
			{
				vec3_t normal;
				VectorScale (transitionpos[side2]->normal, transitionside[side2]? -1: 1, normal); // pointing from SOLID to EMPTY
				if (DotProduct (normal, vup) > BRINK_FLOOR_THRESHOLD)
				{
					isfloor = true;
				}
			}
		}
		{
			onfloor = false;
			for (int side2 = 0; side2 < 2; side2++)
			{
				btreepoint_t *tp = GetPointFromEdge (b->edge, side2);
#ifdef HLBSP_BRINKHACK_BUGFIX
				if (tp->infinite)
				{
					continue;
				}
#endif
				for (btreeedge_l::iterator ei = tp->edges->begin (); ei != tp->edges->end (); ei++)
				{
					for (btreeface_l::iterator fi = ei->e->faces->begin (); fi != ei->e->faces->end (); fi++)
					{
#ifdef HLBSP_BRINKHACK_BUGFIX
						if (fi->f->infinite || GetLeafFromFace (fi->f, false)->infinite || GetLeafFromFace (fi->f, true)->infinite)
						{
							PrintOnce ("AnalyzeBrinks: internal error: an infinite object contains a finite object");
							hlassume (false, assume_first);
						}
#endif
						for (int side3 = 0; side3 < 2; side3++)
						{
							vec3_t normal;
							VectorScale (fi->f->plane->normal, (fi->f->planeside != (bool)side3)? -1: 1, normal);
							if (DotProduct (normal, vup) > BRINK_FLOOR_THRESHOLD
								&& GetLeafFromFace (fi->f, side3)->clipnode->content == CONTENTS_SOLID
								&& GetLeafFromFace (fi->f, !side3)->clipnode->content != CONTENTS_SOLID)
							{
								onfloor = true;
							}
						}
					}
				}
			}
		}
		// this code does not fix all the bugs, it only aims to fix most of the bugs
		for (side = 0; side < 2; side++)
		{
			bsurface_t *smovement = transitionpos[side];
			bsurface_t *s;
			for (s = transitionside[!side]? &c.surfaces[!side][0]: &c.surfaces[side][0]; ; s = transitionside[!side]? s->next->next: s->prev->prev)
			{
				bwedge_t *w = transitionside[!side]? s->next: s->prev;
				bsurface_t *snext = transitionside[!side]? w->next: w->prev;
				vec3_t tmp;
				vec_t dot;
				CrossProduct (smovement->normal, snext->normal, tmp);
				dot = DotProduct (tmp, c.axis);
				if (transitionside[!side]? dot < 0.01: dot > -0.01)
				{
					break;
				}
				if (w->content != CONTENTS_SOLID)
				{
					break;
				}
				if (snext == (transitionside[!side]? &c.surfaces[side][0]: &c.surfaces[!side][0]))
				{
					Developer (DEVELOPER_LEVEL_ERROR, "AnalyzeBrinks: surface past 0\n");
					break;
				}
				bfix = true;
				{
					if (DotProduct (smovement->normal, s->normal) > 0.01)
					{
						blocking = false;
					}
					else
					{
						blocking = true;
					}
				}
				bclipnode_t *clipnode = (*b->nodes)[w->nodenum].clipnode;
				int planenum = (*b->nodes)[smovement->nodenum].planenum;
				bool planeside = transitionside[!side]? smovement->nodeside: !smovement->nodeside;
				bbrinklevel_e brinktype;
				brinktype = isfloor? (blocking? BrinkFloorBlocking: BrinkFloor): onfloor? (blocking? BrinkWallBlocking: BrinkWall): BrinkAny;
				if (!AddPartition (clipnode, planenum, planeside, CONTENTS_EMPTY, brinktype))
				{
					berror = true;
				}
			}
		}
		if (berror)
		{
			if (g_developer >= DEVELOPER_LEVEL_FLUFF)
			{
				Developer (DEVELOPER_LEVEL_FLUFF, "AddPartition failed for brink:\n");
				PrintBrink (b);
			}
			countinvalid++;
		}
		else if (!bfix)
		{
			countgood++;
		}
		else
		{
			countfixed++;
		}
	}
	Developer (DEVELOPER_LEVEL_MESSAGE, "brinks: good = %d skipped = %d fixed = %d invalid = %d\n", countgood, countskipped, countfixed, countinvalid);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x24b8, %rsp           # imm = 0x24B8
cmpl $0x0, 0x20(%rdi)
jle 0x11202
movq %rdi, %rbx
leaq 0x20b18ee(%rip), %r12   # 0x20c2410
leaq 0x110d7(%rip), %r13     # 0x21c00
leaq 0x110b0(%rip), %rbp     # 0x21be0
movl $0x1, %r15d
xorl %r14d, %r14d
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rax
movq %rax, 0x10(%rsp)
movl 0x48(%rax), %eax
cmpl $0x5, %eax
jg 0x10bc3
cmpl $0x3, %eax
je 0x10b8b
cmpl $0x5, %eax
je 0x10b8b
movq %rbp, %rdi
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
movq 0x10(%rsp), %rax
cmpl $0x3, 0x48(%rax)
jne 0x111ec
cmpl $0x4, (%r12)
jl 0x10bba
movl $0x4, %edi
movq %r13, %rsi
xorl %eax, %eax
callq 0xa496
movq 0x10(%rsp), %rdi
callq 0xdd81
incl 0xc(%rsp)
jmp 0x111f0
cmpl $0x80, %eax
jl 0x10bf7
cmpl $0x6, (%r12)
jl 0x10bee
movl $0x6, %edi
leaq 0x1104c(%rip), %rsi     # 0x21c29
xorl %eax, %eax
callq 0xa496
movq 0x10(%rsp), %rdi
callq 0xdd81
incl 0x1c(%rsp)
jmp 0x111f0
movq %r14, 0x28(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x10145
testb %al, %al
je 0x10cab
leaq 0xce8(%rsp), %rax
xorl %ecx, %ecx
movl $0x0, 0x40(%rsp,%rcx,4)
movslq 0xb0(%rsp,%rcx,4), %rdx
cmpq $0x2, %rdx
jl 0x10c74
decq %rdx
movq %rax, %rsi
movq 0x20(%rsi), %rdi
movq 0x28(%rsi), %r8
movl (%rdi), %edi
cmpl $-0x2, %edi
sete %r9b
cmpl $-0x2, (%r8)
setne %r8b
xorb %r9b, %r8b
jne 0x10c6b
incl 0x40(%rsp,%rcx,4)
cmpl $-0x2, %edi
movq %rsi, 0x70(%rsp,%rcx,8)
sete 0x6(%rsp,%rcx)
addq $0x30, %rsi
decq %rdx
jne 0x10c3c
leaq 0x1(%rcx), %rdx
addq $0xc00, %rax            # imm = 0xC00
testq %rcx, %rcx
movq %rdx, %rcx
je 0x10c20
movl 0x40(%rsp), %ecx
movl 0x44(%rsp), %eax
testl %ecx, %ecx
sete %dl
testl %eax, %eax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x10cd8
incl 0x8(%rsp)
jmp 0x111b9
cmpl $0x4, (%r12)
jl 0x10ccf
movl $0x4, %edi
leaq 0x10f88(%rip), %rsi     # 0x21c46
xorl %eax, %eax
callq 0xa496
movq 0x10(%rsp), %rdi
callq 0xdd81
incl 0xc(%rsp)
jmp 0x111b9
cmpl $0x2, %ecx
setge %cl
cmpl $0x2, %eax
setge %al
orb %cl, %al
jne 0x11184
movq 0xcd8(%rsp), %rax
movq 0xce0(%rsp), %rcx
cmpl $-0x2, (%rax)
sete %al
cmpl $-0x2, (%rcx)
setne %cl
cmpb %cl, %al
je 0x11184
movq 0x18d8(%rsp), %rax
movq 0x18e0(%rsp), %rcx
cmpl $-0x2, (%rax)
sete %al
cmpl $-0x2, (%rcx)
setne %cl
xorb %al, %cl
je 0x11184
movb 0x6(%rsp), %al
cmpb 0x7(%rsp), %al
jne 0x10d55
leaq 0x10f24(%rip), %rdi     # 0x21c69
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
xorl %eax, %eax
movq $0x0, 0x38(%rsp)
movq 0x70(%rsp,%rax,8), %rcx
cmpb $0x0, 0x6(%rsp,%rax)
movsd 0x1051e(%rip), %xmm0    # 0x21290
jne 0x10d7c
movsd 0x1035c(%rip), %xmm0    # 0x210d8
movsd (%rcx), %xmm1
mulsd %xmm0, %xmm1
movsd 0x8(%rcx), %xmm2
mulsd %xmm0, %xmm2
mulsd 0x10(%rcx), %xmm0
xorpd %xmm3, %xmm3
mulsd %xmm3, %xmm2
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
addsd %xmm0, %xmm1
ucomisd 0x10702(%rip), %xmm1    # 0x214b0
movl $0x1, %ecx
ja 0x10dba
movzbl 0x38(%rsp), %ecx
movq %rcx, 0x38(%rsp)
incq %rax
cmpq $0x1, %rax
je 0x10d60
movq %rbx, 0x48(%rsp)
xorl %ebx, %ebx
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rdi
xorl %esi, %esi
movq %rcx, 0x30(%rsp)
testl %ecx, %ecx
setne %sil
callq 0xe974
cmpb $0x0, 0x18(%rax)
jne 0x10f1f
movq %rax, %r12
movq 0x20(%rax), %rax
movq (%rax), %r13
cmpq %rax, %r13
je 0x10f1f
movq 0x10(%r13), %rax
movq 0x28(%rax), %rax
movq (%rax), %rbp
cmpq %rax, %rbp
je 0x10f10
movq 0x10(%rbp), %rdi
cmpb $0x0, 0x8(%rdi)
jne 0x10e48
xorl %esi, %esi
callq 0xebf1
cmpb $0x0, 0x8(%rax)
jne 0x10e48
movq 0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xebf1
cmpb $0x1, 0x8(%rax)
jne 0x10e5f
leaq 0x10e3a(%rip), %rdi     # 0x21c89
xorl %eax, %eax
callq 0xa1f4
xorl %edi, %edi
xorl %esi, %esi
callq 0xa731
xorl %r14d, %r14d
movq 0x10(%rbp), %rdi
movq 0x30(%rdi), %rax
movzbl 0x3c(%rdi), %ecx
xorl %edx, %edx
cmpl %ecx, %r14d
sete %dl
leal -0x1(,%rdx,2), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd (%rax), %xmm1
mulsd %xmm0, %xmm1
movsd 0x8(%rax), %xmm2
mulsd %xmm0, %xmm2
mulsd 0x10(%rax), %xmm0
xorpd %xmm3, %xmm3
mulsd %xmm3, %xmm2
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
addsd %xmm0, %xmm1
ucomisd 0x105fa(%rip), %xmm1    # 0x214b0
jbe 0x10ef1
xorl %esi, %esi
testl %r14d, %r14d
setne %sil
callq 0xebf1
movq 0x10(%rax), %rax
cmpl $-0x2, 0x20(%rax)
jne 0x10ef1
xorl %esi, %esi
testl %r14d, %r14d
sete %sil
movq 0x10(%rbp), %rdi
callq 0xebf1
movq 0x10(%rax), %rax
cmpl $-0x2, 0x20(%rax)
movzbl %bl, %ebx
cmovnel %r15d, %ebx
incl %r14d
cmpl $0x1, %r14d
je 0x10e62
movq (%rbp), %rbp
movq 0x10(%r13), %rax
cmpq 0x28(%rax), %rbp
jne 0x10e1d
movq (%r13), %r13
cmpq 0x20(%r12), %r13
jne 0x10e09
movq 0x30(%rsp), %rcx
leal 0x1(%rcx), %eax
testl %ecx, %ecx
movl %eax, %ecx
je 0x10dd1
xorl %edx, %edx
movq $0x0, 0x50(%rsp)
movl $0x0, 0x24(%rsp)
movq 0x70(%rsp,%rdx,8), %r12
movl %edx, %eax
xorq $0x1, %rax
movzbl 0x6(%rsp,%rax), %esi
testl %esi, %esi
movq %rdx, %rcx
cmovneq %rax, %rcx
leaq (%rcx,%rcx,2), %r14
movq %rdx, 0x60(%rsp)
cmovneq %rdx, %rax
shlq $0xa, %r14
leaq 0xcb8(%rsp), %rcx
addq %rcx, %r14
movq %rsi, 0x30(%rsp)
leaq 0x8(,%rsi,8), %rdx
leaq (%rax,%rax,2), %rax
shlq $0xa, %rax
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movq %rdx, 0x58(%rsp)
movzbl 0x30(%rsp), %ebp
movq 0x20(%r14,%rbp,8), %rax
movq (%rax,%rdx), %rcx
movsd (%r12), %xmm1
movsd 0x8(%r12), %xmm2
movsd 0x10(%rcx), %xmm4
movsd 0x10(%r12), %xmm0
movsd (%rcx), %xmm5
movsd 0x8(%rcx), %xmm3
movapd %xmm3, %xmm6
mulsd %xmm0, %xmm6
movapd %xmm2, %xmm7
mulsd %xmm4, %xmm7
subsd %xmm6, %xmm7
mulsd %xmm1, %xmm4
movapd %xmm0, %xmm6
mulsd %xmm5, %xmm6
subsd %xmm4, %xmm6
mulsd %xmm2, %xmm5
mulsd %xmm1, %xmm3
mulsd 0x88(%rsp), %xmm6
subsd %xmm5, %xmm3
mulsd 0x80(%rsp), %xmm7
addsd %xmm6, %xmm7
mulsd 0x90(%rsp), %xmm3
addsd %xmm7, %xmm3
testb %bpl, %bpl
je 0x11033
movsd 0x10495(%rip), %xmm4    # 0x214c0
ucomisd %xmm3, %xmm4
jbe 0x1103d
jmp 0x11057
ucomisd 0x1047d(%rip), %xmm3    # 0x214b8
ja 0x11057
cmpl $-0x2, (%rax)
jne 0x11057
cmpq 0x68(%rsp), %rcx
je 0x1105f
cmpb $0x0, 0x30(%rsp)
je 0x1107a
movb 0x1c(%r12), %cl
jmp 0x11082
xorl %r13d, %r13d
jmp 0x11122
xorl %r13d, %r13d
movl $0x1, %edi
leaq 0x10c66(%rip), %rsi     # 0x21cd4
xorl %eax, %eax
callq 0xa496
jmp 0x1111d
movb 0x1c(%r12), %cl
xorb $0x1, %cl
mulsd 0x8(%r14), %xmm2
mulsd (%r14), %xmm1
addsd %xmm2, %xmm1
mulsd 0x10(%r14), %xmm0
addsd %xmm1, %xmm0
movq 0x10(%rsp), %rdx
movq 0x50(%rdx), %rdx
movslq 0x4(%rax), %rax
movq (%rdx), %rdx
leaq (%rax,%rax,4), %rax
xorl %esi, %esi
ucomisd 0x10407(%rip), %xmm0    # 0x214c0
movq 0x20(%rdx,%rax,8), %rdi
setbe %sil
movl $0x2, %eax
subl %esi, %eax
movl $0x4, %r8d
subl %esi, %r8d
movslq 0x18(%r12), %rsi
leaq (%rsi,%rsi,4), %rsi
testb $0x1, %bl
movl $0x5, %r9d
cmovel %r9d, %r8d
movl 0x4(%rdx,%rsi,8), %esi
testb $0x1, 0x38(%rsp)
cmovnel %eax, %r8d
movzbl %cl, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x10970
movl 0x24(%rsp), %ecx
movzbl %cl, %ecx
testb %al, %al
cmovel %r15d, %ecx
movl %ecx, 0x24(%rsp)
movb $0x1, %al
movq %rax, 0x50(%rsp)
movb $0x1, %r13b
movq 0x58(%rsp), %rdx
testb %r13b, %r13b
je 0x11135
movq 0x20(%r14,%rbp,8), %rax
movq (%rax,%rdx), %r14
jmp 0x10f9b
movq 0x60(%rsp), %rcx
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
je 0x10f44
testb $0x1, 0x24(%rsp)
je 0x111c0
leaq 0x20b12bb(%rip), %r12   # 0x20c2410
cmpl $0x4, (%r12)
movq 0x48(%rsp), %rbx
leaq 0x10a9a(%rip), %r13     # 0x21c00
leaq 0x10a73(%rip), %rbp     # 0x21be0
jl 0x10ccf
movl $0x4, %edi
leaq 0x10b74(%rip), %rsi     # 0x21cf3
jmp 0x10cbe
cmpl $0x6, (%r12)
jl 0x111b5
movl $0x6, %edi
leaq 0x10a92(%rip), %rsi     # 0x21c29
xorl %eax, %eax
callq 0xa496
movq 0x10(%rsp), %rdi
callq 0xdd81
leaq 0x80(%rsp), %rdi
callq 0x1086d
incl 0x1c(%rsp)
movq 0x28(%rsp), %r14
jmp 0x111f0
testb $0x1, 0x50(%rsp)
movq 0x48(%rsp), %rbx
leaq 0x20b123f(%rip), %r12   # 0x20c2410
leaq 0x10a28(%rip), %r13     # 0x21c00
leaq 0x10a01(%rip), %rbp     # 0x21be0
movq 0x28(%rsp), %r14
je 0x111ec
incl 0x20(%rsp)
jmp 0x111f0
incl 0x8(%rsp)
incq %r14
movslq 0x20(%rbx), %rax
cmpq %rax, %r14
jl 0x10b59
jmp 0x11222
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
leaq 0x10aea(%rip), %rsi     # 0x21d13
movl $0x3, %edi
movl 0x8(%rsp), %edx
movl 0x1c(%rsp), %ecx
movl 0x20(%rsp), %r8d
movl 0xc(%rsp), %r9d
xorl %eax, %eax
callq 0xa496
addq $0x24b8, %rsp           # imm = 0x24B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq