// File: MarkCBell[P]twister/./twister/kernel/twister.cpp
// Function: annulus::twohandle(bool)
void annulus::twohandle(bool is_above)
{
	output_debugging("handle", 2);
	
	manifold *M = sq[0]->home;
	
	if (M->get_manifold_type() == bundle)
		output_error("Cannot attach two-handles to a bundle.");
	
	if (M->get_num_layers() != 0)
		output_error("Handles must be attached before any twisting or drilling occurs.");
	
	tetra *far, *mid, *near;
	
	for(int i = 0; i < length; i++)
	{
		// Glue together a bunch of prisms and
		// attach them to the tops of the cubes.
		
		// construct three tetras
		far = new tetra(M, handle);
		mid = new tetra(M, handle);
		near = new tetra(M, handle);
		
		// Build the prism
		
		// XOR since up/down inverts uprightness
		far->gluesym(mid, 3, (upright[i] ^ is_above) ? perm(0,3,2,1) : perm(0,1,3,2));
		mid->gluesym(near, 3, perm(3,1,2,0));
		
		// Glue prism to the floor / ceiling
		if (is_above)
		{
			far->gluesym(sq[i]->topleft, 0, perm(1,0,2,3));
			mid->gluesym(sq[i]->topright, 0, (upright[i]) ? perm(2,1,0,3) : perm(2,0,3,1));
		}
		else
		{
			far->gluesym(sq[i]->lowleft, 0, perm(3,2,0,1));
			mid->gluesym(sq[i]->lowright, 0, (upright[i]) ? perm(3,0,1,2) : perm(3,2,0,1));
		}
	}
	
	// The next bit of code depends on the values of
	// "far", "mid", and "near" left over from the
	// end of the previous 'for' loop.
	
	tetra *farprev, *midprev, *nearprev;
	int toface = (is_above) ? 2 : 1;
	for (int i = 0; i < length; i++)
	{
		int prev = (i + length - 1) % length;
		
		// Play pass the pointer.
		farprev = far;
		midprev = mid;
		nearprev = near;
		
		far = (is_above) ? sq[i]->topleft->get_gluedto(1) : sq[i]->lowleft->get_gluedto(3);
		mid = far->get_gluedto(3);
		near = mid->get_gluedto(3);
		
		// Glue the prisms to each other.
		near->gluesym(nearprev, toface, perm(0,2,1,3));
		
		(upright[i] ? far : mid)->gluesym((upright[prev] ? midprev : farprev), toface, perm(0,2,1,3));
	}
	
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x3560
movq -0x170(%rbp), %rdx
leaq 0xdb65(%rip), %rsi      # 0x27565
leaq -0x30(%rbp), %rdi
callq 0x4e10
jmp 0x19a0b
leaq -0x30(%rbp), %rdi
movl $0x2, %esi
callq 0x4c10
jmp 0x19a1b
leaq -0x30(%rbp), %rdi
callq 0x35f8
leaq -0x31(%rbp), %rdi
callq 0x3360
movq -0x178(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x38(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x8d00
cmpl $0x1, %eax
jne 0x19aff
leaq -0x71(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x3560
movq -0x180(%rbp), %rdx
leaq 0xdaf9(%rip), %rsi      # 0x2756c
leaq -0x70(%rbp), %rdi
callq 0x4e10
jmp 0x19a7e
leaq -0x70(%rbp), %rdi
callq 0x4d40
jmp 0x19a89
leaq -0x70(%rbp), %rdi
callq 0x35f8
leaq -0x71(%rbp), %rdi
callq 0x3360
jmp 0x19aff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x19ac0
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 0x1a2a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x19af1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x35f8
leaq -0x71(%rbp), %rdi
callq 0x3360
jmp 0x1a2a1
movq -0x50(%rbp), %rdi
callq 0x20e40
cmpl $0x0, %eax
je 0x19b9f
leaq -0x99(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x3560
movq -0x188(%rbp), %rdx
leaq 0xda61(%rip), %rsi      # 0x27593
leaq -0x98(%rbp), %rdi
callq 0x4e10
jmp 0x19b40
leaq -0x98(%rbp), %rdi
callq 0x4d40
jmp 0x19b4e
leaq -0x98(%rbp), %rdi
callq 0x35f8
leaq -0x99(%rbp), %rdi
callq 0x3360
jmp 0x19b9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x19b8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x35f8
leaq -0x99(%rbp), %rdi
callq 0x3360
jmp 0x1a2a1
movl $0x0, -0xbc(%rbp)
movq -0x178(%rbp), %rcx
movl -0xbc(%rbp), %eax
cmpl (%rcx), %eax
jge 0x1a034
movl $0xc0, %edi
callq 0x3300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x190(%rbp)
movq -0x50(%rbp), %rsi
movq %rsp, %rax
movq $0x0, (%rax)
movl $0x3, %ecx
movl $0x2, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r8d, %edx
callq 0x17070
jmp 0x19bfe
movq -0x190(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl $0xc0, %edi
callq 0x3300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x198(%rbp)
movq -0x50(%rbp), %rsi
movq %rsp, %rax
movq $0x0, (%rax)
movl $0x3, %ecx
movl $0x2, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r8d, %edx
callq 0x17070
jmp 0x19c4c
movq -0x198(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0xc0, %edi
callq 0x3300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x1a0(%rbp)
movq -0x50(%rbp), %rsi
movq %rsp, %rax
movq $0x0, (%rax)
movl $0x3, %ecx
movl $0x2, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r8d, %edx
callq 0x17070
jmp 0x19c9a
movq -0x178(%rbp), %rax
movq -0x1a0(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movq -0xa8(%rbp), %rcx
movq %rcx, -0x1b0(%rbp)
movq -0xb0(%rbp), %rcx
movq %rcx, -0x1a8(%rbp)
movq 0x10(%rax), %rax
movslq -0xbc(%rbp), %rcx
movb (%rax,%rcx), %al
andb $0x1, %al
movzbl %al, %eax
movb -0x9(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
xorl %ecx, %eax
cmpl $0x0, %eax
je 0x19d0e
leaq -0xcc(%rbp), %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x2, %ecx
movl $0x1, %r8d
callq 0x9cc0
jmp 0x19d2c
leaq -0xcc(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0x3, %ecx
movl $0x2, %r8d
callq 0x9cc0
movq -0x1a8(%rbp), %rsi
movq -0x1b0(%rbp), %rdi
movl $0x3, %edx
leaq -0xcc(%rbp), %rcx
callq 0x17390
movq -0xb0(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
leaq -0xdc(%rbp), %rdi
movl $0x3, %esi
movl $0x1, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x9cc0
movq -0x1c0(%rbp), %rdi
movq -0x1b8(%rbp), %rsi
movl $0x3, %edx
leaq -0xdc(%rbp), %rcx
callq 0x17390
testb $0x1, -0x9(%rbp)
je 0x19f1b
movq -0x178(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rcx, -0x1e0(%rbp)
movq 0x8(%rax), %rax
movslq -0xbc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, -0x1d8(%rbp)
leaq -0xec(%rbp), %rdi
movl $0x1, %esi
xorl %edx, %edx
movl $0x2, %ecx
movl $0x3, %r8d
callq 0x9cc0
movq -0x1e0(%rbp), %rdi
movq -0x1d8(%rbp), %rsi
xorl %edx, %edx
leaq -0xec(%rbp), %rcx
callq 0x17390
movq -0x178(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, -0x1d0(%rbp)
movq 0x8(%rax), %rcx
movslq -0xbc(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, -0x1c8(%rbp)
movq 0x10(%rax), %rax
movslq -0xbc(%rbp), %rcx
testb $0x1, (%rax,%rcx)
je 0x19e76
leaq -0xfc(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
xorl %ecx, %ecx
movl $0x3, %r8d
callq 0x9cc0
jmp 0x19e94
leaq -0xfc(%rbp), %rdi
movl $0x2, %esi
xorl %edx, %edx
movl $0x3, %ecx
movl $0x1, %r8d
callq 0x9cc0
movq -0x1c8(%rbp), %rsi
movq -0x1d0(%rbp), %rdi
xorl %edx, %edx
leaq -0xfc(%rbp), %rcx
callq 0x17390
jmp 0x1a01e
movq -0x190(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0xc0, %esi
callq 0x3310
jmp 0x1a2a1
movq -0x198(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0xc0, %esi
callq 0x3310
jmp 0x1a2a1
movq -0x1a0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl $0xc0, %esi
callq 0x3310
jmp 0x1a2a1
movq -0x178(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rcx, -0x200(%rbp)
movq 0x8(%rax), %rax
movslq -0xbc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1f8(%rbp)
leaq -0x10c(%rbp), %rdi
movl $0x3, %esi
movl $0x2, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x9cc0
movq -0x200(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
xorl %edx, %edx
leaq -0x10c(%rbp), %rcx
callq 0x17390
movq -0x178(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq %rcx, -0x1f0(%rbp)
movq 0x8(%rax), %rcx
movslq -0xbc(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x28(%rcx), %rcx
movq %rcx, -0x1e8(%rbp)
movq 0x10(%rax), %rax
movslq -0xbc(%rbp), %rcx
testb $0x1, (%rax,%rcx)
je 0x19fe4
leaq -0x11c(%rbp), %rdi
movl $0x3, %esi
xorl %edx, %edx
movl $0x1, %ecx
movl $0x2, %r8d
callq 0x9cc0
jmp 0x1a002
leaq -0x11c(%rbp), %rdi
movl $0x3, %esi
movl $0x2, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x9cc0
movq -0x1e8(%rbp), %rsi
movq -0x1f0(%rbp), %rdi
xorl %edx, %edx
leaq -0x11c(%rbp), %rcx
callq 0x17390
jmp 0x1a020
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x19ba9
movb -0x9(%rbp), %dl
movl $0x1, %eax
movl $0x2, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x13c(%rbp)
movl $0x0, -0x140(%rbp)
movq -0x178(%rbp), %rcx
movl -0x140(%rbp), %eax
cmpl (%rcx), %eax
jge 0x1a298
movq -0x178(%rbp), %rcx
movl -0x140(%rbp), %eax
addl (%rcx), %eax
subl $0x1, %eax
cltd
idivl (%rcx)
movl %edx, -0x144(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x138(%rbp)
testb $0x1, -0x9(%rbp)
je 0x1a0e3
movq -0x178(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x140(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x20b60
movq %rax, -0x208(%rbp)
jmp 0x1a10e
movq -0x178(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x140(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rdi
movl $0x3, %esi
callq 0x20b60
movq %rax, -0x208(%rbp)
movq -0x208(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movl $0x3, %esi
callq 0x20b60
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
movl $0x3, %esi
callq 0x20b60
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x220(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x218(%rbp)
movl -0x13c(%rbp), %eax
movl %eax, -0x20c(%rbp)
leaq -0x154(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
movl $0x1, %ecx
movl $0x3, %r8d
callq 0x9cc0
movq -0x220(%rbp), %rdi
movq -0x218(%rbp), %rsi
movl -0x20c(%rbp), %edx
leaq -0x154(%rbp), %rcx
callq 0x17390
movq -0x178(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x140(%rbp), %rcx
testb $0x1, (%rax,%rcx)
je 0x1a1da
movq -0xa8(%rbp), %rax
movq %rax, -0x228(%rbp)
jmp 0x1a1e8
movq -0xb0(%rbp), %rax
movq %rax, -0x228(%rbp)
movq -0x178(%rbp), %rax
movq -0x228(%rbp), %rcx
movq %rcx, -0x230(%rbp)
movq 0x10(%rax), %rax
movslq -0x144(%rbp), %rcx
testb $0x1, (%rax,%rcx)
je 0x1a21e
movq -0x130(%rbp), %rax
movq %rax, -0x238(%rbp)
jmp 0x1a22c
movq -0x128(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq %rax, -0x248(%rbp)
movl -0x13c(%rbp), %eax
movl %eax, -0x23c(%rbp)
leaq -0x164(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
movl $0x1, %ecx
movl $0x3, %r8d
callq 0x9cc0
movq -0x230(%rbp), %rdi
movq -0x248(%rbp), %rsi
movl -0x23c(%rbp), %edx
leaq -0x164(%rbp), %rcx
callq 0x17390
movl -0x140(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x140(%rbp)
jmp 0x1a057
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3550
nopw (%rax,%rax)