// File: /workspace/github_cpp_makefile_star2_O3/devanshdalal[P]cloud-computing-concepts-part-2/MP2Node.cpp
// Function: MP2Node::updateRing()
void MP2Node::updateRing()
{
	/*
	 * Implement this. Parts of it are already implemented
	 */
	vector<Node> curMemList;
	// bool change = false;

	/*
	 *  Step 1. Get the current membership list from Membership Protocol / MP1
	 */
	curMemList = getMembershipList();

	/*
	 * Step 2: Construct the ring
	 */
	// Sort the list based on the hashCode
	sort(curMemList.begin(), curMemList.end());

	int curr_id = -1;
	for (int i = 0; i < ring.size(); ++i)
	{
		if (*(ring[i].getAddress()) == memberNode->addr)
		{
			curr_id = i;
			break;
		}
	}

	int new_id = -1;
	for (int i = 0; i < curMemList.size(); ++i)
	{
		if (*(curMemList[i].getAddress()) == memberNode->addr)
		{
			new_id = i;
			break;
		}
	}

	bool may_rehash = false;
	if (ring.size() >= 3)
	{
		for (int prev = -3; prev < 3; ++prev)
		{
			int safe_id = (curr_id + prev + (int)ring.size()) % ring.size();
			int safe_new_id = (new_id + prev + (int)curMemList.size()) % curMemList.size();
			if (*(curMemList[safe_new_id].getAddress()) == *(ring[safe_id].getAddress()))
			{
			}
			else
			{
				may_rehash = true;
				break;
			}
		}
	}
	ring = curMemList;

	/*
	 * Step 3: Run the stabilization protocol IF REQUIRED
	 */
	// Run stabilization protocol if the hash table size is greater than zero and if there has been a changed in the ring
	if (may_rehash)
	{
		std::cout << "Running stabilizationProtocol on " << memberNode->addr.getAddress() << "\n";
		stabilizationProtocol();
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0xedde
movq 0x10(%rsp), %rax
leaq 0x50(%rsp), %r14
movaps (%r14), %xmm0
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movaps (%rsp), %xmm1
movaps %xmm0, (%rsp)
leaq 0x30(%rsp), %rdi
movaps %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movq $0x0, 0x10(%r14)
callq 0xb770
movq %r14, %rdi
callq 0xb770
movq (%rsp), %r14
movq 0x8(%rsp), %r15
cmpq %r15, %r14
je 0xeb9a
movq %r15, %rax
subq %r14, %rax
sarq $0x5, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1396f
movq %r14, %rdi
movq %r15, %rsi
callq 0x13a25
movq 0x30(%r13), %rdi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %rdi, 0x38(%r13)
je 0xebf2
xorl %r14d, %r14d
xorl %r12d, %r12d
addq %r14, %rdi
callq 0x14a0c
movq 0x60(%r13), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xcd4a
testb %al, %al
jne 0xebf2
incq %r12
movq 0x30(%r13), %rdi
movq 0x38(%r13), %rax
subq %rdi, %rax
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r12, %rax
ja 0xebb5
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq (%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
je 0xec40
xorl %r14d, %r14d
xorl %r15d, %r15d
addq %r14, %rdi
callq 0x14a0c
movq 0x60(%r13), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xcd4a
testb %al, %al
jne 0xec3d
incq %r15
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r15, %rax
ja 0xec03
jmp 0xec40
movl %r15d, %ebp
leaq 0x30(%r13), %rax
movq %rax, 0x28(%rsp)
movq 0x38(%r13), %rax
subq 0x30(%r13), %rax
cmpq $0x41, %rax
jb 0xecf5
movq %r13, 0x20(%rsp)
addl $-0x3, %ebp
movslq %ebp, %r13
addl $-0x3, %r12d
movl $0x4, %ebp
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rbx
movq 0x38(%rax), %r14
movq (%rsp), %rcx
movq 0x8(%rsp), %rsi
subq %rcx, %rsi
sarq $0x5, %rsi
movslq %esi, %rax
addq %r13, %rax
xorl %edx, %edx
divq %rsi
movslq %edx, %rdi
shlq $0x5, %rdi
addq %rcx, %rdi
callq 0x14a0c
movq %rax, %r15
subq %rbx, %r14
sarq $0x5, %r14
leal (%r12,%r14), %eax
cltq
xorl %edx, %edx
divq %r14
movslq %edx, %rdi
shlq $0x5, %rdi
movq 0x28(%rsp), %rax
addq (%rax), %rdi
callq 0x14a0c
movq %r15, %rdi
movq %rax, %rsi
callq 0xcd4a
testb %al, %al
je 0xecf9
decl %ebp
incq %r13
incl %r12d
cmpl $-0x2, %ebp
jne 0xec6f
negl %ebp
cmpl $0x2, %ebp
setb %bl
jmp 0xecfb
xorl %ebx, %ebx
jmp 0xed00
movb $0x1, %bl
movq 0x20(%rsp), %r13
movq %rsp, %rsi
movq 0x28(%rsp), %rdi
callq 0x13574
testb %bl, %bl
je 0xed88
movq 0x112b8(%rip), %rdi     # 0x1ffd0
leaq 0x9f5f(%rip), %rsi      # 0x18c7e
movl $0x21, %edx
callq 0x32d0
movq 0x60(%r13), %rsi
addq $0x8, %rsi
leaq 0x30(%rsp), %rdi
callq 0xb42e
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x11284(%rip), %rdi     # 0x1ffd0
callq 0x32d0
leaq 0x959d(%rip), %rsi      # 0x182f5
movl $0x1, %edx
movq %rax, %rdi
callq 0x32d0
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xed80
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3280
movq %r13, %rdi
callq 0xef30
movq %rsp, %rdi
callq 0xb770
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xedcb
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xedce
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0xedce
jmp 0xedcb
jmp 0xedcb
jmp 0xedcb
jmp 0xedcb
jmp 0xedcb
movq %rax, %rbx
movq %rsp, %rdi
callq 0xb770
movq %rbx, %rdi
callq 0x3470