// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %rbx
leaq 0xce8(%rsp), %r15
leaq 0x20b1833(%rip), %r12   # 0x20c0410
leaq 0xff74(%rip), %r13      # 0x1eb58
leaq 0xff4d(%rip), %rbp      # 0x1eb38
leaq 0xffef(%rip), %r14      # 0x1ebe1
xorl %edx, %edx
movl $0x0, 0x3c(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movslq 0x20(%rbx), %rax
cmpq %rax, %rdx
jge 0xf2ae
movq 0x28(%rbx), %rax
movq (%rax,%rdx,8), %rax
movq %rax, 0x18(%rsp)
movl 0x48(%rax), %eax
cmpl $0x5, %eax
movq %rdx, 0x10(%rsp)
jg 0xec9a
cmpl $0x3, %eax
je 0xec65
cmpl $0x5, %eax
je 0xec65
movq %rbp, %rdi
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movl 0x48(%rax), %eax
cmpl $0x3, %eax
jne 0xf2a2
cmpl $0x4, (%r12)
jl 0xec91
pushq $0x4
popq %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x9035
movq 0x18(%rsp), %rdi
callq 0xc2ac
movq 0x10(%rsp), %rdx
incl 0x28(%rsp)
jmp 0xf2a6
cmpl $0x80, %eax
jb 0xecd1
cmpl $0x6, (%r12)
jl 0xecc8
pushq $0x6
popq %rdi
leaq 0xfecf(%rip), %rsi      # 0x1eb81
xorl %eax, %eax
callq 0x9035
movq 0x18(%rsp), %rdi
callq 0xc2ac
movq 0x10(%rsp), %rdx
incl 0x2c(%rsp)
jmp 0xf2a6
movq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xe37c
testb %al, %al
je 0xed59
movq %r15, %rax
xorl %ecx, %ecx
cmpq $0x2, %rcx
je 0xed81
andl $0x0, 0x48(%rsp,%rcx,4)
movslq 0xb0(%rsp,%rcx,4), %rdx
xorl %esi, %esi
movq %rax, %rdi
pushq $0x1
popq %r8
cmpq %rdx, %r8
jge 0xed4e
movq 0x20(%rdi), %r9
movq 0x28(%rdi), %r10
movl (%r9), %r9d
cmpl $-0x2, %r9d
sete %r11b
cmpl $-0x2, (%r10)
setne %r10b
xorb %r11b, %r10b
jne 0xed45
incl %esi
cmpl $-0x2, %r9d
movl %esi, 0x48(%rsp,%rcx,4)
movq %rdi, 0x70(%rsp,%rcx,8)
sete 0xe(%rsp,%rcx)
incq %r8
addq $0x30, %rdi
jmp 0xed0c
incq %rcx
addq $0xc00, %rax            # imm = 0xC00
jmp 0xecec
cmpl $0x4, (%r12)
jl 0xed7b
pushq $0x4
popq %rdi
leaq 0xfe34(%rip), %rsi      # 0x1eb9e
xorl %eax, %eax
callq 0x9035
movq 0x18(%rsp), %rdi
callq 0xc2ac
incl 0x28(%rsp)
jmp 0xeda1
movl 0x48(%rsp), %ecx
movl 0x4c(%rsp), %eax
testl %ecx, %ecx
sete %dl
testl %eax, %eax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0xedab
incl 0x24(%rsp)
movq 0x10(%rsp), %rdx
jmp 0xf2a6
cmpl $0x2, %ecx
setge %cl
cmpl $0x2, %eax
setge %al
orb %cl, %al
movq 0x10(%rsp), %rdx
jne 0xeea4
movq 0xcd8(%rsp), %rax
movq 0xce0(%rsp), %rcx
cmpl $-0x2, (%rax)
sete %al
cmpl $-0x2, (%rcx)
setne %cl
cmpb %cl, %al
je 0xeea4
movq 0x18d8(%rsp), %rax
movq 0x18e0(%rsp), %rcx
cmpl $-0x2, (%rax)
sete %al
cmpl $-0x2, (%rcx)
setne %cl
xorb %al, %cl
je 0xeea4
movb 0xe(%rsp), %al
cmpb 0xf(%rsp), %al
jne 0xee2d
leaq 0xfda4(%rip), %rdi      # 0x1ebc1
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
xorl %eax, %eax
movq $0x0, 0x40(%rsp)
cmpq $0x2, %rax
je 0xeedc
cmpb $0x0, 0xe(%rsp,%rax)
movsd 0xf379(%rip), %xmm0     # 0x1e1c8
jne 0xee59
movsd 0xf1cf(%rip), %xmm0     # 0x1e028
movq 0x70(%rsp,%rax,8), %rcx
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 0xf578(%rip), %xmm1     # 0x1e408
seta %cl
movq 0x40(%rsp), %rdx
orb %cl, %dl
movq %rdx, 0x40(%rsp)
incq %rax
jmp 0xee38
cmpl $0x6, (%r12)
jl 0xecc8
pushq $0x6
popq %rdi
leaq 0xfcc8(%rip), %rsi      # 0x1eb81
xorl %eax, %eax
callq 0x9035
movq 0x18(%rsp), %rdi
callq 0xc2ac
leaq 0x80(%rsp), %rdi
callq 0xe93b
jmp 0xecc3
movq %rbx, 0x50(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpl $0x2, %ebx
je 0xf03d
movq 0x18(%rsp), %rax
movq 0x58(%rax), %rdi
xorl %esi, %esi
testl %ebx, %ebx
setne %sil
callq 0xcd13
cmpb $0x0, 0x18(%rax)
jne 0xf036
movq %rax, %r15
movl %ebx, 0x30(%rsp)
movq 0x20(%rax), %rax
movq %rax, %rbx
movq (%rbx), %rbx
cmpq %rax, %rbx
je 0xf032
movq 0x10(%rbx), %rax
movq 0x28(%rax), %rax
movq %rax, %r13
movq (%r13), %r13
cmpq %rax, %r13
je 0xf029
movq 0x10(%r13), %rdi
cmpb $0x0, 0x8(%rdi)
jne 0xef69
xorl %esi, %esi
callq 0xcf42
cmpb $0x0, 0x8(%rax)
jne 0xef69
movq 0x10(%r13), %rdi
pushq $0x1
popq %rsi
callq 0xcf42
cmpb $0x1, 0x8(%rax)
jne 0xef7c
movq %r14, %rdi
xorl %eax, %eax
callq 0x8d93
xorl %edi, %edi
xorl %esi, %esi
callq 0x92bb
xorl %r12d, %r12d
cmpl $0x2, %r12d
je 0xf01c
movq 0x10(%r13), %rdi
movq 0x30(%rdi), %rax
movzbl 0x3c(%rdi), %ecx
xorl %edx, %edx
cmpl %ecx, %r12d
sete %dl
leal -0x1(,%rdx,2), %ecx
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 0xf42e(%rip), %xmm1     # 0x1e408
jbe 0xf014
xorl %esi, %esi
testl %r12d, %r12d
setne %sil
callq 0xcf42
movq 0x10(%rax), %rax
cmpl $-0x2, 0x20(%rax)
jne 0xf014
xorl %esi, %esi
testl %r12d, %r12d
sete %sil
movq 0x10(%r13), %rdi
callq 0xcf42
movq 0x10(%rax), %rax
cmpl $-0x2, 0x20(%rax)
setne %al
orb %al, %bpl
incl %r12d
jmp 0xef7f
movq 0x10(%rbx), %rax
movq 0x28(%rax), %rax
jmp 0xef33
movq 0x20(%r15), %rax
jmp 0xef1c
movl 0x30(%rsp), %ebx
incl %ebx
jmp 0xeee5
xorl %esi, %esi
movq $0x0, 0x30(%rsp)
xorl %r8d, %r8d
movq 0x10(%rsp), %rdx
cmpq $0x2, %rsi
je 0xf231
movl %esi, %eax
xorq $0x1, %rax
movzbl 0xe(%rsp,%rax), %edi
testl %edi, %edi
movq %rsi, %rcx
cmovneq %rax, %rcx
movq 0x70(%rsp,%rsi,8), %r15
movq %rsi, 0x58(%rsp)
cmovneq %rsi, %rax
imulq $0xc00, %rcx, %r12      # imm = 0xC00
leaq 0xcb8(%rsp), %rcx
addq %rcx, %r12
movq %rdi, 0x68(%rsp)
leaq 0x8(,%rdi,8), %r13
imulq $0xc00, %rax, %rax      # imm = 0xC00
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movzbl 0x68(%rsp), %ebx
movq 0x20(%r12,%rbx,8), %rax
movq (%rax,%r13), %rcx
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm2
movsd 0x10(%rcx), %xmm4
movsd 0x10(%r15), %xmm1
movsd (%rcx), %xmm5
movsd 0x8(%rcx), %xmm3
movapd %xmm3, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm2, %xmm7
mulsd %xmm4, %xmm7
subsd %xmm6, %xmm7
mulsd %xmm0, %xmm4
movapd %xmm1, %xmm6
mulsd %xmm5, %xmm6
subsd %xmm4, %xmm6
mulsd %xmm2, %xmm5
mulsd %xmm0, %xmm3
mulsd 0x88(%rsp), %xmm6
subsd %xmm5, %xmm3
mulsd 0x80(%rsp), %xmm7
addsd %xmm6, %xmm7
mulsd 0x90(%rsp), %xmm3
addsd %xmm7, %xmm3
testb %bl, %bl
je 0xf141
movsd 0xf2e2(%rip), %xmm4     # 0x1e418
ucomisd %xmm3, %xmm4
jbe 0xf14f
jmp 0xf224
ucomisd 0xf2c7(%rip), %xmm3     # 0x1e410
ja 0xf224
cmpl $-0x2, (%rax)
jne 0xf224
cmpq 0x60(%rsp), %rcx
je 0xf208
mulsd 0x8(%r12), %xmm2
mulsd (%r12), %xmm0
addsd %xmm2, %xmm0
mulsd 0x10(%r12), %xmm1
addsd %xmm0, %xmm1
xorl %ecx, %ecx
ucomisd 0xf28f(%rip), %xmm1     # 0x1e418
setbe %cl
movslq 0x4(%rax), %rax
imulq $0x28, %rax, %rax
movslq 0x18(%r15), %rdx
imulq $0x28, %rdx, %rsi
xorl %edx, %edx
cmpb 0x1c(%r15), %bl
sete %dl
pushq $0x2
popq %r9
subl %ecx, %r9d
pushq $0x4
popq %r8
subl %ecx, %r8d
movq 0x18(%rsp), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx), %rcx
movq 0x20(%rcx,%rax), %rdi
testb $0x1, %bpl
pushq $0x5
popq %rax
cmovel %eax, %r8d
movl 0x4(%rcx,%rsi), %esi
testb $0x1, 0x40(%rsp)
cmovnel %r9d, %r8d
pushq $-0x1
popq %rcx
callq 0xea35
xorb $0x1, %al
movq 0x30(%rsp), %rcx
orb %al, %cl
movq %rcx, 0x30(%rsp)
movq 0x20(%r12,%rbx,8), %rax
movq (%rax,%r13), %r12
movb $0x1, %r8b
movq 0x10(%rsp), %rdx
jmp 0xf0aa
pushq $0x1
popq %rdi
leaq 0xfa1a(%rip), %rsi      # 0x1ec2c
xorl %eax, %eax
movq %r8, %rbx
callq 0x9035
movq %rbx, %r8
movq 0x10(%rsp), %rdx
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0xf050
testb $0x1, 0x30(%rsp)
je 0xf274
leaq 0x20b11d1(%rip), %r12   # 0x20c0410
cmpl $0x4, (%r12)
movq 0x50(%rsp), %rbx
leaq 0xce8(%rsp), %r15
leaq 0xf900(%rip), %r13      # 0x1eb58
leaq 0xf8d9(%rip), %rbp      # 0x1eb38
jl 0xec91
pushq $0x4
popq %rdi
leaq 0xf9dc(%rip), %rsi      # 0x1ec4b
jmp 0xec7b
testb $0x1, %r8b
movq 0x50(%rsp), %rbx
leaq 0xce8(%rsp), %r15
leaq 0x20b1184(%rip), %r12   # 0x20c0410
leaq 0xf8c5(%rip), %r13      # 0x1eb58
leaq 0xf89e(%rip), %rbp      # 0x1eb38
je 0xf2a2
incl 0x3c(%rsp)
jmp 0xf2a6
incl 0x24(%rsp)
incq %rdx
jmp 0xec14
leaq 0xf9b6(%rip), %rsi      # 0x1ec6b
pushq $0x3
popq %rdi
movl 0x24(%rsp), %edx
movl 0x2c(%rsp), %ecx
movl 0x3c(%rsp), %r8d
movl 0x28(%rsp), %r9d
xorl %eax, %eax
callq 0x9035
addq $0x24b8, %rsp           # imm = 0x24B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq