// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/lerp.cpp
// Function: TryMakeSquare(localtriangulation_t*, int)
static bool TryMakeSquare (localtriangulation_t *lt, int i)
{
	localtriangulation_t::Wedge *w1;
	localtriangulation_t::Wedge *w2;
	localtriangulation_t::Wedge *w3;
	vec3_t v;
	vec3_t dir1;
	vec3_t dir2;
	vec_t angle;

	w1 = &lt->sortedwedges[i];
	w2 = &lt->sortedwedges[(i + 1) % (int)lt->sortedwedges.size ()];
	w3 = &lt->sortedwedges[(i + 2) % (int)lt->sortedwedges.size ()];

	// (o, p1, p2) and (o, p2, p3) must be triangles and not in a square
	if (w1->shape != localtriangulation_t::Wedge::eTriangular || w2->shape != localtriangulation_t::Wedge::eTriangular)
	{
		return false;
	}
	
	// (o, p1, p3) must be a triangle
	angle = GetAngle (w1->leftdirection, w3->leftdirection, lt->normal);
	angle = GetAngleDiff (angle, 0);
	if (angle >= TRIANGLE_SHAPE_THRESHOLD)
	{
		return false;
	}

	// (p2, p1, p3) must be a triangle
	VectorSubtract (w1->leftspot, w2->leftspot, v);
	if (!GetDirection (v, lt->normal, dir1))
	{
		return false;
	}
	VectorSubtract (w3->leftspot, w2->leftspot, v);
	if (!GetDirection (v, lt->normal, dir2))
	{
		return false;
	}
	angle = GetAngle (dir2, dir1, lt->normal);
	angle = GetAngleDiff (angle, 0);
	if (angle >= TRIANGLE_SHAPE_THRESHOLD)
	{
		return false;
	}

	w1->shape = localtriangulation_t::Wedge::eSquareLeft;
	VectorSubtract (vec3_origin, dir1, w1->wedgenormal);
	w2->shape = localtriangulation_t::Wedge::eSquareRight;
	VectorCopy (dir2, w2->wedgenormal);
	return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movslq %esi, %rcx
imulq $0x2c, %rcx, %r15
movq 0x70(%rdi), %r14
movq 0x78(%rdi), %rax
leal 0x1(%rcx), %esi
subq %r14, %rax
pushq $0x2c
popq %rdi
cqto
idivq %rdi
movq %rax, %rdi
movl %esi, %eax
cltd
idivl %edi
movslq %edx, %rax
imulq $0x2c, %rax, %r13
addl $0x2, %ecx
movl %ecx, %eax
cltd
idivl %edi
movl (%r14,%r13), %eax
orl (%r14,%r15), %eax
jne 0x1152c
addq %r14, %r15
movslq %edx, %rax
imulq $0x2c, %rax, %r12
addq %r14, %r12
leaq 0x14(%r15), %rdi
leaq 0x14(%r12), %rsi
addq $0x44, %rbx
movq %rbx, %rdx
callq 0x104b7
cvtss2sd %xmm0, %xmm1
addsd 0x241e0(%rip), %xmm1    # 0x355a0
cvtsd2ss %xmm1, %xmm1
xorps %xmm3, %xmm3
movaps %xmm0, %xmm2
cmpltss %xmm3, %xmm2
movaps %xmm2, %xmm3
andnps %xmm0, %xmm3
andps %xmm1, %xmm2
orps %xmm3, %xmm2
cvtss2sd %xmm2, %xmm0
ucomisd 0x24201(%rip), %xmm0    # 0x355e8
jae 0x1152c
addq %r13, %r14
movsd 0x8(%r15), %xmm0
movsd 0x8(%r14), %xmm1
subps %xmm1, %xmm0
leaq 0x20(%rsp), %rdi
movlps %xmm0, (%rdi)
movss 0x10(%r15), %xmm0
subss 0x10(%r14), %xmm0
movss %xmm0, 0x8(%rdi)
leaq 0x14(%rsp), %rdx
movq %rbx, %rsi
callq 0x1044e
ucomiss 0x20c44(%rip), %xmm0    # 0x32070
jne 0x11434
jnp 0x1152c
movsd 0x8(%r12), %xmm0
movsd 0x8(%r14), %xmm1
subps %xmm1, %xmm0
leaq 0x20(%rsp), %rdi
movlps %xmm0, (%rdi)
movss 0x10(%r12), %xmm0
subss 0x10(%r14), %xmm0
movss %xmm0, 0x8(%rdi)
leaq 0x8(%rsp), %rdx
movq %rbx, %rsi
callq 0x1044e
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x11479
jnp 0x1152c
leaq 0x8(%rsp), %rdi
leaq 0x14(%rsp), %rsi
movq %rbx, %rdx
callq 0x104b7
cvtss2sd %xmm0, %xmm1
addsd 0x24109(%rip), %xmm1    # 0x355a0
cvtsd2ss %xmm1, %xmm1
movaps %xmm0, %xmm2
cmpltss 0x20bc9(%rip), %xmm2    # 0x32070
movaps %xmm2, %xmm3
andnps %xmm0, %xmm3
andps %xmm1, %xmm2
orps %xmm3, %xmm2
cvtss2sd %xmm2, %xmm0
ucomisd 0x24129(%rip), %xmm0    # 0x355e8
jae 0x1152c
movl $0x3, (%r15)
leaq 0x22861(%rip), %rax     # 0x33d30
movss (%rax), %xmm0
subss 0x14(%rsp), %xmm0
movss 0x4(%rax), %xmm1
movss %xmm0, 0x20(%r15)
subss 0x18(%rsp), %xmm1
movss %xmm1, 0x24(%r15)
movss 0x8(%rax), %xmm0
subss 0x1c(%rsp), %xmm0
movss %xmm0, 0x28(%r15)
movl $0x4, (%r14)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x20(%r14)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x24(%r14)
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x28(%r14)
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq