// File: MarkCBell[P]twister/./twister/kernel/twister.cpp
// Function: manifold::insert_layer()
void manifold::insert_layer()
{
	output_debugging("insert_layer", 2);
	
	if (!last_tetra)  // Empty manifold?
		return;
	
	// Reset the layer flags on cubes and marked points.
	for (int i = 0; i < int(marked_points.size()); i++)
		marked_points[i] = standard;
	
	for (int i = 0; i < int(cubes.size()); i++)
		cubes[i]->set_status(true);
	
	num_layers++;
	
	// Duplicate layer #0.
	for (int i = 0; i < int(cubes.size()); i++)
	{
		cube *C = cubes[i];
		cube *C2 = new cube(*this, num_layers, C);
		
		C->get_tetra(top, left)->subbedby(C2->get_tetra(top, left), 1, perm(0,1,2,3));
		C->get_tetra(top, right)->subbedby(C2->get_tetra(top, right), 2, perm(0,1,2,3));
		
		C->get_tetra(top, left)->gluesym(C2->get_tetra(low, left), 1, perm(0,3,2,1));
		C->get_tetra(top, right)->gluesym(C2->get_tetra(low, right), 2, perm(0,1,3,2));
		
		delete C2;
	}
	
	// Glue the tetra in layer #L together and subbedby the top most faces in.
	for (tetra *current = last_tetra; current->get_layer() == num_layers; current = current->get_prev())
		for (int i = 0; i < 4; i++)
			if (current->get_gluedto(i) == NULL)
			{
				tetra *myparent = current->get_parent();
				tetra *myuncle = myparent->get_gluedto(i);
				
				if ((myuncle != NULL) && (myuncle->get_layer() == 0))
				{
					tetra *mycousin = myuncle->get_child();
					if (mycousin != NULL) 
						current->gluesym(mycousin, i, myparent->get_gluing(i));
				}
			}
	
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x29(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x3560
movq -0xd8(%rbp), %rdx
leaq 0xbffe(%rip), %rsi      # 0x277a6
leaq -0x28(%rbp), %rdi
callq 0x4e10
jmp 0x1b7b3
leaq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0x4c10
jmp 0x1b7c3
leaq -0x28(%rbp), %rdi
callq 0x35f8
leaq -0x29(%rbp), %rdi
callq 0x3360
movq -0xe0(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1b819
jmp 0x1bca9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b80b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x35f8
leaq -0x29(%rbp), %rdi
callq 0x3360
jmp 0x1bcb2
movl $0x0, -0x40(%rbp)
movq -0xe0(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, -0xe4(%rbp)
addq $0x58, %rdi
callq 0x9ad0
movq %rax, %rcx
movl -0xe4(%rbp), %eax
cmpl %ecx, %eax
jge 0x1b86b
movq -0xe0(%rbp), %rdi
addq $0x58, %rdi
movslq -0x40(%rbp), %rsi
callq 0x9af0
movl $0x0, (%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1b820
movl $0x0, -0x44(%rbp)
movq -0xe0(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, -0xe8(%rbp)
addq $0x40, %rdi
callq 0x9b10
movq %rax, %rcx
movl -0xe8(%rbp), %eax
cmpl %ecx, %eax
jge 0x1b8c4
movq -0xe0(%rbp), %rdi
addq $0x40, %rdi
movslq -0x44(%rbp), %rsi
callq 0x8d20
movq (%rax), %rdi
movl $0x1, %esi
callq 0x9b30
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1b872
movq -0xe0(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movl $0x0, -0x48(%rbp)
movq -0xe0(%rbp), %rdi
movl -0x48(%rbp), %eax
movl %eax, -0xec(%rbp)
addq $0x40, %rdi
callq 0x9b10
movq %rax, %rcx
movl -0xec(%rbp), %eax
cmpl %ecx, %eax
jge 0x1bb45
movq -0xe0(%rbp), %rdi
addq $0x40, %rdi
movslq -0x48(%rbp), %rsi
callq 0x8d20
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x40, %edi
callq 0x3300
movq -0xe0(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf8(%rbp)
movl 0x38(%rsi), %edx
movq -0x50(%rbp), %rcx
callq 0x179a0
jmp 0x1b94c
movq -0xf8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x20bc0
movq %rax, -0x140(%rbp)
movq -0x58(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x20bc0
movq %rax, -0x138(%rbp)
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0x2, %ecx
movl $0x3, %r8d
callq 0x9cc0
movq -0x140(%rbp), %rdi
movq -0x138(%rbp), %rsi
movl $0x1, %edx
leaq -0x68(%rbp), %rcx
callq 0x17650
movq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x20bc0
movq %rax, -0x130(%rbp)
movq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x20bc0
movq %rax, -0x128(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0x2, %ecx
movl $0x3, %r8d
callq 0x9cc0
movq -0x130(%rbp), %rdi
movq -0x128(%rbp), %rsi
movl $0x2, %edx
leaq -0x78(%rbp), %rcx
callq 0x17650
movq -0x50(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x20bc0
movq %rax, -0x120(%rbp)
movq -0x58(%rbp), %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x20bc0
movq %rax, -0x118(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x2, %ecx
movl $0x1, %r8d
callq 0x9cc0
movq -0x120(%rbp), %rdi
movq -0x118(%rbp), %rsi
movl $0x1, %edx
leaq -0x88(%rbp), %rcx
callq 0x17390
movq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x20bc0
movq %rax, -0x110(%rbp)
movq -0x58(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x20bc0
movq %rax, -0x108(%rbp)
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0x3, %ecx
movl $0x2, %r8d
callq 0x9cc0
movq -0x110(%rbp), %rdi
movq -0x108(%rbp), %rsi
movl $0x2, %edx
leaq -0x98(%rbp), %rcx
callq 0x17390
movq -0x58(%rbp), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0x1bb13
movq -0x100(%rbp), %rdi
movl $0x40, %esi
callq 0x3310
jmp 0x1bb15
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1b8db
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x40, %esi
callq 0x3310
jmp 0x1bcb2
movq -0xe0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x211e0
movq -0xe0(%rbp), %rcx
cmpl 0x38(%rcx), %eax
jne 0x1bca7
movl $0x0, -0xa4(%rbp)
cmpl $0x4, -0xa4(%rbp)
jge 0x1bc8d
movq -0xa0(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x20b60
cmpq $0x0, %rax
jne 0x1bc77
movq -0xa0(%rbp), %rdi
callq 0x215c0
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x20b60
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x1bc75
movq -0xb8(%rbp), %rdi
callq 0x211e0
cmpl $0x0, %eax
jne 0x1bc75
movq -0xb8(%rbp), %rdi
callq 0x20e60
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x1bc73
movq -0xa0(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x150(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0x144(%rbp)
movq -0xb0(%rbp), %rsi
movl -0xa4(%rbp), %edx
leaq -0xd0(%rbp), %rdi
callq 0x20b80
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rsi
movl -0x144(%rbp), %edx
leaq -0xd0(%rbp), %rcx
callq 0x17390
jmp 0x1bc75
jmp 0x1bc77
jmp 0x1bc79
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x1bb7d
jmp 0x1bc8f
movq -0xa0(%rbp), %rdi
callq 0x212e0
movq %rax, -0xa0(%rbp)
jmp 0x1bb57
jmp 0x1bca9
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x3550
nopl (%rax,%rax)