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

    logFile << "getFromAddress: Connecting to "<< address << "..." << std::endl;

    int ret = connect_to_server(address.c_str(), port, &connectionToServer);
    if(ret!=0)
    {
        logFile <<"ERROR getFromAddress: Cannot connect to "<<address<< std::endl;
        std::cout <<"ERROR getFromAddress: Cannot connect to "<<address<< std::endl;
        return false;
    }
    else{
        
        Message_fs msg;
        msg.type = MSG_GET;
        memset(msg.filename, '\0', 200);
        remoteFile.copy(msg.filename, remoteFile.length());

        write(connectionToServer, &msg, sizeof(Message_fs)); // Send filename
        read (connectionToServer, &msg, sizeof(Message_fs)); // Receive size

        if (msg.size == 0)
        {
            std::cout << "File does not exist: " << remoteFile << std::endl;
            close(connectionToServer);
            return false;
        }
        else
        {
            char * buffer = new char [msg.size];

            //size_t ret = read(connectionToServer, buffer,  msg.size);
            size_t ret =splitRead( connectionToServer, buffer, msg.size );
            // std::cout << "getFromAddress: Received: " << ret <<" of "<<msg.size << std::endl;

            std::ofstream file(localFile, std::ofstream::binary);

            if (!file.good())
            {
                std::cout << "Could not open file: " << localFile << std::endl;
                logFile << "Could not open file: " << localFile << std::endl;
                return false;
            }

            file.write(buffer, msg.size); 
            file.close();  

            delete buffer;

            close(connectionToServer);
            return true;
        }        
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
leaq 0x5927(%rip), %rsi      # 0xfc34
callq 0x4370
movq %rax, %rdi
movq %r12, %rsi
callq 0x4300
leaq 0x5494(%rip), %rsi      # 0xf7b8
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq (%r12), %rdi
movl 0x200(%r14), %esi
leaq -0x2c(%rbp), %rdx
callq 0xddff
testl %eax, %eax
je 0xa39b
leaq 0x5900(%rip), %rbx      # 0xfc53
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4370
movq %rax, %rdi
movq %r12, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
movq 0xbc48(%rip), %rdi      # 0x15fc0
movq %rbx, %rsi
callq 0x4370
movq %rax, %rdi
movq %r12, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
xorl %r15d, %r15d
jmp 0xa4fe
leaq -0x10c(%rbp), %r12
movl $0x1, -0x4(%r12)
movl $0xc8, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x8(%r15), %rdx
movq %r15, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x4090
movl -0x2c(%rbp), %edi
leaq -0x110(%rbp), %r12
movl $0xe0, %edx
movq %r12, %rsi
callq 0x4150
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
movq %r12, %rsi
callq 0x4490
movq 0xd0(%r12), %r13
testq %r13, %r13
je 0xa490
movq %r13, %rdi
callq 0x4030
movq %rax, %r12
movl -0x2c(%rbp), %edi
movq %rax, %rsi
movq %r13, %rdx
callq 0xe0b0
leaq -0x310(%rbp), %r15
pushq $0x4
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4630
movq (%r15), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x2f0(%rbp,%rax)
sete %r15b
je 0xa4c3
movq 0xbb73(%rip), %rdi      # 0x15fc0
leaq 0x5737(%rip), %rsi      # 0xfb8b
callq 0x4370
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
leaq 0x5718(%rip), %rsi      # 0xfb8b
movq %r14, %rdi
callq 0x4370
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
jmp 0xa4f2
movq 0xbb29(%rip), %rdi      # 0x15fc0
leaq 0x57de(%rip), %rsi      # 0xfc7c
callq 0x4370
movq %rax, %rdi
movq %r15, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
movl -0x2c(%rbp), %edi
callq 0x4650
jmp 0xa393
movq -0x40(%rbp), %rdx
leaq -0x310(%rbp), %rdi
movq %r12, %rsi
callq 0x4430
leaq -0x310(%rbp), %rdi
callq 0x4480
movq %r12, %rdi
callq 0x4350
movl -0x2c(%rbp), %edi
callq 0x4650
leaq -0x310(%rbp), %rdi
callq 0x4678
movl %r15d, %eax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x310(%rbp), %rdi
callq 0x4678
movq %rbx, %rdi
callq 0x45d0