// File: /workspace/github_cpp_makefile_star2_O2/davidcairuz[P]network-internet-relay-chat/client.cpp
// Function: main
int main(int argc, char* argv[]) {
    signal(SIGINT, Sigint_handler);

    if (argc < 2) {
        cout << "Provide your ip, please\n";
        return 1; 
    }

    string ip = argv[1];
    if (!check_ip(ip)) {
        cout << "Invalid IP\n";
        return 1;
    }

    pthread_t tid_receive;
    pthread_t tid_send;
    string command = "";
    
    nickname = get_nickname();
    cout << get_menu();

    // menu enquanto desconectado
    while (command != "/connect") {
        if(!getline(cin, command, '\n')) {
            cout << "quitting" << endl;
            exit(0);
        }

        if (command == "/menu") 
            cout << get_menu();
        else if(command == "/quit") 
            exit(0);
        else if(command == "/ping") 
            cout << "You've got to be connected!" << endl;
        else if(command.size() >= 9 && command.substr(0, 9) == "/nickname") {
            string new_nickname = command.substr(10, command.size());
            
            if (check_username(nickname)) {
                nickname = new_nickname;
                cout << "Nickname updated to: " << nickname << endl;        
            } else {
                cout << "Type a valid username (a-z, A-Z, ., _, -, size > 2)";
            }    
        } else if(command.size() >= 5 && command.substr(0, 5) == "/join") {
            cout << "You've got to be connected!" << endl;
        } else if (command.size() >= 5 && command.substr(0, 5) == "/kick") {
            cout << "You've got to be connected!" << endl;
        } else if (command.size() >= 5 && command.substr(0, 5) == "/mute") {
            cout << "You've got to be connected!" << endl;
        } else if (command.size() >= 7 && command.substr(0, 7) == "/unmute") {
            cout << "You've got to be connected!" << endl;
        }
    }

    client_socket = new Socket(ip, nickname);
    client_socket->Check();

    client_socket->Connect();
    client_socket->Check();

    if (pthread_create(&tid_send, NULL, client_send_thread, NULL) != 0) {
        cout << "Failed to create thread to send message" << endl;
        return 1;
    }
    if (pthread_create(&tid_receive, NULL, client_receive_thread, NULL) != 0) {
        cout << "Failed to create thread to receive message" << endl;
        return 1;
    }

    while (!quit);

    cout << "É nois flw vlw" << endl;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rbx
movl %edi, %r14d
leaq -0x7d7(%rip), %rsi      # 0x2c94
pushq $0x2
popq %rdi
callq 0x21f0
cmpl $0x1, %r14d
jg 0x3494
movq 0x3b50(%rip), %rdi      # 0x6fd0
leaq 0xf3a(%rip), %rsi       # 0x43c1
callq 0x2180
pushq $0x1
popq %rbx
jmp 0x3927
movq 0x8(%rbx), %rsi
leaq -0xd8(%rbp), %rbx
leaq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x254a
leaq -0x118(%rbp), %rdi
movq %rbx, %rsi
callq 0x2230
leaq -0x118(%rbp), %rbx
movq %rbx, %rdi
callq 0x2f29
movl %eax, %r14d
movq %rbx, %rdi
callq 0x22e8
testb %r14b, %r14b
je 0x3809
leaq 0xda0(%rip), %rsi       # 0x4284
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x254a
leaq -0x68(%rbp), %rdi
callq 0x2e0e
leaq 0x3c97(%rip), %rdi      # 0x7198
leaq -0x68(%rbp), %rbx
movq %rbx, %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x22e8
leaq -0x68(%rbp), %rdi
callq 0x2d45
movq 0x3aab(%rip), %rdi      # 0x6fd0
leaq -0x68(%rbp), %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x22e8
leaq -0x48(%rbp), %r15
movq 0x3a9f(%rip), %r13      # 0x6fe0
leaq 0xe08(%rip), %r14       # 0x4350
movq %r15, %rdi
leaq 0xe94(%rip), %rsi       # 0x43e6
callq 0x3a0d
testb %al, %al
je 0x3824
movq %r13, %rdi
movq %r15, %rsi
pushq $0xa
popq %rdx
callq 0x22a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x395a
movq %r15, %rdi
movq %r14, %rsi
callq 0x39fd
testb %al, %al
je 0x35af
movq %rbx, %rdi
callq 0x2d45
movq 0x3a33(%rip), %rdi      # 0x6fd0
movq %rbx, %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x22e8
jmp 0x3548
movq %r15, %rdi
leaq 0xd91(%rip), %rsi       # 0x434a
callq 0x39fd
testb %al, %al
jne 0x3975
movq %r15, %rdi
leaq 0xe28(%rip), %rsi       # 0x43f8
callq 0x39fd
testb %al, %al
je 0x35f9
movq 0x39f0(%rip), %rdi      # 0x6fd0
leaq 0xe17(%rip), %rsi       # 0x43fe
callq 0x2180
movq %rax, %rdi
callq 0x20d0
jmp 0x3548
movq -0x40(%rbp), %rax
cmpq $0x9, %rax
jb 0x36d2
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
pushq $0x9
popq %rcx
callq 0x21c0
movq %rbx, %rdi
leaq 0xd35(%rip), %rsi       # 0x4356
callq 0x39fd
movl %eax, %r13d
movq %rbx, %rdi
callq 0x22e8
testb %r13b, %r13b
je 0x36c7
movq -0x40(%rbp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
pushq $0xa
popq %rdx
callq 0x21c0
leaq -0xf8(%rbp), %rdi
leaq 0x3b3e(%rip), %rsi      # 0x7198
callq 0x2230
leaq -0xf8(%rbp), %r12
movq %r12, %rdi
callq 0x2d9c
movl %eax, %r13d
movq %r12, %rdi
callq 0x22e8
testb %r13b, %r13b
je 0x37ea
leaq 0x3b0f(%rip), %rdi      # 0x7198
movq %rbx, %rsi
callq 0x20e0
movq 0x3948(%rip), %r13      # 0x6fe0
movq 0x3931(%rip), %rdi      # 0x6fd0
leaq 0xcee(%rip), %rsi       # 0x4394
callq 0x2180
movq %rax, %rdi
leaq 0x3ae3(%rip), %rsi      # 0x7198
callq 0x2160
movq %rax, %rdi
callq 0x20d0
jmp 0x35a5
movq -0x40(%rbp), %rax
movq 0x390e(%rip), %r13      # 0x6fe0
cmpq $0x5, %rax
jb 0x3548
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
pushq $0x5
popq %rcx
callq 0x21c0
movq %rbx, %rdi
leaq 0xcb4(%rip), %rsi       # 0x43aa
callq 0x39fd
movl %eax, %r13d
movq %rbx, %rdi
callq 0x22e8
testb %r13b, %r13b
movq 0x38d0(%rip), %r13      # 0x6fe0
jne 0x35d9
cmpq $0x5, -0x40(%rbp)
jb 0x3548
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
pushq $0x5
popq %rcx
callq 0x21c0
movq %rbx, %rdi
leaq 0xcdf(%rip), %rsi       # 0x441a
callq 0x39fd
movl %eax, %r13d
movq %rbx, %rdi
callq 0x22e8
testb %r13b, %r13b
movq 0x388b(%rip), %r13      # 0x6fe0
jne 0x35d9
cmpq $0x5, -0x40(%rbp)
jb 0x3548
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
pushq $0x5
popq %rcx
callq 0x21c0
movq %rbx, %rdi
leaq 0xca0(%rip), %rsi       # 0x4420
callq 0x39fd
movl %eax, %r13d
movq %rbx, %rdi
callq 0x22e8
testb %r13b, %r13b
movq 0x3846(%rip), %r13      # 0x6fe0
jne 0x35d9
cmpq $0x7, -0x40(%rbp)
jb 0x3548
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
pushq $0x7
popq %rcx
callq 0x21c0
movq %rbx, %rdi
leaq 0xc61(%rip), %rsi       # 0x4426
callq 0x39fd
movl %eax, %r13d
movq %rbx, %rdi
callq 0x22e8
testb %r13b, %r13b
movq 0x3801(%rip), %r13      # 0x6fe0
je 0x3548
jmp 0x35d9
movq 0x37df(%rip), %rdi      # 0x6fd0
leaq 0xb68(%rip), %rsi       # 0x4360
callq 0x2180
movq 0x37dc(%rip), %r13      # 0x6fe0
jmp 0x35a5
movq 0x37c0(%rip), %rdi      # 0x6fd0
leaq 0xbc3(%rip), %rsi       # 0x43da
callq 0x2180
pushq $0x1
popq %rbx
jmp 0x391b
movl $0xa0, %edi
callq 0x2190
movq %rax, %r14
leaq -0xa8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x2230
leaq 0x394d(%rip), %rsi      # 0x7198
leaq -0x88(%rbp), %rdi
callq 0x2230
leaq -0xa8(%rbp), %rsi
leaq -0x88(%rbp), %rdx
movq %r14, %rdi
movl $0x2000, %ecx           # imm = 0x2000
callq 0x2624
movq %r14, 0x3967(%rip)      # 0x71e0
leaq -0x88(%rbp), %rdi
callq 0x22e8
leaq -0xa8(%rbp), %rdi
callq 0x22e8
movq 0x3948(%rip), %rdi      # 0x71e0
callq 0x2b92
movq 0x393c(%rip), %rdi      # 0x71e0
callq 0x288e
movq 0x3930(%rip), %rdi      # 0x71e0
callq 0x2b92
leaq -0x825(%rip), %rdx      # 0x3097
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x2290
testl %eax, %eax
je 0x38d9
leaq 0xb57(%rip), %rsi       # 0x442e
jmp 0x38fb
leaq -0x995(%rip), %rdx      # 0x2f4b
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x2290
testl %eax, %eax
je 0x393b
leaq 0xb5b(%rip), %rsi       # 0x4456
movq 0x36ce(%rip), %rdi      # 0x6fd0
callq 0x2180
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbx
leaq -0x48(%rbp), %rdi
callq 0x22e8
leaq -0xd8(%rbp), %rdi
callq 0x22e8
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x368e(%rip), %rdi      # 0x6fd0
leaq 0xb38(%rip), %rsi       # 0x4481
callq 0x2180
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x20d0
jmp 0x3912
movq 0x366f(%rip), %rdi      # 0x6fd0
leaq 0xa87(%rip), %rsi       # 0x43ef
callq 0x2180
movq %rax, %rdi
callq 0x20d0
xorl %edi, %edi
callq 0x2200
jmp 0x39dd
jmp 0x39dd
jmp 0x39dd
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x22e8
jmp 0x3996
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x22e8
jmp 0x39a7
movq %rax, %rbx
movl $0xa0, %esi
movq %r14, %rdi
callq 0x21b0
jmp 0x39e0
jmp 0x39cb
jmp 0x39dd
jmp 0x39dd
jmp 0x39c2
jmp 0x39dd
jmp 0x39dd
movq %rax, %rbx
jmp 0x39e9
jmp 0x39dd
jmp 0x39cb
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x22e8
jmp 0x39e0
jmp 0x39dd
jmp 0x39dd
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x22e8
leaq -0xd8(%rbp), %rdi
callq 0x22e8
movq %rbx, %rdi
callq 0x2260