// File: /workspace/github_cpp_makefile_star2_O1/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 0x3695(%rip), %rbx      # 0x5fe0
leaq -0x100(%rbp), %r15
leaq 0x3807(%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
setne %al
je 0x2b7c
cmpb $0x0, (%r14)
je 0x2b3f
movb %al, -0x2a(%rbp)
movb $0x42, -0x29(%rbp)
movq 0x35e2(%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 0x16a8(%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 0x2aa2
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 0x2bda
movb $0x55, 0xfa(%r13)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x100(%r13)
movl (%r14), %edi
callq 0x25df
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0x24f9
movq %r14, %rdi
callq 0x2230
jmp 0x2b3c
movb $0x42, 0xfa(%r13)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x100(%r13)
movl 0x3696(%rip), %edi      # 0x6154
callq 0x25f0
movq %rax, -0x38(%rbp)
movl 0x368b(%rip), %edi      # 0x6158
callq 0x25fe
movq %rax, %r14
cmpl $0x0, (%rax)
jle 0x2b16
movq -0x38(%rbp), %r15
xorl %r12d, %r12d
movl (%r15), %edi
cmpl 0x3672(%rip), %edi      # 0x615c
je 0x2b07
callq 0x25df
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x24f9
movq %rbx, %rdi
callq 0x2230
incq %r12
movslq (%r14), %rax
addq $0x38, %r15
cmpq %rax, %r12
jl 0x2ae1
movq -0x38(%rbp), %rdi
callq 0x2230
movq %r14, %rdi
callq 0x2230
movq 0x34b2(%rip), %rbx      # 0x5fe0
leaq -0x100(%rbp), %r15
leaq 0x3624(%rip), %r12      # 0x6160
movb -0x2a(%rbp), %al
xorl %r13d, %r13d
movb %al, %r13b
movq 0x3484(%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
testl %r13d, %r13d
jne 0x2959
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq