// File: zkl94[P]distributed-key-value-store/MP2Node.cpp
// Function: MP2Node::updateRing()
void MP2Node::updateRing() {
    vector<Node> curMemList;

    /*
     *  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());

    /*
     * 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 changes in the ring

    // if stabilization protocol needs to be run, cache the list (now the old list and new list are both present)
    // and then let the stabilization protocol do the work

    // for testing purpose
    // this->ring = curMemList;

    if (this->ring.size() == 0) {
        this->ring = curMemList;
        return;
    }

    // first compare the size
    if (this->ring.size() != curMemList.size()) {
        this->new_ring = curMemList;
        this->stabilizationProtocol();
        return;
    }

    // compare every node hash
    for (int i = 0; i < this->ring.size(); ++i) {
        if (this->ring[i].nodeHashCode != curMemList[i].nodeHashCode) {
            this->new_ring = curMemList;
            this->stabilizationProtocol();
            return;
        }
    }

    // it doesn't change
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x10bf0
movq -0x68(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x1a1b0
jmp 0x19fff
leaq -0x20(%rbp), %rdi
movq %rdi, -0x70(%rbp)
leaq -0x38(%rbp), %rsi
movq %rsi, -0x78(%rbp)
callq 0x10c30
movq -0x78(%rbp), %rdi
callq 0x10c60
movq -0x70(%rbp), %rdi
callq 0x1e5e0
movq -0x70(%rbp), %rdi
movq %rax, -0x50(%rbp)
callq 0x1e610
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1e5a0
jmp 0x1a046
movq -0x68(%rbp), %rdi
addq $0x30, %rdi
callq 0x10cc0
cmpq $0x0, %rax
jne 0x1a092
movq -0x68(%rbp), %rdi
addq $0x30, %rdi
leaq -0x20(%rbp), %rsi
callq 0x1e640
jmp 0x1a06c
movl $0x1, -0x5c(%rbp)
jmp 0x1a187
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x10c60
jmp 0x1a199
movq -0x68(%rbp), %rdi
addq $0x30, %rdi
callq 0x10cc0
movq %rax, -0x80(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x10cc0
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
je 0x1a0e2
movq -0x68(%rbp), %rdi
addq $0x70, %rdi
leaq -0x20(%rbp), %rsi
callq 0x1e640
jmp 0x1a0cb
movq -0x68(%rbp), %rdi
callq 0x1a340
jmp 0x1a0d6
movl $0x1, -0x5c(%rbp)
jmp 0x1a187
movl $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rdi
movslq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
addq $0x30, %rdi
callq 0x10cc0
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
jae 0x1a180
movq -0x68(%rbp), %rdi
addq $0x30, %rdi
movslq -0x60(%rbp), %rsi
callq 0x13400
movq 0x10(%rax), %rax
movq %rax, -0x90(%rbp)
movslq -0x60(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x13400
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq 0x10(%rcx), %rax
je 0x1a170
movq -0x68(%rbp), %rdi
addq $0x70, %rdi
leaq -0x20(%rbp), %rsi
callq 0x1e640
jmp 0x1a15c
movq -0x68(%rbp), %rdi
callq 0x1a340
jmp 0x1a167
movl $0x1, -0x5c(%rbp)
jmp 0x1a187
jmp 0x1a172
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x1a0e9
movl $0x1, -0x5c(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x10c60
addq $0x90, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x4550
nopw %cs:(%rax,%rax)