// File: /workspace/github_cpp_makefile_star2_O3/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 $0x148, %rsp            # imm = 0x148
movl %r8d, -0x3c(%rbp)
movl %ecx, %r13d
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x5617(%rip), %rsi      # 0x13cba
movl $0x21, %edx
callq 0x43f0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r12
leaq 0x5616(%rip), %rsi      # 0x13cdc
movl $0x7, %edx
movq %rax, %rdi
callq 0x43f0
movq %r12, %rdi
movl %r14d, -0x34(%rbp)
movl %r14d, %esi
movl -0x3c(%rbp), %r14d
callq 0x45f0
movq %rax, %r12
leaq 0x50c4(%rip), %rsi      # 0x137b4
movl $0x4, %edx
movq %rax, %rdi
callq 0x43f0
movq %r12, %rdi
movl %r13d, -0x38(%rbp)
movl %r13d, %esi
callq 0x45f0
movq %rax, %r12
leaq 0x5473(%rip), %rsi      # 0x13b89
movl $0x4, %edx
movq %rax, %rdi
callq 0x43f0
movq %r12, %rdi
movl %r14d, %esi
callq 0x45a0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movq (%r15), %rdi
movl 0x200(%rbx), %esi
leaq -0x2c(%rbp), %rdx
callq 0x1161a
movl %eax, %r12d
testl %eax, %eax
je 0xe7c3
leaq 0x556c(%rip), %rsi      # 0x13ce4
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x43f0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
jmp 0xe8f4
movl %r12d, -0x30(%rbp)
movq %rbx, -0x68(%rbp)
movzbl %r14b, %eax
addl $0x3, %eax
leaq -0x168(%rbp), %r14
movl %eax, (%r14)
movl -0x34(%rbp), %eax
movl %eax, 0xd8(%r14)
movl -0x38(%rbp), %eax
movl %eax, 0xdc(%r14)
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
movq %r14, %rsi
callq 0x4170
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
movq %r14, %rsi
callq 0x44b0
movq 0xd0(%r14), %rbx
testq %rbx, %rbx
je 0xe8f0
leaq -0x50(%rbp), %r12
leaq -0x78(%rbp), %r13
leaq -0x88(%rbp), %r14
movq %r13, %r15
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
leaq -0x168(%rbp), %rsi
callq 0x44b0
movq %r12, -0x60(%rbp)
leaq -0x164(%rbp), %r12
movq %r12, %rdi
callq 0x41a0
leaq (%rax,%r12), %rdx
leaq -0x60(%rbp), %rdi
movq %r12, %rsi
callq 0xa592
movq -0x98(%rbp), %r13
movq %r13, %rdi
callq 0x4030
movq %rax, %r12
movl -0x2c(%rbp), %edi
movq %rax, %rsi
movq %r13, %rdx
callq 0x1190e
movq %r15, %r13
movq %r15, -0x88(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x56fa
movq -0x98(%rbp), %rcx
movq -0x68(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xd912
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0xe8cd
callq 0x4360
movq %r12, %rdi
callq 0x4360
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %r12
cmpq %r12, %rdi
je 0xe8e7
callq 0x4360
decq %rbx
jne 0xe82f
movl -0x30(%rbp), %r12d
testl %r12d, %r12d
sete %al
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xe924
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r13, %rdi
je 0xe927
callq 0x4360
jmp 0xe927
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xe939
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop