// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %r14
leaq 0x38(%rdi), %rax
movq %rax, -0x68(%rbp)
leaq 0x138(%rdi), %rax
movq %rax, -0x30(%rbp)
leaq 0x158(%rdi), %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x58(%rbp)
movq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5986
movq -0x70(%rbp), %r12
movl 0x8(%r12), %eax
cmpq $0x5, %rax
ja 0x4101
leaq 0x3ee0(%rip), %rcx      # 0x8004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x30(%rbp), %rdi
leaq 0x3f7c(%rip), %rsi      # 0x80b4
callq 0x5a1a
testb %al, %al
je 0x4436
movq -0x60(%rbp), %rdi
leaq 0x3f64(%rip), %rsi      # 0x80b4
callq 0x5a1a
testb %al, %al
je 0x4436
movq %r14, %rdi
movq %r12, %rsi
callq 0x473e
leaq 0x9041(%rip), %rdi      # 0xd1b0
callq 0x5a2a
movq 0x8e5d(%rip), %rdi      # 0xcfd8
leaq 0x3f34(%rip), %rsi      # 0x80b6
callq 0x31e0
movq %rax, %rbx
leaq 0x10(%r12), %rsi
leaq -0x50(%rbp), %r13
movq %r13, %rdi
callq 0x3250
movq %rbx, %rdi
movq %r13, %rsi
callq 0x31b0
movq %rax, %rdi
leaq 0x3f2d(%rip), %rsi      # 0x80dd
callq 0x31e0
movq %rax, %rdi
callq 0x3120
jmp 0x449e
leaq 0x8fe7(%rip), %rdi      # 0xd1b0
callq 0x5a2a
movq 0x8e03(%rip), %rdi      # 0xcfd8
leaq 0x3f45(%rip), %rsi      # 0x8121
callq 0x31e0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x31b0
movq %rax, %rdi
leaq 0x3f21(%rip), %rsi      # 0x8118
callq 0x31e0
movq %rax, %rdi
leaq 0x3f14(%rip), %rsi      # 0x811a
callq 0x31e0
movq %rax, %r14
leaq 0x10(%r12), %rbx
leaq -0x50(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3250
movq %r14, %rdi
movq %r13, %rsi
callq 0x31b0
movq %rax, %rdi
callq 0x3120
leaq -0x50(%rbp), %rdi
callq 0x30f0
leaq 0x8f6b(%rip), %rdi      # 0xd1b0
callq 0x3180
leaq -0xd8(%rbp), %rdi
movq %rbx, %rsi
movq %rdi, %rbx
callq 0x3250
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x49de
jmp 0x4319
leaq 0x8f39(%rip), %rdi      # 0xd1b0
callq 0x5a2a
movq 0x8d55(%rip), %rdi      # 0xcfd8
leaq 0x3ec0(%rip), %rsi      # 0x814a
callq 0x31e0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x31b0
movq %rax, %rdi
leaq 0x3e73(%rip), %rsi      # 0x8118
callq 0x31e0
movq %rax, %rdi
leaq 0x3e66(%rip), %rsi      # 0x811a
callq 0x31e0
movq %rax, %r14
leaq 0x10(%r12), %rbx
leaq -0x50(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3250
movq %r14, %rdi
movq %r13, %rsi
callq 0x31b0
movq %rax, %rdi
callq 0x3120
leaq -0x50(%rbp), %rdi
callq 0x30f0
leaq 0x8ebd(%rip), %rdi      # 0xd1b0
callq 0x3180
leaq -0xb8(%rbp), %rdi
movq %rbx, %rsi
movq %rdi, %rbx
callq 0x3250
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4a4e
movq %rbx, %rdi
jmp 0x4516
leaq -0x90(%rbp), %r15
leaq 0x8e81(%rip), %rdi      # 0xd1b0
callq 0x5a2a
movq 0x8c9d(%rip), %rdi      # 0xcfd8
leaq 0x3daf(%rip), %rsi      # 0x80f1
callq 0x31e0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x31b0
movq %rax, %rdi
leaq 0x3dbb(%rip), %rsi      # 0x8118
callq 0x31e0
movq %rax, %rdi
leaq 0x3dae(%rip), %rsi      # 0x811a
callq 0x31e0
movq %rax, %rbx
leaq 0x10(%r12), %r14
leaq -0x50(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
movq %rbx, %rdi
movq %r13, %rsi
callq 0x31b0
movq %rax, %rdi
leaq 0x3daa(%rip), %rsi      # 0x8147
callq 0x31e0
movq %rax, %r13
leaq 0x30(%r12), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3250
movq %r13, %rdi
movq %r15, %rsi
callq 0x31b0
movq %rax, %rdi
callq 0x3120
leaq -0x90(%rbp), %rdi
callq 0x30f0
leaq -0x50(%rbp), %rdi
callq 0x30f0
leaq 0x8dcc(%rip), %rdi      # 0xd1b0
callq 0x3180
cmpb $0x1, 0x50(%r12)
jne 0x44d5
leaq -0x158(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3250
leaq -0x138(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3250
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x4848
movq %r15, %rdi
movq %r13, %r15
jmp 0x450e
movq %r14, %rdi
movq %r12, %rsi
callq 0x47ac
testb %al, %al
je 0x451d
leaq 0x8d60(%rip), %rdi      # 0xd1b0
callq 0x5a2a
movq 0x8b7c(%rip), %rdi      # 0xcfd8
leaq 0x3c53(%rip), %rsi      # 0x80b6
callq 0x31e0
movq %rax, %rbx
leaq 0x10(%r12), %rsi
leaq -0x50(%rbp), %r13
movq %r13, %rdi
callq 0x3250
movq %rbx, %rdi
movq %r13, %rsi
callq 0x31b0
movq %rax, %rdi
leaq 0x3c58(%rip), %rsi      # 0x80e9
callq 0x31e0
movq %rax, %rdi
callq 0x3120
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x30f0
leaq 0x8cff(%rip), %rdi      # 0xd1b0
callq 0x3180
movq 0x30(%r14), %rdi
leaq 0x86b7(%rip), %rax      # 0xcb78
movq %rax, -0x50(%rbp)
movq $0x1, -0x48(%rbp)
movq (%rdi), %rax
movq %rbx, %rsi
jmp 0x4541
leaq -0x118(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3250
leaq -0xf8(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3250
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x496a
movq %r13, %rdi
callq 0x30f0
movq %r15, %rdi
callq 0x30f0
jmp 0x4543
movq 0x30(%r14), %rdi
leaq 0x8650(%rip), %rax      # 0xcb78
movq %rax, -0x50(%rbp)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, -0x48(%rbp)
movq (%rdi), %rax
leaq -0x50(%rbp), %rsi
callq *(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x10(%rax)
jmp 0x4101
leaq 0x8c57(%rip), %rbx      # 0xd1b0
movq %rbx, %rdi
callq 0x5a2a
movq 0x8a70(%rip), %rdi      # 0xcfd8
leaq 0x3c04(%rip), %rsi      # 0x8173
callq 0x31e0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x31b0
movq %rax, %rdi
callq 0x3120
movq %rbx, %rdi
callq 0x3180
movq (%r12), %rax
movq %r12, %rdi
callq *0x10(%rax)
movq %r14, %rdi
callq 0x4aea
leaq 0x5a1(%rip), %rax       # 0x4b4a
leaq -0x50(%rbp), %rsi
movq %rax, (%rsi)
andq $0x0, 0x8(%rsi)
leaq 0x20(%r14), %rdx
leaq -0x98(%rbp), %rcx
movq %r14, (%rcx)
addq $0x118, %r14            # imm = 0x118
leaq -0x90(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %r8
callq 0x5a40
movq %rbx, %rdi
callq 0x32e0
leaq -0x90(%rbp), %rdi
callq 0x58aa
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x58aa
jmp 0x468c
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x30f0
jmp 0x4627
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
jmp 0x4687
movq %rax, %rbx
jmp 0x4644
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x30f0
leaq -0x158(%rbp), %rdi
jmp 0x4687
jmp 0x4680
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
jmp 0x4687
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
jmp 0x4687
jmp 0x4680
jmp 0x4680
jmp 0x4680
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x30f0
jmp 0x4683
jmp 0x4680
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x30f0
movq %rbx, %rdi
callq 0x32f0