// File: /workspace/github_c_makefile_star2_O1/umbrant[P]Non-blocking-Buffers/nbb.c
// Function: nbb_init_service
int nbb_init_service(int num_channels, const char* name) 
{
  char request[MAX_MSG_LEN] = {};
  char num_channel[2]; // TODO: Make it constants?
  char pid[PID_MAX_STRLEN + 1];

  assert(num_channels > 0 && name != NULL);

  sem_id = sem_open(SEM_KEY, 0);
  if(sem_id == SEM_FAILED) {
    perror("! nbb_init_service(): Unable to obtain semaphore\n");
    return -1;
  }

  // BEGIN CRITICAL SECTION
  //TODO: sem_wait(sem_id);

  sprintf(num_channel, "%d", num_channels);
  sprintf(pid, "%d", getpid());

  strcpy(request, SERVICE);
  strcat(request, " ");
  strcat(request, name);
  strcat(request, " ");
  strcat(request, num_channel); 
  strcat(request, " ");
  strcat(request, pid);

  printf("request: %s, len: %zu\n", request, strlen(request));

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

  if(!strcmp(recv, NAMESERVER_CHANNEL_FULL)) {
    printf("! nbb_init_service(): Reserving channel unsuccessful\n");
  
    sem_post(sem_id);
    return -1;
  }
  else {
    printf("recv (%d): %.*s\n", recv_len, recv_len, recv);
    printf("** Acquired the following channels: %.*s\n", recv_len, recv);

    int i;
    int channel;
    char* tmp;

    tmp = strtok(recv, " ");
    for(i = 1;i <= num_channels;i++) { 
      channel = atoi(tmp);
      if(nbb_open_channel(name, channel, channel + READ_WRITE_CONV, IPC_CREAT) == -1) {
        //TODO: service_exit();
        printf("! nbb_init_service(): Failed to open the %d-th channel\n", i);
        sem_post(sem_id);
        free(recv);
        return -1;
      }
      tmp = strtok(NULL, " ");
    }

    signal(SIGUSR1, nbb_recv_data);

    sem_post(sem_id);
    free(recv);
    return 0;
  }
  // END CRITICAL SECTION
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x20(%rsp), %rdi
movl $0x3e8, %edx            # imm = 0x3E8
xorl %esi, %esi
callq 0x10e0
testl %r14d, %r14d
jle 0x1ee7
testq %rbx, %rbx
je 0x1ee7
leaq 0x151e(%rip), %rdi      # 0x31d7
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
movq %rax, 0x4a97(%rip)      # 0x6760
testq %rax, %rax
je 0x1ed6
leaq 0x1344(%rip), %r15      # 0x301d
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1220
callq 0x1090
leaq 0x1a(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1220
leaq 0x20(%rsp), %r15
movw $0x30, (%r15)
movq %r15, %rdi
callq 0x10b0
movw $0x20, %bp
movw %bp, 0x20(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1210
movq %r15, %rdi
callq 0x10b0
movw %bp, 0x20(%rsp,%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x1210
movq %r15, %rdi
callq 0x10b0
movw %bp, 0x20(%rsp,%rax)
movq %r15, %rdi
movq %r13, %rsi
callq 0x1210
movq %r15, %rdi
callq 0x10b0
leaq 0x150c(%rip), %rdi      # 0x3279
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x8(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %r15, %rdi
callq 0x1614
testl %eax, %eax
je 0x1dc1
leaq 0x1c0f(%rip), %rdi      # 0x39a6
callq 0x1070
movq 0x49bd(%rip), %rdi      # 0x6760
callq 0x10f0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r15
leaq 0x1ce3(%rip), %rsi      # 0x3ab0
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
je 0x1e9e
movl 0x14(%rsp), %edx
leaq 0x14a8(%rip), %rdi      # 0x3290
xorl %ebp, %ebp
movl %edx, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x10c0
movl 0x14(%rsp), %esi
movq 0x8(%rsp), %rdx
leaq 0x149b(%rip), %rdi      # 0x32a1
xorl %eax, %eax
callq 0x10c0
movq 0x8(%rsp), %rdi
leaq 0x1282(%rip), %rsi      # 0x309b
callq 0x11c0
testl %r14d, %r14d
jle 0x1e72
negl %r14d
movl $0x1, %r15d
leaq 0x1268(%rip), %r12      # 0x309b
movq %rax, %rdi
callq 0x11f0
leal 0x3e8(%rax), %edx
movq %rbx, %rdi
movl %eax, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x1732
cmpl $-0x1, %eax
je 0x1eaa
xorl %edi, %edi
movq %r12, %rsi
callq 0x11c0
leal (%r14,%r15), %ecx
incl %ecx
movl %r15d, %edx
incl %edx
movl %edx, %r15d
cmpl $0x1, %ecx
jne 0x1e33
leaq 0x8d(%rip), %rsi        # 0x1f06
movl $0xa, %edi
callq 0x1140
movq 0x48d6(%rip), %rdi      # 0x6760
callq 0x10f0
movq 0x8(%rsp), %rdi
callq 0x1030
jmp 0x1dad
leaq 0x1acc(%rip), %rdi      # 0x3971
jmp 0x1d97
leaq 0x141a(%rip), %rdi      # 0x32cb
movl %r15d, %esi
xorl %eax, %eax
callq 0x10c0
movq 0x489e(%rip), %rdi      # 0x6760
callq 0x10f0
movq 0x8(%rsp), %rdi
callq 0x1030
jmp 0x1da8
leaq 0x136a(%rip), %rdi      # 0x3247
callq 0x11b0
jmp 0x1da8
leaq 0x1310(%rip), %rdi      # 0x31fe
leaq 0x12a0(%rip), %rsi      # 0x3195
leaq 0x1323(%rip), %rcx      # 0x321f
movl $0x56, %edx
callq 0x10d0