// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x3560
movq -0xc0(%rbp), %rdx
leaq 0x9def(%rip), %rsi      # 0x27617
leaq -0x30(%rbp), %rdi
callq 0x4e10
jmp 0x1d833
leaq -0x30(%rbp), %rdi
movl $0x2, %esi
callq 0x4c10
jmp 0x1d843
leaq -0x30(%rbp), %rdi
callq 0x35f8
leaq -0x31(%rbp), %rdi
callq 0x3360
movq -0xc8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1d8a1
movq $0x0, -0x8(%rbp)
jmp 0x1db26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1d893
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x35f8
leaq -0x31(%rbp), %rdi
callq 0x3360
jmp 0x1db33
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
leaq -0x90(%rbp), %rdi
movl $0x3, %esi
movl $0x1, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x9cc0
leaq -0x90(%rbp), %rdi
addq $0x10, %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x2, %ecx
movl $0x1, %r8d
callq 0x9cc0
leaq -0x90(%rbp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0x3, %ecx
movl $0x2, %r8d
callq 0x9cc0
leaq -0x90(%rbp), %rdi
addq $0x30, %rdi
xorl %esi, %esi
movl $0x2, %edx
movl $0x1, %ecx
movl $0x3, %r8d
callq 0x9cc0
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
je 0x1da98
movl $0x0, -0x9c(%rbp)
cmpl $0x4, -0x9c(%rbp)
jge 0x1da7e
movq -0x98(%rbp), %rdi
movl -0x9c(%rbp), %esi
callq 0x20b60
cmpq $0x0, %rax
je 0x1d988
jmp 0x1da48
movl $0xc0, %edi
callq 0x3300
movq %rax, -0xd8(%rbp)
movq -0x98(%rbp), %rdi
callq 0x21320
movl %eax, -0xcc(%rbp)
jmp 0x1d9ad
movq -0x98(%rbp), %rdi
callq 0x21340
movl %eax, -0xdc(%rbp)
jmp 0x1d9c1
movq -0x98(%rbp), %rdi
callq 0x211e0
movl %eax, -0xe0(%rbp)
jmp 0x1d9d5
movl -0xe0(%rbp), %r9d
movl -0xdc(%rbp), %r8d
movl -0xcc(%rbp), %ecx
movq -0xc8(%rbp), %rsi
movq -0xd8(%rbp), %rdi
movq %rsp, %rax
movq $0x0, (%rax)
movl $0x3, %edx
callq 0x17070
jmp 0x1da0d
movq -0xd8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x98(%rbp), %rsi
movslq -0x9c(%rbp), %rax
leaq -0x90(%rbp), %rcx
shlq $0x4, %rax
addq %rax, %rcx
movl $0x3, %edx
callq 0x17390
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x1d95e
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0xc0, %esi
callq 0x3310
jmp 0x1db33
jmp 0x1da80
movq -0x98(%rbp), %rdi
callq 0x212e0
movq %rax, -0x98(%rbp)
jmp 0x1d946
movq -0x50(%rbp), %rdi
callq 0x21300
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x1db19
movl $0x0, -0xb4(%rbp)
cmpl $0x3, -0xb4(%rbp)
jge 0x1db02
movq -0xb0(%rbp), %rdi
movl -0xb4(%rbp), %esi
callq 0x20b60
cmpq $0x0, %rax
je 0x1dadf
jmp 0x1daf1
movq -0xb0(%rbp), %rdi
movl -0xb4(%rbp), %esi
callq 0x177f0
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x1dabc
jmp 0x1db04
movq -0xb0(%rbp), %rdi
callq 0x21300
movq %rax, -0xb0(%rbp)
jmp 0x1daa8
movq -0x50(%rbp), %rdi
callq 0x21300
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3550
nopl (%rax)