// File: /workspace/github_cpp_makefile_star2_O1/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 $0x178, %rsp            # imm = 0x178
movq %rsi, %rbx
movq %rdi, 0x20(%rsp)
leaq 0x168(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5b60(%rip), %rsi      # 0x176a2
leaq 0x5b5e(%rip), %rdx      # 0x176a7
leaq 0x158(%rsp), %r14
movq %r14, %rdi
callq 0x86fe
movq %r14, %rdi
movl $0x2, %esi
callq 0x4209
movq 0x158(%rsp), %rdi
cmpq %r15, %rdi
je 0x11b83
movq 0x168(%rsp), %rsi
incq %rsi
callq 0x3220
testq %rbx, %rbx
je 0x11b9d
movq %rbx, %rax
movl $0xffffffff, 0xb8(%rax) # imm = 0xFFFFFFFF
movq (%rax), %rax
testq %rax, %rax
jne 0x11b8b
movq %rbx, 0x8(%rsp)
testq %rbx, %rbx
movq %rbx, 0x148(%rsp)
je 0x11d03
movq %rbx, %rax
xorl %ebx, %ebx
leaq 0x30(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x28(%rsp), %r12
cmpl $-0x1, 0xb8(%rax)
jne 0x11ceb
movl %ebx, 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 %r14, %rdi
xorl %esi, %esi
callq 0x13f08
movq 0x60(%rsp), %rax
movq 0x70(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x11c26
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x60(%rsp)
jmp 0x11cd1
movq %r14, %rdi
movq %r15, %rsi
callq 0x1407e
jmp 0x11cd1
movq (%rax), %r13
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x11c4d
addq $0x8, %rax
jmp 0x11c7f
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(%r13,%rbp,8), %rax
movq %rax, 0x28(%rsp)
cmpl $-0x1, 0xb8(%rax)
jne 0x11cc8
movl %ebx, 0xb8(%rax)
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x11cbd
movq %rax, (%rcx)
addq $0x8, 0x60(%rsp)
jmp 0x11cc8
movq %r14, %rdi
movq %r12, %rsi
callq 0x1407e
incq %rbp
cmpq $0x3, %rbp
jne 0x11c86
movq 0x40(%rsp), %rax
cmpq %rax, 0x60(%rsp)
jne 0x11c36
incl %ebx
movq %r14, %rdi
callq 0x13ac8
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x11bc7
jmp 0x11d05
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movl $0x0, 0x3c(%rax)
testl %ebx, %ebx
jle 0x12153
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
leaq 0x30(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x148(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %rcx, %rax
testq %rcx, %rcx
je 0x11d89
cmpl %r15d, 0xb8(%rax)
jne 0x11d77
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x11d6c
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0x11d77
movq %r12, %rdi
movq %r14, %rdx
callq 0x14292
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x11d4c
movq %r15, 0x150(%rsp)
movq 0x38(%rsp), %r13
subq 0x30(%rsp), %r13
btl $0x3, %r13d
jae 0x11dfd
leaq 0x138(%rsp), %rax
movq %rax, 0x128(%rsp)
leaq 0x128(%rsp), %r15
movq %r15, %rdi
leaq 0x58e4(%rip), %rsi      # 0x176a8
leaq 0x5939(%rip), %rdx      # 0x17704
callq 0x86fe
movq %r15, %rdi
callq 0x4397
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x11dfd
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x3220
shrq $0x3, %r13
leal 0x1(%r13,%r13,2), %eax
movslq %eax, %rsi
movl $0x0, 0x28(%rsp)
movq %r14, %rdi
leaq 0x28(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x84d2
testl %r13d, %r13d
jle 0x11e68
movl %r13d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x30(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movl %r14d, %esi
movl $0x3, %edx
callq 0x11a96
cltq
movq 0x8(%rsp), %rcx
incl (%rcx,%rax,4)
incl %r14d
cmpl $0x3, %r14d
jne 0x11e37
incq %rbp
cmpq %r15, %rbp
jne 0x11e34
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax)
je 0x11ecd
leaq 0x118(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x108(%rsp), %r14
movq %r14, %rdi
leaq 0x5871(%rip), %rsi      # 0x17705
leaq 0x5892(%rip), %rdx      # 0x1772d
callq 0x86fe
movq %r14, %rdi
callq 0x4397
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x11ecd
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x3220
movl %r13d, %r14d
shrl $0x1f, %r14d
addl %r13d, %r14d
sarl %r14d
negl %r14d
testl %r13d, %r13d
jle 0x11f8c
leal (%r13,%r13,2), %r15d
cmpl $0x2, %r15d
movl $0x1, %eax
cmovll %eax, %r15d
negq %r15
movl $0x1, %ebp
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %eax
cltd
idivl %ebp
testl %edx, %edx
je 0x11f6a
leaq 0xf8(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
leaq 0x57fd(%rip), %rsi      # 0x1772e
leaq 0x5849(%rip), %rdx      # 0x17781
callq 0x86fe
movq %r12, %rdi
callq 0x4397
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x11f6a
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x8(%rsp), %rax
movl (%rax,%rbp,4), %eax
cltd
idivl %ebp
addl %eax, %r14d
leaq (%r15,%rbp), %rax
incq %rax
incq %rbp
cmpq $0x1, %rax
jne 0x11f00
cmpl $0x3, %r14d
setl %al
testb $0x1, %r14b
sete %cl
testb %cl, %al
leaq 0xa8(%rsp), %r15
leaq 0xc8(%rsp), %rbp
jne 0x12001
leaq 0xd8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq %rbp, %rdi
leaq 0x57ba(%rip), %rsi      # 0x17782
leaq 0x57d5(%rip), %rdx      # 0x177a4
callq 0x86fe
movq %rbp, %rdi
callq 0x4397
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x12001
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x3220
testl %r14d, %r14d
je 0x1206e
cmpl $0x2, %r14d
jne 0x12080
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x120e1
leaq 0xb8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq %r15, %rdi
leaq 0x576a(%rip), %rsi      # 0x177a5
leaq 0x578f(%rip), %rdx      # 0x177d1
callq 0x86fe
movq %r15, %rdi
callq 0x4397
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x120e1
movq 0xb8(%rsp), %rsi
jmp 0x120d9
movq 0x20(%rsp), %rcx
movl 0x3c(%rcx), %r14d
leal 0x1(%r14), %eax
movl %eax, 0x3c(%rcx)
jmp 0x120e1
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %r14
movq %r14, %rdi
leaq 0x5730(%rip), %rsi      # 0x177d2
leaq 0x5738(%rip), %rdx      # 0x177e1
callq 0x86fe
movq %r14, %rdi
callq 0x4285
movq 0x88(%rsp), %rdi
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x120e1
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3220
testl %r13d, %r13d
jle 0x12107
movq 0x30(%rsp), %rax
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
movl %r14d, 0xb4(%rdx)
incq %rcx
cmpq %rcx, %r13
jne 0x120f4
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1211e
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
leaq 0x8(%rsp), %r14
movq 0x150(%rsp), %r15
leaq 0x30(%rsp), %r12
je 0x12147
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
incl %r15d
cmpl %ebx, %r15d
jne 0x11d26
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x158(%rsp), %rdi
cmpq %r15, %rdi
je 0x122cf
movq 0x168(%rsp), %rsi
incq %rsi
jmp 0x122bb
jmp 0x122c2
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x12292
movq 0xb8(%rsp), %rsi
jmp 0x1228a
jmp 0x12265
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x12292
movq 0x98(%rsp), %rsi
jmp 0x1228a
jmp 0x12265
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x12292
movq 0xd8(%rsp), %rsi
jmp 0x1228a
movq %rax, %rbx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x122a9
movq 0x138(%rsp), %rsi
incq %rsi
jmp 0x122a4
jmp 0x12265
jmp 0x12260
jmp 0x12265
movq %rax, %rbx
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x12292
movq 0x118(%rsp), %rsi
jmp 0x1228a
jmp 0x12260
movq %rax, %rbx
jmp 0x122a9
movq %rax, %rbx
jmp 0x12292
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x12292
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x122a9
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x122cf
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3220
jmp 0x122cf
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x13ac8
movq %rbx, %rdi
callq 0x33e0