// File: /workspace/github_cpp_makefile_star2_O1/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 0xf126
leaq 0x20(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movaps (%rsp), %xmm0
movq 0x10(%rsp), %rax
leaq 0x50(%rsp), %r14
movups (%r14), %xmm1
movaps %xmm1, (%rsp)
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movaps (%rdi), %xmm1
movaps %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
movups %xmm1, (%r14)
movq $0x0, 0x10(%r14)
callq 0xb860
movq %r14, %rdi
callq 0xb860
movq (%rsp), %r14
movq 0x8(%rsp), %r15
cmpq %r15, %r14
je 0xeed0
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 0x13c37
movq %r14, %rdi
movq %r15, %rsi
callq 0x13ced
movq 0x30(%r13), %rdi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rdi, 0x38(%r13)
je 0xef25
xorl %r14d, %r14d
xorl %ebx, %ebx
addq %r14, %rdi
callq 0x14cc4
movq 0x60(%r13), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xce6e
testb %al, %al
jne 0xef25
incq %rbx
movq 0x30(%r13), %rdi
movq 0x38(%r13), %rax
subq %rdi, %rax
sarq $0x5, %rax
addq $0x20, %r14
cmpq %rbx, %rax
ja 0xeee9
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq (%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
je 0xef73
xorl %r14d, %r14d
xorl %r15d, %r15d
addq %r14, %rdi
callq 0x14cc4
movq 0x60(%r13), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xce6e
testb %al, %al
jne 0xef70
incq %r15
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
addq $0x20, %r14
cmpq %r15, %rax
ja 0xef36
jmp 0xef73
movl %r15d, %ebp
leaq 0x30(%r13), %rax
movq %rax, 0x18(%rsp)
movq 0x38(%r13), %rax
subq 0x30(%r13), %rax
cmpq $0x41, %rax
jb 0xf043
addl $-0x3, %ebp
movslq %ebp, %rax
movq %rax, 0x48(%rsp)
addl $-0x3, %ebx
movq %rbx, 0x40(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x30(%r13), %rbx
movq %r13, %r12
movq 0x38(%r13), %r13
movq (%rsp), %rcx
movq 0x8(%rsp), %rsi
subq %rcx, %rsi
sarq $0x5, %rsi
movslq %esi, %rax
movq 0x48(%rsp), %rdx
addq %r14, %rdx
addq %rdx, %rax
xorl %edx, %edx
divq %rsi
movslq %edx, %rdi
shlq $0x5, %rdi
addq %rcx, %rdi
callq 0x14cc4
movq %rax, %r15
subq %rbx, %r13
sarq $0x5, %r13
movq 0x40(%rsp), %rax
addl %r14d, %eax
addl %r13d, %eax
cltq
xorl %edx, %edx
divq %r13
movslq %edx, %rdi
shlq $0x5, %rdi
movq 0x18(%rsp), %rax
addq (%rax), %rdi
callq 0x14cc4
movq %r15, %rdi
movq %rax, %rsi
callq 0xce6e
movzbl %bpl, %ebp
testb %al, %al
movl $0x1, %ecx
cmovel %ecx, %ebp
cmpl $0x5, %r14d
setne %cl
incq %r14
testb %cl, %al
movq %r12, %r13
jne 0xefa6
jmp 0xf045
xorl %ebp, %ebp
movq %rsp, %rsi
movq 0x18(%rsp), %rdi
callq 0x13868
testb $0x1, %bpl
je 0xf0cf
movq 0x10f71(%rip), %rdi     # 0x1ffd0
leaq 0x9c58(%rip), %rsi      # 0x18cbe
movl $0x21, %edx
callq 0x32d0
movq 0x60(%r13), %rsi
addq $0x8, %rsi
leaq 0x20(%rsp), %rdi
callq 0xb51c
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x10f3d(%rip), %rdi     # 0x1ffd0
callq 0x32d0
leaq 0x9256(%rip), %rsi      # 0x182f5
movl $0x1, %edx
movq %rax, %rdi
callq 0x32d0
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf0c7
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3280
movq %r13, %rdi
callq 0xf278
movq %rsp, %rdi
callq 0xb860
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf112
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf115
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0xf115
jmp 0xf112
jmp 0xf112
jmp 0xf112
jmp 0xf112
jmp 0xf112
movq %rax, %rbx
movq %rsp, %rdi
callq 0xb860
movq %rbx, %rdi
callq 0x3470
nop