// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]MQSim/src/sim/EventTree.cpp
// Function: MQSimEngine::EventTree::RestoreAfterInsert(MQSimEngine::EventTreeNode*)
void EventTree::RestoreAfterInsert(EventTreeNode* x)
	{
		// x and y are used as variable names for brevity, in a more formal
		// implementation, you should probably change the names

		EventTreeNode* y;

		// maintain red-black tree properties after adding x
		while (x != rbTree && x->Parent->Color == 0) {
			// Parent node is .Colored red; 
			// determine traversal path
			// is it on the Left or Right subtree?
			if (x->Parent == x->Parent->Parent->Left) {	
				// get uncle
				y = x->Parent->Parent->Right;
				if (y != NULL && y->Color == 0)
				{	// uncle is red; change x's Parent and uncle to black
					x->Parent->Color = 1;
					y->Color = 1;
					// grandparent must be red. Why? Every red node that is not 
					// a leaf has only black children 
					x->Parent->Parent->Color = 0;
					x = x->Parent->Parent;	// continue loop with grandparent
				} else {
					// uncle is black; determine if x is greater than Parent
					if (x == x->Parent->Right)
					{	// yes, x is greater than Parent; rotate Left
						// make x a Left child
						x = x->Parent;
						RotateLeft(x);
					}
					// no, x is less than Parent
					x->Parent->Color = 1;	// make Parent black
					x->Parent->Parent->Color = 0;		// make grandparent black
					RotateRight(x->Parent->Parent);					// rotate right
				}
			} else {
				// x's Parent is on the Right subtree
				// this code is the same as above with "Left" and "Right" swapped
				y = x->Parent->Parent->Left;
				if (y != NULL && y->Color == 0) {
					x->Parent->Color = 1;
					y->Color = 1;
					x->Parent->Parent->Color = 0;
					x = x->Parent->Parent;
				} else {
					if (x == x->Parent->Left) {
						x = x->Parent;
						RotateRight(x);
					}
					x->Parent->Color = 1;
					x->Parent->Parent->Color = 0;
					RotateLeft(x->Parent->Parent);
				}
			}
		}

		// rbTree should always be black
		rbTree->Color = 1;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq 0x8(%rdi), %rcx
cmpq %rsi, %rcx
je 0x28c76
movq %rsi, %r14
movq 0x30(%rsi), %r15
cmpl $0x0, 0x18(%r15)
jne 0x28c76
movq %rdi, %rbx
leaq 0x18(%r15), %rax
movl $0x1, %ebp
movq 0x30(%r15), %rsi
movq 0x20(%rsi), %rcx
cmpq %rcx, %r15
je 0x28bd5
testq %rcx, %rcx
je 0x28bca
cmpl $0x0, 0x18(%rcx)
je 0x28bef
cmpq 0x20(%r15), %r14
je 0x28c00
movq %r15, %rax
jmp 0x28c16
movq 0x28(%rsi), %rcx
testq %rcx, %rcx
je 0x28be4
cmpl $0x0, 0x18(%rcx)
je 0x28bef
cmpq 0x28(%r15), %r14
je 0x28c2e
movq %r15, %rax
jmp 0x28c44
movl %ebp, (%rax)
movl %ebp, 0x18(%rcx)
movl $0x0, 0x18(%rsi)
movq %rsi, %r14
jmp 0x28c5a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28cde
movq 0x30(%r15), %rax
movq 0x30(%rax), %rsi
movq %r15, %r14
movl $0x1, 0x18(%rax)
movl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x28c88
jmp 0x28c5a
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28c88
movq 0x30(%r15), %rax
movq 0x30(%rax), %rsi
movq %r15, %r14
movl $0x1, 0x18(%rax)
movl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x28cde
movq 0x8(%rbx), %rcx
cmpq %rcx, %r14
je 0x28c76
movq 0x30(%r14), %r15
leaq 0x18(%r15), %rax
cmpl $0x0, 0x18(%r15)
je 0x28bb2
movl $0x1, 0x18(%rcx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq