// File: luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::pullFileFromRange(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, bool)
bool FileSystem::pullFileFromRange( std::string address, int minKey, int maxKey, bool rmRemoteFile ){
    int connectionToServer; //TCP connect to introducer/other nodes

    logFile << "pullFileFromRange: Connecting to "<< address << "... for"<< minKey <<" to "<<maxKey<<" rm:"<<rmRemoteFile << std::endl;
    // cout << "pullFileFromRange: Connecting to "<< address << "..." << std::endl;

    int ret = connect_to_server(address.c_str(), port, &connectionToServer);
    if(ret!=0)
    {
        logFile <<"ERROR pullFileFromRange: Cannot connect to "<<address<< std::endl;
        //std::cout <<"ERROR pullFileFromRange: Cannot connect to "<<address<< std::endl;
        return false;
    }

    Message_fs msg;
    if(rmRemoteFile)
        msg.type = MSG_PULL_MV;
    else
        msg.type = MSG_PULL_CP;
    msg.minKey = minKey;
    msg.maxKey = maxKey;

    write(connectionToServer, &msg, sizeof(Message_fs)); 
    read (connectionToServer, &msg, sizeof(Message_fs));    //read num of files
    size_t totalCount = msg.size;
    
    for(int i=0; i < totalCount; i++){
        
        read (connectionToServer, &msg, sizeof(Message_fs)); //read filename and size
        std::string filename(msg.filename);

        char * buffer = new char [msg.size];
        //size_t pullReadRet = read(connectionToServer, buffer,  msg.size);
        size_t pullReadRet = splitRead(connectionToServer, buffer,  msg.size);
        // cout<<"pullFileFromRange: pullReadRet "<<pullReadRet<<endl;

        saveFile( filename, buffer, msg.size );

        delete buffer;
    }
    
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rsi, -0x198(%rbp)
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x190(%rbp)
leaq 0x87a9(%rip), %rsi      # 0x1cca3
callq 0x43f0
movq -0x198(%rbp), %rsi
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
leaq 0x87ad(%rip), %rsi      # 0x1ccc5
callq 0x43f0
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x4670
movq %rax, %rdi
leaq 0x825a(%rip), %rsi      # 0x1c78c
callq 0x43f0
movq %rax, %rdi
movl -0x20(%rbp), %esi
callq 0x4670
movq %rax, %rdi
leaq 0x8626(%rip), %rsi      # 0x1cb72
callq 0x43f0
movq %rax, %rdi
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x46c0
movq %rax, %rdi
movq 0x12a3d(%rip), %rsi     # 0x26fa8
callq 0x4410
movq -0x198(%rbp), %rdi
callq 0x41d0
movq %rax, %rdi
movq -0x190(%rbp), %rax
movl 0x200(%rax), %esi
leaq -0x28(%rbp), %rdx
callq 0x1aaf0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x145d8
movq -0x190(%rbp), %rdi
leaq 0x8721(%rip), %rsi      # 0x1cccd
callq 0x43f0
movq -0x198(%rbp), %rsi
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movq 0x129de(%rip), %rsi     # 0x26fa8
callq 0x4410
movb $0x0, -0x1(%rbp)
jmp 0x147dd
testb $0x1, -0x21(%rbp)
je 0x145ea
movl $0x4, -0x110(%rbp)
jmp 0x145f4
movl $0x3, -0x110(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x28(%rbp), %edi
leaq -0x110(%rbp), %rsi
movl $0xe0, %edx
callq 0x4190
movl -0x28(%rbp), %edi
leaq -0x110(%rbp), %rsi
movl $0xe0, %edx
callq 0x4560
movq -0x40(%rbp), %rax
movq %rax, -0x118(%rbp)
movl $0x0, -0x11c(%rbp)
movslq -0x11c(%rbp), %rax
cmpq -0x118(%rbp), %rax
jae 0x147d9
movl -0x28(%rbp), %edi
leaq -0x110(%rbp), %rsi
movl $0xe0, %edx
callq 0x4560
leaq -0x10c(%rbp), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x141(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x46b0
movq -0x1a8(%rbp), %rsi
movq -0x1a0(%rbp), %rdx
leaq -0x140(%rbp), %rdi
callq 0x4510
jmp 0x146a2
leaq -0x141(%rbp), %rdi
callq 0x4430
movq -0x40(%rbp), %rdi
callq 0x4030
movq %rax, -0x1b0(%rbp)
jmp 0x146c0
movq -0x1b0(%rbp), %rax
movq %rax, -0x160(%rbp)
movl -0x28(%rbp), %edi
movq -0x160(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x1af30
movq %rax, -0x1b8(%rbp)
jmp 0x146ea
movq -0x1b8(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x41e0
jmp 0x1470d
movq -0x190(%rbp), %rdi
movq -0x160(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq -0x188(%rbp), %rsi
callq 0x13310
jmp 0x1472d
leaq -0x188(%rbp), %rdi
callq 0x42a0
movq -0x160(%rbp), %rax
movq %rax, -0x1c0(%rbp)
cmpq $0x0, %rax
je 0x14759
movq -0x1c0(%rbp), %rdi
callq 0x43c0
leaq -0x140(%rbp), %rdi
callq 0x42a0
movl -0x11c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x11c(%rbp)
jmp 0x1463d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x150(%rbp)
movl %eax, -0x154(%rbp)
leaq -0x141(%rbp), %rdi
callq 0x4430
jmp 0x147eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x150(%rbp)
movl %eax, -0x154(%rbp)
jmp 0x147cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x150(%rbp)
movl %eax, -0x154(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x42a0
leaq -0x140(%rbp), %rdi
callq 0x42a0
jmp 0x147eb
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0x150(%rbp), %rdi
callq 0x46a0
nopw (%rax,%rax)