// File: devanshdalal[P]cloud-computing-concepts-part-2/MP1Node.cpp
// Function: MP1Node::PingK(int)
void MP1Node::PingK(int node_id)
{
    // std::cout << "Start PingK " << this_node_ << " " << node_id << "\n";
    // const auto current_time = par->getcurrtime();
    for (int i = 0; i < SWIMK; ++i)
    {
        int new_x = rand() % memberNode->memberList.size();
        auto &e = memberNode->memberList[new_x];
        const int &new_id = e.getid();
        if (new_id == this_node_ || new_id == node_id)
        {
            --i;
            continue;
        }
        const int &port = e.getport();
        Address a(std::to_string(new_id) + ":" + std::to_string(port));
        MessageHdr *msg = CreateMessage(PING, this_node_, node_id);
        if (!emulNet->ENsend(&memberNode->addr, &a, (char *)msg, MSGSIZE))
        {
            // std::cout << "SWIMK ENsend failed\n";
            // failed to send to this node.
        }
        else
        {
            // std::cout << "SWIMK ENsend passed" << new_id << "\n";
        }
        free(msg);
    }
    // std::cout << "End PingK\n";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0x10(%rbp)
cmpl $0x2, -0x10(%rbp)
jge 0x6495
callq 0x40e0
movl %eax, %ecx
movq -0xe8(%rbp), %rax
movslq %ecx, %rcx
movq %rcx, -0xf0(%rbp)
movq 0x20(%rax), %rdi
addq $0x28, %rdi
callq 0x6ad0
movq %rax, %rcx
movq -0xf0(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq -0xe8(%rbp), %rax
movl %edx, %ecx
movl %ecx, -0x14(%rbp)
movq 0x20(%rax), %rdi
addq $0x28, %rdi
movslq -0x14(%rbp), %rsi
callq 0x6b00
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x172a0
movq -0xe8(%rbp), %rcx
movl %eax, -0x2c(%rbp)
leaq -0x2c(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl 0xa4(%rcx), %eax
je 0x62e7
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0x62f5
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x6487
movq -0x20(%rbp), %rdi
callq 0x172b0
cwtl
movl %eax, -0x3c(%rbp)
leaq -0x3c(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %esi
leaq -0xa8(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x67e0
movq -0xf8(%rbp), %rsi
leaq 0x24ab0(%rip), %rdx     # 0x2ade1
leaq -0x88(%rbp), %rdi
callq 0x6790
jmp 0x633f
movq -0x38(%rbp), %rax
movl (%rax), %esi
leaq -0xd8(%rbp), %rdi
callq 0x67e0
jmp 0x6353
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
callq 0x6e20
jmp 0x636c
leaq -0x42(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x6900
jmp 0x637b
leaq -0x68(%rbp), %rdi
callq 0x41e0
leaq -0xd8(%rbp), %rdi
callq 0x41e0
leaq -0x88(%rbp), %rdi
callq 0x41e0
leaq -0xa8(%rbp), %rdi
callq 0x41e0
movq -0xe8(%rbp), %rax
movl 0xa4(%rax), %esi
movl -0xc(%rbp), %edx
movl $0x3, %edi
callq 0x6010
movq %rax, %rcx
movq -0xe8(%rbp), %rax
movq %rcx, -0xe0(%rbp)
movq 0x8(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0xe0(%rbp), %rcx
leaq -0x42(%rbp), %rdx
movl $0xc, %r8d
callq 0xcad0
cmpl $0x0, %eax
jne 0x6479
jmp 0x647b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x646b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x645f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x6453
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x41e0
leaq -0xd8(%rbp), %rdi
callq 0x41e0
leaq -0x88(%rbp), %rdi
callq 0x41e0
leaq -0xa8(%rbp), %rdi
callq 0x41e0
jmp 0x649e
jmp 0x647b
movq -0xe0(%rbp), %rdi
callq 0x43e0
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x6254
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x4570
nopw (%rax,%rax)