// File: /workspace/github_cpp_makefile_star2_O3/billDrett[P]Bulletin-Boards/boardServer.cpp
// Function: BoardServer::parseGetFiles(char*, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void BoardServer::parseGetFiles(char* buffer, int nread, int state, std::string& previousRead)
{
    string name, tmpName;
    char* tmp = buffer;
    int len, fd, id;

    if(previousRead.size() != 30) //ta prwta 30 bytes exoun to chanel id kai to onoma tou file
    {
        if(nread < (30 - previousRead.size())) //an dn arkoun tha diavasw ta ypoloipa se epomeno read
        {
            previousRead.append(buffer, nread);
            return;
        }
        len = 30 - previousRead.size(); // grafw osa apomenoun
        previousRead.append(buffer, len);
        tmp = &buffer[len];
        nread -= len;

        istringstream iss(previousRead); //ama yparxei hdh to arxeio ston server dn theloume na to xalasei
        iss>>id >>name;
        tmpName = name;
        fileNameExists(tmpName);
        if(name != tmpName) //ama ypaxei hdh to onoma antikatastash me to kainourgio
        {
            previousRead.erase(previousRead.find(name));
            previousRead.append(tmpName);
            for(int i = previousRead.size(); i < 30; i++)
            {
                previousRead.append(" ");
            }
        }
    }

    istringstream iss(previousRead);
    iss>>id >>name;
    if(iss.fail())
    {
        perror("cant get id and file name");
        return;
    }
    fd = open(name.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0644);
    write(fd, tmp, nread); //grafoume to arxeio
    close(fd);


    if(state == waitingEndCommand) //afou to pirame olo to vazoume kai sto chanel
    {
        string* stringName = new string (name);
        this->addFileToChanel(id, stringName);
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %r8, %r13
movl %ecx, -0x54(%rbp)
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, -0x88(%rbp)
leaq -0x40(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x68(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq 0x8(%r8), %rax
cmpq $0x1e, %rax
je 0x6345
movslq %r12d, %rdx
movl $0x1e, %ecx
subq %rax, %rcx
cmpq %rdx, %rcx
jbe 0x6233
movq %r13, %rdi
movq %r15, %rsi
callq 0x2060
jmp 0x6424
movl $0x1e, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %rdx, -0x80(%rbp)
callq 0x2060
leaq -0x208(%rbp), %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0x2380
leaq -0x208(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x2180
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22c0
leaq -0x78(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x2170
leaq -0x78(%rbp), %rdi
callq 0x669e
movq -0x48(%rbp), %r14
cmpq -0x70(%rbp), %r14
jne 0x62ba
testq %r14, %r14
je 0x631c
movq -0x78(%rbp), %rsi
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2240
testl %eax, %eax
jne 0x62be
jmp 0x631c
movq -0x50(%rbp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x23b0
movq 0x8(%r13), %rcx
cmpq %rax, %rcx
jb 0x6476
movq %rax, 0x8(%r13)
movq (%r13), %rcx
movb $0x0, (%rcx,%rax)
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %r13, %rdi
callq 0x20d0
movl 0x8(%r13), %r14d
cmpl $0x1d, %r14d
jg 0x631c
addl $-0x1e, %r14d
leaq 0x1e00(%rip), %rbx      # 0x810c
movq %r13, %rdi
movq %rbx, %rsi
callq 0x23f0
incl %r14d
jne 0x630c
movq -0x80(%rbp), %rax
addq %rax, %r15
subl %eax, %r12d
movq 0x5c9b(%rip), %rsi      # 0xbfc8
leaq -0x208(%rbp), %rdi
callq 0x22f0
leaq -0x190(%rbp), %rdi
callq 0x20e0
leaq -0x208(%rbp), %rdi
movq %r13, %rsi
movl $0x8, %edx
callq 0x2380
leaq -0x208(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x2180
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x22c0
movq -0x208(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x1e8(%rbp,%rax)
jne 0x6468
movq -0x50(%rbp), %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x21b0
movl %eax, %ebx
movslq %r12d, %rdx
movl %eax, %edi
movq %r15, %rsi
callq 0x20f0
movl %ebx, %edi
callq 0x2400
cmpl $0x3, -0x54(%rbp)
jne 0x6405
movl $0x20, %edi
callq 0x2220
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x37a4
movl -0x2c(%rbp), %esi
movq -0x88(%rbp), %rdi
callq 0x49ca
testq %rax, %rax
je 0x6405
movq %rax, %rdi
movq %r14, %rsi
callq 0x790e
movq 0x5bbc(%rip), %rsi      # 0xbfc8
leaq -0x208(%rbp), %rdi
callq 0x22f0
leaq -0x190(%rbp), %rdi
callq 0x20e0
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x643d
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x6456
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e24(%rip), %rdi      # 0x8293
callq 0x21d0
jmp 0x6405
leaq 0x1e44(%rip), %rdi      # 0x82c1
leaq 0x1e29(%rip), %rsi      # 0x82ad
movq %rax, %rdx
xorl %eax, %eax
callq 0x2340
jmp 0x64a4
movq %rax, %rbx
movl $0x20, %esi
movq %r14, %rdi
callq 0x2230
jmp 0x64b2
jmp 0x64a4
movq %rax, %rbx
jmp 0x64d1
jmp 0x64af
jmp 0x64af
jmp 0x64af
movq %rax, %rbx
movq 0x5b0f(%rip), %rsi      # 0xbfc8
leaq -0x208(%rbp), %rdi
callq 0x22f0
leaq -0x190(%rbp), %rdi
callq 0x20e0
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x64ea
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x6503
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0