// File: 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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x12bc0
movq -0xa0(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x1c5f0
jmp 0x1c1f5
leaq -0x20(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
leaq -0x38(%rbp), %rsi
movq %rsi, -0xb0(%rbp)
callq 0x12c00
movq -0xb0(%rbp), %rdi
callq 0x12c30
movq -0xa8(%rbp), %rdi
callq 0x20dc0
movq -0xa8(%rbp), %rdi
movq %rax, -0x50(%rbp)
callq 0x20df0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x20ee0
jmp 0x1c24b
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x60(%rbp)
movq -0xa0(%rbp), %rdi
movslq -0x60(%rbp), %rax
movq %rax, -0xb8(%rbp)
addq $0x30, %rdi
callq 0x12c90
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1c300
movq -0xa0(%rbp), %rdi
addq $0x30, %rdi
movslq -0x60(%rbp), %rsi
callq 0x15460
movq %rax, %rdi
callq 0x25a90
movq %rax, -0xc0(%rbp)
jmp 0x1c2a8
movq -0xc0(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq 0x60(%rax), %rsi
addq $0x8, %rsi
callq 0x17130
movb %al, -0xc1(%rbp)
jmp 0x1c2cb
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x1c2d7
jmp 0x1c2f0
movl -0x60(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x1c300
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1c5d5
jmp 0x1c2f2
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1c259
movl $0xffffffff, -0x64(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x68(%rbp)
movslq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x12c90
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x1c396
movslq -0x68(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x15460
movq %rax, %rdi
callq 0x25a90
movq %rax, -0xd8(%rbp)
jmp 0x1c34f
movq -0xd8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq 0x60(%rax), %rsi
addq $0x8, %rsi
callq 0x17130
movb %al, -0xd9(%rbp)
jmp 0x1c372
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x1c37e
jmp 0x1c386
movl -0x68(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x1c396
jmp 0x1c388
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x1c30e
movq -0xa0(%rbp), %rdi
movb $0x0, -0x69(%rbp)
addq $0x30, %rdi
callq 0x12c90
cmpq $0x3, %rax
jb 0x1c503
movl $0xfffffffd, -0x70(%rbp) # imm = 0xFFFFFFFD
cmpl $0x3, -0x70(%rbp)
jge 0x1c501
movq -0xa0(%rbp), %rdi
movl -0x5c(%rbp), %eax
movl -0x70(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x114(%rbp)
addq $0x30, %rdi
movq %rdi, -0x120(%rbp)
callq 0x12c90
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl -0x114(%rbp), %eax
addl %ecx, %eax
cltq
movq %rax, -0x110(%rbp)
callq 0x12c90
movq %rax, %rcx
movq -0x110(%rbp), %rax
xorl %edx, %edx
movq %rdx, -0x100(%rbp)
divq %rcx
movl %edx, %eax
movl %eax, -0x74(%rbp)
movl -0x64(%rbp), %eax
movl -0x70(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x104(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x12c90
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl -0x104(%rbp), %eax
addl %ecx, %eax
cltq
movq %rax, -0xf0(%rbp)
callq 0x12c90
movq -0x100(%rbp), %rdx
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movq -0xf0(%rbp), %rax
divq %rcx
movl %edx, %eax
movl %eax, -0x78(%rbp)
movslq -0x78(%rbp), %rsi
callq 0x15460
movq %rax, %rdi
callq 0x25a90
movq %rax, -0xe8(%rbp)
jmp 0x1c49d
movq -0xa0(%rbp), %rdi
addq $0x30, %rdi
movslq -0x74(%rbp), %rsi
callq 0x15460
movq %rax, %rdi
callq 0x25a90
movq %rax, -0x128(%rbp)
jmp 0x1c4c2
movq -0x128(%rbp), %rsi
movq -0xe8(%rbp), %rdi
callq 0x17130
movb %al, -0x129(%rbp)
jmp 0x1c4dd
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x1c4e9
jmp 0x1c4eb
jmp 0x1c4f1
movb $0x1, -0x69(%rbp)
jmp 0x1c501
jmp 0x1c4f3
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x1c3bb
jmp 0x1c503
movq -0xa0(%rbp), %rdi
addq $0x30, %rdi
leaq -0x20(%rbp), %rsi
callq 0x20f20
jmp 0x1c519
testb $0x1, -0x69(%rbp)
je 0x1c5c3
movq 0x1da9e(%rip), %rdi     # 0x39fc8
leaq 0xe791(%rip), %rsi      # 0x2acc2
callq 0x42f0
movq %rax, -0x138(%rbp)
jmp 0x1c53f
movq -0xa0(%rbp), %rax
movq 0x60(%rax), %rsi
addq $0x8, %rsi
leaq -0x98(%rbp), %rdi
callq 0x12740
jmp 0x1c55c
movq -0x138(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x42a0
movq %rax, -0x140(%rbp)
jmp 0x1c578
movq -0x140(%rbp), %rdi
leaq 0xddbf(%rip), %rsi      # 0x2a345
callq 0x42f0
jmp 0x1c58d
leaq -0x98(%rbp), %rdi
callq 0x41e0
movq -0xa0(%rbp), %rdi
callq 0x1c780
jmp 0x1c5a7
jmp 0x1c5c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x41e0
jmp 0x1c5d5
leaq -0x20(%rbp), %rdi
callq 0x12c30
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
leaq -0x20(%rbp), %rdi
callq 0x12c30
movq -0x40(%rbp), %rdi
callq 0x4570
nopw (%rax,%rax)