// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x400(%rbp), %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x10d0
cmpl $0x0, -0x8(%rbp)
jle 0x2184
cmpq $0x0, -0x10(%rbp)
je 0x2184
jmp 0x21a3
leaq 0x20a1(%rip), %rdi      # 0x422c
leaq 0x2017(%rip), %rsi      # 0x41a9
movl $0x56, %edx
leaq 0x20af(%rip), %rcx      # 0x424d
callq 0x10c0
leaq 0x2041(%rip), %rdi      # 0x41eb
xorl %esi, %esi
movb $0x0, %al
callq 0x1110
movq %rax, 0x55a6(%rip)      # 0x7760
cmpq $0x0, 0x559e(%rip)      # 0x7760
jne 0x21dc
leaq 0x20aa(%rip), %rdi      # 0x4275
callq 0x11b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x248b
leaq -0x402(%rbp), %rdi
movl -0x8(%rbp), %edx
leaq 0x1e7d(%rip), %rsi      # 0x406a
movb $0x0, %al
callq 0x1220
leaq -0x408(%rbp), %rax
movq %rax, -0x438(%rbp)
callq 0x1080
movq -0x438(%rbp), %rdi
movl %eax, %edx
leaq 0x1e53(%rip), %rsi      # 0x406a
movb $0x0, %al
callq 0x1220
leaq -0x400(%rbp), %rdi
leaq 0x28e8(%rip), %rsi      # 0x4b14
callq 0x1040
leaq -0x400(%rbp), %rdi
leaq 0x1ea9(%rip), %rsi      # 0x40e8
callq 0x1210
leaq -0x400(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1210
leaq -0x400(%rbp), %rdi
leaq 0x1e86(%rip), %rsi      # 0x40e8
callq 0x1210
leaq -0x400(%rbp), %rdi
leaq -0x402(%rbp), %rsi
callq 0x1210
leaq -0x400(%rbp), %rdi
leaq 0x1e60(%rip), %rsi      # 0x40e8
callq 0x1210
leaq -0x400(%rbp), %rdi
leaq -0x408(%rbp), %rsi
callq 0x1210
leaq -0x400(%rbp), %rax
movq %rax, -0x430(%rbp)
leaq -0x400(%rbp), %rdi
callq 0x10a0
movq -0x430(%rbp), %rsi
movq %rax, %rdx
leaq 0x1fdc(%rip), %rdi      # 0x42a7
movb $0x0, %al
callq 0x10b0
leaq -0x400(%rbp), %rdi
leaq -0x410(%rbp), %rsi
leaq -0x414(%rbp), %rdx
callq 0x1640
cmpl $0x0, %eax
je 0x2317
leaq 0x1fc6(%rip), %rdi      # 0x42be
movb $0x0, %al
callq 0x10b0
movq 0x545a(%rip), %rdi      # 0x7760
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x248b
movq -0x410(%rbp), %rdi
leaq 0x27fb(%rip), %rsi      # 0x4b20
callq 0x1100
cmpl $0x0, %eax
jne 0x2355
leaq 0x1fbf(%rip), %rdi      # 0x42f5
movb $0x0, %al
callq 0x10b0
movq 0x541c(%rip), %rdi      # 0x7760
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x248b
movl -0x414(%rbp), %esi
movl -0x414(%rbp), %edx
movq -0x410(%rbp), %rcx
leaq 0x1fbc(%rip), %rdi      # 0x432b
movb $0x0, %al
callq 0x10b0
movl -0x414(%rbp), %esi
movq -0x410(%rbp), %rdx
leaq 0x1fb2(%rip), %rdi      # 0x433c
movb $0x0, %al
callq 0x10b0
movq -0x410(%rbp), %rdi
leaq 0x1d49(%rip), %rsi      # 0x40e8
callq 0x11c0
movq %rax, -0x428(%rbp)
movl $0x1, -0x418(%rbp)
movl -0x418(%rbp), %eax
cmpl -0x8(%rbp), %eax
jg 0x245b
movq -0x428(%rbp), %rdi
callq 0x11f0
movl %eax, -0x41c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x41c(%rbp), %esi
movl -0x41c(%rbp), %edx
addl $0x3e8, %edx            # imm = 0x3E8
movl $0x200, %ecx            # imm = 0x200
callq 0x1790
cmpl $-0x1, %eax
jne 0x2430
movl -0x418(%rbp), %esi
leaq 0x1f5e(%rip), %rdi      # 0x4366
movb $0x0, %al
callq 0x10b0
movq 0x534a(%rip), %rdi      # 0x7760
callq 0x10e0
movq -0x410(%rbp), %rdi
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x248b
xorl %eax, %eax
movl %eax, %edi
leaq 0x1cad(%rip), %rsi      # 0x40e8
callq 0x11c0
movq %rax, -0x428(%rbp)
movl -0x418(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x418(%rbp)
jmp 0x23b5
movl $0xa, %edi
leaq 0x39(%rip), %rsi        # 0x24a0
callq 0x1140
movq 0x52ed(%rip), %rdi      # 0x7760
callq 0x10e0
movq -0x410(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x440, %rsp            # imm = 0x440
popq %rbp
retq
nopw (%rax,%rax)