// File: /workspace/github_c_makefile_star2_O2/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 0x1e87
testq %rbx, %rbx
je 0x1e87
leaq 0x1568(%rip), %rdi      # 0x31d7
xorl %esi, %esi
xorl %eax, %eax
callq 0x1110
movq %rax, 0x4ae1(%rip)      # 0x6760
testq %rax, %rax
je 0x1e76
leaq 0x138e(%rip), %r15      # 0x301d
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1230
callq 0x1090
leaq 0x1a(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1230
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 0x1220
movq %r15, %rdi
callq 0x10b0
movw %bp, 0x20(%rsp,%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x1220
movq %r15, %rdi
callq 0x10b0
movw %bp, 0x20(%rsp,%rax)
movq %r15, %rdi
movq %r13, %rsi
callq 0x1220
movq %r15, %rdi
callq 0x10b0
leaq 0x1556(%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 0x161c
testl %eax, %eax
je 0x1d73
leaq 0x1c59(%rip), %rdi      # 0x39a6
callq 0x1070
movq 0x4a07(%rip), %rdi      # 0x6760
callq 0x10f0
pushq $-0x1
popq %rax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r15
leaq 0x1d31(%rip), %rsi      # 0x3ab0
movq %r15, %rdi
callq 0x1100
testl %eax, %eax
je 0x1e12
movl 0x14(%rsp), %edx
leaq 0x14f6(%rip), %rdi      # 0x3290
movl %edx, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x10c0
movl 0x14(%rsp), %esi
movq 0x8(%rsp), %rdx
leaq 0x14eb(%rip), %rdi      # 0x32a1
xorl %eax, %eax
callq 0x10c0
movq 0x8(%rsp), %rdi
leaq 0x12d2(%rip), %r15      # 0x309b
movq %r15, %rsi
callq 0x11d0
negl %r14d
pushq $0x1
popq %r12
leal (%r14,%r12), %ecx
cmpl $0x1, %ecx
je 0x1e1e
movq %rax, %rdi
callq 0x1200
leal 0x3e8(%rax), %edx
movq %rbx, %rdi
movl %eax, %esi
movl $0x200, %ecx            # imm = 0x200
callq 0x172a
cmpl $-0x1, %eax
je 0x1e4a
xorl %edi, %edi
movq %r15, %rsi
callq 0x11d0
incl %r12d
jmp 0x1dd8
leaq 0x1b58(%rip), %rdi      # 0x3971
jmp 0x1d4d
leaq 0x7f(%rip), %rsi        # 0x1ea4
pushq $0xa
popq %rdi
callq 0x1140
movq 0x492c(%rip), %rdi      # 0x6760
callq 0x10f0
movq 0x8(%rsp), %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x1d61
leaq 0x147a(%rip), %rdi      # 0x32cb
movl %r12d, %esi
xorl %eax, %eax
callq 0x10c0
movq 0x48fe(%rip), %rdi      # 0x6760
callq 0x10f0
movq 0x8(%rsp), %rdi
callq 0x1030
jmp 0x1d5e
leaq 0x13ca(%rip), %rdi      # 0x3247
callq 0x11c0
jmp 0x1d5e
leaq 0x1370(%rip), %rdi      # 0x31fe
leaq 0x1300(%rip), %rsi      # 0x3195
leaq 0x1383(%rip), %rcx      # 0x321f
pushq $0x56
popq %rdx
callq 0x10d0