// File: FreeSlave[P]vhlt/hlrad/lerp.cpp
// Function: CreateLocalTriangulation(facetriangulation_t const*, int)
static localtriangulation_t *CreateLocalTriangulation (const facetriangulation_t *facetrian, int patchnum)
{
	localtriangulation_t *lt;
	int i;
	const patch_t *patch;
	vec_t dot;
	int facenum;
	localtriangulation_t::Wedge *w;
	localtriangulation_t::Wedge *wnext;
	vec_t angle;
	vec_t total;
	vec3_t v;
	vec3_t normal;

	facenum = facetrian->facenum;
	patch = &g_patches[patchnum];
	lt = new localtriangulation_t;

	// Fill basic information for this local triangulation
	lt->plane = *getPlaneFromFaceNumber (facenum);
	lt->plane.dist += DotProduct (g_face_offset[facenum], lt->plane.normal);
	lt->winding = *patch->winding;
	VectorMA (patch->origin, -PATCH_HUNT_OFFSET, lt->plane.normal, lt->center);
	dot = DotProduct (lt->center, lt->plane.normal) - lt->plane.dist;
	VectorMA (lt->center, -dot, lt->plane.normal, lt->center);
	if (!point_in_winding_noedge (lt->winding, lt->plane, lt->center, DEFAULT_EDGE_WIDTH))
	{
		snap_to_winding_noedge (lt->winding, lt->plane, lt->center, DEFAULT_EDGE_WIDTH, 4 * DEFAULT_EDGE_WIDTH);
	}
	VectorCopy (lt->plane.normal, lt->normal);
	lt->patchnum = patchnum;
	lt->neighborfaces = facetrian->neighbors;

	// Gather all patches from nearby faces
	GatherPatches (lt, facetrian);
	
	// Remove distant patches
	PurgePatches (lt);

	// Calculate wedge types
	total = 0.0;
	for (i = 0; i < (int)lt->sortedwedges.size (); i++)
	{
		w = &lt->sortedwedges[i];
		wnext = &lt->sortedwedges[(i + 1) % (int)lt->sortedwedges.size ()];

		angle = GetAngle (w->leftdirection, wnext->leftdirection, lt->normal);
		if (g_drawlerp && ((int)lt->sortedwedges.size () >= 2 && fabs (angle) <= (0.9*Q_PI/180)))
		{
			Developer (DEVELOPER_LEVEL_SPAM, "Debug: triangulation: internal error 9.\n");
		}
		angle = GetAngleDiff (angle, 0);
		if ((int)lt->sortedwedges.size () == 1)
		{
			angle = 2 * Q_PI;
		}
		total += angle;

		if (angle <= Q_PI + NORMAL_EPSILON)
		{
			if (angle < TRIANGLE_SHAPE_THRESHOLD)
			{
				w->shape = localtriangulation_t::Wedge::eTriangular;
				VectorClear (w->wedgenormal);
			}
			else
			{
				w->shape = localtriangulation_t::Wedge::eConvex;
				VectorSubtract (wnext->leftspot, w->leftspot, v);
				GetDirection (v, lt->normal, w->wedgenormal);
			}
		}
		else
		{
			w->shape = localtriangulation_t::Wedge::eConcave;
			VectorAdd (wnext->leftdirection, w->leftdirection, v);
			CrossProduct (lt->normal, v, normal);
			VectorSubtract (wnext->leftdirection, w->leftdirection, v);
			VectorAdd (normal, v, normal);
			GetDirection (normal, lt->normal, w->wedgenormal);
			if (g_drawlerp && VectorLength (w->wedgenormal) == 0)
			{
				Developer (DEVELOPER_LEVEL_SPAM, "Debug: triangulation: internal error 10.\n");
			}
		}
	}
	if (g_drawlerp && ((int)lt->sortedwedges.size () > 0 && fabs (total - 2 * Q_PI) > 10 * NORMAL_EPSILON))
	{
		Developer (DEVELOPER_LEVEL_SPAM, "Debug: triangulation: internal error 11.\n");
	}
#ifdef HLRAD_BILINEARINTERPOLATION
	FindSquares (lt);
#endif

	// Calculate hull points
	PlaceHullPoints (lt);

	return lt;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movl %esi, 0x7c(%rsp)
movq 0x80(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x58(%rsp)
leaq 0x53d6048(%rip), %rax   # 0x53eda00
movq (%rax), %rax
movslq 0x7c(%rsp), %rcx
imulq $0x138, %rcx, %rcx      # imm = 0x138
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movl $0xa0, %edi
callq 0x4320
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
callq 0x21870
jmp 0x179eb
movq 0x10(%rsp), %rax
movq %rax, 0x70(%rsp)
movl 0x58(%rsp), %edi
callq 0x5ce10
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movl 0x10(%rcx), %ecx
movl %ecx, 0x10(%rax)
movslq 0x58(%rsp), %rcx
leaq 0x5455fea(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm1
movq 0x70(%rsp), %rax
movss (%rax), %xmm2
movslq 0x58(%rsp), %rcx
leaq 0x5455fca(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
movq 0x70(%rsp), %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movslq 0x58(%rsp), %rcx
leaq 0x5455fa0(%rip), %rax   # 0x546da10
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
movq 0x70(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x70(%rsp), %rax
addss 0xc(%rax), %xmm0
movss %xmm0, 0xc(%rax)
movq 0x60(%rsp), %rax
movq 0x28(%rax), %rsi
movq 0x70(%rsp), %rdi
addq $0x18, %rdi
callq 0xe8e0
movq 0x60(%rsp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0x70(%rsp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x55d29(%rip), %xmm2    # 0x6d800
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x38(%rax)
movq 0x60(%rsp), %rax
movss 0xc(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0x70(%rsp), %rax
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x55cef(%rip), %xmm2    # 0x6d800
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x3c(%rax)
movq 0x60(%rsp), %rax
movss 0x10(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0x70(%rsp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x55cb5(%rip), %xmm2    # 0x6d800
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x40(%rax)
movq 0x70(%rsp), %rax
movss 0x38(%rax), %xmm1
movq 0x70(%rsp), %rax
movss (%rax), %xmm2
movq 0x70(%rsp), %rax
movss 0x3c(%rax), %xmm0
movq 0x70(%rsp), %rax
mulss 0x4(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x70(%rsp), %rax
movss 0x40(%rax), %xmm0
movq 0x70(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x70(%rsp), %rax
subss 0xc(%rax), %xmm0
movss %xmm0, 0x5c(%rsp)
movq 0x70(%rsp), %rax
movss 0x38(%rax), %xmm1
movss 0x5c(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x70(%rsp), %rax
movss (%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x38(%rax)
movq 0x70(%rsp), %rax
movss 0x3c(%rax), %xmm1
movss 0x5c(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x70(%rsp), %rax
movss 0x4(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x3c(%rax)
movq 0x70(%rsp), %rax
movss 0x40(%rax), %xmm1
movss 0x5c(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x70(%rsp), %rax
movss 0x8(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x40(%rax)
movq 0x70(%rsp), %rdi
addq $0x18, %rdi
movq 0x70(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x38, %rdx
movss 0x55b43(%rip), %xmm0    # 0x6d7c8
callq 0x4a180
testb $0x1, %al
jne 0x17cde
movq 0x70(%rsp), %rdi
addq $0x18, %rdi
movq 0x70(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x38, %rdx
movss 0x55b1b(%rip), %xmm0    # 0x6d7c8
movss 0x55b17(%rip), %xmm1    # 0x6d7cc
callq 0x4ac00
jmp 0x17cde
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movl $0xa0, %esi
callq 0x4330
jmp 0x18247
movq 0x70(%rsp), %rax
movss (%rax), %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x44(%rax)
movq 0x70(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x48(%rax)
movq 0x70(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x70(%rsp), %rax
movss %xmm0, 0x4c(%rax)
movl 0x7c(%rsp), %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x50(%rax)
movq 0x80(%rsp), %rsi
addq $0x8, %rsi
movq 0x70(%rsp), %rdi
addq $0x58, %rdi
callq 0x218c0
movq 0x70(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x1ad00
movq 0x70(%rsp), %rdi
callq 0x1b360
xorps %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x70(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x181c5
movq 0x70(%rsp), %rdi
addq $0x70, %rdi
movslq 0x6c(%rsp), %rsi
callq 0x21b80
movq %rax, 0x50(%rsp)
movq 0x70(%rsp), %rax
addq $0x70, %rax
movq %rax, (%rsp)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
movq 0x70(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
movq (%rsp), %rdi
movq %rax, %rcx
movl 0x8(%rsp), %eax
cltd
idivl %ecx
movslq %edx, %rsi
callq 0x21b80
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
addq $0x14, %rdi
movq 0x48(%rsp), %rsi
addq $0x14, %rsi
movq 0x70(%rsp), %rdx
addq $0x44, %rdx
callq 0x187a0
movss %xmm0, 0x44(%rsp)
leaq 0x5517c4a(%rip), %rax   # 0x552fa61
testb $0x1, (%rax)
je 0x17e5f
movq 0x70(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
cmpl $0x2, %eax
jl 0x17e5f
movss 0x44(%rsp), %xmm0
callq 0x11ce0
cvtss2sd %xmm0, %xmm1
movsd 0x559d2(%rip), %xmm0    # 0x6d818
ucomisd %xmm1, %xmm0
jb 0x17e5f
movl $0x5, %edi
leaq 0x55c58(%rip), %rsi     # 0x6dab0
movb $0x0, %al
callq 0xbbf0
movss 0x44(%rsp), %xmm0
xorps %xmm1, %xmm1
callq 0x18920
movss %xmm0, 0x44(%rsp)
movq 0x70(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
cmpl $0x1, %eax
jne 0x17e94
movss 0x55942(%rip), %xmm0    # 0x6d7d0
movss %xmm0, 0x44(%rsp)
movss 0x44(%rsp), %xmm0
addss 0x40(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x44(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x55968(%rip), %xmm0    # 0x6d820
ucomisd %xmm1, %xmm0
jb 0x17f88
movss 0x44(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x55954(%rip), %xmm0    # 0x6d828
ucomisd %xmm1, %xmm0
jbe 0x17f0e
movq 0x50(%rsp), %rax
movl $0x0, (%rax)
movq 0x50(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x28(%rax)
movq 0x50(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x24(%rax)
movq 0x50(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x20(%rax)
jmp 0x17f83
movq 0x50(%rsp), %rax
movl $0x1, (%rax)
movq 0x48(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x34(%rsp)
movq 0x48(%rsp), %rax
movss 0xc(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0xc(%rax), %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x48(%rsp), %rax
movss 0x10(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x10(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
leaq 0x34(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x44, %rsi
movq 0x50(%rsp), %rdx
addq $0x20, %rdx
callq 0x18680
jmp 0x181b3
movq 0x50(%rsp), %rax
movl $0x2, (%rax)
movq 0x48(%rsp), %rax
movss 0x14(%rax), %xmm0
movq 0x50(%rsp), %rax
addss 0x14(%rax), %xmm0
movss %xmm0, 0x34(%rsp)
movq 0x48(%rsp), %rax
movss 0x18(%rax), %xmm0
movq 0x50(%rsp), %rax
addss 0x18(%rax), %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x48(%rsp), %rax
movss 0x1c(%rax), %xmm0
movq 0x50(%rsp), %rax
addss 0x1c(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x70(%rsp), %rax
movss 0x48(%rax), %xmm0
movss 0x3c(%rsp), %xmm2
movq 0x70(%rsp), %rax
movss 0x4c(%rax), %xmm1
mulss 0x38(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x28(%rsp)
movq 0x70(%rsp), %rax
movss 0x4c(%rax), %xmm0
movss 0x34(%rsp), %xmm2
movq 0x70(%rsp), %rax
movss 0x44(%rax), %xmm1
mulss 0x3c(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x2c(%rsp)
movq 0x70(%rsp), %rax
movss 0x44(%rax), %xmm0
movss 0x38(%rsp), %xmm2
movq 0x70(%rsp), %rax
movss 0x48(%rax), %xmm1
mulss 0x34(%rsp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x30(%rsp)
movq 0x48(%rsp), %rax
movss 0x14(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x14(%rax), %xmm0
movss %xmm0, 0x34(%rsp)
movq 0x48(%rsp), %rax
movss 0x18(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x18(%rax), %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x48(%rsp), %rax
movss 0x1c(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x1c(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movss 0x28(%rsp), %xmm0
addss 0x34(%rsp), %xmm0
movss %xmm0, 0x28(%rsp)
movss 0x2c(%rsp), %xmm0
addss 0x38(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x30(%rsp), %xmm0
addss 0x3c(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
leaq 0x28(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x44, %rsi
movq 0x50(%rsp), %rdx
addq $0x20, %rdx
callq 0x18680
leaq 0x5517928(%rip), %rax   # 0x552fa61
testb $0x1, (%rax)
je 0x181b1
movq 0x50(%rsp), %rax
movss 0x20(%rax), %xmm0
movq 0x50(%rsp), %rax
mulss 0x20(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x50(%rsp), %rax
movss 0x24(%rax), %xmm1
movq 0x50(%rsp), %rax
mulss 0x24(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movq 0x50(%rsp), %rax
movss 0x28(%rax), %xmm1
movq 0x50(%rsp), %rax
mulss 0x28(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x181b1
jp 0x181b1
movl $0x5, %edi
leaq 0x5592f(%rip), %rsi     # 0x6dad9
movb $0x0, %al
callq 0xbbf0
jmp 0x181b3
jmp 0x181b5
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x17d6c
leaq 0x5517895(%rip), %rax   # 0x552fa61
testb $0x1, (%rax)
je 0x18226
movq 0x70(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
cmpl $0x0, %eax
jle 0x18226
movss 0x40(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x5561a(%rip), %xmm1    # 0x6d810
subsd %xmm1, %xmm0
movaps 0x5566f(%rip), %xmm1    # 0x6d870
pand %xmm1, %xmm0
movsd 0x555fb(%rip), %xmm1    # 0x6d808
ucomisd %xmm1, %xmm0
jbe 0x18226
movl $0x5, %edi
leaq 0x558e4(%rip), %rsi     # 0x6db03
movb $0x0, %al
callq 0xbbf0
movq 0x70(%rsp), %rdi
callq 0x1c7e0
movq 0x70(%rsp), %rdi
callq 0x1cab0
movq 0x70(%rsp), %rax
addq $0x88, %rsp
retq
movq 0x20(%rsp), %rdi
callq 0x45c0
nopw %cs:(%rax,%rax)