// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x128(%rbp), %r15
movq %r15, %rdi
callq 0x8734
movq %r14, %rdi
movq %r15, %rsi
callq 0xbbfc
movl %eax, %r15d
leaq -0x128(%rbp), %rdi
callq 0x41b0
leaq 0x268(%r14), %r13
movq %r13, %rdi
callq 0x8608
movq 0x258(%r14), %rax
subq 0x250(%r14), %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
subl %r15d, %eax
movq %r13, -0x58(%rbp)
je 0xc5fb
cmpl $0x1, %eax
je 0xc4e8
cmpl $0x2, %eax
jne 0xc71d
movl 0x290(%r14), %ecx
leaq -0x50(%rbp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0xc2a4
movl 0x290(%r14), %ecx
leaq -0x80(%rbp), %rdi
pushq $0x3
popq %rdx
movq %r14, %rsi
callq 0xc2a4
leaq -0x80(%rbp), %rdi
movl 0x20(%rdi), %r15d
callq 0x41b0
leaq -0x148(%rbp), %rdi
movq %rbx, %rsi
callq 0x4450
leaq -0x148(%rbp), %rbx
movq %rbx, %rdi
callq 0x95d4
movl %eax, %r12d
movq %rbx, %rdi
callq 0x41b0
leaq -0x100(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4450
leaq -0x100(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0xb8ea
movl %eax, %ebx
leaq -0x100(%rbp), %rdi
callq 0x41b0
movzbl %bl, %esi
movq %r14, %rdi
callq 0x4560
leaq 0x3892(%rip), %rsi      # 0xfd0b
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r15d, %esi
callq 0x45a0
leaq 0x32f9(%rip), %rsi      # 0xf78c
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r12d, %esi
callq 0x45a0
leaq 0x3857(%rip), %rsi      # 0xfd04
movq %rax, %rdi
callq 0x4370
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x4300
leaq 0x2e5a(%rip), %rsi      # 0xf322
movq %rax, %rdi
callq 0x4370
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
jmp 0xc709
movl 0x290(%r14), %ecx
leaq -0x50(%rbp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0xc2a4
movl 0x290(%r14), %ecx
leaq -0x80(%rbp), %rdi
pushq $0x3
popq %rdx
movq %r14, %rsi
callq 0xc2a4
leaq -0x80(%rbp), %rdi
movl 0x20(%rdi), %r15d
callq 0x41b0
movl 0x290(%r14), %ecx
leaq -0x80(%rbp), %rdi
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0xc2a4
leaq -0x80(%rbp), %rdi
movl 0x20(%rdi), %r12d
callq 0x41b0
leaq -0xe0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4450
leaq -0xe0(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0xb8ea
movl %eax, %ebx
leaq -0xe0(%rbp), %rdi
callq 0x41b0
movzbl %bl, %esi
movq %r14, %rdi
callq 0x4560
leaq 0x377f(%rip), %rsi      # 0xfd0b
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r15d, %esi
callq 0x45a0
leaq 0x31e6(%rip), %rsi      # 0xf78c
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r12d, %esi
callq 0x45a0
leaq 0x3744(%rip), %rsi      # 0xfd04
movq %rax, %rdi
callq 0x4370
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x4300
leaq 0x2d47(%rip), %rsi      # 0xf322
movq %rax, %rdi
callq 0x4370
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
jmp 0xc709
movl 0x290(%r14), %ecx
leaq -0x50(%rbp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0xc2a4
movl 0x290(%r14), %ecx
leaq -0x80(%rbp), %rdi
pushq $0x3
popq %rdx
movq %r14, %rsi
callq 0xc2a4
leaq -0x80(%rbp), %rdi
movl 0x20(%rdi), %r15d
callq 0x41b0
movl 0x290(%r14), %ecx
leaq -0x80(%rbp), %rdi
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0xc2a4
leaq -0x80(%rbp), %rdi
movl 0x20(%rdi), %r12d
callq 0x41b0
leaq -0xc0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4450
leaq -0xc0(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0xb8ea
movl %eax, %ebx
leaq -0xc0(%rbp), %rdi
callq 0x41b0
movzbl %bl, %esi
movq %r14, %rdi
callq 0x4560
leaq 0x366c(%rip), %rsi      # 0xfd0b
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r15d, %esi
callq 0x45a0
leaq 0x30d3(%rip), %rsi      # 0xf78c
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r12d, %esi
callq 0x45a0
leaq 0x3631(%rip), %rsi      # 0xfd04
movq %rax, %rdi
callq 0x4370
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x4300
leaq 0x2c34(%rip), %rsi      # 0xf322
movq %rax, %rdi
callq 0x4370
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
leaq -0x50(%rbp), %rdi
callq 0x41b0
movb $0x1, %r13b
testb %bl, %bl
jne 0xc7f2
movl 0x290(%r14), %ecx
leaq -0x50(%rbp), %rbx
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xc2a4
leaq -0xa0(%rbp), %rdi
movq %rbx, %rsi
callq 0x4450
leaq -0xa0(%rbp), %rsi
movq %r14, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0xb8ea
movl %eax, %r13d
leaq -0xa0(%rbp), %rdi
callq 0x41b0
movzbl %r13b, %esi
movq %r14, %rdi
callq 0x4560
leaq 0x358c(%rip), %rsi      # 0xfd0b
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r15d, %esi
callq 0x45a0
leaq 0x2ff3(%rip), %rsi      # 0xf78c
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r12d, %esi
callq 0x45a0
leaq 0x3551(%rip), %rsi      # 0xfd04
movq %rax, %rdi
callq 0x4370
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x4300
leaq 0x2b54(%rip), %rsi      # 0xf322
movq %rax, %rdi
callq 0x4370
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
leaq -0x50(%rbp), %rdi
callq 0x41b0
movq -0x58(%rbp), %rdi
callq 0x4290
movl %r13d, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
jmp 0xc848
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
jmp 0xc848
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
jmp 0xc848
jmp 0xc861
jmp 0xc861
jmp 0xc861
jmp 0xc861
jmp 0xc861
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x41b0
jmp 0xc864
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
jmp 0xc868
jmp 0xc861
jmp 0xc861
jmp 0xc861
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0
nop