// File: 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
subq $0x1d0, %rsp            # imm = 0x1D0
leaq 0x10(%rbp), %rax
movq %rax, -0x128(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x120(%rbp)
movl (%rax), %eax
subl $0x4, %eax
sete -0xd(%rbp)
leaq 0x90e8(%rip), %rsi      # 0x1cb60
callq 0x43f0
movq %rax, %rdi
movq -0x128(%rbp), %rax
movl 0xd8(%rax), %esi
callq 0x4670
movq %rax, %rdi
leaq 0x8cf0(%rip), %rsi      # 0x1c78c
callq 0x43f0
movq %rax, %rdi
movq -0x128(%rbp), %rax
movl 0xdc(%rax), %esi
callq 0x4670
movq %rax, %rdi
movq 0x134e8(%rip), %rsi     # 0x26fa8
callq 0x4410
leaq -0x28(%rbp), %rdi
callq 0xb510
movq -0x120(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb5f0
jmp 0x13ae3
movq -0x120(%rbp), %rax
movq $0x0, -0x40(%rbp)
addq $0x210, %rax            # imm = 0x210
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x164c0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x164f0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x16520
testb $0x1, %al
jne 0x13b29
jmp 0x13b8a
leaq -0x50(%rbp), %rdi
callq 0x16550
movq %rax, %rcx
movq -0x128(%rbp), %rax
movq %rcx, -0x60(%rbp)
movq -0x60(%rbp), %rcx
movl 0x20(%rcx), %edi
movl 0xd8(%rax), %esi
movl 0xdc(%rax), %edx
callq 0x14060
testb $0x1, %al
jne 0x13b5e
jmp 0x13b7d
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x13b7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1404b
jmp 0x13b7f
leaq -0x50(%rbp), %rdi
callq 0x16570
jmp 0x13b16
movq -0x128(%rbp), %rsi
movq -0x40(%rbp), %rax
movq %rax, 0xd0(%rsi)
movl -0xc(%rbp), %edi
movl $0xe0, %edx
callq 0x4190
jmp 0x13bab
movq -0x120(%rbp), %rax
addq $0x210, %rax            # imm = 0x210
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x164c0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x164f0
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x16520
testb $0x1, %al
jne 0x13bec
jmp 0x13e29
leaq -0x70(%rbp), %rdi
callq 0x16550
movq %rax, %rcx
movq -0x128(%rbp), %rax
movq %rcx, -0x80(%rbp)
movq -0x80(%rbp), %rcx
movl 0x20(%rcx), %edi
movl 0xd8(%rax), %esi
movl 0xdc(%rax), %edx
callq 0x14060
testb $0x1, %al
jne 0x13c24
jmp 0x13e19
movq -0x128(%rbp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, -0x130(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x150(%rbp)
movups %xmm0, 0xb4(%rax)
movups %xmm0, 0xa4(%rax)
movups %xmm0, 0x94(%rax)
movups %xmm0, 0x84(%rax)
movups %xmm0, 0x74(%rax)
movups %xmm0, 0x64(%rax)
movups %xmm0, 0x54(%rax)
movups %xmm0, 0x44(%rax)
movups %xmm0, 0x34(%rax)
movups %xmm0, 0x24(%rax)
movups %xmm0, 0x14(%rax)
movups %xmm0, 0x4(%rax)
movq $0x0, 0xc4(%rax)
movq -0x80(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x4590
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x40c0
jmp 0x13cb6
movq -0x120(%rbp), %rdi
leaq 0x8e9c(%rip), %rsi      # 0x1cb60
callq 0x43f0
movq %rax, -0x158(%rbp)
jmp 0x13cd2
movq -0x158(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x4370
movq %rax, -0x160(%rbp)
jmp 0x13ceb
movq -0x160(%rbp), %rdi
leaq 0x8e79(%rip), %rsi      # 0x1cb72
callq 0x43f0
movq %rax, -0x168(%rbp)
jmp 0x13d07
movq -0x168(%rbp), %rdi
movzbl -0xd(%rbp), %esi
andl $0x1, %esi
callq 0x46c0
movq %rax, -0x170(%rbp)
jmp 0x13d23
movq -0x170(%rbp), %rdi
movq 0x13277(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x13d38
movq -0x80(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x41e0
jmp 0x13d4a
movq -0x120(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x13690
movq %rax, -0x178(%rbp)
jmp 0x13d6d
movq -0x128(%rbp), %rax
movq -0x178(%rbp), %rcx
movq %rcx, 0xd0(%rax)
leaq -0xa8(%rbp), %rdi
callq 0x42a0
movq -0x128(%rbp), %rsi
movl -0xc(%rbp), %edi
movl $0xe0, %edx
callq 0x4190
jmp 0x13da4
movq -0x128(%rbp), %rax
movl -0xc(%rbp), %edi
movq -0x88(%rbp), %rsi
movq 0xd0(%rax), %rdx
callq 0x1ad90
jmp 0x13dc3
movq -0x88(%rbp), %rax
movq %rax, -0x180(%rbp)
cmpq $0x0, %rax
je 0x13de3
movq -0x180(%rbp), %rdi
callq 0x43c0
testb $0x1, -0xd(%rbp)
je 0x13e17
movq -0x80(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xb9e0
jmp 0x13df8
jmp 0x13e17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x42a0
jmp 0x1404b
jmp 0x13e19
jmp 0x13e1b
leaq -0x70(%rbp), %rdi
callq 0x16570
jmp 0x13bd6
movl $0x0, -0xac(%rbp)
movslq -0xac(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xd610
movq %rax, %rcx
movq -0x188(%rbp), %rax
cmpq %rcx, %rax
jae 0x14020
movslq -0xac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x16590
movq %rax, -0x190(%rbp)
jmp 0x13e76
movq -0x190(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
callq 0x41e0
jmp 0x13e8b
movq -0x120(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x12a20
movb %al, -0x191(%rbp)
jmp 0x13ea6
leaq -0xd0(%rbp), %rdi
callq 0x42a0
movb -0x191(%rbp), %al
testb $0x1, %al
jne 0x13ec1
jmp 0x13f54
movq -0x120(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movslq -0xac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x16590
movq %rax, -0x1a0(%rbp)
jmp 0x13ee8
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rdi
callq 0x4370
movq %rax, -0x1b0(%rbp)
jmp 0x13f04
movq -0x1b0(%rbp), %rdi
leaq 0x8b99(%rip), %rsi      # 0x1caab
callq 0x43f0
movq %rax, -0x1b8(%rbp)
jmp 0x13f20
movq -0x1b8(%rbp), %rdi
movq 0x1307a(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x13f35
jmp 0x13f54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x42a0
jmp 0x1404b
movq -0x120(%rbp), %rax
addq $0x210, %rax            # imm = 0x210
movq %rax, -0x1c8(%rbp)
movslq -0xac(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x16590
movq %rax, -0x1c0(%rbp)
jmp 0x13f81
movq -0x1c0(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x41e0
jmp 0x13f96
leaq -0xf8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x142e0
jmp 0x13fab
movq -0x1c8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x165d0
jmp 0x13fc0
leaq -0xf8(%rbp), %rdi
callq 0x166f0
leaq -0x118(%rbp), %rdi
callq 0x42a0
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x13e33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x14012
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x166f0
leaq -0x118(%rbp), %rdi
callq 0x42a0
jmp 0x1404b
movq -0x120(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb620
jmp 0x14035
leaq -0x28(%rbp), %rdi
callq 0xb870
movb $0x1, %al
andb $0x1, %al
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0xb870
movq -0x30(%rbp), %rdi
callq 0x46a0
nopl (%rax)