// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
pushq $0x1
popq %rbp
cmpq (%r14), %rcx
je 0x20743
cmpl $0x1, 0x18(%rcx)
jne 0x20743
leaq 0x30(%rcx), %r15
movq 0x30(%rcx), %rsi
movq 0x20(%rsi), %rax
cmpq %rax, %rcx
je 0x20682
cmpl $0x0, 0x18(%rax)
jne 0x2066a
movl $0x1, 0x18(%rax)
andl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x203d8
movq (%r15), %rsi
movq 0x20(%rsi), %rax
movq 0x20(%rax), %rcx
movq 0x28(%rax), %rdx
cmpl $0x1, 0x18(%rdx)
movl 0x18(%rcx), %edi
jne 0x206c2
cmpl $0x1, %edi
je 0x206bc
jmp 0x206e8
movq 0x28(%rsi), %rax
cmpl $0x0, 0x18(%rax)
jne 0x206a6
movl $0x1, 0x18(%rax)
andl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x20382
movq (%r15), %rsi
movq 0x28(%rsi), %rax
movq 0x20(%rax), %rdx
movq 0x28(%rax), %rcx
cmpl $0x1, 0x18(%rdx)
movl 0x18(%rcx), %edi
jne 0x206fe
cmpl $0x1, %edi
jne 0x20724
andl $0x0, 0x18(%rax)
jmp 0x2073b
cmpl $0x1, %edi
jne 0x206e8
movl $0x1, 0x18(%rdx)
andl $0x0, 0x18(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x20382
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 0x203d8
jmp 0x20738
cmpl $0x1, %edi
jne 0x20724
movl $0x1, 0x18(%rdx)
andl $0x0, 0x18(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x203d8
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 0x20382
movq %r14, %r15
movq (%r15), %rcx
jmp 0x20626
movl $0x1, 0x18(%rcx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop