// File: luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::detectLeave(Node)
bool FileSystem::detectLeave( Node leaveNode )
{    
    int positionClock = deleteFromVirtualRing( leaveNode );
    
    virtualRingLock.lock();
    int positionAntiClock = virtualRing.size() + 1 - positionClock;

    // cout<<"node "<< leaveNode.ip_str<<" leaving "<<positionAntiClock<<" before me."<<endl;
    bool success = false;

    int minKey, maxKey;
    if( positionAntiClock == 3 ){
        VirtualNode target = nNodeBefore(1, myPosition);
        minKey = nNodeBefore(3, myPosition).key;
        maxKey = hashString(leaveNode.ip_str);
        success = pullFileFromRange(  target.ip_str, minKey, maxKey, false);
        // cout<<success<<" pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;    
        logFile<<success<<" detectLeave pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;    
    }
    else if( positionAntiClock == 2 ){
        VirtualNode target = nNodeBefore(1, myPosition);
        minKey = nNodeBefore(3, myPosition).key;
        maxKey = nNodeBefore(2, myPosition).key;
        success = pullFileFromRange(  target.ip_str, minKey, maxKey, false);   
        // cout<<success<<" pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;
        logFile<<success<<" detectLeave pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;    
    }
    else if( positionAntiClock == 1 ){
        VirtualNode target = nNodeBefore(1, myPosition);
        minKey = nNodeBefore(3, myPosition).key;
        maxKey = nNodeBefore(2, myPosition).key;
        success = pullFileFromRange(  target.ip_str, minKey, maxKey, false);
        // cout<<success<<" pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;
        logFile<<success<<" detectLeave pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;    
    }

    if(!success){
        VirtualNode target = nNodeBefore(2, myPosition);
        success = pullFileFromRange(  target.ip_str, minKey, maxKey, false);
        // cout<<success<<" pull key second from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;
        logFile<<success<<" detectLeave pull key from "<<minKey<<" to "<<maxKey<<" from "<<target.ip_str<<" "<<target.key<<endl;    
    }

    virtualRingLock.unlock();

    return success;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rsi, -0x288(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0xb8f0
movq -0x280(%rbp), %rdi
movq -0x278(%rbp), %rsi
callq 0x14ac0
movl %eax, -0x26c(%rbp)
jmp 0x15840
leaq -0x40(%rbp), %rdi
callq 0xb7f0
movl -0x26c(%rbp), %eax
movq -0x280(%rbp), %rdi
movl %eax, -0x14(%rbp)
addq $0x268, %rdi            # imm = 0x268
callq 0xb5f0
movq -0x280(%rbp), %rdi
addq $0x250, %rdi            # imm = 0x250
callq 0x16470
addq $0x1, %rax
movslq -0x14(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x50(%rbp)
movb $0x0, -0x51(%rbp)
cmpl $0x3, -0x50(%rbp)
jne 0x15ad5
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x88(%rbp), %rdi
movl $0x1, %edx
callq 0x156b0
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0xb0(%rbp), %rdi
movl $0x3, %edx
callq 0x156b0
jmp 0x158d2
movl -0x90(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x163d0
movq -0x288(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x41e0
jmp 0x158fc
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x10600
movq -0x290(%rbp), %rdi
movl %eax, -0x5c(%rbp)
callq 0x42a0
leaq -0xf0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x41e0
jmp 0x15933
movq -0x280(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
leaq -0xf0(%rbp), %rsi
xorl %r8d, %r8d
callq 0x144c0
movb %al, -0x291(%rbp)
jmp 0x15957
movb -0x291(%rbp), %al
andb $0x1, %al
movb %al, -0x51(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x42a0
movq -0x280(%rbp), %rdi
movzbl -0x51(%rbp), %esi
andl $0x1, %esi
callq 0x46c0
movq %rax, -0x2a0(%rbp)
jmp 0x1598a
movq -0x2a0(%rbp), %rdi
leaq 0x7384(%rip), %rsi      # 0x1cd1c
callq 0x43f0
movq %rax, -0x2a8(%rbp)
jmp 0x159a6
movq -0x2a8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x4670
movq %rax, -0x2b0(%rbp)
jmp 0x159be
movq -0x2b0(%rbp), %rdi
leaq 0x6dc0(%rip), %rsi      # 0x1c78c
callq 0x43f0
movq %rax, -0x2b8(%rbp)
jmp 0x159da
movq -0x2b8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x4670
movq %rax, -0x2c0(%rbp)
jmp 0x159f2
movq -0x2c0(%rbp), %rdi
leaq 0x7315(%rip), %rsi      # 0x1cd15
callq 0x43f0
movq %rax, -0x2c8(%rbp)
jmp 0x15a0e
movq -0x2c8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x4370
movq %rax, -0x2d0(%rbp)
jmp 0x15a2a
movq -0x2d0(%rbp), %rdi
leaq 0x68ea(%rip), %rsi      # 0x1c322
callq 0x43f0
movq %rax, -0x2d8(%rbp)
jmp 0x15a46
movq -0x2d8(%rbp), %rdi
movl -0x68(%rbp), %esi
callq 0x4670
movq %rax, -0x2e0(%rbp)
jmp 0x15a5e
movq -0x2e0(%rbp), %rdi
movq 0x1153c(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x15a73
leaq -0x88(%rbp), %rdi
callq 0x163d0
jmp 0x15f3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xb7f0
jmp 0x16124
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x15ac4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x42a0
leaq -0x88(%rbp), %rdi
callq 0x163d0
jmp 0x16124
cmpl $0x2, -0x50(%rbp)
jne 0x15d07
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x118(%rbp), %rdi
movl $0x1, %edx
callq 0x156b0
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x140(%rbp), %rdi
movl $0x3, %edx
callq 0x156b0
jmp 0x15b1d
movl -0x120(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x163d0
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x168(%rbp), %rdi
movl $0x2, %edx
callq 0x156b0
jmp 0x15b52
movl -0x148(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x163d0
leaq -0x188(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x41e0
jmp 0x15b7c
movq -0x280(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
leaq -0x188(%rbp), %rsi
xorl %r8d, %r8d
callq 0x144c0
movb %al, -0x2e1(%rbp)
jmp 0x15ba0
movb -0x2e1(%rbp), %al
andb $0x1, %al
movb %al, -0x51(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x42a0
movq -0x280(%rbp), %rdi
movzbl -0x51(%rbp), %esi
andl $0x1, %esi
callq 0x46c0
movq %rax, -0x2f0(%rbp)
jmp 0x15bd3
movq -0x2f0(%rbp), %rdi
leaq 0x713b(%rip), %rsi      # 0x1cd1c
callq 0x43f0
movq %rax, -0x2f8(%rbp)
jmp 0x15bef
movq -0x2f8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x4670
movq %rax, -0x300(%rbp)
jmp 0x15c07
movq -0x300(%rbp), %rdi
leaq 0x6b77(%rip), %rsi      # 0x1c78c
callq 0x43f0
movq %rax, -0x308(%rbp)
jmp 0x15c23
movq -0x308(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x4670
movq %rax, -0x310(%rbp)
jmp 0x15c3b
movq -0x310(%rbp), %rdi
leaq 0x70cc(%rip), %rsi      # 0x1cd15
callq 0x43f0
movq %rax, -0x318(%rbp)
jmp 0x15c57
movq -0x318(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x4370
movq %rax, -0x320(%rbp)
jmp 0x15c73
movq -0x320(%rbp), %rdi
leaq 0x66a1(%rip), %rsi      # 0x1c322
callq 0x43f0
movq %rax, -0x328(%rbp)
jmp 0x15c8f
movq -0x328(%rbp), %rdi
movl -0xf8(%rbp), %esi
callq 0x4670
movq %rax, -0x330(%rbp)
jmp 0x15caa
movq -0x330(%rbp), %rdi
movq 0x112f0(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x15cbf
leaq -0x118(%rbp), %rdi
callq 0x163d0
jmp 0x15f38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x15cf6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x42a0
leaq -0x118(%rbp), %rdi
callq 0x163d0
jmp 0x16124
cmpl $0x1, -0x50(%rbp)
jne 0x15f36
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x1b0(%rbp), %rdi
movl $0x1, %edx
callq 0x156b0
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x1d8(%rbp), %rdi
movl $0x3, %edx
callq 0x156b0
jmp 0x15d4f
movl -0x1b8(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x163d0
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x200(%rbp), %rdi
movl $0x2, %edx
callq 0x156b0
jmp 0x15d84
movl -0x1e0(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x163d0
leaq -0x220(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x41e0
jmp 0x15dae
movq -0x280(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
leaq -0x220(%rbp), %rsi
xorl %r8d, %r8d
callq 0x144c0
movb %al, -0x331(%rbp)
jmp 0x15dd2
movb -0x331(%rbp), %al
andb $0x1, %al
movb %al, -0x51(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x42a0
movq -0x280(%rbp), %rdi
movzbl -0x51(%rbp), %esi
andl $0x1, %esi
callq 0x46c0
movq %rax, -0x340(%rbp)
jmp 0x15e05
movq -0x340(%rbp), %rdi
leaq 0x6f09(%rip), %rsi      # 0x1cd1c
callq 0x43f0
movq %rax, -0x348(%rbp)
jmp 0x15e21
movq -0x348(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x4670
movq %rax, -0x350(%rbp)
jmp 0x15e39
movq -0x350(%rbp), %rdi
leaq 0x6945(%rip), %rsi      # 0x1c78c
callq 0x43f0
movq %rax, -0x358(%rbp)
jmp 0x15e55
movq -0x358(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x4670
movq %rax, -0x360(%rbp)
jmp 0x15e6d
movq -0x360(%rbp), %rdi
leaq 0x6e9a(%rip), %rsi      # 0x1cd15
callq 0x43f0
movq %rax, -0x368(%rbp)
jmp 0x15e89
movq -0x368(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x4370
movq %rax, -0x370(%rbp)
jmp 0x15ea5
movq -0x370(%rbp), %rdi
leaq 0x646f(%rip), %rsi      # 0x1c322
callq 0x43f0
movq %rax, -0x378(%rbp)
jmp 0x15ec1
movq -0x378(%rbp), %rdi
movl -0x190(%rbp), %esi
callq 0x4670
movq %rax, -0x380(%rbp)
jmp 0x15edc
movq -0x380(%rbp), %rdi
movq 0x110be(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x15ef1
leaq -0x1b0(%rbp), %rdi
callq 0x163d0
jmp 0x15f36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x15f25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x42a0
leaq -0x1b0(%rbp), %rdi
callq 0x163d0
jmp 0x16124
jmp 0x15f38
jmp 0x15f3a
testb $0x1, -0x51(%rbp)
jne 0x16103
movq -0x280(%rbp), %rsi
movl 0x290(%rsi), %ecx
leaq -0x248(%rbp), %rdi
movq %rdi, -0x388(%rbp)
movl $0x2, %edx
callq 0x156b0
movq -0x388(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x41e0
jmp 0x15f7e
movq -0x280(%rbp), %rdi
movl -0x58(%rbp), %edx
movl -0x5c(%rbp), %ecx
leaq -0x268(%rbp), %rsi
xorl %r8d, %r8d
callq 0x144c0
movb %al, -0x389(%rbp)
jmp 0x15fa2
movb -0x389(%rbp), %al
andb $0x1, %al
movb %al, -0x51(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x42a0
movq -0x280(%rbp), %rdi
movzbl -0x51(%rbp), %esi
andl $0x1, %esi
callq 0x46c0
movq %rax, -0x398(%rbp)
jmp 0x15fd5
movq -0x398(%rbp), %rdi
leaq 0x6d39(%rip), %rsi      # 0x1cd1c
callq 0x43f0
movq %rax, -0x3a0(%rbp)
jmp 0x15ff1
movq -0x3a0(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x4670
movq %rax, -0x3a8(%rbp)
jmp 0x16009
movq -0x3a8(%rbp), %rdi
leaq 0x6775(%rip), %rsi      # 0x1c78c
callq 0x43f0
movq %rax, -0x3b0(%rbp)
jmp 0x16025
movq -0x3b0(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x4670
movq %rax, -0x3b8(%rbp)
jmp 0x1603d
movq -0x3b8(%rbp), %rdi
leaq 0x6cca(%rip), %rsi      # 0x1cd15
callq 0x43f0
movq %rax, -0x3c0(%rbp)
jmp 0x16059
movq -0x3c0(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x4370
movq %rax, -0x3c8(%rbp)
jmp 0x16075
movq -0x3c8(%rbp), %rdi
leaq 0x629f(%rip), %rsi      # 0x1c322
callq 0x43f0
movq %rax, -0x3d0(%rbp)
jmp 0x16091
movq -0x3d0(%rbp), %rdi
movl -0x228(%rbp), %esi
callq 0x4670
movq %rax, -0x3d8(%rbp)
jmp 0x160ac
movq -0x3d8(%rbp), %rdi
movq 0x10eee(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x160c1
leaq -0x248(%rbp), %rdi
callq 0x163d0
jmp 0x16103
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x160f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x42a0
leaq -0x248(%rbp), %rdi
callq 0x163d0
jmp 0x16124
movq -0x280(%rbp), %rdi
addq $0x268, %rdi            # imm = 0x268
callq 0xb620
movb -0x51(%rbp), %al
andb $0x1, %al
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x46a0
nopl (%rax)