// File: CMU-SAFARI[P]MQSim/src/sim/EventTree.cpp
// Function: MQSimEngine::EventTree::Restore_after_delete(MQSimEngine::EventTreeNode*)
void EventTree::Restore_after_delete(EventTreeNode* x)
	{
		// maintain Red-Black tree balance after deleting node 			

		EventTreeNode* y;

		while (x != rbTree && x->Color == 1)
		{
			if (x == x->Parent->Left)			// determine sub tree from parent
			{
				y = x->Parent->Right;			// y is x's sibling 
				if (y->Color == 0)
				{	// x is black, y is red - make both black and rotate
					y->Color = 1;
					x->Parent->Color = 0;
					RotateLeft(x->Parent);
					y = x->Parent->Right;
				}
				if (y->Left->Color == 1 &&
					y->Right->Color == 1) {
					// children are both black
					y->Color = 0;		// change parent to red
					x = x->Parent;					// move up the tree
				} else {
					if (y->Right->Color == 1)
					{
						y->Left->Color = 1;
						y->Color = 0;
						RotateRight(y);
						y = x->Parent->Right;
					}
					y->Color = x->Parent->Color;
					x->Parent->Color = 1;
					y->Right->Color = 1;
					RotateLeft(x->Parent);
					x = rbTree;
				}
			} else {
				// right subtree - same as code above with right and left swapped
				y = x->Parent->Left;
				if (y->Color == 0) {
					y->Color = 1;
					x->Parent->Color = 0;
					RotateRight(x->Parent);
					y = x->Parent->Left;
				}

				if (y->Right->Color == 1 &&
					y->Left->Color == 1) {
					y->Color = 0;
					x = x->Parent;
				} else {
					if (y->Left->Color == 1) {
						y->Right->Color = 1;
						y->Color = 0;
						RotateLeft(y);
						y = x->Parent->Left;
					}
					y->Color = x->Parent->Color;
					x->Parent->Color = 1;
					y->Left->Color = 1;
					RotateRight(x->Parent);
					x = rbTree;
				}
			}
		}
		x->Color = 1;
	}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rcx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, 0x7(%rsp)
je 0x4977e
movq 0x18(%rsp), %rax
cmpl $0x1, 0x18(%rax)
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x4978b
jmp 0x49a27
movq 0x18(%rsp), %rax
movq 0x18(%rsp), %rcx
movq 0x30(%rcx), %rcx
cmpq 0x20(%rcx), %rax
jne 0x498e4
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x49801
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movl $0x1, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rsi
callq 0x49030
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x4983e
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x4983e
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x498df
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x4988a
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl $0x1, 0x18(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x10(%rsp), %rsi
callq 0x49130
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movl $0x1, 0x18(%rax)
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movl $0x1, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rsi
callq 0x49030
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x49a22
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x49942
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movl $0x1, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rsi
callq 0x49130
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x4997f
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x4997f
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x49a20
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
cmpl $0x1, 0x18(%rax)
jne 0x499cb
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movl $0x1, 0x18(%rax)
movq 0x10(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x10(%rsp), %rsi
callq 0x49030
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movl $0x1, 0x18(%rax)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl $0x1, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rsi
callq 0x49130
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x49a22
jmp 0x49758
movq 0x18(%rsp), %rax
movl $0x1, 0x18(%rax)
addq $0x28, %rsp
retq
nopl (%rax,%rax)