// File: /workspace/github_c_makefile_star2_O3/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 0x11b1(%rip), %rdi      # 0x3ac0
callq 0x1070
leaq 0x82a(%rip), %r15       # 0x3145
movq %rbx, %rdi
movq %r15, %rsi
callq 0x11d0
movq %rax, %r12
xorl %r14d, %r14d
xorl %edi, %edi
movq %r15, %rsi
callq 0x11d0
movq %rax, %rdi
callq 0x1200
movl %eax, %r13d
xorl %edi, %edi
movq %r15, %rsi
callq 0x11d0
movq %rax, %rdi
callq 0x1200
movl %eax, %r15d
movl $0x32, %edi
movl $0x1, %esi
callq 0x1100
movq %rax, %rbx
leaq 0x3f91(%rip), %rbp      # 0x6900
cmpl $0x0, (%rbp)
je 0x2991
incq %r14
addq $0x28, %rbp
cmpq $0x5, %r14
jne 0x296f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, (%rbp)
movq %r12, %rdi
callq 0x10b0
movq %rax, %rdi
callq 0x1170
movl %r15d, 0x14(%rsp)
movl %r13d, %r15d
movq %rax, %r13
movq %rax, -0x20(%rbp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x1040
movl %r15d, -0x8(%rbp)
movslq %r15d, %r12
leaq (,%r12,4), %rdi
callq 0x1170
movq %rax, -0x18(%rbp)
movl $0x4, %esi
movq %r12, %rdi
callq 0x1100
movq %rax, -0x10(%rbp)
movl 0x14(%rsp), %ecx
movl %ecx, -0x4(%rbp)
leaq 0x1009(%rip), %rdi      # 0x3a02
movq %r13, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x10c0
movl %r14d, %edi
callq 0x2ac7
testl %eax, %eax
je 0x2a2f
leaq 0x100f(%rip), %rdi      # 0x3a28
movl %r15d, %esi
xorl %eax, %eax
callq 0x10c0
movaps 0x10e6(%rip), %xmm0     # 0x3b10
movups %xmm0, (%rbx)
jmp 0x2a9c
leaq 0x1014(%rip), %rdi      # 0x3a4a
movl %r15d, %esi
xorl %eax, %eax
callq 0x10c0
cmpl $0x0, -0x8(%rbp)
jle 0x2a9c
leaq 0x653(%rip), %r14       # 0x30a0
leaq 0xc(%rsp), %r15
xorl %r12d, %r12d
movb $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movq -0x18(%rbp), %rax
movl (%rax,%r12,4), %edx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1220
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1210
movq %rbx, %rdi
callq 0x10b0
movw $0x20, (%rbx,%rax)
incq %r12
movslq -0x8(%rbp), %rax
cmpq %rax, %r12
jl 0x2a55
movq %rbx, %rdi
callq 0x10b0
xorl %edi, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x1667
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030