// File: /workspace/github_cpp_makefile_star2_O2/devanshdalal[P]cloud-computing-concepts-part-2/MP2Node.cpp
// Function: MP2Node::stabilizationProtocol()
void MP2Node::stabilizationProtocol()
{
	PrintRing(ring);
	std::cout << "Rehashing primary " << ht->currentSize() << "\n";
	for (auto it = ht->hashTable.begin(); it != ht->hashTable.end();)
	{
		const auto &key = it->first;
		const auto &value = it->second;
		int current = g_transID++;
		auto nodes = findNodes(key);
		bool belongs_here = false;
		for (auto node_id = 0; node_id < nodes.size(); ++node_id)
		{
			if (node_id == 0 and *(nodes[node_id].getAddress()) == memberNode->addr)
			{
				belongs_here = true;
			}
			else
			{
				auto req = Message(-current, memberNode->addr, MessageType::CREATE, key, value,
								   static_cast<ReplicaType>(node_id));
				emulNet->ENsend(&(memberNode->addr), nodes[node_id].getAddress(), req.toString());
				// local_state_[current] = {MessageType::CREATE, key, value, par->getcurrtime() + WRITE_TIMEOUT, 0};
			}
		}
		if (belongs_here)
		{
			++it;
		}
		else
		{
			it = ht->hashTable.erase(it);
		}
	}
	std::cout << "Rehashing secondary " << secondary->currentSize() << "\n";
	for (auto it = secondary->hashTable.begin(); it != secondary->hashTable.end();)
	{
		const auto &key = it->first;
		const auto &value = it->second;
		int current = g_transID++;
		auto nodes = findNodes(key);
		bool belongs_here = false;
		for (auto node_id = 0; node_id < nodes.size(); ++node_id)
		{
			if (node_id == 1 and *(nodes[node_id].getAddress()) == memberNode->addr)
			{
				belongs_here = true;
			}
			else
			{
				auto req = Message(-current, memberNode->addr, MessageType::CREATE, key, value,
								   static_cast<ReplicaType>(node_id));
				emulNet->ENsend(&(memberNode->addr), nodes[node_id].getAddress(), req.toString());
			}
		}
		if (belongs_here)
		{
			++it;
		}
		else
		{
			it = secondary->hashTable.erase(it);
		}
	}
	std::cout << "Rehashing tertiary " << tertiary->currentSize() << "\n";
	for (auto it = tertiary->hashTable.begin(); it != tertiary->hashTable.end();)
	{
		const auto &key = it->first;
		const auto &value = it->second;
		int current = g_transID++;
		auto nodes = findNodes(key);
		bool belongs_here = false;
		for (auto node_id = 0; node_id < nodes.size(); ++node_id)
		{
			if (node_id == 2 and *(nodes[node_id].getAddress()) == memberNode->addr)
			{
				belongs_here = true;
			}
			else
			{
				auto req = Message(-current, memberNode->addr, MessageType::CREATE, key, value,
								   static_cast<ReplicaType>(node_id));
				emulNet->ENsend(&(memberNode->addr), nodes[node_id].getAddress(), req.toString());
			}
		}
		if (belongs_here)
		{
			++it;
		}
		else
		{
			it = tertiary->hashTable.erase(it);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %rdi, %rbx
leaq 0x30(%rdi), %rsi
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
callq 0xf52e
movq %r14, %rdi
callq 0xc4f8
leaq 0xc8(%rsp), %rdi
callq 0x8f1a
movq 0xe524(%rip), %rdi      # 0x1afc8
leaq 0x624f(%rip), %rsi      # 0x12cfa
callq 0x3250
movq %rax, %r14
movq 0x48(%rbx), %rdi
callq 0x109aa
movq %r14, %rdi
movq %rax, %rsi
callq 0x31a0
leaq 0x5827(%rip), %rsi      # 0x122f5
movq %rax, %rdi
callq 0x3250
movq 0x48(%rbx), %rax
movq 0x20(%rax), %r12
leaq 0xa8(%rsp), %r14
leaq 0x20(%rsp), %r15
leaq 0x238(%rsp), %rbp
movq %rbx, 0x8(%rsp)
addq $0x10, %rax
cmpq %rax, %r12
je 0xccf1
leaq 0x20(%r12), %rsi
movl 0x16dab(%rip), %r13d    # 0x238bc
leal 0x1(%r13), %eax
movl %eax, 0x16da1(%rip)     # 0x238bc
movq %r14, %rdi
movq %rsi, 0x48(%rsp)
callq 0x3370
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xd54e
movq %r12, 0x10(%rsp)
leaq 0x40(%r12), %rax
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x3110
negl %r13d
movq %r13, 0x38(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0xcc91
testq %r15, %r15
jne 0xcba7
callq 0x107d2
movq 0x8(%rsp), %rcx
movq 0x60(%rcx), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xa502
movb $0x1, %cl
testb %al, %al
jne 0xcc7e
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rsi
addq $0x8, %rsi
leaq 0x62(%rsp), %rdi
callq 0xa4e4
leaq 0x198(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x3370
leaq 0x258(%rsp), %r13
movq %r13, %rdi
movq 0x40(%rsp), %rsi
callq 0x3370
movl %r12d, (%rsp)
leaq 0xe0(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x62(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x198(%rsp), %r14
movq %r14, %r8
movq %r13, %r9
callq 0x1176e
movq %r13, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3110
movq 0x8(%rsp), %rax
movq 0x60(%rax), %r13
movq 0x70(%rax), %rbx
movq 0x20(%rsp), %rdi
addq %r15, %rdi
callq 0x107d2
movq %rax, %r14
movq %rbp, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x11c40
addq $0x8, %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x5d28
movq %rbp, %rdi
callq 0x3110
leaq 0xe0(%rsp), %rdi
callq 0xf466
movq 0x18(%rsp), %rax
movl %eax, %ecx
incq %r12
addq $0x20, %r15
movl %ecx, %eax
movq %rax, 0x18(%rsp)
jmp 0xcb64
testb $0x1, 0x18(%rsp)
je 0xccb9
movq 0x10(%rsp), %rdi
callq 0x31b0
movq %rax, %r12
movq 0x8(%rsp), %rbx
leaq 0xa8(%rsp), %r14
leaq 0x20(%rsp), %r15
jmp 0xcce0
movq 0x8(%rsp), %rbx
movq 0x48(%rbx), %rdi
addq $0x8, %rdi
movq 0x10(%rsp), %rsi
callq 0x1069a
leaq 0xa8(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %rax, %r12
movq %r15, %rdi
callq 0x8f1a
movq 0x48(%rbx), %rax
jmp 0xcaf8
movq 0xe2d0(%rip), %rdi      # 0x1afc8
leaq 0x600e(%rip), %rsi      # 0x12d0d
callq 0x3250
movq %rax, %r14
movq 0x50(%rbx), %rdi
callq 0x109aa
movq %r14, %rdi
movq %rax, %rsi
callq 0x31a0
leaq 0x55d3(%rip), %rsi      # 0x122f5
movq %rax, %rdi
callq 0x3250
movq 0x50(%rbx), %rax
movq 0x20(%rax), %r12
leaq 0x88(%rsp), %r14
leaq 0x20(%rsp), %r15
leaq 0x1f8(%rsp), %rbp
addq $0x10, %rax
cmpq %rax, %r12
je 0xcf3a
leaq 0x20(%r12), %rsi
movl 0x16b5c(%rip), %r13d    # 0x238bc
leal 0x1(%r13), %eax
movl %eax, 0x16b52(%rip)     # 0x238bc
movq %r14, %rdi
movq %rsi, 0x48(%rsp)
callq 0x3370
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xd54e
movq %r12, 0x10(%rsp)
leaq 0x40(%r12), %rax
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x3110
negl %r13d
movq %r13, 0x38(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ecx, %ecx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0xcedc
movq %rcx, 0x18(%rsp)
cmpq $0x1, %r12
jne 0xcdf9
addq $0x20, %rdi
callq 0x107d2
movq 0x8(%rsp), %rcx
movq 0x60(%rcx), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xa502
movb $0x1, %cl
testb %al, %al
jne 0xced0
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rsi
addq $0x8, %rsi
leaq 0x5c(%rsp), %rdi
callq 0xa4e4
leaq 0x178(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x3370
leaq 0x218(%rsp), %r13
movq %r13, %rdi
movq 0x40(%rsp), %rsi
callq 0x3370
movl %r12d, (%rsp)
leaq 0xe0(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x5c(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x178(%rsp), %r14
movq %r14, %r8
movq %r13, %r9
callq 0x1176e
movq %r13, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3110
movq 0x8(%rsp), %rax
movq 0x60(%rax), %r13
movq 0x70(%rax), %rbx
movq 0x20(%rsp), %rdi
addq %r15, %rdi
callq 0x107d2
movq %rax, %r14
movq %rbp, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x11c40
addq $0x8, %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x5d28
movq %rbp, %rdi
callq 0x3110
leaq 0xe0(%rsp), %rdi
callq 0xf466
movq 0x18(%rsp), %rax
movl %eax, %ecx
incq %r12
addq $0x20, %r15
jmp 0xcdac
testb $0x1, %cl
je 0xcf02
movq 0x10(%rsp), %rdi
callq 0x31b0
movq %rax, %r12
movq 0x8(%rsp), %rbx
leaq 0x88(%rsp), %r14
leaq 0x20(%rsp), %r15
jmp 0xcf29
movq 0x8(%rsp), %rbx
movq 0x50(%rbx), %rdi
addq $0x8, %rdi
movq 0x10(%rsp), %rsi
callq 0x1069a
leaq 0x88(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %rax, %r12
movq %r15, %rdi
callq 0x8f1a
movq 0x50(%rbx), %rax
jmp 0xcd47
movq 0xe087(%rip), %rdi      # 0x1afc8
leaq 0x5dda(%rip), %rsi      # 0x12d22
callq 0x3250
movq %rax, %r14
movq 0x58(%rbx), %rdi
callq 0x109aa
movq %r14, %rdi
movq %rax, %rsi
callq 0x31a0
leaq 0x538a(%rip), %rsi      # 0x122f5
movq %rax, %rdi
callq 0x3250
movq 0x58(%rbx), %rax
movq 0x20(%rax), %r12
leaq 0x68(%rsp), %r14
leaq 0x20(%rsp), %r15
leaq 0x1b8(%rsp), %rbp
addq $0x10, %rax
cmpq %rax, %r12
je 0xd17a
leaq 0x20(%r12), %rsi
movl 0x16916(%rip), %r13d    # 0x238bc
leal 0x1(%r13), %eax
movl %eax, 0x1690c(%rip)     # 0x238bc
movq %r14, %rdi
movq %rsi, 0x48(%rsp)
callq 0x3370
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xd54e
movq %r12, 0x10(%rsp)
leaq 0x40(%r12), %rax
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x3110
negl %r13d
movq %r13, 0x38(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ecx, %ecx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0xd122
movq %rcx, 0x18(%rsp)
cmpq $0x2, %r12
jne 0xd03f
addq $0x40, %rdi
callq 0x107d2
movq 0x8(%rsp), %rcx
movq 0x60(%rcx), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xa502
movb $0x1, %cl
testb %al, %al
jne 0xd116
movq 0x8(%rsp), %rax
movq 0x60(%rax), %rsi
addq $0x8, %rsi
leaq 0x56(%rsp), %rdi
callq 0xa4e4
leaq 0x158(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x3370
leaq 0x1d8(%rsp), %r13
movq %r13, %rdi
movq 0x40(%rsp), %rsi
callq 0x3370
movl %r12d, (%rsp)
leaq 0xe0(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq 0x56(%rsp), %rdx
xorl %ecx, %ecx
leaq 0x158(%rsp), %r14
movq %r14, %r8
movq %r13, %r9
callq 0x1176e
movq %r13, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3110
movq 0x8(%rsp), %rax
movq 0x60(%rax), %r13
movq 0x70(%rax), %rbx
movq 0x20(%rsp), %rdi
addq %r15, %rdi
callq 0x107d2
movq %rax, %r14
movq %rbp, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x11c40
addq $0x8, %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x5d28
movq %rbp, %rdi
callq 0x3110
leaq 0xe0(%rsp), %rdi
callq 0xf466
movq 0x18(%rsp), %rax
movl %eax, %ecx
incq %r12
addq $0x20, %r15
jmp 0xcff2
testb $0x1, %cl
je 0xd145
movq 0x10(%rsp), %rdi
callq 0x31b0
movq %rax, %r12
movq 0x8(%rsp), %rbx
leaq 0x68(%rsp), %r14
leaq 0x20(%rsp), %r15
jmp 0xd169
movq 0x8(%rsp), %rbx
movq 0x58(%rbx), %rdi
addq $0x8, %rdi
movq 0x10(%rsp), %rsi
callq 0x1069a
leaq 0x68(%rsp), %r14
leaq 0x20(%rsp), %r15
movq %rax, %r12
movq %r15, %rdi
callq 0x8f1a
movq 0x58(%rbx), %rax
jmp 0xcf8d
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0xd28d
jmp 0xd273
jmp 0xd273
jmp 0xd273
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0xd1cd
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0xd1cd
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3110
jmp 0xd292
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
jmp 0xd24e
movq %rax, %rbx
jmp 0xd1f9
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x3110
leaq 0x158(%rsp), %rdi
jmp 0xd260
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
jmp 0xd24e
movq %rax, %rbx
jmp 0xd225
movq %rax, %rbx
leaq 0x218(%rsp), %rdi
callq 0x3110
leaq 0x178(%rsp), %rdi
jmp 0xd260
jmp 0xd273
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x3110
jmp 0xd258
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x3110
jmp 0xd27b
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x3110
jmp 0xd288
jmp 0xd273
jmp 0xd273
jmp 0xd278
jmp 0xd273
jmp 0xd278
jmp 0xd273
movq %rax, %rbx
jmp 0xd288
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xf466
leaq 0x20(%rsp), %rdi
callq 0x8f1a
movq %rbx, %rdi
callq 0x3450