// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rcx
cmpq %rsi, 0x8(%rdi)
je 0x2907d
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
movl $0x1, %ebp
cmpl $0x1, 0x18(%rcx)
jne 0x2907d
leaq 0x30(%rcx), %r15
movq 0x30(%rcx), %rsi
movq 0x20(%rsi), %rax
cmpq %rax, %rcx
je 0x28fac
cmpl $0x0, 0x18(%rax)
jne 0x28f94
movl $0x1, 0x18(%rax)
movl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x28cde
movq (%r15), %rsi
movq 0x20(%rsi), %rax
movq 0x20(%rax), %rcx
movq 0x28(%rax), %rdx
cmpl $0x1, 0x18(%rdx)
movl 0x18(%rcx), %edi
jne 0x28ff2
cmpl $0x1, %edi
je 0x28fe9
jmp 0x2901b
movq 0x28(%rsi), %rax
cmpl $0x0, 0x18(%rax)
jne 0x28fd3
movl $0x1, 0x18(%rax)
movl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x28c88
movq (%r15), %rsi
movq 0x28(%rsi), %rax
movq 0x20(%rax), %rdx
movq 0x28(%rax), %rcx
cmpl $0x1, 0x18(%rdx)
movl 0x18(%rcx), %edi
jne 0x29031
cmpl $0x1, %edi
jne 0x2905a
movl $0x0, 0x18(%rax)
jmp 0x29071
cmpl $0x1, %edi
jne 0x2901b
movl $0x1, 0x18(%rdx)
movl $0x0, 0x18(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x28c88
movq (%r15), %rsi
movq 0x20(%rsi), %rax
movq 0x20(%rax), %rcx
movl 0x18(%rsi), %edx
movl %edx, 0x18(%rax)
movl %ebp, 0x18(%rsi)
movl %ebp, 0x18(%rcx)
movq %rbx, %rdi
callq 0x28cde
jmp 0x2906e
cmpl $0x1, %edi
jne 0x2905a
movl $0x1, 0x18(%rdx)
movl $0x0, 0x18(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x28cde
movq (%r15), %rsi
movq 0x28(%rsi), %rax
movq 0x28(%rax), %rcx
movl 0x18(%rsi), %edx
movl %edx, 0x18(%rax)
movl %ebp, 0x18(%rsi)
movl %ebp, 0x18(%rcx)
movq %rbx, %rdi
callq 0x28c88
movq %r14, %r15
movq (%r15), %rcx
cmpq (%r14), %rcx
jne 0x28f56
movl $0x1, 0x18(%rcx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop