// File: /workspace/github_cpp_makefile_star2_O2/MarkCBell[P]twister/./twister/kernel/twister.cpp
// Function: manifold::capoff()
tetra *manifold::capoff()
{
	output_debugging("capoff", 2);
	
	if (!last_tetra)  // Empty manifold?
		return NULL;
	
	tetra *old_last = last_tetra;
	
	// This describes how to attach newguy to the boundary face.
	perm perm_capoff_list[4] = {perm(3,1,2,0), perm(0,3,2,1), perm(0,1,3,2), perm(0,2,1,3)};
	
	// 1) Glue an extra tetrahedron onto each free face in the manifold.
	for (tetra *current = last_tetra; current != NULL; current = current->get_prev())  // Must go backwards, otherwise loop :(
		for (int i = 0; i < 4; i++)
		{
			if (current->get_gluedto(i) != NULL)
				continue;
			
			// Capping tetra have the same properties as the tetra they are connected to.
			tetra *newguy = new tetra(this, cap, current->get_position(), current->get_side(), current->get_layer());  
			newguy->gluesym(current, 3, perm_capoff_list[i]);
		}
	
	// 2) Glue the three free faces of the tetrahedra created in 1) together using walk_about().
	for (tetra *current = old_last->get_next(); current != NULL; current = current->get_next())
		for (int i = 0; i < 3; i++)
		{
			if (current->get_gluedto(i) != NULL)
				continue;
			
			current->walk_about(i);
		}
	
	return old_last->get_next();  // Return a pointer to the first cappoff tetra, if NULL then there's no boundary.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
leaq 0x3d8d(%rip), %rsi      # 0x11633
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x3e82
leaq 0x10(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x3d1a
leaq 0x10(%rsp), %rdi
callq 0x3458
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0xd9e0
movq %rax, 0x8(%rsp)
leaq 0x8(%rbx), %r15
pushq $0x3
popq %rbp
pushq $0x1
popq %r12
pushq $0x2
popq %r13
leaq 0x30(%rsp), %rdi
movl %ebp, %esi
movl %r12d, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x6790
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl %ebp, %edx
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x6790
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl %r12d, %edx
movl %ebp, %ecx
movl %r13d, %r8d
callq 0x6790
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl %r13d, %edx
movl %r12d, %ecx
movl %ebp, %r8d
callq 0x6790
movq (%r15), %r15
testq %r15, %r15
je 0xd9aa
leaq 0x30(%rsp), %r12
pushq $0x2
popq %r14
cmpq $0x6, %r14
je 0xd9a4
cmpq $0x0, (%r15,%r14,8)
jne 0xd99b
movl $0xc0, %edi
callq 0x3240
movq %rax, %r13
movl 0x74(%r15), %ecx
movl 0x78(%r15), %r8d
movl 0x90(%r15), %r9d
andq $0x0, (%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0xa3fa
movq %r13, %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r12, %rcx
callq 0xa5d2
incq %r14
addq $0x10, %r12
jmp 0xd950
addq $0x8, %r15
jmp 0xd93f
movq 0x8(%rsp), %r15
movq %r15, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xd9db
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0xd9b2
cmpq $0x0, 0x10(%rbx,%r14,8)
jne 0xd9d6
movq %rbx, %rdi
movl %r14d, %esi
callq 0xa81a
incq %r14
jmp 0xd9bd
movq (%r15), %rax
jmp 0xd9e2
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3458
jmp 0xda15
movq %rax, %rbx
jmp 0xda15
movq %rax, %rbx
movl $0xc0, %esi
movq %r13, %rdi
callq 0x3250
movq %rbx, %rdi
callq 0x33d0