// File: /workspace/github_cpp_makefile_star2_O3/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 0x114f5
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 0x115c0
movl %eax, %r14d
movl $0x2d, %edx
movq %rbx, %rdi
leaq 0x8478(%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 0xa592
movl -0x1a0(%rbp), %eax
testl %eax, %eax
jne 0xb7b6
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 0x56fa
movq -0xd0(%rbp), %rcx
movq %rbx, %rdi
leaq -0xc0(%rbp), %rsi
movq %r15, %rdx
callq 0xd912
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0xb7a8
callq 0x4360
movq %r15, %rdi
callq 0x4360
movl -0x1a0(%rbp), %eax
cmpl $0x1, %eax
jne 0xb899
leaq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %rdi
callq 0x56fa
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0xdc0a
movq %rax, -0xd0(%rbp)
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0xb816
callq 0x4360
movl $0xe0, %edx
movl %r14d, %edi
leaq -0x1a0(%rbp), %rsi
callq 0x4170
movq -0xd0(%rbp), %rdx
testq %rdx, %rdx
je 0xb84f
movq -0x60(%rbp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x117ea
movq %r15, %rdi
callq 0x4360
jmp 0xb899
movl $0x13, %edx
movq 0xd765(%rip), %rdi      # 0x18fc0
leaq 0x8301(%rip), %rsi      # 0x13b63
callq 0x43f0
movq 0xd752(%rip), %rcx      # 0x18fc0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq 0xd734(%rip), %rdi      # 0x18fc0
callq 0x4040
movq %rax, %rdi
callq 0x4260
cmpl $0x2, -0x1a0(%rbp)
jne 0xb91f
movq -0x58(%rbp), %rdi
callq 0x44d0
movq %r13, %r12
testl %eax, %eax
jne 0xb96d
movq (%r12), %r12
cmpq %r13, %r12
je 0xb8df
leaq 0x10(%r12), %r15
movq %r15, %rdi
leaq -0x48(%rbp), %rsi
callq 0x4050
testl %eax, %eax
jne 0xb8b6
movq %r13, %rdi
movq %r15, %rsi
callq 0xfef6
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 0x56fa
leaq -0x80(%rbp), %rsi
callq 0xd150
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0xb91f
callq 0x4360
movl -0x1a0(%rbp), %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0xb94a
movl $0x1c, %ecx
movq %rsp, %rdi
leaq -0x1a0(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %rdi
movl %r14d, %esi
callq 0xdeb0
movl %r14d, %edi
callq 0x46b0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %r15
cmpq %r15, %rdi
je 0xb6a3
callq 0x4360
jmp 0xb6a3
movl %eax, %edi
leaq -0x38(%rbp), %r14
callq 0x4240
jmp 0xb9be
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
jmp 0xb9b0
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
jmp 0xb9b0
movq %rax, %rbx
jmp 0xb9c5
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0xb9c1
callq 0x4360
jmp 0xb9c1
jmp 0xb9be
movq %rax, %rbx
leaq -0x38(%rbp), %r14
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0xb9d3
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop