// File: /workspace/github_cpp_makefile_star2_O2/luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::sendFileByRange(int, FileSystem::Message_fs)
bool FileSystem::sendFileByRange(int connFd, Message_fs msg )
{
    bool deleteMyCopy = ( (msg.type == MSG_PULL_MV) ? true : false );

    logFile<<"sendFileByRange: "<<msg.minKey<<" to "<<msg.maxKey<<endl;

    std::vector<std::string> toremove;
    filesLock.lock();
    size_t count = 0;
    for( auto &file : files ){
        if( inRange(file.key, msg.minKey, msg.maxKey) )
            count++;
    }
    msg.size = count;
    write(connFd, &msg, sizeof(Message_fs));
    for( auto &file : files )
    {
        if( inRange(file.key, msg.minKey, msg.maxKey) )
        {
            memset(msg.filename, '\0', 200);
            file.filename.copy(msg.filename, file.filename.length() );

            // cout<<"sendFileByRange: "<<file.filename<<" "<<msg.filename<<endl;
            logFile<<"sendFileByRange: "<<file.filename<<" rm:"<<deleteMyCopy<<endl;

            char * buffer; 
            msg.size = readFile(file.filename, &buffer);

            write(connFd, &msg, sizeof(Message_fs));
            splitWrite(connFd, buffer, msg.size);

            delete buffer;

            if(deleteMyCopy)
            {
                //files.remove( file );   //maintain file list
                toremove.push_back(file.filename);
            }
        }
    }

    for (int i = 0; i < toremove.size(); ++i)
    {
        if ( deleteFile( toremove.at(i) ) ) 
        {
            logFile << toremove.at(i) << "deleted" << std::endl;
        }
        files.remove(toremove.at(i));
    }

    filesLock.unlock();

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %esi, -0x2c(%rbp)
movq %rdi, %r15
movl 0x10(%rbp), %eax
movl %eax, -0x30(%rbp)
leaq 0x4772(%rip), %rsi      # 0xfb4f
callq 0x4370
movl 0xe8(%rbp), %ebx
movq %rax, %rdi
movl %ebx, %esi
callq 0x45a0
leaq 0x4393(%rip), %rsi      # 0xf78c
movq %rax, %rdi
callq 0x4370
movl 0xec(%rbp), %r14d
movq %rax, %rdi
movl %r14d, %esi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
leaq 0x228(%r15), %rdi
movq %rdi, -0x68(%rbp)
callq 0x8608
leaq 0x10(%rbp), %r13
movq %r15, -0x40(%rbp)
leaq 0x210(%r15), %r12
xorl %r15d, %r15d
movq %r12, -0x38(%rbp)
movq (%r12), %r12
cmpq -0x38(%rbp), %r12
je 0xb46e
movl 0x30(%r12), %edi
movl %ebx, %esi
movl %r14d, %edx
callq 0xb6f2
movzbl %al, %eax
addq %rax, %r15
jmp 0xb44d
movq %r15, 0xd0(%r13)
movl $0xe0, %edx
movl -0x2c(%rbp), %edi
movq %r13, %rsi
callq 0x4150
leaq 0x4(%r13), %rax
movq %rax, -0x70(%rbp)
leaq -0xf8(%rbp), %rbx
movq -0x38(%rbp), %r15
movq (%r15), %r15
cmpq -0x38(%rbp), %r15
je 0xb5aa
movl 0x30(%r15), %edi
movl 0xd8(%r13), %esi
movl 0xdc(%r13), %edx
callq 0xb6f2
testb %al, %al
je 0xb498
leaq 0x10(%r15), %r12
movl $0xc8, %edx
movq -0x70(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x18(%r15), %rdx
movq %r12, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x4090
movq -0x40(%rbp), %rdi
leaq 0x465c(%rip), %rsi      # 0xfb4f
callq 0x4370
movq %rax, %rdi
movq %r12, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x4654(%rip), %rsi      # 0xfb61
callq 0x4370
xorl %esi, %esi
cmpl $0x4, -0x30(%rbp)
sete %sil
movq %rax, %rdi
callq 0x4560
movq %rax, %rdi
callq 0x41d0
movq %rbx, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4450
movq -0x40(%rbp), %rdi
movq %r14, %rbx
movq %r14, %rsi
leaq -0x98(%rbp), %rdx
callq 0xb1da
movq %rax, 0xd0(%r13)
movq %rbx, %rdi
callq 0x41b0
movl $0xe0, %edx
movl -0x2c(%rbp), %edi
movq %r13, %rsi
callq 0x4150
movq -0x98(%rbp), %rbx
movq 0xd0(%r13), %rdx
movl -0x2c(%rbp), %edi
movq %rbx, %rsi
callq 0xdfc0
movq %rbx, %rdi
callq 0x4350
cmpl $0x4, -0x30(%rbp)
jne 0xb5a2
leaq -0x60(%rbp), %rdi
movq %r12, %rsi
callq 0x8780
movq %r14, %rbx
jmp 0xb498
xorl %r14d, %r14d
leaq -0x60(%rbp), %r12
leaq -0xd8(%rbp), %r13
leaq -0xb8(%rbp), %rbx
leaq -0x98(%rbp), %r15
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0xb674
movq %r12, %rdi
movq %r14, %rsi
callq 0xc9b4
movq %r13, %rdi
movq %rax, %rsi
callq 0x4450
movq %r13, %rsi
callq 0xab14
movq %r13, %rdi
callq 0x41b0
movq %r12, %rdi
movq %r14, %rsi
callq 0xc9b4
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x4478(%rip), %rsi      # 0xfa9a
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq %r12, %rdi
movq %r14, %rsi
callq 0xc9b4
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4450
movq %r15, %rdi
movq %rbx, %rsi
callq 0xb818
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0xc9d6
movq %r15, %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x41b0
incq %r14
jmp 0xb5c6
movq -0x68(%rbp), %rdi
callq 0x4290
leaq -0x60(%rbp), %rdi
callq 0x8708
movb $0x1, %al
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb6de
jmp 0xb6de
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
jmp 0xb6d1
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
jmp 0xb6d1
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x41b0
jmp 0xb6ca
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x41b0
jmp 0xb6e1
jmp 0xb6de
jmp 0xb6de
jmp 0xb6de
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x8708
movq %rbx, %rdi
callq 0x45d0