// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x77c3(%rip), %rsi      # 0x13c5c
movl $0x1e, %edx
callq 0x43f0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
leaq 0x7322(%rip), %rsi      # 0x137e0
movl $0x3, %edx
movq %rax, %rdi
callq 0x43f0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movq (%r12), %rdi
movl 0x200(%rbx), %esi
leaq -0x2c(%rbp), %rdx
callq 0x1161a
testl %eax, %eax
je 0xc5ba
leaq 0x7766(%rip), %r14      # 0x13c7b
movl $0x28, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x43f0
movq (%r12), %rsi
movq 0x8(%r12), %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
movq 0xca59(%rip), %rbx      # 0x18fc0
movl $0x28, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x43f0
movq (%r12), %rsi
movq 0x8(%r12), %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
xorl %r15d, %r15d
jmp 0xc7db
leaq -0x30c(%rbp), %r12
movl $0x1, -0x4(%r12)
movl $0xc8, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x4220
movq 0x8(%r15), %rdx
movq %r15, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x40a0
movl -0x2c(%rbp), %edi
leaq -0x310(%rbp), %r12
movl $0xe0, %edx
movq %r12, %rsi
callq 0x4170
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
movq %r12, %rsi
callq 0x44b0
movq 0xd0(%r12), %r13
testq %r13, %r13
je 0xc712
movq %r13, %rdi
callq 0x4030
movq %rax, %r12
movl -0x2c(%rbp), %edi
movq %rax, %rsi
movq %r13, %rdx
callq 0x1190e
leaq -0x230(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x46a0
movq (%r15), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x210(%rbp,%rax)
sete %r15b
je 0xc773
movq 0xc94e(%rip), %rdi      # 0x18fc0
leaq 0x753a(%rip), %rsi      # 0x13bb3
movl $0x15, %edx
callq 0x43f0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0xc92f(%rip), %rdi      # 0x18fc0
callq 0x43f0
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
leaq 0x74ec(%rip), %rsi      # 0x13bb3
movl $0x15, %edx
movq %rbx, %rdi
callq 0x43f0
movq (%r14), %rsi
movq 0x8(%r14), %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 0xc7a5
movq 0xc8a7(%rip), %rbx      # 0x18fc0
leaq 0x7584(%rip), %rsi      # 0x13ca4
movl $0x15, %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
movl -0x2c(%rbp), %edi
callq 0x46b0
jmp 0xc5b2
movq -0x240(%rbp), %rdx
leaq -0x230(%rbp), %rdi
movq %r12, %rsi
callq 0x4460
leaq -0x230(%rbp), %rdi
callq 0x44a0
movq %r12, %rdi
callq 0x4360
movl -0x2c(%rbp), %edi
callq 0x46b0
movq 0xc80c(%rip), %rax      # 0x18fb8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x228(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x230(%rbp,%rcx)
callq 0x42f0
leaq -0x138(%rbp), %rdi
callq 0x4160
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 -0x230(%rbp), %rdi
callq 0x46c8
movq %rbx, %rdi
callq 0x4630
nop