// File: /workspace/github_cpp_makefile_star2_O2/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
leaq -0x68(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, -0x80(%rbp)
movb $0x0, (%rax)
pushq $-0x1
popq %rax
movl %eax, -0x30(%rbp)
leaq -0x480(%rbp), %rbx
xorl %r15d, %r15d
movq -0x80(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x70(%rbp), %rax
leaq (%rax,%rbp), %rsi
addq $-0x480, %rsi           # imm = 0xFB80
movl $0x400, %edx            # imm = 0x400
subq %rax, %rdx
callq 0x2300
movq %rax, %r14
movl %r14d, -0x2c(%rbp)
testl %r14d, %r14d
jle 0x384f
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %r13
movq %rbx, %rdi
movq %r13, %rdx
callq 0x2180
addl %r14d, %r13d
movl %r13d, -0x2c(%rbp)
leaq -0x78(%rbp), %rdi
leaq 0x3f29(%rip), %rsi      # 0x721b
callq 0x23d0
movl -0x2c(%rbp), %edx
movq %rbx, %rdi
leaq 0x3e15(%rip), %rsi      # 0x7119
callq 0x53c0
testq %rax, %rax
jne 0x3834
cmpl $0x4, %r15d
ja 0x34b4
movl %r15d, %eax
leaq 0x3d62(%rip), %rcx      # 0x7088
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rbx, %r14
jmpq *%rax
movq %rbx, %rdi
leaq 0x3dd3(%rip), %rsi      # 0x710f
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r13
testq %rax, %rax
jne 0x339e
movq %rbx, %rdi
leaq 0x3de2(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
xorl %r15d, %r15d
movl $0x0, %r12d
testq %rax, %rax
je 0x329a
movq %r14, %rsi
decq %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
movq %r14, %r12
jmp 0x329a
movq %rbx, %r13
jmp 0x341f
movq %rbx, %r13
movq %r13, %rdi
leaq 0x3d57(%rip), %rsi      # 0x70ff
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r12
testq %rax, %rax
je 0x3511
movq 0x7c0c(%rip), %rdi      # 0xafd0
leaq 0x3d70(%rip), %rsi      # 0x713b
callq 0x2230
movq %rax, %rdi
callq 0x2170
movq %r12, %r13
jmp 0x33e0
movq %rbx, %r13
movl -0x2c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq %r13, %rdi
leaq 0x3e4e(%rip), %rsi      # 0x723e
leaq -0x58(%rbp), %rdx
callq 0x547f
movq %rax, %r14
movl -0x58(%rbp), %esi
leaq 0x3d3f(%rip), %rdi      # 0x7145
movq %r13, %rdx
xorl %eax, %eax
callq 0x2030
pushq $0x2
popq %rax
movl %eax, %r15d
testq %r14, %r14
je 0x329a
movq 0x7baa(%rip), %rdi      # 0xafd0
callq 0x2170
movq %r13, %rdi
leaq 0x3cda(%rip), %rsi      # 0x710f
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
testq %rax, %rax
je 0x34c2
movq %r14, %rdi
leaq 0x3cfa(%rip), %rsi      # 0x714a
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r13
testq %rax, %rax
jne 0x3560
movq %r14, %rdi
leaq 0x3cca(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
movl $0x0, %r12d
pushq $0x4
popq %rax
movl %eax, %r15d
testq %r14, %r14
je 0x329a
movq %r14, %rsi
decq %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
movq %r14, %r12
pushq $0x4
popq %rax
movl %eax, %r15d
jmp 0x329a
jle 0x329a
movq %rbx, %r13
jmp 0x3569
movq %r13, %rdi
leaq 0x3c6d(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
movl $0x0, %r12d
pushq $0x3
popq %rax
movl %eax, %r15d
testq %r14, %r14
je 0x329a
movq %r14, %rsi
decq %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
movq %r14, %r12
pushq $0x3
popq %rax
movl %eax, %r15d
jmp 0x329a
movq %r13, %rdi
leaq 0x3c1e(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
movl $0x0, %r12d
pushq $0x1
popq %rax
movl %eax, %r15d
testq %r14, %r14
je 0x329a
movq %r14, %rsi
decq %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
movq %r14, %r12
pushq $0x1
popq %rax
movl %eax, %r15d
jmp 0x329a
movq %r13, %r12
pushq $0x5
popq %rax
movl %eax, %r15d
leal -0x5(%r15), %eax
cmpl $0x3, %eax
ja 0x3569
leaq 0x3b23(%rip), %rcx      # 0x709c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
andq $0x0, -0x50(%rbp)
movb $0x0, -0x48(%rbp)
movq %r13, %rdi
leaq 0x3bb4(%rip), %rsi      # 0x7151
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r12
movl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
testq %r12, %r12
je 0x3723
movq %r12, %rdi
leaq 0x3b5c(%rip), %rsi      # 0x711e
leaq -0x34(%rbp), %rdx
callq 0x547f
testq %rax, %rax
je 0x3723
movslq -0x34(%rbp), %rdx
leaq -0x58(%rbp), %rdi
movq %r12, %rsi
callq 0x22d0
leaq -0x58(%rbp), %rdi
leaq 0x3b05(%rip), %rsi      # 0x70f4
callq 0x3b3f
testb %al, %al
je 0x362f
movq 0x79d1(%rip), %rdi      # 0xafd0
leaq 0x3b4e(%rip), %rsi      # 0x7154
callq 0x2230
movq %rax, %rdi
callq 0x2170
leaq -0x58(%rbp), %rdi
callq 0x2130
movq %r12, %r13
pushq $0x6
popq %rax
movl %eax, %r15d
jmp 0x3569
movq %r13, %r12
jmp 0x3692
movl -0x30(%rbp), %edi
callq 0x23f0
leaq -0x58(%rbp), %rdi
callq 0x54a9
movq -0x58(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x21c0
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
je 0x3712
movq 0x7967(%rip), %rdi      # 0xafd0
leaq -0x58(%rbp), %rsi
callq 0x2200
movq %rax, %rdi
leaq 0x3b02(%rip), %rsi      # 0x717e
callq 0x2230
movq %rax, %rdi
callq 0x2170
leaq -0x58(%rbp), %rdi
callq 0x2130
movq %r12, %rdi
leaq 0x3a63(%rip), %rsi      # 0x70ff
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r13
testq %rax, %rax
je 0x37e5
movq %r13, %r12
movl -0x2c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq %r13, %rdi
leaq 0x3b7a(%rip), %rsi      # 0x723e
leaq -0x58(%rbp), %rdx
callq 0x547f
movq %rax, %r14
movslq -0x58(%rbp), %rdx
movl -0x30(%rbp), %edi
movq %r13, %rsi
callq 0x20e0
testq %r14, %r14
je 0x37da
movl -0x2c(%rbp), %edx
movq %r13, %rdi
leaq 0x3a5c(%rip), %rsi      # 0x7151
callq 0x53c0
testq %rax, %rax
je 0x3767
movl -0x30(%rbp), %edi
callq 0x23f0
pushq $0x5
popq %rax
movl %eax, %r15d
jmp 0x3569
leaq 0x3a50(%rip), %rdi      # 0x7169
callq 0x21e0
jmp 0x3662
movq %r13, %rdi
leaq 0x3a0c(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r12
testq %rax, %rax
je 0x3753
leaq -0x1(%r12), %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
leaq -0x58(%rbp), %rdi
callq 0x2130
pushq $0x5
popq %rax
movl %eax, %r15d
jmp 0x329a
movl -0x30(%rbp), %edi
callq 0x23f0
movq %r13, %rdi
leaq 0x39d1(%rip), %rsi      # 0x714a
leaq -0x2c(%rbp), %rdx
callq 0x5421
testq %r12, %r12
jne 0x384f
movq %rax, %rdi
leaq 0x39a4(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
movl $0x0, %r12d
pushq $0x8
popq %rax
movl %eax, %r15d
testq %r14, %r14
je 0x329a
movq %r14, %rsi
decq %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
movq %r14, %r12
pushq $0x8
popq %rax
movl %eax, %r15d
jmp 0x329a
pushq $0x7
popq %rax
movl %eax, %r15d
jmp 0x329a
movq %r12, %rdi
leaq 0x394a(%rip), %rsi      # 0x7139
leaq -0x2c(%rbp), %rdx
callq 0x5421
movq %rax, %r14
movl $0x0, %r12d
pushq $0x6
popq %rax
movl %eax, %r15d
testq %r14, %r14
je 0x329a
movq %r14, %rsi
decq %rsi
movslq -0x2c(%rbp), %rdx
incq %rdx
leaq -0x78(%rbp), %rdi
callq 0x22d0
movq %r14, %r12
pushq $0x6
popq %rax
movl %eax, %r15d
jmp 0x329a
movq 0x7795(%rip), %rdi      # 0xafd0
leaq 0x38de(%rip), %rsi      # 0x7120
callq 0x2230
movq %rax, %rdi
callq 0x2170
leaq -0x78(%rbp), %rdi
callq 0x2130
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3886
jmp 0x3878
jmp 0x3878
jmp 0x3886
jmp 0x3886
jmp 0x3886
jmp 0x3886
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x2130
jmp 0x3889
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x2390