// File: luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::saveFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, char*, unsigned long)
bool FileSystem::saveFile(std::string filename, char* buffer, size_t length)
{
    bool alreadyThere = false;
    filesLock.lock();  
    for(auto &file : files){
        if(file.filename.compare(filename) == 0){
            alreadyThere = true;
            break;
        }
    }
    filesLock.unlock();

    if(alreadyThere)
        return true;

    std::string root = "files/";
    std::ofstream newfile(root + filename, std::ofstream::binary);

    newfile.write(buffer, length);
    newfile.close();

    FileEntry fileEntry(filename);

    filesLock.lock();
    files.push_back( fileEntry );
    filesLock.unlock();

    logFile << "saveFile " << root + filename << " saved" << std::endl;
    // cout << "saveFile " << root + filename << " saved" << std::endl;

    // cout<<"saveFile: "<<fileEntry.filename<<endl;

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movq %rsi, -0x320(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x318(%rbp)
movb $0x0, -0x29(%rbp)
addq $0x228, %rdi            # imm = 0x228
callq 0xb5f0
movq -0x318(%rbp), %rax
addq $0x210, %rax            # imm = 0x210
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x164c0
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x164f0
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x16520
testb $0x1, %al
jne 0x1338b
jmp 0x133c0
leaq -0x40(%rbp), %rdi
callq 0x16550
movq -0x320(%rbp), %rsi
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x4040
cmpl $0x0, %eax
jne 0x133b3
movb $0x1, -0x29(%rbp)
jmp 0x133c0
jmp 0x133b5
leaq -0x40(%rbp), %rdi
callq 0x16570
jmp 0x13378
movq -0x318(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb620
testb $0x1, -0x29(%rbp)
je 0x133e2
movb $0x1, -0x1(%rbp)
jmp 0x13676
leaq -0x71(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0x46b0
movq -0x328(%rbp), %rdx
leaq 0x977a(%rip), %rsi      # 0x1cb7a
leaq -0x70(%rbp), %rdi
callq 0x4510
jmp 0x1340b
leaq -0x71(%rbp), %rdi
callq 0x4430
movq -0x320(%rbp), %rdx
leaq -0x2a8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x16820
jmp 0x1342d
leaq -0x288(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
movl $0x4, %edx
callq 0x4720
jmp 0x13447
leaq -0x2a8(%rbp), %rdi
callq 0x42a0
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x288(%rbp), %rdi
callq 0x44d0
jmp 0x13469
leaq -0x288(%rbp), %rdi
callq 0x4550
jmp 0x13477
movq -0x320(%rbp), %rsi
leaq -0x2f0(%rbp), %rdi
callq 0x41e0
jmp 0x1348c
leaq -0x2d0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x142e0
jmp 0x134a1
leaq -0x2f0(%rbp), %rdi
callq 0x42a0
movq -0x318(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb5f0
jmp 0x134c2
movq -0x318(%rbp), %rdi
addq $0x210, %rdi            # imm = 0x210
leaq -0x2d0(%rbp), %rsi
callq 0x168a0
jmp 0x134de
movq -0x318(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb620
jmp 0x134f3
movq -0x318(%rbp), %rdi
leaq 0x9680(%rip), %rsi      # 0x1cb81
callq 0x43f0
movq %rax, -0x330(%rbp)
jmp 0x1350f
movq -0x320(%rbp), %rdx
leaq -0x310(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x16820
jmp 0x13528
movq -0x330(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x4370
movq %rax, -0x338(%rbp)
jmp 0x13544
movq -0x338(%rbp), %rdi
leaq 0x9639(%rip), %rsi      # 0x1cb8b
callq 0x43f0
movq %rax, -0x340(%rbp)
jmp 0x13560
movq -0x340(%rbp), %rdi
movq 0x13a3a(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x13575
leaq -0x310(%rbp), %rdi
callq 0x42a0
movb $0x1, -0x1(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x166f0
leaq -0x288(%rbp), %rdi
callq 0x4768
leaq -0x70(%rbp), %rdi
callq 0x42a0
jmp 0x13676
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x4430
jmp 0x13684
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x1366b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x42a0
jmp 0x1366b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x1365f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x2f0(%rbp), %rdi
callq 0x42a0
jmp 0x1365f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x13653
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x310(%rbp), %rdi
callq 0x42a0
leaq -0x2d0(%rbp), %rdi
callq 0x166f0
leaq -0x288(%rbp), %rdi
callq 0x4768
leaq -0x70(%rbp), %rdi
callq 0x42a0
jmp 0x13684
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x46a0
nopl (%rax)