// File: FreeSlave[P]vhlt/hlrad/lerp.cpp
// Function: GatherPatches(localtriangulation_t*, facetriangulation_t const*)
static void GatherPatches (localtriangulation_t *lt, const facetriangulation_t *facetrian)
{
	int i;
	int facenum2;
	const dplane_t *dp2;
	const patch_t *patch2;
	int patchnum2;
	vec3_t v;
	localtriangulation_t::Wedge point;
	std::vector< localtriangulation_t::Wedge > points;
	std::vector< std::pair< vec_t, int > > angles;
	vec_t angle;

	if (!g_lerp_enabled)
	{
		lt->sortedwedges.resize (0);
		return;
	}

	points.resize (0);
	for (i = 0; i < (int)lt->neighborfaces.size (); i++)
	{
		facenum2 = lt->neighborfaces[i];
		dp2 = getPlaneFromFaceNumber (facenum2);
		for (patch2 = g_face_patches[facenum2]; patch2; patch2 = patch2->next)
		{
			patchnum2 = patch2 - g_patches;
			
			point.leftpatchnum = patchnum2;
			VectorMA (patch2->origin, -PATCH_HUNT_OFFSET, dp2->normal, v);

			// Do permission tests using the original position of the patch
			if (patchnum2 == lt->patchnum || point_in_winding (lt->winding, lt->plane, v))
			{
				continue;
			}
			if (facenum2 != facetrian->facenum && TestLineSegmentIntersectWall (facetrian, lt->center, v))
			{
				continue;
			}
#ifdef HLRAD_FARPATCH_FIX
			if (TestFarPatch (lt, v, *patch2->winding))
			{
				continue;
			}
#endif

			// Store the adapted position of the patch
			if (!CalcAdaptedSpot (lt, v, facenum2, point.leftspot))
			{
				continue;
			}
			if (GetDirection (point.leftspot, lt->normal, point.leftdirection) <= 2 * ON_EPSILON)
			{
				continue;
			}
			points.push_back (point);
		}
	}

	// Sort the patches into clockwise order
	angles.resize ((int)points.size ());
	for (i = 0; i < (int)points.size (); i++)
	{
		angle = GetAngle (points[0].leftdirection, points[i].leftdirection, lt->normal);
		if (i == 0)
		{
			if (g_drawlerp && fabs (angle) > NORMAL_EPSILON)
			{
				Developer (DEVELOPER_LEVEL_SPAM, "Debug: triangulation: internal error 7.\n");
			}
			angle = 0.0;
		}
		angles[i].first = GetAngleDiff (angle, 0);
		angles[i].second = i;
	}
	std::sort (angles.begin (), angles.end ());

	lt->sortedwedges.resize ((int)points.size ());
	for (i = 0; i < (int)points.size (); i++)
	{
		lt->sortedwedges[i] = points[angles[i].second];
	}
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x21ba0
leaq 0x80(%rsp), %rdi
callq 0x22530
cmpl $0x0, 0x63678(%rip)     # 0x7e3b0
jne 0x1ad8b
movq 0x110(%rsp), %rdi
addq $0x70, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x22540
jmp 0x1ad51
movl $0x1, 0x68(%rsp)
jmp 0x1b333
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x227b0
leaq 0x98(%rsp), %rdi
callq 0x22810
jmp 0x1b355
xorl %eax, %eax
movl %eax, %esi
leaq 0x98(%rsp), %rdi
callq 0x22540
jmp 0x1ad9e
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x110(%rsp), %rdi
addq $0x58, %rdi
callq 0x1eee0
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x1b0b9
movq 0x110(%rsp), %rdi
addq $0x58, %rdi
movslq 0x104(%rsp), %rsi
callq 0x1fc20
movl (%rax), %eax
movl %eax, 0x100(%rsp)
movl 0x100(%rsp), %edi
callq 0x5ce10
movq %rax, 0x48(%rsp)
jmp 0x1ae09
movq 0x48(%rsp), %rax
movq %rax, 0xf8(%rsp)
movslq 0x100(%rsp), %rcx
leaq 0x5292bdb(%rip), %rax   # 0x52ada00
movq (%rax,%rcx,8), %rax
movq %rax, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
je 0x1b0a1
movq 0xf0(%rsp), %rax
leaq 0x53d2bb1(%rip), %rcx   # 0x53eda00
movq (%rcx), %rcx
subq %rcx, %rax
movl $0x138, %ecx            # imm = 0x138
cqto
idivq %rcx
movl %eax, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0xb8(%rsp)
movq 0xf0(%rsp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0xf8(%rsp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x52963(%rip), %xmm2    # 0x6d800
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xe0(%rsp)
movq 0xf0(%rsp), %rax
movss 0xc(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0xf8(%rsp), %rax
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x52924(%rip), %xmm2    # 0x6d800
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xe4(%rsp)
movq 0xf0(%rsp), %rax
movss 0x10(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0xf8(%rsp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x528e5(%rip), %xmm2    # 0x6d800
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xe8(%rsp)
movl 0xec(%rsp), %eax
movq 0x110(%rsp), %rcx
cmpl 0x50(%rcx), %eax
je 0x1af73
movq 0x110(%rsp), %rsi
movq %rsi, %rdi
addq $0x18, %rdi
leaq 0xe0(%rsp), %rdx
xorps %xmm0, %xmm0
callq 0x49e50
movb %al, 0x47(%rsp)
jmp 0x1af69
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x1af73
jmp 0x1af78
jmp 0x1b089
movl 0x100(%rsp), %eax
movq 0x108(%rsp), %rcx
cmpl (%rcx), %eax
je 0x1afc1
movq 0x108(%rsp), %rdi
movq 0x110(%rsp), %rsi
addq $0x38, %rsi
leaq 0xe0(%rsp), %rdx
callq 0x1dff0
movb %al, 0x46(%rsp)
jmp 0x1afb2
movb 0x46(%rsp), %al
testb $0x1, %al
jne 0x1afbc
jmp 0x1afc1
jmp 0x1b089
movq 0x110(%rsp), %rdi
movq 0xf0(%rsp), %rax
movq 0x28(%rax), %rdx
leaq 0xe0(%rsp), %rsi
callq 0x1e630
movb %al, 0x45(%rsp)
jmp 0x1afe8
movb 0x45(%rsp), %al
testb $0x1, %al
jne 0x1aff2
jmp 0x1aff7
jmp 0x1b089
movq 0x110(%rsp), %rdi
movl 0x100(%rsp), %edx
leaq 0xbc(%rsp), %rcx
leaq 0xe0(%rsp), %rsi
callq 0x14560
movb %al, 0x44(%rsp)
jmp 0x1b021
movb 0x44(%rsp), %al
testb $0x1, %al
jne 0x1b02d
jmp 0x1b02b
jmp 0x1b089
leaq 0xbc(%rsp), %rdi
movq 0x110(%rsp), %rsi
addq $0x44, %rsi
leaq 0xc8(%rsp), %rdx
callq 0x18680
movss %xmm0, 0x40(%rsp)
jmp 0x1b056
movss 0x40(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x52780(%rip), %xmm0    # 0x6d7e8
ucomisd %xmm1, %xmm0
jb 0x1b070
jmp 0x1b089
leaq 0x98(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
callq 0x225e0
jmp 0x1b087
jmp 0x1b089
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
jmp 0x1ae31
jmp 0x1b0a3
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x1ada9
leaq 0x98(%rsp), %rdi
callq 0x1f6c0
movslq %eax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x22650
jmp 0x1b0d8
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x3c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f6c0
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1b22f
xorl %eax, %eax
movl %eax, %esi
leaq 0x98(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x21b80
movq 0x28(%rsp), %rdi
addq $0x14, %rax
movq %rax, 0x30(%rsp)
movslq 0x104(%rsp), %rsi
callq 0x21b80
movq 0x30(%rsp), %rdi
movq %rax, %rsi
addq $0x14, %rsi
movq 0x110(%rsp), %rdx
addq $0x44, %rdx
callq 0x187a0
movss %xmm0, 0x38(%rsp)
jmp 0x1b160
movss 0x38(%rsp), %xmm0
movss %xmm0, 0x7c(%rsp)
cmpl $0x0, 0x104(%rsp)
jne 0x1b1bf
leaq 0x55148e4(%rip), %rax   # 0x552fa61
testb $0x1, (%rax)
je 0x1b1b6
movss 0x7c(%rsp), %xmm0
callq 0x11ce0
cvtss2sd %xmm0, %xmm0
movsd 0x4eecf(%rip), %xmm1    # 0x6a068
ucomisd %xmm1, %xmm0
jbe 0x1b1b6
leaq 0x52987(%rip), %rsi     # 0x6db2d
xorl %eax, %eax
movl $0x5, %edi
callq 0xbbf0
jmp 0x1b1b4
jmp 0x1b1b6
xorps %xmm0, %xmm0
movss %xmm0, 0x7c(%rsp)
movss 0x7c(%rsp), %xmm0
xorps %xmm1, %xmm1
callq 0x18920
movss %xmm0, 0x20(%rsp)
movslq 0x104(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x226f0
movss 0x20(%rsp), %xmm0
movss %xmm0, (%rax)
movl 0x104(%rsp), %eax
movl %eax, 0x24(%rsp)
movslq 0x104(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x226f0
movl 0x24(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x1b0e3
leaq 0x80(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x22750
movq 0x18(%rsp), %rdi
movq %rax, 0x60(%rsp)
callq 0x22780
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x22710
jmp 0x1b266
movq 0x110(%rsp), %rax
addq $0x70, %rax
movq %rax, 0x10(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f6c0
movq 0x10(%rsp), %rdi
movslq %eax, %rsi
callq 0x22540
jmp 0x1b293
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1f6c0
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x1b32b
movslq 0x104(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x226f0
movslq 0x4(%rax), %rsi
leaq 0x98(%rsp), %rdi
callq 0x21b80
movq %rax, (%rsp)
movq 0x110(%rsp), %rdi
addq $0x70, %rdi
movslq 0x104(%rsp), %rsi
callq 0x21b80
movq (%rsp), %rsi
movq %rax, %rdi
movl $0x2c, %edx
callq 0x4280
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x1b29e
movl $0x0, 0x68(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x227b0
leaq 0x98(%rsp), %rdi
callq 0x22810
addq $0x118, %rsp            # imm = 0x118
retq
movq 0x70(%rsp), %rdi
callq 0x45c0
nop