// File: andresziemecki[P]MQTT/mqtt.cpp
// Function: Client::startClient()
void Client::startClient(void){
    Message *m;
    bool disconnect = false;
    do{ 
        recvQueue.pop(&m); 
        switch (m->getType()){
            case Message::Type::CONNECT: {
                ConnectMsg *M = (ConnectMsg *)m;
                if (u_p.username == "$" && u_p.password=="$"){
                    Register_User(M);
                    mutex_cout.lock();
                    cout << "From SimClient\tMessage: CONNECT\tfrom: "<< M->getUsername() << " Registered"<< endl;
                    mutex_cout.unlock();
                    cif->recvMsg(ConnAckMsg(ConnAckMsg::Status::CONNECTION_OK));
                    }
                else if (check_username(M)){
                    mutex_cout.lock();
                    cout << "From SimClient\tMessage: CONNECT\tfrom: "<< M->getUsername() << "Logined" << endl;
                    mutex_cout.unlock();
                    cif->recvMsg(ConnAckMsg(ConnAckMsg::Status::CONNECTION_OK));}
                else
                    cif->recvMsg(ConnAckMsg(ConnAckMsg::Status::LOGIN_ERROR));
                delete(M);
                break;}
            case Message::Type::CONNACK: 
                break; // No hay connack hacia el Client, por eso no hay nada
            case Message::Type::PUBLISH:{
                PublishMsg *M = (PublishMsg *)m;
                mutex_cout.lock();
                cout << "From SimClient\tMessage: PUBLISH\tfrom: "<< u_p.username<<"\t" << "Topic " << M->getTopic() << ": " << M->getValue() << endl;
                mutex_cout.unlock();
                if (M->getRetain())
                    insert_retainedTopic (M->getTopic(), M->getValue());
                else
                    warn_everybody_thisTopic(M->getTopic(), M->getValue());
                delete(M);
                break;} 
            case Message::Type::SUBSCRIBE: {
                SubscribeMsg *M = (SubscribeMsg *)m;
                mutex_cout.lock();
                cout << "From SimClient\tMessage: SUBSCRIBE\tfrom: "<< u_p.username<<"\t" << "Topic "<< M->getTopic()<< endl;
                mutex_cout.unlock();
                insert_subscriber(M->getTopic());
                delete(M);
                break; }
            case Message::Type::UNSUBSCRIBE:{
                    UnsubscribeMsg *M = (UnsubscribeMsg *)m;
                    mutex_cout.lock();
                    cout << "From SimClient\tMessage: UNSUCRIBE\tfrom: "<< u_p.username<<"\t" << "Topic "<< M->getTopic() << endl;
                    mutex_cout.unlock();
                    unsuscribe_topic(M->getTopic());
                    delete(M);
                break; }
            case Message::Type::DISCONNECT:{
                DisconnectMsg *M = (DisconnectMsg *)m;
                mutex_cout.lock();
                cout << "From SimClient\tMessage: DISCONNECT\tfrom: "<< u_p.username<<endl;
                mutex_cout.unlock();
                disconnect = true;
                delete(M);
                break;}
        }
    }while(!disconnect);
    unsuscribe_all_topics(); 
    std::thread thr (&Broker::ClientDestruction,B,this, Client_TopicName);
    thr.detach();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x228(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x228(%rbp), %rdi
addq $0x38, %rdi
leaq -0x10(%rbp), %rsi
callq 0x75b0
movq -0x10(%rbp), %rdi
callq 0x7670
movl %eax, %ecx
movq %rcx, -0x230(%rbp)
subl $0x5, %eax
ja 0x530b
movq -0x230(%rbp), %rax
leaq 0xd659(%rip), %rcx      # 0x12004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x228(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
addq $0x138, %rdi            # imm = 0x138
leaq 0xd6e7(%rip), %rsi      # 0x120b8
callq 0x7690
testb $0x1, %al
jne 0x49df
jmp 0x4b30
movq -0x228(%rbp), %rdi
addq $0x138, %rdi            # imm = 0x138
addq $0x20, %rdi
leaq 0xd6c0(%rip), %rsi      # 0x120b8
callq 0x7690
testb $0x1, %al
jne 0x4a06
jmp 0x4b30
movq -0x228(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x54c0
leaq 0x177cb(%rip), %rdi     # 0x1c1e8
callq 0x76c0
movq 0x175af(%rip), %rdi     # 0x1bfd8
leaq 0xd68a(%rip), %rsi      # 0x120ba
callq 0x3200
movq %rax, -0x248(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x76f0
movq -0x248(%rbp), %rdi
movq -0x240(%rbp), %rsi
callq 0x31d0
movq %rax, -0x238(%rbp)
jmp 0x4a6c
movq -0x238(%rbp), %rdi
leaq 0xd667(%rip), %rsi      # 0x120e1
callq 0x3200
movq %rax, -0x250(%rbp)
jmp 0x4a88
movq -0x250(%rbp), %rdi
movq 0x1752a(%rip), %rsi     # 0x1bfc0
callq 0x3220
jmp 0x4a9d
leaq -0x40(%rbp), %rdi
callq 0x3160
leaq 0x1773b(%rip), %rdi     # 0x1c1e8
callq 0x7720
movq -0x228(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x260(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0x258(%rbp)
xorl %esi, %esi
callq 0x7740
movq -0x260(%rbp), %rdi
movq -0x258(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x4aee
leaq -0x60(%rbp), %rdi
callq 0x77a0
jmp 0x4cdb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x77a0
jmp 0x53b7
movq -0x228(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x5540
testb $0x1, %al
jne 0x4b49
jmp 0x4c6c
leaq 0x17698(%rip), %rdi     # 0x1c1e8
callq 0x76c0
movq 0x1747c(%rip), %rdi     # 0x1bfd8
leaq 0xd557(%rip), %rsi      # 0x120ba
callq 0x3200
movq %rax, -0x278(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x80(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x76f0
movq -0x278(%rbp), %rdi
movq -0x270(%rbp), %rsi
callq 0x31d0
movq %rax, -0x268(%rbp)
jmp 0x4b9f
movq -0x268(%rbp), %rdi
leaq 0xd540(%rip), %rsi      # 0x120ed
callq 0x3200
movq %rax, -0x280(%rbp)
jmp 0x4bbb
movq -0x280(%rbp), %rdi
movq 0x173f7(%rip), %rsi     # 0x1bfc0
callq 0x3220
jmp 0x4bd0
leaq -0x80(%rbp), %rdi
callq 0x3160
leaq 0x17608(%rip), %rdi     # 0x1c1e8
callq 0x7720
movq -0x228(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x290(%rbp)
leaq -0x90(%rbp), %rdi
movq %rdi, -0x288(%rbp)
xorl %esi, %esi
callq 0x7740
movq -0x290(%rbp), %rdi
movq -0x288(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x4c24
leaq -0x90(%rbp), %rdi
callq 0x77a0
jmp 0x4cd9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x77a0
jmp 0x53b7
movq -0x228(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x2a0(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x298(%rbp)
movl $0x1, %esi
callq 0x7740
movq -0x2a0(%rbp), %rdi
movq -0x298(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x4cae
leaq -0xa0(%rbp), %rdi
callq 0x77a0
jmp 0x4cd9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x77a0
jmp 0x53b7
jmp 0x4cdb
movq -0x20(%rbp), %rax
movq %rax, -0x2a8(%rbp)
cmpq $0x0, %rax
je 0x4cf9
movq -0x2a8(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x530b
jmp 0x530b
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq 0x174d3(%rip), %rdi     # 0x1c1e8
callq 0x76c0
movq 0x172b7(%rip), %rdi     # 0x1bfd8
leaq 0xd3cd(%rip), %rsi      # 0x120f5
callq 0x3200
movq -0x228(%rbp), %rsi
movq %rax, %rdi
addq $0x138, %rsi            # imm = 0x138
callq 0x31d0
movq %rax, %rdi
leaq 0xd3cf(%rip), %rsi      # 0x1211c
callq 0x3200
movq %rax, %rdi
leaq 0xd3c2(%rip), %rsi      # 0x1211e
callq 0x3200
movq %rax, -0x2c0(%rbp)
movq -0xa8(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x77c0
movq -0x2c0(%rbp), %rdi
movq -0x2b8(%rbp), %rsi
callq 0x31d0
movq %rax, -0x2b0(%rbp)
jmp 0x4d9e
movq -0x2b0(%rbp), %rdi
leaq 0xd39f(%rip), %rsi      # 0x1214b
callq 0x3200
movq %rax, -0x2c8(%rbp)
jmp 0x4dba
movq -0xa8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x77f0
jmp 0x4dcf
movq -0x2c8(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x31d0
movq %rax, -0x2d0(%rbp)
jmp 0x4deb
movq -0x2d0(%rbp), %rdi
movq 0x171c7(%rip), %rsi     # 0x1bfc0
callq 0x3220
jmp 0x4e00
leaq -0xe8(%rbp), %rdi
callq 0x3160
leaq -0xc8(%rbp), %rdi
callq 0x3160
leaq 0x173c9(%rip), %rdi     # 0x1c1e8
callq 0x7720
movq -0xa8(%rbp), %rdi
callq 0x7820
testb $0x1, %al
jne 0x4e39
jmp 0x4f08
movq -0xa8(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x77c0
movq -0xa8(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x77f0
jmp 0x4e61
movq -0x228(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x128(%rbp), %rdx
callq 0x5610
jmp 0x4e7d
leaq -0x128(%rbp), %rdi
callq 0x3160
leaq -0x108(%rbp), %rdi
callq 0x3160
jmp 0x4f9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4ec0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x3160
leaq -0xc8(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4ef7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x3160
leaq -0x108(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq -0xa8(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x77c0
movq -0xa8(%rbp), %rsi
leaq -0x168(%rbp), %rdi
callq 0x77f0
jmp 0x4f30
movq -0x228(%rbp), %rdi
leaq -0x148(%rbp), %rsi
leaq -0x168(%rbp), %rdx
callq 0x57e0
jmp 0x4f4c
leaq -0x168(%rbp), %rdi
callq 0x3160
leaq -0x148(%rbp), %rdi
callq 0x3160
jmp 0x4f9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4f8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3160
leaq -0x148(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq -0xa8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
cmpq $0x0, %rax
je 0x4fbe
movq -0x2d8(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x530b
movq -0x10(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq 0x17213(%rip), %rdi     # 0x1c1e8
callq 0x76c0
movq 0x16ff7(%rip), %rdi     # 0x1bfd8
leaq 0xd13d(%rip), %rsi      # 0x12125
callq 0x3200
movq -0x228(%rbp), %rsi
movq %rax, %rdi
addq $0x138, %rsi            # imm = 0x138
callq 0x31d0
movq %rax, %rdi
leaq 0xd10f(%rip), %rsi      # 0x1211c
callq 0x3200
movq %rax, %rdi
leaq 0xd102(%rip), %rsi      # 0x1211e
callq 0x3200
movq %rax, -0x2f0(%rbp)
movq -0x170(%rbp), %rsi
leaq -0x190(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x7840
movq -0x2f0(%rbp), %rdi
movq -0x2e8(%rbp), %rsi
callq 0x31d0
movq %rax, -0x2e0(%rbp)
jmp 0x505e
movq -0x2e0(%rbp), %rdi
movq 0x16f54(%rip), %rsi     # 0x1bfc0
callq 0x3220
jmp 0x5073
leaq -0x190(%rbp), %rdi
callq 0x3160
leaq 0x17162(%rip), %rdi     # 0x1c1e8
callq 0x7720
movq -0x170(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x7840
movq -0x228(%rbp), %rdi
movq -0x2f8(%rbp), %rsi
callq 0x5890
jmp 0x50ba
leaq -0x1b0(%rbp), %rdi
callq 0x3160
movq -0x170(%rbp), %rax
movq %rax, -0x300(%rbp)
cmpq $0x0, %rax
je 0x50e7
movq -0x300(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x530b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq -0x10(%rbp), %rax
movq %rax, -0x1b8(%rbp)
leaq 0x170b0(%rip), %rdi     # 0x1c1e8
callq 0x76c0
movq 0x16e94(%rip), %rdi     # 0x1bfd8
leaq 0xd003(%rip), %rsi      # 0x1214e
callq 0x3200
movq -0x228(%rbp), %rsi
movq %rax, %rdi
addq $0x138, %rsi            # imm = 0x138
callq 0x31d0
movq %rax, %rdi
leaq 0xcfac(%rip), %rsi      # 0x1211c
callq 0x3200
movq %rax, %rdi
leaq 0xcf9f(%rip), %rsi      # 0x1211e
callq 0x3200
movq %rax, -0x318(%rbp)
movq -0x1b8(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
movq %rdi, -0x310(%rbp)
callq 0x7870
movq -0x318(%rbp), %rdi
movq -0x310(%rbp), %rsi
callq 0x31d0
movq %rax, -0x308(%rbp)
jmp 0x51c1
movq -0x308(%rbp), %rdi
movq 0x16df1(%rip), %rsi     # 0x1bfc0
callq 0x3220
jmp 0x51d6
leaq -0x1d8(%rbp), %rdi
callq 0x3160
leaq 0x16fff(%rip), %rdi     # 0x1c1e8
callq 0x7720
movq -0x1b8(%rbp), %rsi
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x320(%rbp)
callq 0x7870
movq -0x228(%rbp), %rdi
movq -0x320(%rbp), %rsi
callq 0x59d0
jmp 0x521d
leaq -0x1f8(%rbp), %rdi
callq 0x3160
movq -0x1b8(%rbp), %rax
movq %rax, -0x328(%rbp)
cmpq $0x0, %rax
je 0x524a
movq -0x328(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x530b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x3160
jmp 0x53b7
movq -0x10(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq 0x16f4d(%rip), %rdi     # 0x1c1e8
callq 0x76c0
movq 0x16d31(%rip), %rdi     # 0x1bfd8
leaq 0xcec9(%rip), %rsi      # 0x12177
callq 0x3200
movq -0x228(%rbp), %rsi
movq %rax, %rdi
addq $0x138, %rsi            # imm = 0x138
callq 0x31d0
movq %rax, %rdi
movq 0x16ced(%rip), %rsi     # 0x1bfc0
callq 0x3220
leaq 0x16f09(%rip), %rdi     # 0x1c1e8
callq 0x7720
movb $0x1, -0x11(%rbp)
movq -0x200(%rbp), %rax
movq %rax, -0x330(%rbp)
cmpq $0x0, %rax
je 0x5309
movq -0x330(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x530b
jmp 0x530d
movb -0x11(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x496e
movq -0x228(%rbp), %rdi
callq 0x5af0
movq -0x228(%rbp), %r8
leaq 0x84c(%rip), %rax       # 0x5b80
movq %rax, -0x218(%rbp)
movq $0x0, -0x210(%rbp)
movq %r8, %rdx
addq $0x20, %rdx
movq %r8, -0x220(%rbp)
addq $0x118, %r8             # imm = 0x118
leaq -0x208(%rbp), %rdi
movq %rdi, -0x338(%rbp)
leaq -0x218(%rbp), %rsi
leaq -0x220(%rbp), %rcx
callq 0x78a0
movq -0x338(%rbp), %rdi
callq 0x3330
jmp 0x538a
leaq -0x208(%rbp), %rdi
callq 0x7460
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x7460
movq -0x48(%rbp), %rdi
callq 0x3340