// File: 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
subq $0x120, %rsp            # imm = 0x120
leaq -0xd0(%rbp), %rsi
movq 0x4597(%rip), %rdi      # 0x6fe0
movl $0xc8, %edx
callq 0x2050
movl $0x108, %edi            # imm = 0x108
callq 0x2170
movq %rax, %rdi
movq %rdi, -0x118(%rbp)
xorl %esi, %esi
movl $0x108, %edx            # imm = 0x108
callq 0x20a0
movq -0x118(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
leaq 0x46c1(%rip), %rsi      # 0x7150
callq 0x2130
movq -0xd8(%rbp), %rdi
addq $0x32, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x2130
leaq -0xd0(%rbp), %rdi
leaq 0x1583(%rip), %rsi      # 0x403c
callq 0x21d0
cmpl $0x0, %eax
jne 0x2ac8
jmp 0x2ce7
movq -0xd8(%rbp), %rdi
addq $0x32, %rdi
callq 0x2080
cmpq $0x0, %rax
je 0x2ccd
movb $0x42, -0xd9(%rbp)
movq 0x44e0(%rip), %rdi      # 0x6fd0
movq 0x44c1(%rip), %rsi      # 0x6fb8
callq 0x2180
movq %rax, %rdi
leaq 0x15aa(%rip), %rsi      # 0x40b0
callq 0x2160
movq 0x44ce(%rip), %rdi      # 0x6fe0
leaq -0xd9(%rbp), %rsi
callq 0x21b0
movsbl -0xd9(%rbp), %eax
cmpl $0x55, %eax
je 0x2b3a
movsbl -0xd9(%rbp), %eax
cmpl $0x75, %eax
jne 0x2bd5
movl $0x38, %edi
callq 0x2170
movq %rax, %rdi
movq %rdi, -0x120(%rbp)
xorl %esi, %esi
movl $0x38, %edx
callq 0x20a0
movq -0x120(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x2d60
movq -0xd8(%rbp), %rax
movb $0x55, 0xfa(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x2120
movq %rax, %rcx
movq -0xd8(%rbp), %rax
movq %rcx, 0x100(%rax)
movq -0xe8(%rbp), %rax
movl (%rax), %edi
callq 0x2700
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x25a0
movq -0xf0(%rbp), %rdi
callq 0x2220
jmp 0x2ccb
movq -0xd8(%rbp), %rax
movb $0x42, 0xfa(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x2120
movq %rax, %rcx
movq -0xd8(%rbp), %rax
movq %rcx, 0x100(%rax)
movl 0x4541(%rip), %edi      # 0x7144
callq 0x2720
movq %rax, -0xf8(%rbp)
movl 0x4533(%rip), %edi      # 0x7148
callq 0x2740
movq %rax, -0x100(%rbp)
movl $0x0, -0x104(%rbp)
movl -0x104(%rbp), %eax
movq -0x100(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x2cb3
movq -0xf8(%rbp), %rax
movslq -0x104(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl 0x44f3(%rip), %eax      # 0x714c
jne 0x2c5d
jmp 0x2c9f
movq -0xf8(%rbp), %rax
movslq -0x104(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edi
callq 0x2700
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x25a0
movq -0x110(%rbp), %rdi
callq 0x2220
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x2c2b
movq -0xf8(%rbp), %rdi
callq 0x2220
movq -0x100(%rbp), %rdi
callq 0x2220
jmp 0x2ccd
jmp 0x2ccf
movq 0x42fa(%rip), %rdi      # 0x6fd0
movq 0x42db(%rip), %rsi      # 0x6fb8
callq 0x2180
jmp 0x2a3b
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq