// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rsi, %r15
movq %rdi, %r12
movl $0x40, %edi
callq 0x2140
movq %rax, %rbx
leaq 0x58(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x775e
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x7c80
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x6d2e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2150
movq (%r12), %rsi
testq %rsi, %rsi
je 0x6fbc
movq %rbx, 0x40(%rsp)
movq $0x0, 0x8(%r12)
leaq 0x30(%rsp), %rbx
movl $0x0, 0xc(%rsp)
leaq 0x20(%rsp), %r13
movq %r12, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
movq %r12, (%rsp)
movq %r12, %r14
movq %r13, %rdi
callq 0x7cda
movq 0x8(%r15), %rdx
cmpq 0x28(%rsp), %rdx
jne 0x6d9f
movq 0x20(%rsp), %r12
testq %rdx, %rdx
je 0x6da8
movq (%r15), %rdi
movq %r12, %rsi
callq 0x2160
testl %eax, %eax
sete %bpl
jmp 0x6dab
movq 0x20(%rsp), %r12
xorl %ebp, %ebp
jmp 0x6dab
movb $0x1, %bpl
cmpq %rbx, %r12
je 0x6dc0
movq 0x30(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2150
testb %bpl, %bpl
jne 0x6fc0
movq (%r14), %rsi
movq %r13, %rdi
callq 0x7cda
movq %r15, %rdi
movq %r13, %rsi
callq 0x2040
movl %eax, %r12d
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6df9
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2150
testl %r12d, %r12d
js 0x6e56
movq (%r14), %rsi
movq %r13, %rdi
callq 0x7cda
movq %r15, %rdi
movq %r13, %rsi
callq 0x2040
movl %eax, %r12d
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6e2e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2150
testl %r12d, %r12d
jle 0x6f48
movq (%r14), %rdi
callq 0x7cfc
cmpl $0x1, %eax
jne 0x6e6d
movq (%r14), %rdi
movq (%rsp), %rax
movq %rdi, 0x8(%rax)
movq %r14, 0x10(%rsp)
jmp 0x6e75
movq (%r14), %rdi
callq 0x7cfc
cmpl $-0x1, %eax
je 0x6ed6
movq (%rsp), %rax
movq 0x8(%rax), %rdi
jmp 0x6ee6
movq (%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x6ecc
callq 0x7cfc
testl %eax, %eax
jns 0x6ecc
movl $0x16, %edx
movq 0x7149(%rip), %r12      # 0xdfd8
movq %r12, %rdi
leaq 0x3326(%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, 0xc(%rsp)
movq (%r14), %rdi
callq 0x7d26
jmp 0x6f45
movq (%r14), %rdi
movq (%rsp), %rax
movq %rdi, 0x8(%rax)
movq %r14, 0x10(%rsp)
testq %rdi, %rdi
je 0x6f3d
callq 0x7cfc
testl %eax, %eax
jle 0x6f3d
movl $0x16, %edx
movq 0x70d8(%rip), %r12      # 0xdfd8
movq %r12, %rdi
leaq 0x329e(%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, 0x1c(%rsp)
movq (%r14), %rdi
callq 0x7d1a
movq %rax, %r14
movq (%r14), %rsi
testq %rsi, %rsi
jne 0x6d6f
movq 0x40(%rsp), %rax
movq %rax, (%r14)
movq (%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x7020
testb $0x1, 0x1c(%rsp)
je 0x6fcf
callq 0x7d26
movq (%rax), %r14
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x7d26
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6478
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0x6366
movq 0x8(%rbx), %rdi
callq 0x7d08
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
jne 0x6fda
jmp 0x6fc0
movq %rbx, (%r12)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, 0xc(%rsp)
je 0x7141
testb $0x1, 0xc(%rsp)
je 0x6fc0
callq 0x7d1a
movq (%rax), %r14
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x7d1a
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6366
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0x6478
movq 0x8(%rbx), %rdi
callq 0x7d08
jmp 0x6fc0
movq (%rsp), %rax
movq (%rax), %rsi
testq %rsi, %rsi
je 0x6fc0
leaq 0x20(%rsp), %r14
movq (%rsp), %r13
movq %r14, %rdi
callq 0x7cda
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
movl %eax, %ebp
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x7061
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2150
testl %ebp, %ebp
js 0x70ad
movq (%r13), %rsi
movq %r14, %rdi
callq 0x7cda
movq %r15, %rdi
movq %r14, %rsi
callq 0x2040
movl %eax, %ebp
movq 0x20(%rsp), %rdi
cmpq %rbx, %rdi
je 0x7095
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2150
testl %ebp, %ebp
jle 0x70c4
movq (%r13), %rdi
callq 0x7d00
movq (%r13), %rdi
callq 0x7d26
jmp 0x70bf
movq (%r13), %rdi
callq 0x7d04
movq (%r13), %rdi
callq 0x7d1a
movq %rax, %r13
jmp 0x711e
movq (%r13), %rsi
movq %r14, %rdi
callq 0x7cda
movq 0x8(%r15), %rdx
cmpq 0x28(%rsp), %rdx
jne 0x70f8
movq 0x20(%rsp), %r12
testq %rdx, %rdx
je 0x7101
movq (%r15), %rdi
movq %r12, %rsi
callq 0x2160
testl %eax, %eax
sete %bpl
jmp 0x7104
movq 0x20(%rsp), %r12
xorl %ebp, %ebp
jmp 0x7104
movb $0x1, %bpl
cmpq %rbx, %r12
je 0x7119
movq 0x30(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2150
testb %bpl, %bpl
jne 0x712b
movq (%r13), %rsi
testq %rsi, %rsi
jne 0x7035
movq (%rsp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
jne 0x6f6d
jmp 0x6fc0
callq 0x7cfc
cmpl $-0x1, %eax
movq (%rsp), %rbx
jne 0x7160
movq 0x8(%rbx), %rsi
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0x6478
movq 0x8(%rbx), %rdi
callq 0x7cfc
cmpl $0x1, %eax
je 0x6f97
jmp 0x6fa8
movq %rbx, %r15
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x719c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2150
jmp 0x719c
movq %rbx, %r15
movq %rax, %rbx
movl $0x40, %esi
movq %r15, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x21e0
jmp 0x71b7
jmp 0x71b7
jmp 0x71b7
movq %rax, %rdi
callq 0x7752
nop