// File: /workspace/github_cpp_makefile_star2_O3/MarkCBell[P]twister/./twister/kernel/twister.cpp
// Function: manifold::identify_cusps(tetra*)
void manifold::identify_cusps(tetra *capoff_tetra)
{
	output_debugging("cusps", 2);
	
	// Clear the temp value everywhere.
	for (tetra *current = capoff_tetra; current != NULL; current = current->get_next())
		current->set_temp(-1);
	
	// Write theoretical cusp numbers to the temp field on every cusp.
	int theoretical_num_cusps = 0;  // This is how many cusps we find, although some might not be toroidal.
	for (tetra *basepoint = capoff_tetra; basepoint != NULL; basepoint = basepoint->get_next())
	{
		if (basepoint->get_temp() != -1) continue;  // Has already been assigned a theoretical cusp number.
		basepoint->set_temp(theoretical_num_cusps);
		
		std::queue<tetra *> tetra_list;
		tetra_list.push(basepoint);
		while (!(tetra_list.empty()))
		{
			tetra *current = tetra_list.front();
			tetra_list.pop();
			
			for (int i = 0; i < 3; i++)
			{
				tetra *adjacent = current->get_gluedto(i);
				if (adjacent->get_temp() == -1)
				{
					adjacent->set_temp(theoretical_num_cusps);
					tetra_list.push(adjacent);
				}
			}
		}
		theoretical_num_cusps++;
	}
	
	num_cusps = 0;  // The actual number of cusps that are toroidal. Klein bottles aren't possible as our manifolds are orientable.
	
	// Work on each cusp in turn.
	for (int cusp = 0; cusp < theoretical_num_cusps; cusp++)
	{
		std::vector<tetra *> cusp_tetra;  // All the tetra in this cusp.
		for (tetra *current = capoff_tetra; current != NULL; current = current->get_next())
			if (current->get_temp() == cusp) cusp_tetra.push_back(current);
		
		int num_faces = (int) cusp_tetra.size();
		
		// We've capped off, so the link of any point must be a closed surface.
		// Since Euler characteristic = V - E + F = V - (F/2), F should be even.
		if (num_faces % 2 != 0)
			output_error("Bad vertex link detected. Possible cause; not capping off before computing cusp information.");
		
		int num_vertices = num_faces * 3;
		std::vector<int> degree_frequency(num_vertices + 1, 0);  // The frequency of each degree.
		
		for (int i = 0; i < num_faces; i++)
			for (int j = 0; j < 3; j++)
				degree_frequency[edge_degree(cusp_tetra[i], j, 3)]++;
		
		if (degree_frequency[0] != 0)
			output_error("There should be no vertices of degree 0.");
		
		int Euler_characteristic = -(num_faces / 2);  // Now we work out the Euler characteristic of this cusp.
		
		for (int i = 1; i < num_vertices + 1; i++)
		{
			if (degree_frequency[i] % i != 0)
				output_error("Bad vertex link detected. A vertex of degree k should appear a multiple of k times.");
			
			Euler_characteristic += (degree_frequency[i] / i);
		}
		
		// Since the manifold is orientable, Euler characteristic must be even and <= 2.
		if ((Euler_characteristic > 2) || (Euler_characteristic % 2 != 0))
			output_error("Invalid cusp Euler characteristic.");
		
		// We could install the Euler characteristic to the cusp, but we wont need it again.
		int cusp_number = -1;  // The cusp number we're going to assign to this cusp.
		if (Euler_characteristic == 2)  // Spherical cusp.
		{
			if (this->get_manifold_type() == bundle) output_error("Spherical cusps are not possible in bundles.");
		}
		else if (Euler_characteristic == 0)  // Torus cusp.
		{
			cusp_number = num_cusps++;
		}
		else  // Other cusp.
		{
			output_warning("Non-torus Cusp.");  // Non-torus cusps. SnapPy wont like this.
		}
		
		// Write the real cusp number into the cusp tetra.
		for (int i = 0; i < num_faces; i++)
			cusp_tetra[i]->set_cusp_number(cusp_number);
	}
	
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rsi, %r15
movq %rdi, 0x28(%rsp)
leaq 0x178(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x5a8c(%rip), %rsi      # 0x176e2
leaq 0x5a8a(%rip), %rdx      # 0x176e7
leaq 0x168(%rsp), %rbx
movq %rbx, %rdi
callq 0x892e
movq %rbx, %rdi
movl $0x2, %esi
callq 0x4229
movq 0x168(%rsp), %rdi
cmpq %r14, %rdi
je 0x11c97
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x3220
testq %r15, %r15
je 0x1228b
movq %r15, %rax
movl $0xffffffff, 0xb8(%rax) # imm = 0xFFFFFFFF
movq (%rax), %rax
testq %rax, %rax
jne 0x11ca3
movq %r15, 0x10(%rsp)
testq %r15, %r15
je 0x1228b
movq %r15, %rax
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbx
leaq 0x10(%rsp), %r13
leaq 0x88(%rsp), %r12
movq %rax, 0x158(%rsp)
cmpl $-0x1, 0xb8(%rax)
jne 0x11e25
movl %r15d, 0xb8(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x13fce
movq 0x60(%rsp), %rax
movq 0x70(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x11d48
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq 0x60(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x60(%rsp)
jmp 0x11d58
movq %rbx, %rdi
movq %r13, %rsi
callq 0x14144
movq 0x60(%rsp), %rcx
movq 0x40(%rsp), %rax
cmpq %rax, %rcx
je 0x11e15
movq (%rax), %r14
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x11d7d
addq $0x8, %rax
jmp 0x11daf
movq 0x48(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x3220
movq 0x58(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x58(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x48(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
movq 0x10(%r14,%rbp,8), %rax
movq %rax, 0x88(%rsp)
cmpl $-0x1, 0xb8(%rax)
jne 0x11dfc
movl %r15d, 0xb8(%rax)
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11df1
movq %rax, (%rcx)
addq $0x8, 0x60(%rsp)
jmp 0x11dfc
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14144
incq %rbp
cmpq $0x3, %rbp
jne 0x11db6
movq 0x40(%rsp), %rax
cmpq %rax, 0x60(%rsp)
jne 0x11d66
incl %r15d
movq %rbx, %rdi
callq 0x13b9c
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x11ce3
movq 0x28(%rsp), %rax
movl $0x0, 0x3c(%rax)
testl %r15d, %r15d
jle 0x12297
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbx
leaq 0x30(%rsp), %rbp
leaq 0x98(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x158(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rcx, %rax
testq %rcx, %rcx
je 0x11f33
cmpl %r12d, 0xb8(%rax)
jne 0x11eb5
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x11eaa
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0x11eb5
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x14358
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x11e8a
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
movq %rax, %rbp
shrq $0x3, %rbp
btl $0x3, %eax
jae 0x11f35
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %r14, %rdi
leaq 0x57f0(%rip), %rsi      # 0x176e8
leaq 0x5845(%rip), %rdx      # 0x17744
callq 0x892e
movq %r14, %rdi
callq 0x43b0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x11f35
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3220
jmp 0x11f35
xorl %ebp, %ebp
leal 0x1(%rbp,%rbp,2), %eax
movslq %eax, %rsi
movl $0x0, 0x88(%rsp)
movq %rbx, %rdi
leaq 0x88(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0x85f6
movl %ebp, %r14d
testl %ebp, %ebp
jle 0x11f99
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
movq (%rax,%r13,8), %rdi
movl %ebx, %esi
movl $0x3, %edx
callq 0x11bad
movslq %eax, %rcx
movq 0x10(%rsp), %rax
incl (%rax,%rcx,4)
incl %ebx
cmpl $0x3, %ebx
jne 0x11f68
incq %r13
cmpq %r14, %r13
jne 0x11f66
jmp 0x11f9e
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
je 0x11ffe
leaq 0x148(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x138(%rsp), %rbx
movq %rbx, %rdi
leaq 0x5780(%rip), %rsi      # 0x17745
leaq 0x57a1(%rip), %rdx      # 0x1776d
callq 0x892e
movq %rbx, %rdi
callq 0x43b0
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x11ffe
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x3220
movq %r12, 0x90(%rsp)
movl %ebp, %ebx
shrl $0x1f, %ebx
addl %ebp, %ebx
sarl %ebx
negl %ebx
testl %ebp, %ebp
movq %rbp, 0x160(%rsp)
jle 0x120b0
leal (%rbp,%rbp,2), %ebp
incl %ebp
movl $0x1, %r13d
movq 0x10(%rsp), %rcx
movl (%rcx,%r13,4), %eax
cltd
idivl %r13d
testl %edx, %edx
je 0x12099
leaq 0x128(%rsp), %rax
movq %rax, 0x118(%rsp)
leaq 0x118(%rsp), %r12
movq %r12, %rdi
leaq 0x570e(%rip), %rsi      # 0x1776e
leaq 0x575a(%rip), %rdx      # 0x177c1
callq 0x892e
movq %r12, %rdi
callq 0x43b0
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x12099
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x10(%rsp), %rcx
movl (%rcx,%r13,4), %eax
cltd
idivl %r13d
addl %eax, %ebx
incq %r13
cmpq %r13, %rbp
jne 0x12032
cmpl $0x3, %ebx
setl %al
testb $0x1, %bl
sete %cl
testb %cl, %al
leaq 0xd8(%rsp), %r13
leaq 0xf8(%rsp), %rbp
jne 0x12123
leaq 0x108(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq %rbp, %rdi
leaq 0x56d8(%rip), %rsi      # 0x177c2
leaq 0x56f3(%rip), %rdx      # 0x177e4
callq 0x892e
movq %rbp, %rdi
callq 0x43b0
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x12123
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x3220
testl %ebx, %ebx
je 0x121a2
cmpl $0x2, %ebx
movq 0x90(%rsp), %r12
leaq 0x30(%rsp), %rbp
jne 0x121bf
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x1221f
leaq 0xe8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq %r13, %rdi
leaq 0x567a(%rip), %rsi      # 0x177e5
leaq 0x569f(%rip), %rdx      # 0x17811
callq 0x892e
movq %r13, %rdi
callq 0x43b0
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x1221f
movq 0xe8(%rsp), %rsi
jmp 0x12217
movq 0x28(%rsp), %rcx
movl 0x3c(%rcx), %ebx
leal 0x1(%rbx), %eax
movl %eax, 0x3c(%rcx)
movq 0x90(%rsp), %r12
leaq 0x30(%rsp), %rbp
jmp 0x1221f
leaq 0xc8(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x5631(%rip), %rsi      # 0x17812
leaq 0x5639(%rip), %rdx      # 0x17821
callq 0x892e
movq %rbx, %rdi
callq 0x42a5
movq 0xb8(%rsp), %rdi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x1221f
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x3220
cmpl $0x0, 0x160(%rsp)
jle 0x12242
movq 0x30(%rsp), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movl %ebx, 0xb4(%rdx)
incq %rcx
cmpq %rcx, %r14
jne 0x12230
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x12259
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
leaq 0x10(%rsp), %rbx
leaq 0x98(%rsp), %r14
je 0x1227d
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
incl %r12d
cmpl %r15d, %r12d
jne 0x11e60
jmp 0x12297
movq 0x28(%rsp), %rax
movl $0x0, 0x3c(%rax)
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x168(%rsp), %rdi
cmpq %r14, %rdi
je 0x1240f
movq 0x178(%rsp), %rsi
incq %rsi
jmp 0x123fb
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x123d2
movq 0xe8(%rsp), %rsi
jmp 0x123ca
jmp 0x123a5
jmp 0x12402
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x123e9
movq 0xa8(%rsp), %rsi
incq %rsi
jmp 0x123e4
jmp 0x123a0
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x123d2
movq 0xc8(%rsp), %rsi
jmp 0x123ca
jmp 0x123a5
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x123d2
movq 0x108(%rsp), %rsi
jmp 0x123ca
jmp 0x123a5
jmp 0x123a5
movq %rax, %rbx
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x123d2
movq 0x148(%rsp), %rsi
jmp 0x123ca
jmp 0x123a0
movq %rax, %rbx
jmp 0x123e9
movq %rax, %rbx
jmp 0x123d2
movq %rax, %rbx
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x123d2
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x123e9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x1240f
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
jmp 0x1240f
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x13b9c
movq %rbx, %rdi
callq 0x33e0