// File: /workspace/github_c_makefile_star2_O0/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_recv_data
void nbb_recv_data(int signum)
{
  int i;
  char* recv;
  size_t recv_len = 0;
  int retval = -1;
  // char* reply_msg;
  int is_new_conn_msg = 0;

  // Attempt to debug Qt. XXX: Remove when done.
  printf("***NBB***: Inside signal handler\n");

  // Since i = 0 is already reserved for nameserver
  for(i = 1;channel_list[i].in_use && i < SERVICE_MAX_CHANNELS;i++) {
    retval = nbb_read_item(i, (void**)&recv, &recv_len);

    if(retval == OK) {
      if (memcmp(recv, NEW_CONN_NOTIFY_MSG, NEW_CONN_NOTIFY_MSG_LEN) == 0) {       
        recv = (char*)realloc(recv, recv_len + 1);
        recv[recv_len] = '\0';

        char* tmp = NULL;

/*
				char* msg_start = (char *) recv + NEW_CONN_NOTIFY_MSG_LEN + 1;
				int name_len = 0;

				connected_nodes[i].pid = (int) strtol(msg_start, &tmp, 10);
				name_len = recv_len - 1 - NEW_CONN_NOTIFY_MSG_LEN - ((int) (tmp - recv));
				connected_nodes[i].name = (char *) malloc(sizeof(char) * (name_len+1));
				assert(connected_nodes[i].name != NULL);
				strncpy(connected_nodes[i].name, tmp + 1, name_len);
				connected_nodes[i].name[name_len] = '\0';
*/

        strtok(recv, " ");
        tmp = strtok(NULL, " ");
        connected_nodes[i].pid = atoi(tmp);
        tmp = strtok(NULL, " ");
        connected_nodes[i].name = (char*)malloc(strlen(tmp) * sizeof(char));
        strcpy(connected_nodes[i].name, tmp);


        printf("***NBB***: New connection on slot %d from client_name: %s with pid: %d\n", i, connected_nodes[i].name, connected_nodes[i].pid);

        is_new_conn_msg = 1;
      }

      // Notify of new connection on slot i
      if (is_new_conn_msg && channel_list[i].new_conn != NULL) {
        channel_list[i].new_conn(i, channel_list[i].arg);
      }

      // We only have new data when the msg is "real data" (not our initial msg).
      // Notify event of new available data on slot i
      if (!is_new_conn_msg && channel_list[i].new_data != NULL) {
        channel_list[i].new_data(i);
      }

      printf("** Received '%.*s' from shm id %d\n",
             (int) recv_len, recv, channel_list[i].read_id);

      if (!is_new_conn_msg) {
        nbb_flush_shm(i, recv, recv_len);
      }

      // XXX: This is for debugging. Remove before production.
      // Reply message
      /* reply_msg = (char*)calloc(recv_len, sizeof(char));
      memcpy(reply_msg, recv, recv_len);
      nbb_insert_item(i, reply_msg, recv_len);
      free(reply_msg); */

      recv_len = 0; 
      free(recv);
    }

  }

  signal(SIGUSR1, nbb_recv_data);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movq $0x0, -0x18(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x20(%rbp)
leaq 0x223a(%rip), %rdi      # 0x4702
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x8(%rbp)
movslq -0x8(%rbp), %rax
leaq 0x4c5f(%rip), %rcx      # 0x7140
imulq $0x58, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpl $0x0, 0x50(%rcx)
movb %al, -0x29(%rbp)
je 0x24fd
cmpl $0xf, -0x8(%rbp)
setl %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
testb $0x1, %al
jne 0x2509
jmp 0x2742
movl -0x8(%rbp), %edi
leaq -0x10(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x1e60
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2732
movq -0x10(%rbp), %rdi
leaq 0x1f96(%rip), %rsi      # 0x44c7
movl $0x7, %edx
callq 0x10f0
cmpl $0x0, %eax
jne 0x2652
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x1, %rsi
callq 0x1170
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1b70(%rip), %rsi      # 0x40e8
callq 0x11c0
xorl %eax, %eax
movl %eax, %edi
leaq 0x1b60(%rip), %rsi      # 0x40e8
callq 0x11c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x11f0
movl %eax, %ecx
movslq -0x8(%rbp), %rdx
leaq 0x50c9(%rip), %rax      # 0x7670
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1b2c(%rip), %rsi      # 0x40e8
callq 0x11c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x10a0
movq %rax, %rdi
shlq $0x0, %rdi
callq 0x1160
movq %rax, %rcx
movslq -0x8(%rbp), %rdx
leaq 0x5088(%rip), %rax      # 0x7670
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movslq -0x8(%rbp), %rcx
leaq 0x5073(%rip), %rax      # 0x7670
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x1040
movl -0x8(%rbp), %esi
movslq -0x8(%rbp), %rcx
leaq 0x5052(%rip), %rax      # 0x7670
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movslq -0x8(%rbp), %rcx
leaq 0x503d(%rip), %rax      # 0x7670
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
leaq 0x20e0(%rip), %rdi      # 0x4724
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x26a2
movslq -0x8(%rbp), %rcx
leaq 0x4add(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x38(%rax)
je 0x26a2
movslq -0x8(%rbp), %rcx
leaq 0x4ac4(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x38(%rax), %rax
movl -0x8(%rbp), %edi
movslq -0x8(%rbp), %rdx
leaq 0x4aab(%rip), %rcx      # 0x7140
imulq $0x58, %rdx, %rdx
addq %rdx, %rcx
movq 0x48(%rcx), %rsi
callq *%rax
cmpl $0x0, -0x20(%rbp)
jne 0x26dc
movslq -0x8(%rbp), %rcx
leaq 0x4a8d(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x40(%rax)
je 0x26dc
movslq -0x8(%rbp), %rcx
leaq 0x4a74(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x40(%rax), %rax
movl -0x8(%rbp), %edi
callq *%rax
movq -0x18(%rbp), %rax
movl %eax, %esi
movq -0x10(%rbp), %rdx
movslq -0x8(%rbp), %rcx
leaq 0x4a4f(%rip), %rax      # 0x7140
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
leaq 0x206a(%rip), %rdi      # 0x476c
movb $0x0, %al
callq 0x10b0
cmpl $0x0, -0x20(%rbp)
jne 0x2721
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rax
movl %eax, %edx
callq 0x2e60
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1030
jmp 0x2734
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x24d6
movl $0xa, %edi
leaq -0x2ae(%rip), %rsi      # 0x24a0
callq 0x1140
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)