// File: /workspace/github_cpp_makefile_star2_O2/luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::putToAddress(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::putToAddress(std::string address, std::string localFile, std::string remoteFile)
{
    int connectionToServer; //TCP connect to introducer/other nodes

    // printf("Local file: %s\n", localFile.c_str());
    // printf("Remote file: %s\n", remoteFile.c_str());

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

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

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

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

        // Read the file
        file.seekg (0, file.end);
        int length = file.tellg();
        file.seekg (0, file.beg);        

        char * buffer = new char [length];

        file.read (buffer,length);

        Message_fs msg;
        msg.type = MSG_PUT;

        memset(msg.filename, '\0', 200);
        remoteFile.copy(msg.filename, remoteFile.length());
        msg.filename[remoteFile.length()+1] = '\0';
        msg.size = length;
        //std::string test = msg.filename;
        //std::cout << "test: :" << test << std::endl;

        write(connectionToServer, &msg, sizeof(Message_fs) );
        write(connectionToServer, buffer, length);     

        close(connectionToServer);
        delete buffer;
        file.close(); 
        return true;
    }   
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
leaq -0x238(%rbp), %r13
pushq $0x4
popq %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x4530
movq (%r13), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x218(%rbp,%rax)
je 0x9e26
movq 0xc1e0(%rip), %rdi      # 0x15fc0
leaq 0x5dca(%rip), %rsi      # 0xfbb1
callq 0x4370
movq %rax, %rdi
movq %r12, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
leaq 0x5dab(%rip), %rsi      # 0xfbb1
movq %r15, %rdi
callq 0x4370
movq %rax, %rdi
movq %r12, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
jmp 0x9ec2
leaq 0x5da8(%rip), %rsi      # 0xfbd5
movq %r15, %rdi
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
leaq 0x5971(%rip), %rsi      # 0xf7b8
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq (%r14), %rdi
movl 0x200(%r15), %esi
leaq -0x2c(%rbp), %rdx
callq 0xddff
testl %eax, %eax
je 0x9ee4
leaq 0x5d7d(%rip), %rsi      # 0xfbf2
movq %r15, %rdi
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
movq 0xc129(%rip), %rdi      # 0x15fc0
leaq 0x5d54(%rip), %rsi      # 0xfbf2
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
leaq -0x238(%rbp), %rdi
callq 0x40d0
xorl %ebx, %ebx
leaq -0x238(%rbp), %rdi
callq 0x40f0
movl %ebx, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x238(%rbp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x4440
leaq -0x238(%rbp), %rdi
callq 0x45b0
movq %rax, %r14
leaq -0x238(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4440
movslq %r14d, %r15
testq %r15, %r15
pushq $-0x1
popq %rdi
cmovnsq %r15, %rdi
callq 0x4030
movq %rax, %r14
leaq -0x238(%rbp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x43e0
leaq -0x314(%rbp), %r12
leaq -0x318(%rbp), %rdi
movl $0xcc, %edx
xorl %esi, %esi
callq 0x4210
movq 0x8(%rbx), %rdx
movq %rbx, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x4090
movq 0x8(%rbx), %rax
movb $0x0, -0x313(%rbp,%rax)
leaq -0x318(%rbp), %rsi
movq %r15, 0xd0(%rsi)
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
callq 0x4150
movl -0x2c(%rbp), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4150
movl -0x2c(%rbp), %edi
callq 0x4650
movq %r14, %rdi
callq 0x4350
leaq -0x238(%rbp), %rdi
callq 0x40d0
movb $0x1, %bl
jmp 0x9ec4
jmp 0x9fc8
jmp 0x9fc8
jmp 0x9fc8
jmp 0x9fc8
jmp 0x9fc8
movq %rax, %rbx
leaq -0x238(%rbp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x45d0
nop