// File: /workspace/github_cpp_makefile_star2_O3/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 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5cd6(%rip), %rsi      # 0x176db
leaq 0x5cd5(%rip), %rdx      # 0x176e1
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x892e
movq %r14, %rdi
movl $0x2, %esi
callq 0x4229
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x11a3d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3220
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x11b6d
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movl $0x3, %esi
movl $0x1, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x8860
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x2, %ecx
movl $0x1, %r8d
callq 0x8860
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0x3, %ecx
movl $0x2, %r8d
callq 0x8860
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0x2, %edx
movl $0x1, %ecx
movl $0x3, %r8d
callq 0x8860
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x11b34
movl $0x2, %ebp
movq %r14, %r12
cmpq $0x0, (%r15,%rbp,8)
jne 0x11b21
movl $0xc0, %edi
callq 0x3210
movq %rax, %r13
movl 0x74(%r15), %ecx
movl 0x78(%r15), %r8d
movl 0x90(%r15), %r9d
movq $0x0, (%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0xde5c
movq %r13, %rdi
movq %r15, %rsi
movl $0x3, %edx
movq %r12, %rcx
callq 0xe04e
addq $0x10, %r12
incq %rbp
cmpq $0x6, %rbp
jne 0x11ad3
movq 0x8(%r15), %r15
jmp 0x11ac6
movq 0x8(%rsp), %r15
movq (%r15), %rbx
testq %rbx, %rbx
je 0x11b6d
xorl %r14d, %r14d
cmpq $0x0, 0x10(%rbx,%r14,8)
jne 0x11b57
movq %rbx, %rdi
movl %r14d, %esi
callq 0xe318
incq %r14
cmpq $0x3, %r14
jne 0x11b44
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x11b41
movq (%r15), %rax
jmp 0x11b6f
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x11ba5
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x11ba0
movq %rax, %rbx
movl $0xc0, %esi
movq %r13, %rdi
callq 0x3220
movq %rbx, %rdi
callq 0x33e0