// File: /workspace/github_cpp_makefile_star2_O3/billDrett[P]Bulletin-Boards/boardClient.cpp
// Function: BoardClient::readMessages()
void BoardClient::readMessages()
{
    char* tmp;
    char* tmp1;
    int fp = -1;
    int nread;
    char buffer [BUFFERSIZE];
    states state = waitingMessage;
    string  tmpTag;

    while((nread = read(pipeServer2Client, buffer + tmpTag.size(), BUFFERSIZE - tmpTag.size())) > 0) //diavazoume thn apanthsh tou server mexri na vroume to <end><File>
    {                                                                                                //an se proigoumeno read diavase meros tou tag to prosthetei sthn arxh tou buffer kai diavazei ligotera stoixeia
        strncpy(buffer, tmpTag.c_str(), tmpTag.size()); //prostetw sthn arxh tou buffer to proigoumeno tag efoson yparxei
        nread += tmpTag.size();
        tmpTag = "";
        tmp = buffer;

        if(findSubstring(tmp, "<NONE>", nread) != NULL)//ama lavw to NONE tote dn yparxei to chanel
        {
            cout<<"The chanel doesnt exists"<<endl;
            return;
        }

        if(state == waitingMessage) //prwta to message tag
        {
            tmp1 = getAfterTag(tmp, "<Message>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL) // mporei na pira meros tou tag, to apothikeuw kai to prosthetw sthn arxh tou neou read
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = MessageWaitStart;
        }

        if(state == MessageWaitStart) //meta perimeno to start tag
        {
            tmp1 = getAfterTag(tmp, "<start>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = MessageWaitEnd;
            cout<<"Message :"<<endl;
        }

        if(state == MessageWaitEnd)
        {
            int copyNread = nread;
            if(getBeforeTag(tmp, "<end>", copyNread) != NULL)
            {
                state = waitingEndMessage;
            }

            printf("%.*s", copyNread, tmp); //mporei to string na mhn teleiwnei me \0 gia auto xrhsimopoiw auth thn print
        }

        if(state == waitingEndMessage)
        {
            cout<<endl;
            tmp1 = getAfterTag(tmp, "<Message>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = waitingFile;
        }

        if(state == waitingFile) //teleiwsa me to Message perimenw ta files twra
        {
            tmp1 = getAfterTag(tmp, "<File>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = FileWaitName;
        }

        if(state < FileWaitName) continue; //dn thelw na mpw sto loop enw dn exw ftasei se ena apo ta parakatw stadia giati tote dn tha termatistei pote
        while(1) //dn kserw posa files tha diavasei gia auto tha stamathsei otan dei to <File>
        {
            if(state == FileWaitName)
            {
                string fileName;
                int lenght;

                tmp1 = getAfterTag(tmp, "<:", nread); //pairnw to onoma tou file
                lenght = nread;
                if((tmp1 == NULL) || getBeforeTag(tmp1, ">", lenght) == NULL)
                {
                    if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                    {
                        tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                    }
                    break;
                }
                tmp = tmp1;

                fileName.assign(tmp, lenght);
                if(fileName == " ")//keno file, to chanel dn eixe arxeia
                {
                    cout<<"No files to download"<<endl;
                    state = FileWaitStart;
                    continue;
                }
                close(fp);

                fileNameExists(fileName); //elenxo an yparxei hdh to onoma

                fp=open(fileName.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644); //anoigw to arxeio gia grapsimo
                if(fp == -1)
                {
                    perror("problem opening file");
                }

                state = FileWaitStart;
                cout<<fileName<<"is downloaded"<<endl;
            }

            if(state == FileWaitStart)
            {
                tmp1 = getAfterTag(tmp, "<start>", nread);
                if(tmp1 == NULL)
                {
                    if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                    {
                        tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                    }
                    break;
                }
                tmp = tmp1;
                state = FileWaitEnd;
            }

            if(state == FileWaitEnd)
            {
                int copyNread = nread;
                if(getBeforeTag(tmp, "<end>", copyNread) != NULL)
                {
                    state = waitingEndFile;
                }

                write(fp, tmp, copyNread);
                if(state == FileWaitEnd) break; //an exw kai alla na diavastoun pairnw nea dodomena apo to pipe
            }

            if(state == waitingEndFile)
            {
                if(findSubstring(tmp, "<:", nread) != NULL) //an yparxei akoma arxeio epistrofi pisw
                {
                    state = FileWaitName;
                    close(fp);
                    continue;
                }
                close(fp);
                tmp = getAfterTag(tmp, "<File>", nread);
                if(tmp1 == NULL)
                {
                    if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                    {
                        tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                    }
                    break;
                }
                return;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdi, -0x80(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
leaq -0x480(%rbp), %r14
xorl %r15d, %r15d
movq -0x80(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x48(%rbp), %rax
leaq (%rax,%rbp), %rsi
addq $-0x480, %rsi           # imm = 0xFB80
movl $0x400, %edx            # imm = 0x400
subq %rax, %rdx
callq 0x2300
movq %rax, %r12
movl %r12d, -0x2c(%rbp)
testl %r12d, %r12d
jle 0x42b0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %r13
movq %r14, %rdi
movq %r13, %rdx
callq 0x2160
addl %r13d, %r12d
movl %r12d, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movq %r13, %rdx
leaq 0x4606(%rip), %rcx      # 0x8233
xorl %r8d, %r8d
callq 0x2360
movl -0x2c(%rbp), %edx
movq %r14, %rdi
leaq 0x44ef(%rip), %rsi      # 0x8131
callq 0x65b8
testq %rax, %rax
jne 0x426a
cmpl $0x4, %r15d
ja 0x3e4e
movl %r15d, %eax
leaq 0x444c(%rip), %rcx      # 0x80b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %r14, %r13
jmpq *%rax
movq %r14, %rdi
leaq 0x44ad(%rip), %rsi      # 0x8127
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
testq %rax, %rax
jne 0x3ce4
movq %r14, %rdi
leaq 0x44bc(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
xorl %ebx, %ebx
movl $0x0, %r15d
testq %rax, %rax
je 0x3bd0
movq %r12, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
xorl %r15d, %r15d
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movq %r12, %rbx
jmp 0x3bd0
movq %r14, %r12
jmp 0x3d94
movq %r14, %r12
movq %r12, %rdi
leaq 0x4429(%rip), %rsi      # 0x8117
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %rbx
testq %rax, %rax
je 0x3eaa
movl $0x9, %edx
movq 0x82c1(%rip), %rdi      # 0xbfd0
leaq 0x443d(%rip), %rsi      # 0x8153
callq 0x2280
movq 0x82ae(%rip), %rcx      # 0xbfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x8290(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %rbx, %r12
jmp 0x3d55
movq %r14, %r12
movl -0x2c(%rbp), %eax
movl %eax, -0x78(%rbp)
movq %r12, %rdi
leaq 0x44f1(%rip), %rsi      # 0x8256
leaq -0x78(%rbp), %rdx
callq 0x6674
movq %rax, %r13
movl -0x78(%rbp), %esi
leaq 0x43e2(%rip), %rdi      # 0x815d
movq %r12, %rdx
xorl %eax, %eax
callq 0x2040
movl $0x2, %r15d
testq %r13, %r13
je 0x3bd0
movq 0x8235(%rip), %rcx      # 0xbfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x8217(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %r12, %rdi
leaq 0x4357(%rip), %rsi      # 0x8127
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r13
testq %rax, %rax
je 0x3e5c
movq %r13, %rdi
leaq 0x4377(%rip), %rsi      # 0x8162
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
testq %rax, %rax
jne 0x3ef8
movq %r13, %rdi
leaq 0x4347(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
movl $0x4, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd0
movq %r12, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movq %r12, %rbx
jmp 0x3bd0
jle 0x3bd0
movq %r14, %r12
jmp 0x3f01
movq %r12, %rdi
leaq 0x42eb(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
movl $0x3, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd0
movq %r12, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movq %r12, %rbx
jmp 0x3bd0
movq %r12, %rdi
leaq 0x429d(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
movl $0x1, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd0
movq %r12, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movq %r12, %rbx
jmp 0x3bd0
movl $0x5, %r15d
movq %r12, %rbx
cmpl $0x5, %r15d
jne 0x40f1
leaq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x68(%rbp)
movq %r12, %rdi
leaq 0x4240(%rip), %rsi      # 0x8169
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %rbx
movl -0x2c(%rbp), %eax
movl %eax, -0x54(%rbp)
testq %rbx, %rbx
je 0x401c
movq %rbx, %rdi
leaq 0x41e8(%rip), %rsi      # 0x8136
leaq -0x54(%rbp), %rdx
callq 0x6674
testq %rax, %rax
je 0x401c
movslq -0x54(%rbp), %r8
movq -0x70(%rbp), %rdx
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2360
leaq -0x78(%rbp), %rdi
leaq 0x418b(%rip), %rsi      # 0x810c
callq 0x20c0
testl %eax, %eax
je 0x406b
movl -0x30(%rbp), %edi
callq 0x2400
leaq -0x78(%rbp), %rdi
callq 0x669e
movq -0x78(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x21b0
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
je 0x418e
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq 0x8000(%rip), %rdi      # 0xbfd0
callq 0x2280
movq %rax, %r12
movl $0xd, %edx
movq %rax, %rdi
leaq 0x41af(%rip), %rsi      # 0x8196
callq 0x2280
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
xorl %r13d, %r13d
jmp 0x40bb
movq %r12, %rdi
leaq 0x412b(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %rbx
movl $0x5, %r15d
testq %rax, %rax
je 0x4061
movq %rbx, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movl $0x5, %r13d
jmp 0x40c4
xorl %ebx, %ebx
movl $0x5, %r13d
jmp 0x40c4
movl $0x14, %edx
movq 0x7f59(%rip), %rdi      # 0xbfd0
leaq 0x40ee(%rip), %rsi      # 0x816c
callq 0x2280
movq 0x7f46(%rip), %rcx      # 0xbfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x7f28(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl $0x4, %r13d
movl $0x6, %r15d
movq %rbx, %r12
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x40dd
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
testl %r13d, %r13d
je 0x40f1
cmpl $0x4, %r13d
je 0x3f01
jmp 0x3bd0
cmpl $0x8, %r15d
je 0x4164
cmpl $0x7, %r15d
je 0x412a
cmpl $0x6, %r15d
jne 0x3f01
movq %r12, %rdi
leaq 0x4006(%rip), %rsi      # 0x8117
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %rbx
movq %rax, %r13
testq %rax, %rax
jne 0x412d
jmp 0x421c
movq %r12, %r13
movl -0x2c(%rbp), %eax
movl %eax, -0x78(%rbp)
movq %r13, %rdi
leaq 0x4119(%rip), %rsi      # 0x8256
leaq -0x78(%rbp), %rdx
callq 0x6674
movq %rax, %r12
movslq -0x78(%rbp), %rdx
movl -0x30(%rbp), %edi
movq %r13, %rsi
callq 0x20f0
testq %r12, %r12
je 0x4211
movq %r13, %r12
movl -0x2c(%rbp), %edx
movq %r12, %rdi
leaq 0x3ff8(%rip), %rsi      # 0x8169
callq 0x65b8
testq %rax, %rax
je 0x419f
movl $0x5, %r15d
movl -0x30(%rbp), %edi
callq 0x2400
jmp 0x3f01
leaq 0x3fec(%rip), %rdi      # 0x8181
callq 0x21d0
jmp 0x3fc1
movl -0x30(%rbp), %edi
callq 0x2400
movq %r12, %rdi
leaq 0x3fb1(%rip), %rsi      # 0x8162
leaq -0x2c(%rbp), %rdx
callq 0x6616
testq %rbx, %rbx
jne 0x42b0
movq %rax, %rdi
leaq 0x3f84(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
movl $0x8, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd0
movq %r12, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movq %r12, %rbx
jmp 0x3bd0
movl $0x7, %r15d
jmp 0x3bd0
movq %r12, %rdi
leaq 0x3f2b(%rip), %rsi      # 0x8151
leaq -0x2c(%rbp), %rdx
callq 0x6616
movq %rax, %r12
movl $0x6, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd0
movq %r12, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
movq %r12, %rbx
jmp 0x3bd0
movq 0x7d5f(%rip), %rbx      # 0xbfd0
leaq 0x3ec0(%rip), %rsi      # 0x8138
movl $0x18, %edx
movq %rbx, %rdi
callq 0x2280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x7d2d(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x42c9
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4303
jmp 0x4303
jmp 0x4303
jmp 0x4303
jmp 0x4303
movq %rax, %rbx
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x4306
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0x4306
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x431f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0
nop