// File: /workspace/github_cpp_makefile_star2_O3/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 $0x40, %rsp
movslq %esi, %rcx
imulq $0x2c, %rcx, %r15
movq 0x70(%rdi), %r14
movq 0x78(%rdi), %rdx
leal 0x1(%rcx), %eax
subq %r14, %rdx
shrq $0x2, %rdx
imull $0xba2e8ba3, %edx, %esi # imm = 0xBA2E8BA3
cltd
idivl %esi
movslq %edx, %rax
imulq $0x2c, %rax, %r13
addl $0x2, %ecx
movl %ecx, %eax
cltd
idivl %esi
movl (%r14,%r13), %eax
orl (%r14,%r15), %eax
jne 0x1387f
movq %rdi, %rbx
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 0x125f4
cvtss2sd %xmm0, %xmm1
addsd 0x26ed3(%rip), %xmm1    # 0x3a5e0
cvtsd2ss %xmm1, %xmm1
xorps %xmm3, %xmm3
movaps %xmm0, %xmm2
cmpltss %xmm3, %xmm2
movaps %xmm2, %xmm3
andnps %xmm0, %xmm3
andps %xmm1, %xmm2
orps %xmm3, %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
ucomisd 0x26f21(%rip), %xmm0    # 0x3a658
jae 0x1387f
addq %r13, %r14
movsd 0x8(%r14), %xmm1
movsd 0x8(%r15), %xmm0
movaps %xmm1, 0x30(%rsp)
subps %xmm1, %xmm0
leaq 0x20(%rsp), %rdi
movlps %xmm0, (%rdi)
movss 0x10(%r15), %xmm0
movss 0x10(%r14), %xmm1
movss %xmm1, (%rsp)
subss %xmm1, %xmm0
movss %xmm0, 0x8(%rdi)
leaq 0x4(%rsp), %rdx
movq %rbx, %rsi
callq 0x12521
ucomiss 0x23926(%rip), %xmm0    # 0x370b0
jne 0x13792
jnp 0x1387f
movsd 0x8(%r12), %xmm0
subps 0x30(%rsp), %xmm0
leaq 0x20(%rsp), %rdi
movlps %xmm0, (%rdi)
movss 0x10(%r12), %xmm0
subss (%rsp), %xmm0
movss %xmm0, 0x8(%rdi)
leaq 0x10(%rsp), %rdx
movq %rbx, %rsi
callq 0x12521
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x137d2
jnp 0x1387f
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rsi
movq %rbx, %rdx
callq 0x125f4
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
addsd 0x26ded(%rip), %xmm1    # 0x3a5e0
cvtsd2ss %xmm1, %xmm1
movaps %xmm0, %xmm2
cmpltss 0x238ad(%rip), %xmm2    # 0x370b0
movaps %xmm2, %xmm3
andnps %xmm0, %xmm3
andps %xmm1, %xmm2
orps %xmm3, %xmm2
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
ucomisd 0x26e3a(%rip), %xmm0    # 0x3a658
jae 0x1387f
movl $0x3, (%r15)
leaq 0x255a2(%rip), %rax     # 0x38dd0
movss (%rax), %xmm0
movss 0x4(%rax), %xmm1
subss 0x4(%rsp), %xmm0
movss %xmm0, 0x20(%r15)
subss 0x8(%rsp), %xmm1
movss %xmm1, 0x24(%r15)
movss 0x8(%rax), %xmm0
subss 0xc(%rsp), %xmm0
movss %xmm0, 0x28(%r15)
movl $0x4, (%r14)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x20(%r14)
movss 0x18(%rsp), %xmm0
movss %xmm0, 0x28(%r14)
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop