// File: davidcairuz[P]network-internet-relay-chat/client.cpp
// Function: client_send_thread(void*)
void* client_send_thread(void* arg) {
    cout << "Connected to server" << endl;
    client_socket->Write(nickname);
    string message = "";

    while (!quit) {
        if (!getline(cin, message, '\n')) {
            cout << "Tiau" << endl;
            message = "/quit";
        }

        // checa mensagens especiais
        if (message == "/menu") {
            cout << get_menu();
            continue;
            
        } else if(message.size() >= 10 && message.substr(0, 9) == "/nickname") {
            if (message.size () < 12) 
                cout << "Type a valid username (a-z, A-Z, ., _, -, size > 2)";
            
            string new_nickname = message.substr(10, message.size());
            
            if (check_username(nickname)) {
                nickname = new_nickname;
                cout << "Nickname updated to: " << nickname << endl;        
            }

        } else if(message.size() >= 6 && message.substr(0, 5) == "/join") {
            string new_channel = message.substr(6, message.size());
            
            if (check_channel_name(new_channel)) {
                channel = new_channel;
            } else {
                cout << "Invalid channel " << channel << endl;
                continue;
            }
        }

        // envia cada catch da mesagem
        for (int i = 0; (unsigned int)i < message.size(); i += Socket::buffer_size) {
            client_socket->Write(message.substr(i, Socket::buffer_size));
            client_socket->Check();
        }

        if (message == "/quit") {
            quit = true;
        }

        message = "/quit";
    }

    pthread_exit(NULL);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq 0x50ea(%rip), %rdi      # 0x8fd0
leaq 0x2444(%rip), %rsi      # 0x6331
callq 0x2220
movq %rax, %rdi
movq 0x50bc(%rip), %rsi      # 0x8fb8
callq 0x2270
movq 0x5388(%rip), %rax      # 0x9290
movq %rax, -0x188(%rbp)
leaq 0x5332(%rip), %rsi      # 0x9248
leaq -0x28(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x20f0
movq -0x188(%rbp), %rdi
movq -0x180(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x33d0
jmp 0x3f40
leaq -0x28(%rbp), %rdi
callq 0x2448
leaq -0x59(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x23c0
movq -0x190(%rbp), %rdx
leaq 0x231d(%rip), %rsi      # 0x6284
leaq -0x58(%rbp), %rdi
callq 0x2840
jmp 0x3f72
leaq -0x59(%rbp), %rdi
callq 0x2290
movb 0x5307(%rip), %al       # 0x9288
xorb $-0x1, %al
testb $0x1, %al
jne 0x3f8c
jmp 0x45ef
movq 0x504d(%rip), %rdi      # 0x8fe0
leaq -0x58(%rbp), %rsi
movl $0xa, %edx
callq 0x23f0
movq %rax, -0x198(%rbp)
jmp 0x3faa
movq -0x198(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2120
movb %al, -0x199(%rbp)
jmp 0x3fc8
movb -0x199(%rbp), %al
testb $0x1, %al
jne 0x3fd7
jmp 0x4061
movq 0x4ff2(%rip), %rdi      # 0x8fd0
leaq 0x2360(%rip), %rsi      # 0x6345
callq 0x2220
movq %rax, -0x1a8(%rbp)
jmp 0x3ff3
movq -0x1a8(%rbp), %rdi
movq 0x4fb7(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x4008
leaq 0x233b(%rip), %rsi      # 0x634a
leaq -0x58(%rbp), %rdi
callq 0x2330
jmp 0x401a
jmp 0x4061
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2448
jmp 0x4603
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x2290
jmp 0x4603
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x45fa
leaq 0x22e8(%rip), %rsi      # 0x6350
leaq -0x58(%rbp), %rdi
callq 0x5250
movb %al, -0x1a9(%rbp)
jmp 0x4079
movb -0x1a9(%rbp), %al
testb $0x1, %al
jne 0x4085
jmp 0x40ca
leaq -0x80(%rbp), %rdi
callq 0x38d0
jmp 0x4090
movq 0x4f39(%rip), %rdi      # 0x8fd0
leaq -0x80(%rbp), %rsi
callq 0x2200
jmp 0x40a2
leaq -0x80(%rbp), %rdi
callq 0x2448
jmp 0x3f7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x2448
jmp 0x45fa
leaq -0x58(%rbp), %rdi
callq 0x2160
movq %rax, %rcx
movb $0x0, -0xa1(%rbp)
xorl %eax, %eax
cmpq $0xa, %rcx
movb %al, -0x1aa(%rbp)
jb 0x4136
xorl %eax, %eax
movl %eax, %edx
leaq -0xa0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x9, %ecx
callq 0x2260
jmp 0x4106
movb $0x1, -0xa1(%rbp)
leaq 0x2242(%rip), %rsi      # 0x6356
leaq -0xa0(%rbp), %rdi
callq 0x5250
movb %al, -0x1ab(%rbp)
jmp 0x4128
movb -0x1ab(%rbp), %al
movb %al, -0x1aa(%rbp)
jmp 0x4136
movb -0x1aa(%rbp), %al
movb %al, -0x1ac(%rbp)
testb $0x1, -0xa1(%rbp)
jne 0x414d
jmp 0x4159
leaq -0xa0(%rbp), %rdi
callq 0x2448
movb -0x1ac(%rbp), %al
testb $0x1, %al
jne 0x4168
jmp 0x42d5
leaq -0x58(%rbp), %rdi
callq 0x2160
cmpq $0xc, %rax
jae 0x41b6
movq 0x4e52(%rip), %rdi      # 0x8fd0
leaq 0x21db(%rip), %rsi      # 0x6360
callq 0x2220
jmp 0x418c
jmp 0x41b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
testb $0x1, -0xa1(%rbp)
jne 0x41a5
jmp 0x41b1
leaq -0xa0(%rbp), %rdi
callq 0x2448
jmp 0x45fa
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x2160
movq -0x1b8(%rbp), %rsi
movq %rax, %rcx
leaq -0xc8(%rbp), %rdi
movl $0xa, %edx
callq 0x2260
jmp 0x41e3
leaq 0x505e(%rip), %rsi      # 0x9248
leaq -0xe8(%rbp), %rdi
callq 0x20f0
jmp 0x41f8
leaq -0xe8(%rbp), %rdi
callq 0x39b0
movb %al, -0x1b9(%rbp)
jmp 0x420c
leaq -0xe8(%rbp), %rdi
callq 0x2448
movb -0x1b9(%rbp), %al
testb $0x1, %al
jne 0x4227
jmp 0x42b3
leaq 0x501a(%rip), %rdi      # 0x9248
leaq -0xc8(%rbp), %rsi
callq 0x2030
jmp 0x423c
movq 0x4d8d(%rip), %rdi      # 0x8fd0
leaq 0x214a(%rip), %rsi      # 0x6394
callq 0x2220
movq %rax, -0x1c8(%rbp)
jmp 0x4258
movq -0x1c8(%rbp), %rdi
leaq 0x4fe2(%rip), %rsi      # 0x9248
callq 0x2200
movq %rax, -0x1d0(%rbp)
jmp 0x4274
movq -0x1d0(%rbp), %rdi
movq 0x4d36(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x4289
jmp 0x42b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x42c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x2448
jmp 0x42c4
leaq -0xc8(%rbp), %rdi
callq 0x2448
jmp 0x44e6
leaq -0xc8(%rbp), %rdi
callq 0x2448
jmp 0x45fa
leaq -0x58(%rbp), %rdi
callq 0x2160
movq %rax, %rcx
movb $0x0, -0x109(%rbp)
xorl %eax, %eax
cmpq $0x6, %rcx
movb %al, -0x1d1(%rbp)
jb 0x4341
xorl %eax, %eax
movl %eax, %edx
leaq -0x108(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x5, %ecx
callq 0x2260
jmp 0x4311
movb $0x1, -0x109(%rbp)
leaq 0x208b(%rip), %rsi      # 0x63aa
leaq -0x108(%rbp), %rdi
callq 0x5250
movb %al, -0x1d2(%rbp)
jmp 0x4333
movb -0x1d2(%rbp), %al
movb %al, -0x1d1(%rbp)
jmp 0x4341
movb -0x1d1(%rbp), %al
movb %al, -0x1d3(%rbp)
testb $0x1, -0x109(%rbp)
jne 0x4358
jmp 0x4364
leaq -0x108(%rbp), %rdi
callq 0x2448
movb -0x1d3(%rbp), %al
testb $0x1, %al
jne 0x4373
jmp 0x44e4
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x2160
movq -0x1e0(%rbp), %rsi
movq %rax, %rcx
leaq -0x130(%rbp), %rdi
movl $0x6, %edx
callq 0x2260
jmp 0x43a0
leaq -0x150(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x20f0
jmp 0x43b5
leaq -0x150(%rbp), %rdi
callq 0x3800
movb %al, -0x1e1(%rbp)
jmp 0x43c9
leaq -0x150(%rbp), %rdi
callq 0x2448
movb -0x1e1(%rbp), %al
testb $0x1, %al
jne 0x43e1
jmp 0x4451
leaq 0x4e80(%rip), %rdi      # 0x9268
leaq -0x130(%rbp), %rsi
callq 0x2030
jmp 0x43f6
jmp 0x44aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
testb $0x1, -0x109(%rbp)
jne 0x4412
jmp 0x441e
leaq -0x108(%rbp), %rdi
callq 0x2448
jmp 0x45fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x44d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x2448
jmp 0x44d3
movq 0x4b78(%rip), %rdi      # 0x8fd0
leaq 0x1f51(%rip), %rsi      # 0x63b0
callq 0x2220
movq %rax, -0x1f0(%rbp)
jmp 0x446d
movq -0x1f0(%rbp), %rdi
leaq 0x4ded(%rip), %rsi      # 0x9268
callq 0x2200
movq %rax, -0x1f8(%rbp)
jmp 0x4489
movq -0x1f8(%rbp), %rdi
movq 0x4b21(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x449e
movl $0x2, -0x154(%rbp)
jmp 0x44b4
movl $0x0, -0x154(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x2448
movl -0x154(%rbp), %eax
testl %eax, %eax
je 0x44d1
jmp 0x44cc
jmp 0x3f7b
jmp 0x44e4
leaq -0x130(%rbp), %rdi
callq 0x2448
jmp 0x45fa
jmp 0x44e6
jmp 0x44e8
movl $0x0, -0x158(%rbp)
movl -0x158(%rbp), %eax
movq %rax, -0x200(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2160
movq %rax, %rcx
movq -0x200(%rbp), %rax
cmpq %rcx, %rax
jae 0x45ad
movq 0x4d6e(%rip), %rax      # 0x9290
movq %rax, -0x208(%rbp)
movslq -0x158(%rbp), %rdx
leaq -0x178(%rbp), %rdi
leaq -0x58(%rbp), %rsi
movl $0x1000, %ecx           # imm = 0x1000
callq 0x2260
jmp 0x4547
movq -0x208(%rbp), %rdi
leaq -0x178(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x33d0
jmp 0x4561
leaq -0x178(%rbp), %rdi
callq 0x2448
movq 0x4d1c(%rip), %rdi      # 0x9290
callq 0x3590
jmp 0x457b
jmp 0x457d
movl -0x158(%rbp), %eax
addl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x158(%rbp)
jmp 0x44f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x2448
jmp 0x45fa
leaq 0x1d96(%rip), %rsi      # 0x634a
leaq -0x58(%rbp), %rdi
callq 0x5250
movb %al, -0x209(%rbp)
jmp 0x45c5
movb -0x209(%rbp), %al
testb $0x1, %al
jne 0x45d1
jmp 0x45d8
movb $0x1, 0x4cb0(%rip)      # 0x9288
leaq 0x1d6b(%rip), %rsi      # 0x634a
leaq -0x58(%rbp), %rdi
callq 0x2330
jmp 0x45ea
jmp 0x3f7b
xorl %eax, %eax
movl %eax, %edi
callq 0x23b0
jmp 0x45fa
leaq -0x58(%rbp), %rdi
callq 0x2448
movq -0x30(%rbp), %rdi
callq 0x23a0
nopl (%rax)