// File: /workspace/github_cpp_makefile_star2_O1/luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::listeningThread()
void FileSystem::listeningThread()
{ 
    int listenFd = open_socket(port); 

    while(true)
    {
        size_t ret;
        int connFd = listen_socket(listenFd);

        logFile<<"listeningThread: someone is contacting me... "<< std::endl;

        Message_fs msg;

        ret = read(connFd, &msg, sizeof(Message_fs));
        std::string filename = msg.filename;
        //cout<<"listening filename: "<<filename<<endl;
        char* buffer;

        if(msg.type == MSG_PUT)
        {
            buffer = new char[msg.size];
            ret = read(connFd, buffer, msg.size);
            saveFile(filename, buffer, msg.size);
            delete buffer;
        }
        if(msg.type == MSG_GET)
        {
            msg.size = readFile(filename, &buffer);
            write(connFd, &msg, sizeof(Message_fs));

            if (msg.size == 0)
            {
                std::cout << "File does not exist" << std::endl;
            }
            else
            {
                ret = splitWrite( connFd, buffer, msg.size );
                delete buffer;    
            }
        }
        if(msg.type == MSG_DELETE)
        {
            filesLock.lock();
            for( auto &file : files )
            {
                if( file.filename.compare(filename) == 0)
                {
                    files.remove(file);
                    break;
                }

            }
            filesLock.unlock();
            deleteFile(filename);
        }
        if(msg.type == MSG_PULL_CP || msg.type == MSG_PULL_MV){
            sendFileByRange(connFd, msg);
        }

        close(connFd);
    }
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdi, %rbx
movl 0x200(%rdi), %edi
callq 0x11677
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %r15
leaq 0x228(%rbx), %rax
movq %rax, -0x58(%rbp)
leaq 0x210(%rbx), %r13
movl -0x4c(%rbp), %edi
callq 0x11740
movl %eax, %r14d
movl $0x2d, %edx
movq %rbx, %rdi
leaq 0x8340(%rip), %rsi      # 0x13b35
callq 0x43f0
movq (%rbx), %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 $0xe0, %edx
movl %r14d, %edi
leaq -0x1a0(%rbp), %rsi
callq 0x44b0
movq %r15, -0x48(%rbp)
leaq -0x19c(%rbp), %r15
movq %r15, %rdi
callq 0x41a0
leaq (%rax,%r15), %rdx
leaq -0x48(%rbp), %rdi
movq %r15, %rsi
callq 0xa686
cmpl $0x0, -0x1a0(%rbp)
jne 0xb8e7
movq -0xd0(%rbp), %r12
movq %r12, %rdi
callq 0x4030
movq %rax, %r15
movq %rax, -0x60(%rbp)
movl %r14d, %edi
movq %rax, %rsi
movq %r12, %rdx
callq 0x44b0
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x570a
movq -0xd0(%rbp), %rcx
movq %rbx, %rdi
leaq -0xc0(%rbp), %rsi
movq %r15, %rdx
callq 0xda3c
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0xb8df
callq 0x4360
movq %r15, %rdi
callq 0x4360
cmpl $0x1, -0x1a0(%rbp)
jne 0xb9d3
leaq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %rdi
callq 0x570a
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0xdd5c
movq %rax, -0xd0(%rbp)
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0xb94b
callq 0x4360
movl $0xe0, %edx
movl %r14d, %edi
leaq -0x1a0(%rbp), %rsi
callq 0x4170
movq -0xd0(%rbp), %rdx
testq %rdx, %rdx
je 0xb989
movq -0x60(%rbp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x11963
testq %r15, %r15
je 0xb9d3
movq %r15, %rdi
callq 0x4360
jmp 0xb9d3
movl $0x13, %edx
movq 0xd62b(%rip), %rdi      # 0x18fc0
leaq 0x81c7(%rip), %rsi      # 0x13b63
callq 0x43f0
movq 0xd618(%rip), %rcx      # 0x18fc0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq 0xd5fa(%rip), %rdi      # 0x18fc0
callq 0x4040
movq %rax, %rdi
callq 0x4260
cmpl $0x2, -0x1a0(%rbp)
jne 0xba59
movq -0x58(%rbp), %rdi
callq 0x44d0
movq %r13, %r12
testl %eax, %eax
jne 0xbaa7
movq (%r12), %r12
cmpq %r13, %r12
je 0xba19
leaq 0x10(%r12), %r15
movq %r15, %rdi
leaq -0x48(%rbp), %rsi
callq 0x4050
testl %eax, %eax
jne 0xb9f0
movq %r13, %rdi
movq %r15, %rsi
callq 0x1003a
movq -0x58(%rbp), %rdi
callq 0x42a0
leaq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %rdi
callq 0x570a
leaq -0x80(%rbp), %rsi
callq 0xd28e
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0xba59
callq 0x4360
movl -0x1a0(%rbp), %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0xba84
movl $0x1c, %ecx
movq %rsp, %rdi
leaq -0x1a0(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl %r14d, %esi
callq 0xe002
movl %r14d, %edi
callq 0x46b0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %r15
cmpq %r15, %rdi
je 0xb7db
callq 0x4360
jmp 0xb7db
movl %eax, %edi
leaq -0x38(%rbp), %r14
callq 0x4240
jmp 0xbaf8
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
jmp 0xbaea
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
jmp 0xbaea
movq %rax, %rbx
jmp 0xbaff
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0xbafb
callq 0x4360
jmp 0xbafb
jmp 0xbaf8
movq %rax, %rbx
leaq -0x38(%rbp), %r14
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0xbb0d
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop