// File: MarkCBell[P]twister/./twister/kernel/twister.cpp
// Function: swirl(manifold*, tetra*, int)
tetra *swirl(manifold *M, tetra *current, int layer_number)
{
	for (int i = 0; i < 3; i++)
	{
		if (current->get_gluedto(i) != NULL)
			continue;  // Stop us going backwards.
		
		current->walk_about(i);
		
		if ((current->get_gluedto(i))->get_layer() != layer_number) 
			current->ungluesym(i);  // Nope, don't want to go out of the layer.
		else if (current->get_gluedto(i)->get_category() == gadget_pup)
		{
			tetra *next_tetra = new tetra(M, filler);
			current->subbedby(next_tetra, i, perm_walk_list[i]);
			current->walk_about(i);
			current = next_tetra;
			break;
		}
		else
			current->ungluesym(i);  // Nope, don't want to go that way.
	}
	
	for (int i = 0; i < 3; i++)
	{
		if (current->get_gluedto(i) != NULL)
			continue;  // Stop us going backwards.
		
		current->walk_about(i);
		
		if (current->get_gluedto(i)->get_layer() != layer_number)
			current->ungluesym(i);  // Nope, don't want to go out of the layer.
		else if (current->get_gluedto(i)->get_category() != gadget_pup)
		{
			tetra *next_tetra = new tetra(M, filler);
			current->subbedby(next_tetra, i, perm_walk_list[i]);
			current->walk_about(i);
			current = next_tetra;
			break;
		}
		else
			current->ungluesym(i);  // Nope, don't want to go that way.
	}
	return current;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x3, -0x18(%rbp)
jge 0x1c6ca
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x20b60
cmpq $0x0, %rax
je 0x1c5bb
jmp 0x1c6bc
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x177f0
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x20b60
movq %rax, %rdi
callq 0x211e0
cmpl -0x14(%rbp), %eax
je 0x1c5f1
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x17520
jmp 0x1c6ba
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x20b60
movq %rax, %rdi
callq 0x21200
cmpl $0x6, %eax
jne 0x1c6ac
movl $0xc0, %edi
callq 0x3300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rsi
movq %rsp, %rax
movq $0x0, (%rax)
movl $0x7, %edx
movl $0x3, %ecx
movl $0x2, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x17070
jmp 0x1c64d
movq -0x40(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x18(%rbp), %edx
movslq -0x18(%rbp), %rax
leaq 0x190c5(%rip), %rcx     # 0x35730
shlq $0x4, %rax
addq %rax, %rcx
callq 0x17650
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x177f0
movq -0x20(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x1c6ca
movq -0x40(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0xc0, %esi
callq 0x3310
jmp 0x1c808
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0x17520
jmp 0x1c6ba
jmp 0x1c6bc
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1c59a
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x1c7fe
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x20b60
cmpq $0x0, %rax
je 0x1c6f2
jmp 0x1c7f0
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x177f0
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x20b60
movq %rax, %rdi
callq 0x211e0
cmpl -0x14(%rbp), %eax
je 0x1c728
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x17520
jmp 0x1c7ee
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x20b60
movq %rax, %rdi
callq 0x21200
cmpl $0x6, %eax
je 0x1c7e0
movl $0xc0, %edi
callq 0x3300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rsi
movq %rsp, %rax
movq $0x0, (%rax)
movl $0x7, %edx
movl $0x3, %ecx
movl $0x2, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x17070
jmp 0x1c784
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x30(%rbp), %edx
movslq -0x30(%rbp), %rax
leaq 0x18f8e(%rip), %rcx     # 0x35730
shlq $0x4, %rax
addq %rax, %rcx
callq 0x17650
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x177f0
movq -0x38(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x1c7fe
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0xc0, %esi
callq 0x3310
jmp 0x1c808
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x17520
jmp 0x1c7ee
jmp 0x1c7f0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1c6d1
movq -0x10(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x3550
nopw %cs:(%rax,%rax)