// File: /workspace/github_c_makefile_star2_O3/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 0x2350
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x10cc(%rip), %rdi      # 0x31d7
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
movq %rax, 0x4645(%rip)      # 0x6760
testq %rax, %rax
je 0x22e4
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 0x1210
leaq 0x18(%rsp), %rsi
leaq 0x24(%rsp), %rdx
movq %r15, %rdi
callq 0x161c
testl %eax, %eax
je 0x2183
leaq 0x1889(%rip), %rdi      # 0x39fd
callq 0x1070
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2312
movq 0x18(%rsp), %r15
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0x2306
leaq 0x1923(%rip), %rsi      # 0x3ac0
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
je 0x22db
leaq 0x191c(%rip), %rsi      # 0x3ad0
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
je 0x22f5
leaq 0xed0(%rip), %r12       # 0x309b
movq %r15, %rdi
movq %r12, %rsi
callq 0x11c0
movq %rax, %rdi
callq 0x11f0
movl %eax, %r15d
xorl %edi, %edi
movq %r12, %rsi
callq 0x11c0
movq %rax, %rdi
callq 0x11f0
movl %eax, %r13d
leal 0x3e8(%r15), %esi
movq %r14, %rdi
movq %r15, 0x10(%rsp)
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x173a
movl %eax, %ebp
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1160
movslq %ebp, %r12
shlq $0x4, %r12
leaq 0x4447(%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 0xdcf(%rip), %rsi       # 0x301d
leaq 0x2a(%rsp), %r12
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1220
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 0x1210
movq %r13, %rdi
callq 0x10b0
movw %r15w, 0x30(%rsp,%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x1210
movq %r13, %rdi
callq 0x10b0
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x236f
testl %eax, %eax
je 0x2326
leaq 0x112f(%rip), %rdi      # 0x33f9
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10c0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2306
leaq 0x109f(%rip), %rdi      # 0x3381
jmp 0x22fc
leaq 0x1061(%rip), %rdi      # 0x334c
callq 0x11b0
jmp 0x2179
leaq 0x10b3(%rip), %rdi      # 0x33af
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10c0
movq 0x4453(%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 0x1112(%rip), %rdi      # 0x343f
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x10c0
leaq -0x440(%rip), %rsi      # 0x1f04
movl $0xa, %edi
callq 0x1140
jmp 0x2306
leaq 0xfac(%rip), %rdi       # 0x3303
leaq 0xe37(%rip), %rsi       # 0x3195
leaq 0xfb3(%rip), %rcx       # 0x3318
movl $0xa2, %edx
callq 0x10d0