// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
leaq -0x238(%rbp), %r13
movq %r13, %rdi
movq %rdx, %rsi
movl $0x4, %edx
callq 0x4570
movq (%r13), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x218(%rbp,%rax)
je 0xbeda
movq 0xd18a(%rip), %rdi      # 0x18fc0
leaq 0x7d9c(%rip), %rsi      # 0x13bd9
movl $0x23, %edx
callq 0x43f0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0xd169(%rip), %rdi      # 0x18fc0
callq 0x43f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
leaq 0x7d4c(%rip), %rsi      # 0x13bd9
movl $0x23, %edx
movq %rbx, %rdi
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
jmp 0xc004
leaq 0x7d1c(%rip), %rsi      # 0x13bfd
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x43f0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r12
leaq 0x78d9(%rip), %rsi      # 0x137e0
movl $0x3, %edx
movq %rax, %rdi
callq 0x43f0
movq (%r12), %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 0x1179a
testl %eax, %eax
je 0xc026
leaq 0x7cbd(%rip), %rsi      # 0x13c1a
movl $0x26, %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
movq 0xd016(%rip), %rdi      # 0x18fc0
leaq 0x7c69(%rip), %rsi      # 0x13c1a
movl $0x26, %edx
callq 0x43f0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0xcff7(%rip), %rdi      # 0x18fc0
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
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
xorl %esi, %esi
movl $0x2, %edx
callq 0x4470
leaq -0x238(%rbp), %rdi
callq 0x4600
movq %rax, %rbx
leaq -0x238(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4470
movslq %ebx, %r15
testq %r15, %r15
movq $-0x1, %rdi
cmovnsq %r15, %rdi
callq 0x4030
movq %rax, %rbx
leaq -0x238(%rbp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x4420
leaq -0x314(%rbp), %r12
leaq -0x318(%rbp), %rdi
movl $0xcc, %edx
xorl %esi, %esi
callq 0x4220
movq 0x8(%r14), %rdx
movq %r14, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x40a0
movq 0x8(%r14), %rax
movb $0x0, -0x313(%rbp,%rax)
leaq -0x318(%rbp), %rsi
movq %r15, 0xd0(%rsi)
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
callq 0x4170
movl -0x2c(%rbp), %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x4170
movl -0x2c(%rbp), %edi
callq 0x46b0
movq %rbx, %rdi
callq 0x4360
leaq -0x238(%rbp), %rdi
callq 0x40d0
movb $0x1, %bl
jmp 0xc006
jmp 0xc110
jmp 0xc110
jmp 0xc110
jmp 0xc110
jmp 0xc110
movq %rax, %rbx
leaq -0x238(%rbp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x4630
nop