// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x3b6a(%rip), %rsi      # 0x1163a
leaq 0xc8(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x3e82
leaq 0xc8(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x3d1a
leaq 0xc8(%rsp), %rdi
callq 0x3458
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0xdb15
orl $-0x1, 0xb8(%rax)
movq (%rax), %rax
jmp 0xdb04
xorl %r13d, %r13d
leaq 0x50(%rsp), %rbx
leaq 0x28(%rsp), %r14
leaq 0x8(%rsp), %r15
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xdbc1
cmpl $-0x1, 0xb8(%rax)
jne 0xdbb9
movl %r13d, 0xb8(%rax)
movq %rbx, %rdi
callq 0xf11e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf85e
movq 0x60(%rsp), %rax
cmpq %rax, 0x80(%rsp)
je 0xdba9
movq (%rax), %r12
movq %rbx, %rdi
callq 0xfa92
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0xdb5d
movq 0x10(%r12,%rbp,8), %rax
movq %rax, 0x8(%rsp)
cmpl $-0x1, 0xb8(%rax)
jne 0xdba4
movl %r13d, 0xb8(%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xf85e
incq %rbp
jmp 0xdb79
incl %r13d
movq %rbx, %rdi
callq 0xf158
movq 0x28(%rsp), %rax
movq (%rax), %rax
jmp 0xdb2c
movq 0x18(%rsp), %rax
andl $0x0, 0x3c(%rax)
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
leaq 0x28(%rsp), %r14
leaq 0x50(%rsp), %r15
pushq $0x3
popq %rbx
leaq 0xa8(%rsp), %rbp
xorl %eax, %eax
cmpl %r13d, %eax
je 0xdeb9
movl %eax, 0x14(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
andq $0x0, 0x60(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xdc35
movl 0x14(%rsp), %ecx
cmpl %ecx, 0xb8(%rax)
jne 0xdc30
movq %r15, %rdi
movq %r14, %rsi
callq 0xf13a
movq 0x28(%rsp), %rax
movq (%rax), %rax
jmp 0xdc0a
movq 0x58(%rsp), %r15
subq 0x50(%rsp), %r15
btl $0x3, %r15d
jae 0xdc68
movq %rbp, %rdi
leaq 0x39f0(%rip), %rsi      # 0x11640
movq %r14, %rdx
callq 0x3e82
movq %rbp, %rdi
callq 0x3dec
movq %rbp, %rdi
callq 0x3458
shrq $0x3, %r15
leal (%r15,%r15,2), %eax
incl %eax
movslq %eax, %rsi
andl $0x0, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rdx
leaq 0x13(%rsp), %rcx
callq 0x666c
leal (%r15,%r15,2), %eax
movq %rax, 0x48(%rsp)
testl %r15d, %r15d
movl $0x0, %r14d
movq %r15, %r12
cmovgl %r15d, %r14d
xorl %ebp, %ebp
cmpq %r14, %rbp
je 0xdcdc
xorl %r15d, %r15d
cmpl $0x3, %r15d
je 0xdcd7
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0xda1d
cltq
movq 0x28(%rsp), %rcx
incl (%rcx,%rax,4)
incl %r15d
jmp 0xdcaf
incq %rbp
jmp 0xdca7
movq 0x28(%rsp), %rax
cmpl $0x0, (%rax)
je 0xdd12
leaq 0x168(%rsp), %r15
movq %r15, %rdi
leaq 0x39a5(%rip), %rsi      # 0x1169d
leaq 0x8(%rsp), %rdx
callq 0x3e82
movq %r15, %rdi
callq 0x3dec
movq %r15, %rdi
callq 0x3458
movq %r14, 0x40(%rsp)
movl %r12d, %eax
cltd
pushq $-0x2
popq %rcx
idivl %ecx
movl %eax, %ebp
movq 0x48(%rsp), %r14
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
negq %r14
pushq $0x1
popq %r15
leaq (%r14,%r15), %rax
cmpq $0x1, %rax
je 0xdd99
movq 0x28(%rsp), %rax
movl (%rax,%r15,4), %ecx
movl %ecx, %eax
cltd
idivl %r15d
testl %edx, %edx
je 0xdd8c
leaq 0x148(%rsp), %r12
movq %r12, %rdi
leaq 0x395d(%rip), %rsi      # 0x116c6
leaq 0x8(%rsp), %rdx
callq 0x3e82
movq %r12, %rdi
callq 0x3dec
movq %r12, %rdi
callq 0x3458
movq 0x28(%rsp), %rax
movl (%rax,%r15,4), %ecx
movl %ecx, %eax
cltd
idivl %r15d
addl %eax, %ebp
incq %r15
jmp 0xdd3a
cmpl $0x3, %ebp
setl %al
testb $0x1, %bpl
sete %cl
testb %cl, %al
movq 0x40(%rsp), %r14
jne 0xdddb
leaq 0x128(%rsp), %r15
movq %r15, %rdi
leaq 0x3959(%rip), %rsi      # 0x1171a
leaq 0x8(%rsp), %rdx
callq 0x3e82
movq %r15, %rdi
callq 0x3dec
movq %r15, %rdi
callq 0x3458
testl %ebp, %ebp
je 0xde25
cmpl $0x2, %ebp
jne 0xde42
movq 0x18(%rsp), %rax
cmpl $0x1, 0x10(%rax)
pushq $-0x1
popq %rax
leaq 0x50(%rsp), %r15
leaq 0x108(%rsp), %r12
leaq 0xa8(%rsp), %rbp
jne 0xde7e
movq %r12, %rdi
leaq 0x392c(%rip), %rsi      # 0x1173d
leaq 0x8(%rsp), %rdx
callq 0x3e82
movq %r12, %rdi
callq 0x3dec
jmp 0xde73
movq 0x18(%rsp), %rdx
movl 0x3c(%rdx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x3c(%rdx)
leaq 0x50(%rsp), %r15
leaq 0xa8(%rsp), %rbp
jmp 0xde7e
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
leaq 0x3916(%rip), %rsi      # 0x1176a
leaq 0x8(%rsp), %rdx
callq 0x3e82
leaq 0x50(%rsp), %r15
leaq 0xa8(%rsp), %rbp
movq %r12, %rdi
callq 0x3d6c
movq %r12, %rdi
callq 0x3458
pushq $-0x1
popq %rax
movq 0x50(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %r14
je 0xde99
movq (%rcx,%rdx,8), %rsi
movl %eax, 0xb4(%rsi)
incq %rdx
jmp 0xde85
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x6c0e
movq %r15, %rdi
callq 0xf264
movl 0x14(%rsp), %eax
incl %eax
jmp 0xdbea
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x3458
jmp 0xdf7b
movq %rax, %rbx
jmp 0xdf7b
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0xdf4c
jmp 0xdf64
jmp 0xdf64
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0xdf4c
jmp 0xdf55
jmp 0xdf64
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0xdf4c
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3458
jmp 0xdf71
jmp 0xdf3c
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0xdf4c
jmp 0xdf64
jmp 0xdf3c
movq %rax, %rbx
jmp 0xdf71
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x3458
jmp 0xdf67
jmp 0xdf64
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xf158
jmp 0xdf7b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x6c0e
leaq 0x50(%rsp), %rdi
callq 0xf264
movq %rbx, %rdi
callq 0x33d0