// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0xc974
leaq 0x10(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x97ce
movq %r15, %rdi
callq 0x8f1a
movq (%r14), %rdi
movq 0x8(%r14), %rsi
callq 0xf6b9
leaq 0x30(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
pushq $-0x1
popq %rax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r13, %rax
jbe 0xc7c9
addq %r14, %rdi
callq 0x107d2
movq 0x60(%rbx), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xa502
testb %al, %al
jne 0xc7cd
incq %r13
addq $0x20, %r14
jmp 0xc790
pushq $-0x1
popq %r13
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r15, %rax
jbe 0xc813
addq %r14, %rdi
callq 0x107d2
movq 0x60(%rbx), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xa502
testb %al, %al
jne 0xc80e
incq %r15
addq $0x20, %r14
jmp 0xc7d3
movq %r15, 0x28(%rsp)
movq 0x38(%rbx), %rax
subq 0x30(%rbx), %rax
cmpq $0x41, %rax
jb 0xc8b7
pushq $-0x3
popq %r14
cmpl $0x3, %r14d
setne %al
movl %eax, 0xc(%rsp)
je 0xc8bf
movq 0x30(%rbx), %rbp
movq 0x38(%rbx), %r12
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
sarq $0x5, %rsi
movq 0x28(%rsp), %rax
addl %r14d, %eax
addl %esi, %eax
cltq
xorl %edx, %edx
divq %rsi
movslq %edx, %rdi
shlq $0x5, %rdi
addq %rcx, %rdi
callq 0x107d2
movq %rax, %r15
subq %rbp, %r12
sarq $0x5, %r12
leal (%r14,%r13), %eax
addl %r12d, %eax
cltq
xorl %edx, %edx
divq %r12
movslq %edx, %rdi
shlq $0x5, %rdi
movq 0x30(%rsp), %rax
addq (%rax), %rdi
callq 0x107d2
movq %r15, %rdi
movq %rax, %rsi
callq 0xa502
incl %r14d
testb %al, %al
jne 0xc829
jmp 0xc8bf
movl $0x0, 0xc(%rsp)
leaq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0xf326
cmpb $0x0, 0xc(%rsp)
je 0xc92b
movq 0xe6ec(%rip), %rdi      # 0x1afc8
leaq 0x637f(%rip), %rsi      # 0x12c62
callq 0x3250
movq %rax, %r14
movq 0x60(%rbx), %rsi
addq $0x8, %rsi
leaq 0x38(%rsp), %rdi
callq 0x8da4
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x3210
leaq 0x59e4(%rip), %rsi      # 0x122f5
movq %rax, %rdi
callq 0x3250
leaq 0x38(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0xca60
leaq 0x10(%rsp), %rdi
callq 0x8f1a
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xc95f
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3110
jmp 0xc962
jmp 0xc95f
jmp 0xc95f
jmp 0xc95f
jmp 0xc95f
jmp 0xc95f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8f1a
movq %rbx, %rdi
callq 0x3450