// File: 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
subq $0x420, %rsp            # imm = 0x420
movq %rdx, -0x378(%rbp)
movq %rsi, %rax
movq -0x378(%rbp), %rsi
movq %rax, -0x370(%rbp)
movq %rsi, -0x368(%rbp)
movq %rcx, -0x360(%rbp)
movq %rdi, -0x10(%rbp)
movq %rax, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x358(%rbp)
leaq -0x238(%rbp), %rdi
movl $0x4, %edx
callq 0x45f0
movq -0x238(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x238(%rbp,%rax), %rdi
callq 0x4620
movb %al, -0x349(%rbp)
jmp 0x112dd
movb -0x349(%rbp), %al
testb $0x1, %al
jne 0x113bd
jmp 0x112ed
movq 0x15ccc(%rip), %rdi     # 0x26fc0
leaq 0xb8c7(%rip), %rsi      # 0x1cbc2
callq 0x43f0
movq %rax, -0x380(%rbp)
jmp 0x11309
movq -0x368(%rbp), %rsi
movq -0x380(%rbp), %rdi
callq 0x4370
movq %rax, -0x388(%rbp)
jmp 0x11325
movq -0x388(%rbp), %rdi
movq 0x15c75(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x1133a
movq -0x358(%rbp), %rdi
leaq 0xb87a(%rip), %rsi      # 0x1cbc2
callq 0x43f0
movq %rax, -0x390(%rbp)
jmp 0x11356
movq -0x368(%rbp), %rsi
movq -0x390(%rbp), %rdi
callq 0x4370
movq %rax, -0x398(%rbp)
jmp 0x11372
movq -0x398(%rbp), %rdi
movq 0x15c28(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x11387
movb $0x0, -0x1(%rbp)
movl $0x1, -0x248(%rbp)
jmp 0x11733
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x4120
jmp 0x1174d
movq -0x358(%rbp), %rdi
leaq 0xb81b(%rip), %rsi      # 0x1cbe6
callq 0x43f0
movq %rax, -0x3a0(%rbp)
jmp 0x113d9
movq -0x370(%rbp), %rsi
movq -0x3a0(%rbp), %rdi
callq 0x4370
movq %rax, -0x3a8(%rbp)
jmp 0x113f5
movq -0x3a8(%rbp), %rdi
leaq 0xb3b5(%rip), %rsi      # 0x1c7b8
callq 0x43f0
movq %rax, -0x3b0(%rbp)
jmp 0x11411
movq -0x3b0(%rbp), %rdi
movq 0x15b89(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x11426
movq -0x370(%rbp), %rdi
callq 0x41d0
movq %rax, %rdi
movq -0x358(%rbp), %rax
movl 0x200(%rax), %esi
leaq -0x2c(%rbp), %rdx
callq 0x1aaf0
movl %eax, -0x3b4(%rbp)
jmp 0x11453
movl -0x3b4(%rbp), %eax
movl %eax, -0x24c(%rbp)
cmpl $0x0, -0x24c(%rbp)
je 0x11527
movq -0x358(%rbp), %rdi
leaq 0xb789(%rip), %rsi      # 0x1cc03
callq 0x43f0
movq %rax, -0x3c0(%rbp)
jmp 0x11488
movq -0x370(%rbp), %rsi
movq -0x3c0(%rbp), %rdi
callq 0x4370
movq %rax, -0x3c8(%rbp)
jmp 0x114a4
movq -0x3c8(%rbp), %rdi
movq 0x15af6(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x114b9
movq 0x15b00(%rip), %rdi     # 0x26fc0
leaq 0xb73c(%rip), %rsi      # 0x1cc03
callq 0x43f0
movq %rax, -0x3d0(%rbp)
jmp 0x114d5
movq -0x370(%rbp), %rsi
movq -0x3d0(%rbp), %rdi
callq 0x4370
movq %rax, -0x3d8(%rbp)
jmp 0x114f1
movq -0x3d8(%rbp), %rdi
movq 0x15aa9(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x11506
leaq -0x238(%rbp), %rdi
callq 0x4100
jmp 0x11514
movb $0x0, -0x1(%rbp)
movl $0x1, -0x248(%rbp)
jmp 0x11733
xorl %eax, %eax
movl %eax, %esi
leaq -0x238(%rbp), %rdi
movl $0x2, %edx
callq 0x44f0
jmp 0x1153e
leaq -0x238(%rbp), %rdi
callq 0x4680
movq %rdx, -0x3e8(%rbp)
movq %rax, -0x3e0(%rbp)
jmp 0x1155a
movq -0x3e8(%rbp), %rax
movq -0x3e0(%rbp), %rcx
movq %rcx, -0x260(%rbp)
movq %rax, -0x258(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x168e0
movq %rax, -0x3f0(%rbp)
jmp 0x1158b
movq -0x3f0(%rbp), %rax
movl %eax, -0x250(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x238(%rbp), %rdi
callq 0x44f0
jmp 0x115aa
movslq -0x250(%rbp), %rdi
testq %rdi, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x4030
movq %rax, -0x3f8(%rbp)
jmp 0x115cd
movq -0x3f8(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x268(%rbp), %rsi
movslq -0x250(%rbp), %rdx
leaq -0x238(%rbp), %rdi
callq 0x4480
jmp 0x115f7
movq -0x360(%rbp), %rdi
movl $0x0, -0x348(%rbp)
leaq -0x344(%rbp), %rax
movq %rax, -0x400(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x410(%rbp)
movups %xmm0, -0x294(%rbp)
movups %xmm0, -0x2a4(%rbp)
movups %xmm0, -0x2b4(%rbp)
movups %xmm0, -0x2c4(%rbp)
movups %xmm0, -0x2d4(%rbp)
movups %xmm0, -0x2e4(%rbp)
movups %xmm0, -0x2f4(%rbp)
movups %xmm0, -0x304(%rbp)
movups %xmm0, -0x314(%rbp)
movups %xmm0, -0x324(%rbp)
movups %xmm0, -0x334(%rbp)
movups %xmm0, -0x344(%rbp)
movq $0x0, -0x284(%rbp)
callq 0x4590
movq -0x360(%rbp), %rdi
movq -0x400(%rbp), %rsi
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x40c0
jmp 0x116a0
movq -0x360(%rbp), %rdi
callq 0x4590
leaq -0x348(%rbp), %rsi
movb $0x0, 0x5(%rax,%rsi)
movslq -0x250(%rbp), %rax
movq %rax, -0x278(%rbp)
movl -0x2c(%rbp), %edi
movl $0xe0, %edx
callq 0x4190
jmp 0x116d5
movl -0x2c(%rbp), %edi
movq -0x268(%rbp), %rsi
movslq -0x250(%rbp), %rdx
callq 0x4190
jmp 0x116ed
movl -0x2c(%rbp), %edi
callq 0x4740
jmp 0x116f7
movq -0x268(%rbp), %rax
movq %rax, -0x418(%rbp)
cmpq $0x0, %rax
je 0x11717
movq -0x418(%rbp), %rdi
callq 0x43c0
leaq -0x238(%rbp), %rdi
callq 0x4100
jmp 0x11725
movb $0x1, -0x1(%rbp)
movl $0x1, -0x248(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x4120
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
movq -0x240(%rbp), %rdi
callq 0x46a0
nopl (%rax)