// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %r8d, %r13d
movl %ecx, %r12d
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x437e(%rip), %rsi      # 0xfc92
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
leaq 0x4389(%rip), %rsi      # 0xfcb4
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r15d, %esi
callq 0x45a0
leaq 0x3e47(%rip), %rsi      # 0xf78c
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r12d, %esi
callq 0x45a0
leaq 0x4202(%rip), %rsi      # 0xfb61
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
movl %r13d, %esi
callq 0x4560
movq %rax, %rdi
callq 0x41d0
movq (%r14), %rdi
movl 0x200(%rbx), %esi
leaq -0x2c(%rbp), %rdx
callq 0xddff
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0xb9ce
leaq 0x4322(%rip), %rsi      # 0xfcbc
movq %rbx, %rdi
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
cmpl $0x0, -0x34(%rbp)
sete %al
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, -0x40(%rbp)
movzbl %r13b, %eax
addl $0x3, %eax
leaq -0x160(%rbp), %rbx
movl %eax, (%rbx)
movl %r15d, 0xd8(%rbx)
movl %r12d, 0xdc(%rbx)
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
movq %rbx, %rsi
callq 0x4150
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
movq %rbx, %rsi
callq 0x4490
movq 0xd0(%rbx), %rbx
leaq -0x80(%rbp), %r13
leaq -0x60(%rbp), %r15
subq $0x1, %rbx
jb 0xb9b5
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
leaq -0x160(%rbp), %rsi
callq 0x4490
movq %r13, %rdi
leaq -0x15c(%rbp), %rsi
leaq -0x2d(%rbp), %rdx
callq 0x4170
movq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x4030
movq %rax, %r12
movl -0x2c(%rbp), %edi
movq %rax, %rsi
movq %r14, %rdx
callq 0xe0b0
movq %r15, %rdi
movq %r13, %rsi
callq 0x4450
movq -0x90(%rbp), %rcx
movq -0x40(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xafda
movq %r15, %rdi
callq 0x41b0
movq %r12, %rdi
callq 0x4350
movq %r13, %rdi
callq 0x41b0
jmp 0xba1f
jmp 0xbaba
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x41b0
jmp 0xbabd
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0