// File: /workspace/github_cpp_makefile_star2_O3/andersonfernandes[P]sm-chat/src/client.cpp
// Function: send_messages()
void send_messages() {
  while(true) {
    char text[200];
    cin.getline(text, 200);

    Message* message = new Message();
    strcpy(message->source_name, current_user.name);
    strcpy(message->text, text);

    if(strcmp(text, ":q") == 0) break;
    else if(strlen(message->text) != 0) {
      char message_mode = 'B';
      cout << endl << "Select the message mode ([U]nicast or [B]roadcast): ";
      cin >> message_mode;

      if(message_mode == 'U' || message_mode == 'u') {
        User* user_to_chat = new User();
        select_user(user_to_chat);

        message->mode = UNICAST;
        message->sent_at = time(0);

        ShmQueue* shmq = att_shmq(user_to_chat->shmq_id);
        enqueue(shmq, message);
        shmdt(shmq);
      } else {
        message->mode = BROADCAST;
        message->sent_at = time(0);

        User *users = att_users(users_shmid);
        int *users_count = att_users_count(users_count_shmid);

        for (int i = 0; i < *users_count; i++) {
          if (users[i].shmq_id == current_user.shmq_id) continue;

          ShmQueue* shmq = att_shmq(users[i].shmq_id);
          enqueue(shmq, message);
          shmdt(shmq);
        }

        shmdt(users);
        shmdt(users_count);
      }
    }

    cout << endl;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq 0x3699(%rip), %rbx      # 0x5fe0
leaq -0x100(%rbp), %r15
leaq 0x380b(%rip), %r12      # 0x6160
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %ecx
movl $0xc8, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2240
movl $0x108, %edi            # imm = 0x108
callq 0x2190
movq %rax, %r13
movl $0x108, %edx            # imm = 0x108
movq %rax, %rdi
xorl %esi, %esi
callq 0x20a0
movq %r13, %rdi
movq %r12, %rsi
callq 0x2140
leaq 0x32(%r13), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2140
movl -0x100(%rbp), %eax
movl $0x713a, %ecx           # imm = 0x713A
xorl %ecx, %eax
movzbl -0xfe(%rbp), %ecx
orw %ax, %cx
je 0x2b71
cmpb $0x0, (%r14)
je 0x2b3e
movb $0x42, -0x29(%rbp)
movq 0x35ec(%rip), %r14      # 0x5fd0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movl $0x34, %edx
movq %rax, %rdi
leaq 0x16b2(%rip), %rsi      # 0x40cc
callq 0x21a0
movq %rbx, %rdi
leaq -0x29(%rbp), %rsi
callq 0x21c0
movb -0x29(%rbp), %al
andb $-0x21, %al
cmpb $0x55, %al
jne 0x2a98
movl $0x38, %edi
callq 0x2190
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movq $0x0, 0x30(%rax)
movq %rax, %rdi
callq 0x2bcf
movb $0x55, 0xfa(%r13)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x100(%r13)
movl (%r14), %edi
callq 0x25db
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0x24f5
movq %r14, %rdi
callq 0x2230
jmp 0x2b3e
movb $0x42, 0xfa(%r13)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x100(%r13)
movl 0x36a0(%rip), %edi      # 0x6154
callq 0x25ec
movq %rax, -0x38(%rbp)
movl 0x3695(%rip), %edi      # 0x6158
callq 0x25fa
movq %rax, %r14
movl (%rax), %eax
testl %eax, %eax
jle 0x2b18
movl 0x3685(%rip), %ecx      # 0x615c
movq -0x38(%rbp), %r15
xorl %r12d, %r12d
movl (%r15), %edi
cmpl %ecx, %edi
je 0x2b09
callq 0x25db
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x24f5
movq %rbx, %rdi
callq 0x2230
movl 0x3656(%rip), %ecx      # 0x615c
movl (%r14), %eax
incq %r12
movslq %eax, %rdx
addq $0x38, %r15
cmpq %rdx, %r12
jl 0x2ade
movq -0x38(%rbp), %rdi
callq 0x2230
movq %r14, %rdi
callq 0x2230
movq 0x34b0(%rip), %rbx      # 0x5fe0
leaq -0x100(%rbp), %r15
leaq 0x3622(%rip), %r12      # 0x6160
movq 0x348b(%rip), %r14      # 0x5fd0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
jmp 0x2955
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq