// File: /workspace/github_c_makefile_star2_O2/umbrant[P]Non-blocking-Buffers/nameserver.c
// Function: handle_service
void handle_service(char* arg)
{
  printf("** Handling service\n");

  char* service_name = strtok(arg, " ");
  int num_channels = atoi(strtok(NULL, " "));
  int service_pid = atoi(strtok(NULL, " "));
  int slot;
  char* msg = (char*)calloc(50,sizeof(char));
  int i;

  slot = reserve_service_slot();

//printf("test\n");
  if(slot == -1) {
    return;
  }

  service_lists[slot].name = (char*)malloc(strlen(service_name));
  strcpy(service_lists[slot].name, service_name);
  service_lists[slot].num_channels = num_channels;
  service_lists[slot].channel_ids = (int*)malloc(sizeof(int)*num_channels);
  service_lists[slot].is_channel_busy = (int*)calloc(num_channels,sizeof(int));
  service_lists[slot].pid = service_pid;

  printf("** Name: %s, num_channel:%d, pid: %d\n", service_lists[slot].name, num_channels, service_pid);

  if(reserve_channel(slot)) { 
    printf("** Unable to reserve %d channels\n", num_channels);
    strcpy(msg, NAMESERVER_CHANNEL_FULL);
  }
  else {
    printf("** Able to reserve %d channels\n", num_channels);
    for(i = 0;i < service_lists[slot].num_channels;i++) {
      char tmp[5] = "\0"; 
      sprintf(tmp, "%d", service_lists[slot].channel_ids[i]);
      strcat(msg, tmp);
      strcat(msg, " "); 
    }
  }

  nbb_insert_item(CHANNEL_ID, msg, strlen(msg));
  free(msg); 
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x1243(%rip), %rdi      # 0x3ac0
callq 0x1070
leaq 0x8bc(%rip), %r14       # 0x3145
movq %rbx, %rdi
movq %r14, %rsi
callq 0x11e0
movq %rax, %r15
xorl %edi, %edi
movq %r14, %rsi
callq 0x11e0
movq %rax, %rdi
callq 0x1210
movl %eax, %r12d
xorl %edi, %edi
movq %r14, %rsi
callq 0x11e0
movq %rax, %rdi
callq 0x1210
movl %eax, %r14d
pushq $0x32
popq %rdi
pushq $0x1
popq %rsi
callq 0x1100
movq %rax, %rbx
callq 0x2add
cmpl $-0x1, %eax
je 0x29b2
movq %r15, %rdi
movl %eax, %ebp
movl %eax, 0x14(%rsp)
callq 0x10b0
movq %rax, %rdi
callq 0x1170
movq %rax, %r13
movslq %ebp, %rax
movl %r14d, 0x10(%rsp)
imulq $0x28, %rax, %r14
leaq 0x3fe7(%rip), %rbp      # 0x68f0
movq %r13, (%r14,%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1040
movl %r12d, 0x18(%r14,%rbp)
movslq %r12d, %r15
leaq (,%r15,4), %rdi
callq 0x1170
movq %rax, 0x8(%r14,%rbp)
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x1100
movq %rax, 0x10(%r14,%rbp)
movl 0x10(%rsp), %ecx
movl %ecx, 0x1c(%r14,%rbp)
leaq 0x10b0(%rip), %rdi      # 0x3a02
movq %r13, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x10c0
movl 0x14(%rsp), %edi
callq 0x2a2a
testl %eax, %eax
je 0x29c1
leaq 0x10b5(%rip), %rdi      # 0x3a28
movl %r12d, %esi
xorl %eax, %eax
callq 0x10c0
movaps 0x118c(%rip), %xmm0     # 0x3b10
movups %xmm0, (%rbx)
movq %rbx, %rdi
callq 0x10b0
xorl %edi, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x163d
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %r14, %rbp
leaq 0x107f(%rip), %rdi      # 0x3a4a
movl %r12d, %esi
xorl %eax, %eax
callq 0x10c0
leaq 0x6c4(%rip), %r14       # 0x30a0
leaq 0x8(%rsp), %r15
xorl %r12d, %r12d
movslq 0x18(%rbp), %rax
cmpq %rax, %r12
jge 0x2987
andl $0x0, 0x8(%rsp)
movb $0x0, 0xc(%rsp)
movq 0x8(%rbp), %rax
movl (%rax,%r12,4), %edx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1230
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10b0
movw $0x20, (%rbx,%rax)
incq %r12
jmp 0x29e4