// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x840, %rsp            # imm = 0x840
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x277c
jmp 0x279b
leaq 0x1c1b(%rip), %rdi      # 0x439e
leaq 0x1a1f(%rip), %rsi      # 0x41a9
movl $0xa2, %edx
leaq 0x1c1d(%rip), %rcx      # 0x43b3
callq 0x10c0
leaq 0x1a49(%rip), %rdi      # 0x41eb
xorl %esi, %esi
movb $0x0, %al
callq 0x1110
movq %rax, 0x4fae(%rip)      # 0x7760
cmpq $0x0, 0x4fa6(%rip)      # 0x7760
jne 0x27d4
leaq 0x1c24(%rip), %rdi      # 0x43e7
callq 0x11b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2aec
movq 0x4f85(%rip), %rdi      # 0x7760
callq 0x1060
leaq -0x400(%rbp), %rdi
leaq 0x2342(%rip), %rsi      # 0x4b30
callq 0x1040
leaq -0x400(%rbp), %rdi
leaq 0x18e7(%rip), %rsi      # 0x40e8
callq 0x1210
leaq -0x400(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1210
leaq -0x400(%rbp), %rdi
leaq -0x410(%rbp), %rsi
leaq -0x414(%rbp), %rdx
callq 0x1640
cmpl $0x0, %eax
je 0x284f
leaq 0x1be0(%rip), %rdi      # 0x441c
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2aec
cmpq $0x0, -0x410(%rbp)
jne 0x2868
movl $0xffffffff, -0x404(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ad7
movq -0x410(%rbp), %rdi
leaq 0x22ca(%rip), %rsi      # 0x4b40
callq 0x1100
cmpl $0x0, %eax
jne 0x28a1
movq -0x18(%rbp), %rsi
leaq 0x1bcc(%rip), %rdi      # 0x4457
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x404(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ad5
movq -0x410(%rbp), %rdi
leaq 0x22a1(%rip), %rsi      # 0x4b50
callq 0x1100
cmpl $0x0, %eax
jne 0x28da
movq -0x18(%rbp), %rsi
leaq 0x1bc1(%rip), %rdi      # 0x4485
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x404(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ad3
movq -0x410(%rbp), %rdi
leaq 0x1800(%rip), %rsi      # 0x40e8
callq 0x11c0
movq %rax, -0x420(%rbp)
movq -0x420(%rbp), %rdi
callq 0x11f0
movl %eax, -0x428(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x17d7(%rip), %rsi      # 0x40e8
callq 0x11c0
movq %rax, -0x420(%rbp)
movq -0x420(%rbp), %rdi
callq 0x11f0
movl %eax, -0x42c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x428(%rbp), %esi
addl $0x3e8, %esi            # imm = 0x3E8
movl -0x428(%rbp), %edx
xorl %ecx, %ecx
callq 0x1790
movl %eax, -0x424(%rbp)
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1160
movq %rax, %rcx
movslq -0x424(%rbp), %rdx
leaq 0x4d03(%rip), %rax      # 0x7670
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movslq -0x424(%rbp), %rcx
leaq 0x4ceb(%rip), %rax      # 0x7670
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x1040
movl -0x42c(%rbp), %ecx
movslq -0x424(%rbp), %rdx
leaq 0x4cc4(%rip), %rax      # 0x7670
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x424(%rbp), %eax
movl %eax, -0x404(%rbp)
leaq -0x826(%rbp), %rax
movq %rax, -0x840(%rbp)
callq 0x1080
movq -0x840(%rbp), %rdi
movl %eax, %edx
leaq 0x1685(%rip), %rsi      # 0x406a
movb $0x0, %al
callq 0x1220
leaq -0x820(%rbp), %rdi
leaq 0x1acd(%rip), %rsi      # 0x44c7
callq 0x1040
leaq -0x820(%rbp), %rdi
leaq 0x16db(%rip), %rsi      # 0x40e8
callq 0x1210
leaq -0x820(%rbp), %rdi
leaq -0x826(%rbp), %rsi
callq 0x1210
leaq -0x820(%rbp), %rdi
leaq 0x16b5(%rip), %rsi      # 0x40e8
callq 0x1210
leaq -0x820(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1210
movq -0x18(%rbp), %rax
movq %rax, -0x838(%rbp)
leaq -0x820(%rbp), %rax
movq %rax, -0x830(%rbp)
leaq -0x820(%rbp), %rdi
callq 0x10a0
movq -0x838(%rbp), %rdi
movq -0x830(%rbp), %rsi
movq %rax, %rdx
callq 0x2b00
cmpl $0x0, %eax
je 0x2aa6
movq -0x18(%rbp), %rsi
leaq 0x1a3c(%rip), %rdi      # 0x44cf
movb $0x0, %al
callq 0x10b0
movl $0xffffffff, -0x404(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ad1
movl -0x428(%rbp), %esi
movl -0x42c(%rbp), %edx
leaq 0x1a5c(%rip), %rdi      # 0x4515
movb $0x0, %al
callq 0x10b0
movl $0xa, %edi
leaq -0x62c(%rip), %rsi      # 0x24a0
callq 0x1140
jmp 0x2ad3
jmp 0x2ad5
jmp 0x2ad7
movq 0x4c82(%rip), %rdi      # 0x7760
callq 0x10e0
movl -0x404(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x840, %rsp            # imm = 0x840
popq %rbp
retq
nopl (%rax,%rax)