// File: /workspace/github_c_makefile_star2_O2/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_connect_service
int nbb_connect_service(const char* client_name, const char* service_name) 
{
  char request[MAX_MSG_LEN];
  int ret_code;

  assert(service_name != NULL);

  sem_id = sem_open(SEM_KEY, 0);
  if(sem_id == SEM_FAILED) {
    perror("! nbb_connect_service(): Unable to obtain semaphore\n");
    return -1;
  }
  // BEGIN CRITICAL SECTION
  sem_wait(sem_id);

  strcpy(request, CLIENT);
  strcat(request, " ");
  strcat(request, service_name);

  char* recv;
  int recv_len;
  if(nbb_nameserver_connect(request, &recv, &recv_len)) {
    printf("! nbb_connect_service(): Could not connect to nameserver!\n");
    return -1;
  } 

  if(!recv) {
    ret_code = -1;
  }

  else if(!strcmp(recv, UNKNOWN_SERVICE)) {
    printf("! nbb_connect_service(): Invalid service: %s\n", service_name);
    ret_code = -1;
  }
 
  else if(!strcmp(recv, SERVICE_BUSY)) {
    printf("! nbb_connect_service(): Service %s too busy, not enough channel\n", service_name); 
    ret_code = -1;
  }

  else {
    char* tmp; 
    int slot;
    int channel_id;
    int service_pid;
    char msg[MAX_MSG_LEN];
    char pid[PID_MAX_STRLEN + 1];

    tmp = strtok(recv, " ");
    channel_id = atoi(tmp);
    tmp = strtok(NULL, " ");
    service_pid = atoi(tmp);

    slot = nbb_open_channel(client_name, channel_id + READ_WRITE_CONV, channel_id, !IPC_CREAT);

    connected_nodes[slot].name = (char*)malloc(sizeof(char)*MAX_MSG_LEN);
    strcpy(connected_nodes[slot].name, service_name);
    connected_nodes[slot].pid = service_pid;
   
    ret_code = slot;

    sprintf(pid, "%d", getpid());
    strcpy(msg, NEW_CONN_NOTIFY_MSG) ;
    strcat(msg, " ");
    strcat(msg, pid);
    strcat(msg, " ");
    strcat(msg, client_name);

    // Notify service of the new connection
    if (nbb_send(service_name, msg, strlen(msg))) {
      printf("! nbb_connect_service(): Can't notify service '%s' of new connection\n", service_name);
      ret_code = -1;
    } else {
      printf("** Connecting to service successful, channel: %d service pid: %d\n", channel_id, service_pid);
      signal(SIGUSR1, nbb_recv_data);
    }
  }

  // END CRITICAL SECTION
  sem_post(sem_id);

	return ret_code;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x808, %rsp            # imm = 0x808
testq %rsi, %rsi
je 0x22db
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1139(%rip), %rdi      # 0x31d7
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
movq %rax, 0x46b2(%rip)      # 0x6760
testq %rax, %rax
je 0x2271
movq %rax, %rdi
callq 0x1060
leaq 0x420(%rsp), %r15
movw $0x31, (%r15)
movq %r15, %rdi
callq 0x10b0
movw $0x20, 0x420(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1220
leaq 0x18(%rsp), %rsi
leaq 0x24(%rsp), %rdx
movq %r15, %rdi
callq 0x161c
testl %eax, %eax
je 0x2114
leaq 0x18f6(%rip), %rdi      # 0x39fd
callq 0x1070
pushq $-0x1
popq %rbp
jmp 0x229f
movq 0x18(%rsp), %r15
pushq $-0x1
popq %rbp
testq %r15, %r15
je 0x2293
leaq 0x1994(%rip), %rsi      # 0x3ac0
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
je 0x2268
leaq 0x198d(%rip), %rsi      # 0x3ad0
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
je 0x2282
leaq 0xf41(%rip), %r12       # 0x309b
movq %r15, %rdi
movq %r12, %rsi
callq 0x11d0
movq %rax, %rdi
callq 0x1200
movl %eax, %r15d
xorl %edi, %edi
movq %r12, %rsi
callq 0x11d0
movq %rax, %rdi
callq 0x1200
movl %eax, %r13d
leal 0x3e8(%r15), %esi
movq %r14, %rdi
movq %r15, 0x10(%rsp)
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x172a
movl %eax, %ebp
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1160
movslq %ebp, %r12
shlq $0x4, %r12
leaq 0x44b8(%rip), %r15      # 0x6670
movq %rax, (%r12,%r15)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1040
movl %r13d, 0xc(%rsp)
movl %r13d, 0x8(%r12,%r15)
callq 0x1090
leaq 0xe40(%rip), %rsi       # 0x301d
leaq 0x2a(%rsp), %r12
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1230
movabsq $0x2a2a515f512a2a, %rax # imm = 0x2A2A515F512A2A
leaq 0x30(%rsp), %r13
movq %rax, (%r13)
movq %r13, %rdi
callq 0x10b0
movw $0x20, %r15w
movw %r15w, 0x30(%rsp,%rax)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1220
movq %r13, %rdi
callq 0x10b0
movw %r15w, 0x30(%rsp,%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x1220
movq %r13, %rdi
callq 0x10b0
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x22fa
testl %eax, %eax
je 0x22b3
leaq 0x11a0(%rip), %rdi      # 0x33f9
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10c0
pushq $-0x1
popq %rbp
jmp 0x2293
leaq 0x1112(%rip), %rdi      # 0x3381
jmp 0x2289
leaq 0x10d4(%rip), %rdi      # 0x334c
callq 0x11c0
jmp 0x210c
leaq 0x1126(%rip), %rdi      # 0x33af
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10c0
movq 0x44c6(%rip), %rdi      # 0x6760
callq 0x10f0
movl %ebp, %eax
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1185(%rip), %rdi      # 0x343f
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x10c0
leaq -0x42d(%rip), %rsi      # 0x1ea4
pushq $0xa
popq %rdi
callq 0x1140
jmp 0x2293
leaq 0x1021(%rip), %rdi      # 0x3303
leaq 0xeac(%rip), %rsi       # 0x3195
leaq 0x1028(%rip), %rcx      # 0x3318
movl $0xa2, %edx
callq 0x10d0