// File: /workspace/github_c_makefile_star2_O1/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 0x11af(%rip), %rdi      # 0x3ac0
callq 0x1070
leaq 0x828(%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, %ebp
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 0x3f90(%rip), %rax      # 0x6900
cmpl $0x0, (%rax)
je 0x298a
incq %r14
addq $0x28, %rax
cmpq $0x5, %r14
jne 0x2970
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x2990
movl $0x1, (%rax)
cmpl $-0x1, %r14d
je 0x2a4f
movq %r12, %rdi
callq 0x10b0
movq %rax, %rdi
callq 0x1170
movq %rax, %r13
movslq %r14d, %rax
movl %r15d, 0x14(%rsp)
leaq (%rax,%rax,4), %r15
leaq 0x3f20(%rip), %rax      # 0x68e0
movq %r13, (%rax,%r15,8)
movq %r12, %rsi
movq %rax, %r12
movq %r13, %rdi
callq 0x1040
movl %ebp, 0x18(%r12,%r15,8)
movslq %ebp, %r12
leaq (,%r12,4), %rdi
callq 0x1170
leaq 0x3ef2(%rip), %rcx      # 0x68e0
movq %rax, 0x8(%rcx,%r15,8)
movl $0x4, %esi
movq %r12, %rdi
movq %rcx, %r12
callq 0x1100
movq %rax, 0x10(%r12,%r15,8)
movl 0x14(%rsp), %ecx
movl %ecx, 0x1c(%r12,%r15,8)
leaq 0xfea(%rip), %rdi       # 0x3a02
movq %r13, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x10c0
movl %r14d, %edi
callq 0x2afd
testl %eax, %eax
je 0x2a5e
leaq 0xff1(%rip), %rdi       # 0x3a28
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
movaps 0x10c9(%rip), %xmm0     # 0x3b10
movups %xmm0, (%rbx)
jmp 0x2ad2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%r12,%r15,8), %r12
leaq 0xfe1(%rip), %rdi       # 0x3a4a
movl %ebp, %esi
xorl %eax, %eax
callq 0x10c0
cmpl $0x0, 0x18(%r12)
jle 0x2ad2
leaq 0x61f(%rip), %r14       # 0x30a0
leaq 0xc(%rsp), %r15
xorl %r13d, %r13d
movb $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x8(%r12), %rax
movl (%rax,%r13,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 %r13
movslq 0x18(%r12), %rax
cmpq %rax, %r13
jl 0x2a89
movq %rbx, %rdi
callq 0x10b0
xorl %edi, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x166d
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030