// File: davidcairuz[P]network-internet-relay-chat/client.cpp
// Function: client_receive_thread(void*)
void* client_receive_thread(void* arg) {
    string message = "";
    int failed = 0;

    while (!quit) {
        do {
            message = client_socket->Read(client_socket->Get_conn_fd());
            client_socket->Check();
            cout << message << "\n";
            
            if (message == "Too many people here, you are not welcome\n") { // server cheio
                quit = true;
                break;
            } else if (message.empty()) { // checa se houve max_retries erros
                failed++;
                sleep(1);
                
                if (failed >= Socket::max_retries) {
                    cout << "Server is down" << endl;
                    break;
                } else 
                    continue;
            
            } else {
                failed = 0;
            }

            message = "";

        } while (message.size() == Socket::buffer_size && !quit);
    }

    pthread_exit(NULL);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
leaq -0x29(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x23c0
movq -0x68(%rbp), %rdx
leaq 0x259d(%rip), %rsi      # 0x6284
leaq -0x28(%rbp), %rdi
callq 0x2840
jmp 0x3cf2
leaq -0x29(%rbp), %rdi
callq 0x2290
movl $0x0, -0x40(%rbp)
movb 0x5580(%rip), %al       # 0x9288
xorb $-0x1, %al
testb $0x1, %al
jne 0x3d13
jmp 0x3eaf
jmp 0x3d15
movq 0x5574(%rip), %rdi      # 0x9290
movq %rdi, -0x78(%rbp)
callq 0x3570
movl %eax, -0x6c(%rbp)
jmp 0x3d2a
movl -0x6c(%rbp), %edx
movq -0x78(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3210
jmp 0x3d3c
leaq -0x28(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq %rsi, -0x80(%rbp)
callq 0x2300
movq -0x80(%rbp), %rdi
callq 0x2448
movq 0x5533(%rip), %rdi      # 0x9290
callq 0x3590
jmp 0x3d64
movq 0x5265(%rip), %rdi      # 0x8fd0
leaq -0x28(%rbp), %rsi
callq 0x2200
movq %rax, -0x88(%rbp)
jmp 0x3d7d
movq -0x88(%rbp), %rdi
leaq 0x24f8(%rip), %rsi      # 0x6283
callq 0x2220
jmp 0x3d92
leaq 0x255e(%rip), %rsi      # 0x62f7
leaq -0x28(%rbp), %rdi
callq 0x5250
movb %al, -0x89(%rbp)
jmp 0x3daa
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0x3db6
jmp 0x3df6
movb $0x1, 0x54cb(%rip)      # 0x9288
jmp 0x3eaa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x2290
jmp 0x3eba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x2448
jmp 0x3eba
leaq -0x28(%rbp), %rdi
callq 0x2390
testb $0x1, %al
jne 0x3e05
jmp 0x3e54
movl -0x40(%rbp), %eax
incl %eax
movl %eax, -0x40(%rbp)
movl $0x1, %edi
callq 0x2190
jmp 0x3e19
cmpl $0x5, -0x40(%rbp)
jl 0x3e52
movq 0x51aa(%rip), %rdi      # 0x8fd0
leaq 0x24f5(%rip), %rsi      # 0x6322
callq 0x2220
movq %rax, -0x98(%rbp)
jmp 0x3e3b
movq -0x98(%rbp), %rdi
movq 0x516f(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x3e50
jmp 0x3eaa
jmp 0x3e71
movl $0x0, -0x40(%rbp)
jmp 0x3e5d
leaq 0x2420(%rip), %rsi      # 0x6284
leaq -0x28(%rbp), %rdi
callq 0x2330
jmp 0x3e6f
jmp 0x3e71
leaq -0x28(%rbp), %rdi
callq 0x2160
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x1000, %rcx           # imm = 0x1000
movb %al, -0x99(%rbp)
jne 0x3e9c
movb 0x53f4(%rip), %al       # 0x9288
xorb $-0x1, %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x3d15
jmp 0x3d02
xorl %eax, %eax
movl %eax, %edi
callq 0x23b0
jmp 0x3eba
movq -0x38(%rbp), %rdi
callq 0x23a0
nopw %cs:(%rax,%rax)