// File: /workspace/github_cpp_makefile_star2_O1/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, %rax
cmpq %rsi, 0x8(%rdi)
je 0x2923c
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
movl $0x1, %ebp
cmpl $0x1, 0x18(%rax)
jne 0x2923c
leaq 0x30(%rax), %r15
movq 0x30(%rax), %rcx
movq 0x20(%rcx), %rsi
cmpq %rsi, %rax
je 0x291a1
cmpl $0x0, 0x18(%rsi)
jne 0x2913f
movl $0x1, 0x18(%rsi)
movl $0x0, 0x18(%rcx)
movq %rbx, %rdi
movq %rcx, %rsi
callq 0x28e74
movq (%r15), %rax
movq 0x20(%rax), %rsi
movq 0x28(%rsi), %rax
cmpl $0x1, 0x18(%rax)
jne 0x29157
movq 0x20(%rsi), %rcx
cmpl $0x1, 0x18(%rcx)
je 0x291df
movq 0x20(%rsi), %rcx
cmpl $0x1, 0x18(%rcx)
jne 0x2917e
movl $0x1, 0x18(%rax)
movl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x28e1a
movq (%r15), %rax
movq 0x20(%rax), %rsi
movq (%r15), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0x18(%rsi)
movl %ebp, 0x18(%rax)
movq 0x20(%rsi), %rcx
movl %ebp, 0x18(%rcx)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x28e74
jmp 0x2922d
movq 0x28(%rcx), %rsi
cmpl $0x0, 0x18(%rsi)
jne 0x291cb
movl $0x1, 0x18(%rsi)
movl $0x0, 0x18(%rcx)
movq %rbx, %rdi
movq %rcx, %rsi
callq 0x28e1a
movq (%r15), %rax
movq 0x28(%rax), %rsi
movq 0x20(%rsi), %rax
cmpl $0x1, 0x18(%rax)
jne 0x291e8
movq 0x28(%rsi), %rcx
cmpl $0x1, 0x18(%rcx)
jne 0x291e8
movl $0x0, 0x18(%rsi)
jmp 0x29230
movq 0x28(%rsi), %rcx
cmpl $0x1, 0x18(%rcx)
jne 0x2920f
movl $0x1, 0x18(%rax)
movl $0x0, 0x18(%rsi)
movq %rbx, %rdi
callq 0x28e74
movq (%r15), %rax
movq 0x28(%rax), %rsi
movq (%r15), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0x18(%rsi)
movl %ebp, 0x18(%rax)
movq 0x28(%rsi), %rcx
movl %ebp, 0x18(%rcx)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x28e1a
movq %r14, %r15
movq (%r15), %rax
cmpq (%r14), %rax
jne 0x290fa
movl $0x1, 0x18(%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq