// File: /workspace/github_cpp_makefile_star2_O1/ryanbarringtoncox[P]AVLTree/AVLTree.cpp
// Function: AVLTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void AVLTree<T>::insert(T v) {
	Node<T>* temp = new Node<T>(v);
	Node<T>** curr = &root;

	//double rotation flags
	bool rightLeft = false;
	bool leftRight = false;
	
	//tree is empty, insert at root
	if (*curr == 0) {
		*curr = temp;			
	}
	
	else {

		//keep a parent pointer
		Node<T>** dad = &root;
		
		//Critical node is null by default
		cn = 0;
		
		//find spot for new node, keep track of potential cns
		while (*curr != 0) {
			
			if (v == (*curr)->getValue()) return;
			
			if (v < (*curr)->getValue()) {
				if ((*curr)->getBalance() == -1) {
					cn = *curr;
					dad = &*curr;
				}
				if (cn && cn->getBalance() > 0) {
					cout << "Rotate right then left" << endl;
					rightLeft = true;
				}
				curr = &((*curr)->getLeftChild());
			} else if (v > (*curr)->getValue()) {
				if ((*curr)->getBalance() == 1) {
					cn = *curr;	
					dad = &*curr;
				}
				if (cn && cn->getBalance() < 0) {
					cout << "Rotate left then right" << endl;
					leftRight = true;
				}
				curr = &((*curr)->getRightChild());
			}
		}
		
		//insert the node
		*curr = temp;	
		
		
		//no cn, update balances
		if (!cn) {			
			curr = &root;
			while (*curr != 0) {
				if (v < (*curr)->getValue()) {
					(*curr)->decBalance();
					curr = &((*curr)->getLeftChild());
				} else if (v > (*curr)->getValue()) {
					(*curr)->incBalance();					
					curr = &((*curr)->getRightChild());
				}
				else if (v == (*curr)->getValue()) break;
			}			
		}		
		
		//single rotations
		if (cn && !rightLeft && !leftRight) {			
			//cout << "Cn found: " << cn->getValue() << endl;
			curr = &cn;
			
			if ((*curr)->getBalance() == -1) {	
				rightRotation(cn, dad);
			}
			
			if ((*curr)->getBalance()== 1) {	
				leftRotation(cn, dad);
			}
			
			//reset cn balance to zero
			cn->resetBalance();			
			
		}
			
		//double rotations
		if (cn && rightLeft) {
			rightRotation(cn->getRightChild(), &(cn->getRightChild()));
			leftRotation(cn, dad);
			cn->resetBalance();
		}
		if (cn && leftRight) {
			leftRotation(cn->getLeftChild(), &(cn->getLeftChild()));
			rightRotation(cn, dad);
			cn->resetBalance();
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movl $0x40, %edi
callq 0x2140
movq %rax, %r12
leaq 0x48(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x77b6
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
callq 0x7cd4
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x6d86
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2150
cmpq $0x0, (%rbx)
je 0x6f9e
movq $0x0, 0x8(%rbx)
cmpq $0x0, (%rbx)
je 0x6fa6
movq %r12, 0x30(%rsp)
leaq 0x20(%rsp), %rbp
movl $0x0, 0x8(%rsp)
leaq 0x10(%rsp), %r13
movq %rbx, (%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, %r14
movq (%r14), %rsi
movq %r13, %rdi
callq 0x7d2e
movq 0x8(%r15), %rdx
cmpq 0x18(%rsp), %rdx
jne 0x6df8
testq %rdx, %rdx
je 0x6dfd
movq 0x10(%rsp), %rsi
movq (%r15), %rdi
callq 0x2160
testl %eax, %eax
sete %r12b
jmp 0x6e00
xorl %r12d, %r12d
jmp 0x6e00
movb $0x1, %r12b
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6e17
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2150
testb %r12b, %r12b
jne 0x71c7
movq (%r14), %rsi
movq %r13, %rdi
callq 0x7d2e
movq %r15, %rdi
movq %r13, %rsi
callq 0x2040
movl %eax, %r12d
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6e50
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2150
testl %r12d, %r12d
js 0x6f0f
movq (%r14), %rsi
movq %r13, %rdi
callq 0x7d2e
movq %r15, %rdi
movq %r13, %rsi
callq 0x2040
movl %eax, %r12d
movq 0x10(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6e89
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2150
testl %r12d, %r12d
jle 0x6f8d
movq (%r14), %rdi
callq 0x7d50
cmpl $0x1, %eax
jne 0x6eaa
movq (%r14), %rax
movq %rax, 0x8(%rbx)
movq %r14, (%rsp)
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x6f05
callq 0x7d50
testl %eax, %eax
jns 0x6f05
movl $0x16, %edx
movq 0x7110(%rip), %r12      # 0xdfd8
movq %r12, %rdi
leaq 0x32ed(%rip), %rsi      # 0xa1bf
callq 0x2170
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
movb $0x1, %al
movl %eax, 0x8(%rsp)
movq (%r14), %rdi
callq 0x7d7a
jmp 0x6f8a
movq (%r14), %rdi
callq 0x7d50
cmpl $-0x1, %eax
jne 0x6f27
movq (%r14), %rax
movq %rax, 0x8(%rbx)
movq %r14, (%rsp)
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x6f82
callq 0x7d50
testl %eax, %eax
jle 0x6f82
movl $0x16, %edx
movq 0x7093(%rip), %r12      # 0xdfd8
movq %r12, %rdi
leaq 0x3259(%rip), %rsi      # 0xa1a8
callq 0x2170
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq (%r14), %rdi
callq 0x7d6e
movq %rax, %r14
cmpq $0x0, (%r14)
jne 0x6dc8
movq 0x30(%rsp), %r12
jmp 0x6fbd
movq %r12, (%rbx)
jmp 0x71c7
movl $0x0, 0xc(%rsp)
movq %rbx, %r14
movl $0x0, 0x8(%rsp)
movq %rbx, (%rsp)
movq %r12, (%r14)
cmpq $0x0, 0x8(%rbx)
jne 0x70d4
cmpq $0x0, (%rbx)
je 0x70d4
leaq 0x20(%rsp), %r13
leaq 0x10(%rsp), %r14
movq %rbx, %r12
movq (%r12), %rsi
movq %r14, %rdi
callq 0x7d2e
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
movl %eax, %ebp
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x7012
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2150
testl %ebp, %ebp
js 0x705e
movq (%r12), %rsi
movq %r14, %rdi
callq 0x7d2e
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
movl %eax, %ebp
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x7046
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2150
testl %ebp, %ebp
jle 0x7075
movq (%r12), %rdi
callq 0x7d54
movq (%r12), %rdi
callq 0x7d7a
jmp 0x7070
movq (%r12), %rdi
callq 0x7d58
movq (%r12), %rdi
callq 0x7d6e
movq %rax, %r12
jmp 0x70c9
movq (%r12), %rsi
movq %r14, %rdi
callq 0x7d2e
movq 0x8(%r15), %rdx
cmpq 0x18(%rsp), %rdx
jne 0x70a6
testq %rdx, %rdx
je 0x70aa
movq 0x10(%rsp), %rsi
movq (%r15), %rdi
callq 0x2160
testl %eax, %eax
sete %bpl
jmp 0x70ad
xorl %ebp, %ebp
jmp 0x70ad
movb $0x1, %bpl
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x70c4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2150
testb %bpl, %bpl
jne 0x70d4
cmpq $0x0, (%r12)
jne 0x6fe2
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
sete %al
movl 0xc(%rsp), %r14d
movl %r14d, %ecx
movl 0x8(%rsp), %ebp
orb %bpl, %cl
orb %al, %cl
testb $0x1, %cl
jne 0x7135
callq 0x7d50
cmpl $-0x1, %eax
jne 0x710e
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq (%rsp), %rdx
callq 0x64b6
movq 0x8(%rbx), %rdi
callq 0x7d50
cmpl $0x1, %eax
jne 0x712c
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq (%rsp), %rdx
callq 0x63a4
movq 0x8(%rbx), %rdi
callq 0x7d5c
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
setne %al
andb %r14b, %al
cmpb $0x1, %al
jne 0x717e
callq 0x7d7a
movq (%rax), %r14
movq 0x8(%rbx), %rdi
callq 0x7d7a
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x64b6
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq (%rsp), %rdx
callq 0x63a4
movq 0x8(%rbx), %rdi
callq 0x7d5c
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
setne %al
andb %bpl, %al
cmpb $0x1, %al
jne 0x71c7
callq 0x7d6e
movq (%rax), %r14
movq 0x8(%rbx), %rdi
callq 0x7d6e
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x63a4
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq (%rsp), %rdx
callq 0x64b6
movq 0x8(%rbx), %rdi
callq 0x7d5c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x71f5
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2150
jmp 0x71f5
movq %rax, %rbx
movl $0x40, %esi
movq %r12, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x21e0
jmp 0x7210
jmp 0x7210
jmp 0x7210
movq %rax, %rdi
callq 0x77aa