// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movl %esi, 0x64(%rsp)
movq 0x68(%rsp), %rdi
addq $0x70, %rdi
movslq 0x64(%rsp), %rsi
callq 0x21b80
movq %rax, 0x58(%rsp)
movq 0x68(%rsp), %rax
addq $0x70, %rax
movq %rax, (%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movq 0x68(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
movq (%rsp), %rdi
movq %rax, %rcx
movl 0xc(%rsp), %eax
cltd
idivl %ecx
movslq %edx, %rsi
callq 0x21b80
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %rax
addq $0x70, %rax
movq %rax, 0x10(%rsp)
movl 0x64(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x1c(%rsp)
movq 0x68(%rsp), %rdi
addq $0x70, %rdi
callq 0x1f6c0
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cltd
idivl %ecx
movslq %edx, %rsi
callq 0x21b80
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x1e9ed
movq 0x50(%rsp), %rax
cmpl $0x0, (%rax)
je 0x1e9f7
movb $0x0, 0x77(%rsp)
jmp 0x1ec39
movq 0x58(%rsp), %rdi
addq $0x14, %rdi
movq 0x48(%rsp), %rsi
addq $0x14, %rsi
movq 0x68(%rsp), %rdx
addq $0x44, %rdx
callq 0x187a0
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
callq 0x18920
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x4ede5(%rip), %xmm1    # 0x6d828
ucomisd %xmm1, %xmm0
jb 0x1ea53
movb $0x0, 0x77(%rsp)
jmp 0x1ec39
movq 0x58(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x58(%rsp), %rax
movss 0xc(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0xc(%rax), %xmm0
movss %xmm0, 0x40(%rsp)
movq 0x58(%rsp), %rax
movss 0x10(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x10(%rax), %xmm0
movss %xmm0, 0x44(%rsp)
leaq 0x3c(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x44, %rsi
leaq 0x30(%rsp), %rdx
callq 0x18680
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x1eacd
jp 0x1eacd
movb $0x0, 0x77(%rsp)
jmp 0x1ec39
movq 0x48(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x8(%rax), %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x48(%rsp), %rax
movss 0xc(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0xc(%rax), %xmm0
movss %xmm0, 0x40(%rsp)
movq 0x48(%rsp), %rax
movss 0x10(%rax), %xmm0
movq 0x50(%rsp), %rax
subss 0x10(%rax), %xmm0
movss %xmm0, 0x44(%rsp)
leaq 0x3c(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x44, %rsi
leaq 0x24(%rsp), %rdx
callq 0x18680
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x1eb47
jp 0x1eb47
movb $0x0, 0x77(%rsp)
jmp 0x1ec39
leaq 0x24(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq $0x44, %rdx
callq 0x187a0
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
callq 0x18920
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x4ec9d(%rip), %xmm1    # 0x6d828
ucomisd %xmm1, %xmm0
jb 0x1eb9b
movb $0x0, 0x77(%rsp)
jmp 0x1ec39
movq 0x58(%rsp), %rax
movl $0x3, (%rax)
leaq 0x4d2a3(%rip), %rax     # 0x6be50
movss (%rax), %xmm0
subss 0x30(%rsp), %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, 0x20(%rax)
leaq 0x4d288(%rip), %rax     # 0x6be50
movss 0x4(%rax), %xmm0
subss 0x34(%rsp), %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, 0x24(%rax)
leaq 0x4d26c(%rip), %rax     # 0x6be50
movss 0x8(%rax), %xmm0
subss 0x38(%rsp), %xmm0
movq 0x58(%rsp), %rax
movss %xmm0, 0x28(%rax)
movq 0x50(%rsp), %rax
movl $0x4, (%rax)
movss 0x24(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss %xmm0, 0x20(%rax)
movss 0x28(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss %xmm0, 0x24(%rax)
movss 0x2c(%rsp), %xmm0
movq 0x50(%rsp), %rax
movss %xmm0, 0x28(%rax)
movb $0x1, 0x77(%rsp)
movb 0x77(%rsp), %al
andb $0x1, %al
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)
nop