// File: /workspace/github_cpp_makefile_star2_O1/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 0x42a6
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 0x45ea(%rip), %rcx      # 0x821f
xorl %r8d, %r8d
callq 0x2360
movl -0x2c(%rbp), %edx
movq %r14, %rdi
leaq 0x44d3(%rip), %rsi      # 0x811d
callq 0x6594
testq %rax, %rax
jne 0x4260
movq %r14, %r12
testl %r15d, %r15d
jne 0x3c88
movq %r14, %rdi
leaq 0x44a9(%rip), %rsi      # 0x8113
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %r12
testq %rax, %rax
je 0x3e24
movl $0x1, %r15d
movq %r12, %rbx
cmpl $0x1, %r15d
jne 0x3d00
movq %r12, %rdi
leaq 0x446b(%rip), %rsi      # 0x8103
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
testq %rax, %rax
je 0x3dde
movl $0x9, %edx
movq 0x8317(%rip), %rdi      # 0xbfd0
leaq 0x447f(%rip), %rsi      # 0x813f
callq 0x2280
movq 0x8304(%rip), %rcx      # 0xbfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x82e6(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl $0x2, %r15d
movq %rbx, %r12
cmpl $0x2, %r15d
jne 0x3d41
movl -0x2c(%rbp), %eax
movl %eax, -0x78(%rbp)
movq %r12, %rdi
leaq 0x452c(%rip), %rsi      # 0x8242
leaq -0x78(%rbp), %rdx
callq 0x664c
cmpq $0x1, %rax
movl $0x3, %r15d
sbbl $0x0, %r15d
movl -0x78(%rbp), %esi
leaq 0x4412(%rip), %rdi      # 0x8149
movq %r12, %rdx
xorl %eax, %eax
callq 0x2040
cmpl $0x3, %r15d
jne 0x3da1
movq 0x8282(%rip), %rcx      # 0xbfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x8264(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %r12, %rdi
leaq 0x4390(%rip), %rsi      # 0x8113
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
testq %rax, %rax
je 0x3e6a
movl $0x4, %r15d
movq %rbx, %r12
cmpl $0x4, %r15d
jne 0x3dcf
movq %r12, %rdi
leaq 0x439d(%rip), %rsi      # 0x814e
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
testq %rax, %rax
je 0x41cc
movl $0x5, %r15d
movq %rbx, %r12
cmpl $0x5, %r15d
jl 0x3bd8
jmp 0x3ed1
movq %r12, %rdi
leaq 0x4355(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
movl $0x1, %r15d
testq %rax, %rax
je 0x3bd8
movq %rbx, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
jmp 0x3bd8
movq %r14, %rdi
leaq 0x430f(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
xorl %r15d, %r15d
testq %rax, %rax
je 0x3bd8
movq %rbx, %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
jmp 0x3bd8
movq %r12, %rdi
leaq 0x42c9(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
movl $0x3, %r15d
testq %rax, %rax
je 0x3bd8
movq %rbx, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
jmp 0x3bd8
movl $0x5, %r15d
movl -0x30(%rbp), %edi
callq 0x2400
jmp 0x3ed1
leaq 0x42a6(%rip), %rdi      # 0x816d
callq 0x21d0
jmp 0x3f91
cmpl $0x5, %r15d
jne 0x40bf
leaq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x68(%rbp)
movq %r12, %rdi
leaq 0x425c(%rip), %rsi      # 0x8155
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
movl -0x2c(%rbp), %eax
movl %eax, -0x54(%rbp)
testq %rbx, %rbx
je 0x3fec
movq %rbx, %rdi
leaq 0x4204(%rip), %rsi      # 0x8122
leaq -0x54(%rbp), %rdx
callq 0x664c
testq %rax, %rax
je 0x3fec
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 0x41a7(%rip), %rsi      # 0x80f8
callq 0x20c0
testl %eax, %eax
je 0x4039
movl -0x30(%rbp), %edi
callq 0x2400
leaq -0x78(%rbp), %rdi
callq 0x6676
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 0x3ec0
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq 0x8030(%rip), %rdi      # 0xbfd0
callq 0x2280
movq %rax, %r12
movl $0xd, %edx
movq %rax, %rdi
leaq 0x41cb(%rip), %rsi      # 0x8182
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 0x4089
movq %r12, %rdi
leaq 0x4147(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
movl $0x5, %r15d
testq %rax, %rax
je 0x4031
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 0x4092
movl $0x5, %r13d
jmp 0x4092
movl $0x14, %edx
movq 0x7f8b(%rip), %rdi      # 0xbfd0
leaq 0x410c(%rip), %rsi      # 0x8158
callq 0x2280
movq 0x7f78(%rip), %rcx      # 0xbfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq 0x7f5a(%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 0x40ab
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
testl %r13d, %r13d
je 0x40bf
cmpl $0x4, %r13d
je 0x3ed1
jmp 0x3bd8
cmpl $0x6, %r15d
jne 0x40ed
movq %r12, %rdi
leaq 0x4034(%rip), %rsi      # 0x8103
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
testq %rax, %rax
je 0x4212
movl $0x7, %r15d
movq %rbx, %r12
cmpl $0x7, %r15d
jne 0x4135
movl -0x2c(%rbp), %eax
movl %eax, -0x78(%rbp)
movq %r12, %rdi
leaq 0x413f(%rip), %rsi      # 0x8242
leaq -0x78(%rbp), %rdx
callq 0x664c
movq %rax, %r13
movslq -0x78(%rbp), %rdx
movl -0x30(%rbp), %edi
movq %r12, %rsi
callq 0x20f0
xorl %r15d, %r15d
testq %r13, %r13
setne %r15b
addl $0x7, %r15d
testq %r13, %r13
je 0x3bd8
cmpl $0x8, %r15d
jne 0x3ed1
movl -0x2c(%rbp), %edx
movq %r12, %rdi
leaq 0x4009(%rip), %rsi      # 0x8155
callq 0x6594
testq %rax, %rax
jne 0x3eb0
movl -0x30(%rbp), %edi
callq 0x2400
movq %r12, %rdi
leaq 0x3fe2(%rip), %rsi      # 0x814e
leaq -0x2c(%rbp), %rdx
callq 0x65ee
testq %rbx, %rbx
jne 0x42a6
movq %rax, %rdi
leaq 0x3fb5(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %r12
movl $0x8, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd8
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 0x3bd8
movq %r12, %rdi
leaq 0x3f67(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %rbx
movl $0x4, %r15d
testq %rax, %rax
je 0x3bd8
movq %rbx, %rcx
decq %rcx
movslq -0x2c(%rbp), %r8
incq %r8
movq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x2360
jmp 0x3bd8
movq %r12, %rdi
leaq 0x3f21(%rip), %rsi      # 0x813d
leaq -0x2c(%rbp), %rdx
callq 0x65ee
movq %rax, %r12
movl $0x6, %r15d
movl $0x0, %ebx
testq %rax, %rax
je 0x3bd8
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 0x3bd8
movq 0x7d69(%rip), %rbx      # 0xbfd0
leaq 0x3eb6(%rip), %rsi      # 0x8124
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 0x7d37(%rip), %rdi      # 0xbfd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x42bf
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 0x42f9
jmp 0x42f9
jmp 0x42f9
jmp 0x42f9
jmp 0x42f9
movq %rax, %rbx
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x42fc
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0x42fc
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x4315
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0
nop