// File: /workspace/github_cpp_makefile_star2_O3/st0ne[P]XReflectorPlus/dxrfd.cpp
// Function: link_to_ref(char*)
static int link_to_ref(char *call)
{
   char payload[MAXHOSTNAMELEN + 1];

   /* IP-port */
   char search_value[MAXHOSTNAMELEN + 7];

   inbound_type::iterator inbound_pos;
   inbound *inbound_ptr;
   pair<inbound_type::iterator,bool> inbound_insert_pair;
   struct sockaddr_in sin;
   unsigned char queryCommand[56];
   unsigned short j;

   char local_mod = ' ';
   char ref[CALL_SIZE + 1];
   char remote_mod = ' ';

   local_mod = call[0];

   if ((local_mod != 'A') && (local_mod != 'B') && 
       (local_mod != 'C') && (local_mod != 'D')) 
   {
      traceit("Invalid local module %c for linking\n", local_mod);
      return -1;
   }

   memset(ref, ' ', sizeof(ref));
   memcpy(ref, call + 1, 6);
   ref[8] = '\0'; 

   /* Is it blocked ? */
   if (blocks.find(ref) != blocks.end())
      return -1;

   if ((memcmp(ref, "REF", 3) != 0) && (memcmp(ref, "XRF", 3) != 0))
   {
      traceit("XRF or REF only\n");
      return -1;
   }

   if (strcmp(ref, OWNER) == 0)
   {
      traceit("Can not link to itself\n");
      return -1;
   }

   remote_mod = call[7];
   if ((remote_mod != 'A') && (remote_mod != 'B') && (remote_mod != 'C') && 
       (remote_mod != 'D') && (remote_mod != 'X'))
   {
      traceit("Invalid remote module %c\n", remote_mod);
      return -1;
   }

   payload[0] = '\0';
   /* get the IP address from database */
   if (!get_ip(ref, payload))
      return -1;

   /* No IP in db? */
   if (payload[0] == '\0')
   {
      traceit("No host or IP address for %s\n", ref);
      return -1;
   }

   if (strcmp(payload, "0.0.0.0") == 0)
   {
      traceit("Invalid IP in db\n");
      return -1;
   }

   if (memcmp(ref, "XRF", 3) == 0)
   {
      return link_to_xrf(local_mod, ref, remote_mod, payload);
   }

   /* Is it already linked? */
   sprintf(search_value, "%s-20001", payload);

   inbound_pos = inbound_list.find(search_value);
   if (inbound_pos != inbound_list.end())
   {
      inbound_ptr = (inbound *)inbound_pos->second;
      if (!inbound_ptr->is_ref)
      {
         traceit("This connection is not a reflector\n");
         return -1;
      }

      /* update the links */
      if (remote_mod == 'X')
         remote_mod = ' ';
      else
      if ((inbound_ptr->links[0] == remote_mod) ||
          (inbound_ptr->links[1] == remote_mod) ||
          (inbound_ptr->links[2] == remote_mod) ||
          (inbound_ptr->links[3] == remote_mod) ||
          (inbound_ptr->links[4] == remote_mod))
      {
         traceit ("Already set or duplicate assignment\n");
         return 0;
      }

      if (local_mod == 'A')
         inbound_ptr->links[0] = remote_mod;
      else
      if (local_mod == 'B')
         inbound_ptr->links[1] = remote_mod;
      else
      if (local_mod == 'C')
         inbound_ptr->links[2] = remote_mod;
      else
      if (local_mod == 'D')
         inbound_ptr->links[3] = remote_mod;
      else
         inbound_ptr->links[4] = remote_mod;

      if ((inbound_ptr->links[0] == ' ') && 
          (inbound_ptr->links[1] == ' ') &&
          (inbound_ptr->links[2] == ' ') &&
          (inbound_ptr->links[3] == ' ') &&
          (inbound_ptr->links[4] == ' '))
          
      {
         /* all links removed, disconnect from remote system */

         traceit("All links removed, disconnecting from %s\n", inbound_ptr->call);
         refbuf[0] = 5;
         refbuf[1] = 0;
         refbuf[2] = 24;
         refbuf[3] = 0;
         refbuf[4] = 0;
         sendto(ref_sock,(char *)refbuf,5,0,
                (struct sockaddr *)&(inbound_ptr->sin),
                sizeof(struct sockaddr_in));

         free(inbound_pos->second);
         inbound_pos->second = NULL;
         inbound_list.erase(inbound_pos);
      } 
      print_links_file();
      return 0;
   }

   if (remote_mod == 'X')
   {
      traceit("X can not be used, there is no connection to unlink\n");
      return -1;
   }

   if ((inbound_list.size() + 1) > MAX_OTHER_USERS)
   {
      traceit("Over the MAX_OTHER_USERS limit of %d\n", MAX_OTHER_USERS);
      return -1;
   }

   sin.sin_family = AF_INET;
   sin.sin_addr.s_addr = inet_addr(payload);
   sin.sin_port = htons(20001);
  
   /* assume that we will be ok */
   inbound_ptr = (inbound *)malloc(sizeof(inbound));
   if (inbound_ptr)
   {
      inbound_ptr->countdown = TIMEOUT;
      memcpy((char *)&(inbound_ptr->sin),(char *)&sin, sizeof(struct sockaddr_in));
      strcpy(inbound_ptr->call, ref);
      time(&(inbound_ptr->connect_time));
      inbound_ptr->isMute = false;
      inbound_ptr->mod = ' ';
      inbound_ptr->is_ref = true;
      inbound_ptr->links[0] = inbound_ptr->links[1] = inbound_ptr->links[2] = inbound_ptr->links[3] = inbound_ptr->links[4] = ' '; 
      if (local_mod == 'A')
         inbound_ptr->links[0] = remote_mod;
      else
      if (local_mod == 'B')
         inbound_ptr->links[1] = remote_mod;
      else
      if (local_mod == 'C')
         inbound_ptr->links[2] = remote_mod;
      else
      if (local_mod == 'D')
         inbound_ptr->links[3] = remote_mod;
      else
         inbound_ptr->links[4] = remote_mod;

      strcpy(inbound_ptr->serial, "REF     ");

      inbound_insert_pair = inbound_list.insert(pair<string, inbound *>(search_value, inbound_ptr));
      if (inbound_insert_pair.second)
      {
         traceit("new CALL=%s,REPEATER,ip=%s, users=%d\n",
                  inbound_ptr->call,search_value,inbound_list.size() + a_user_list.size());

         /* request to connect */
         queryCommand[0] = 5;
         queryCommand[1] = 0;
         queryCommand[2] = 24;
         queryCommand[3] = 0;
         queryCommand[4] = 1;
         for (j = 0; j < 2; j++)
            sendto(ref_sock,(char *)queryCommand,5,0,
                   (struct sockaddr *)&(inbound_ptr->sin),
                   sizeof(struct sockaddr_in));

         print_links_file();
      }
      else
      {
         traceit("failed to add CALL=%s,ip=%s\n",inbound_ptr->call,search_value);
         free(inbound_ptr);
         inbound_ptr = NULL;
         return -1;
      }
   }
   else
   {
      traceit("malloc() failed for call=%s,ip=%s\n",ref,search_value);
      return -1;
   }
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movsbl (%rdi), %r14d
leal -0x45(%r14), %eax
cmpb $-0x5, %al
ja 0x96c0
leaq 0x385b(%rip), %rdi      # 0xcf07
movl %r14d, %esi
xorl %eax, %eax
callq 0x26fc
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x98de
movq %rdi, %r15
leaq 0xc(%rsp), %rbx
movw $0x2020, 0x6(%rbx)      # imm = 0x2020
movl 0x1(%rdi), %eax
movl %eax, (%rbx)
movzwl 0x5(%rdi), %eax
movw %ax, 0x4(%rbx)
movb $0x0, 0x8(%rbx)
leaq 0x30(%rsp), %r13
movq %r13, -0x10(%r13)
movq %rbx, %rdi
callq 0x2100
leaq (%rsp,%rax), %rdx
addq $0xc, %rdx
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xa598
leaq 0x7c24(%rip), %rdi      # 0x11330
leaq 0x20(%rsp), %rsi
callq 0xab3e
movq %rax, %r12
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x9730
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x7c01(%rip), %rax      # 0x11338
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rax, %r12
jne 0x98de
movl $0x4552, %eax           # imm = 0x4552
xorl 0xc(%rsp), %eax
movzbl 0xe(%rsp), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x977d
movl $0x5258, %eax           # imm = 0x5258
xorl 0xc(%rsp), %eax
movzbl 0xe(%rsp), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x977d
leaq 0x37b8(%rip), %rdi      # 0xcf30
jmp 0x986c
leaq 0xb354(%rip), %rsi      # 0x14ad8
leaq 0xc(%rsp), %rdi
callq 0x2330
testl %eax, %eax
je 0x9865
movb 0x7(%r15), %dl
movsbl %dl, %ebp
movzbl %bpl, %eax
leal -0x41(%rax), %ecx
cmpl $0x4, %ecx
jb 0x97b2
cmpl $0x58, %eax
jne 0x98ce
movb %dl, 0xb(%rsp)
movb $0x0, 0x60(%rsp)
leaq 0x20(%rsp), %r15
movq %r13, (%r15)
leaq 0xc(%rsp), %r12
movq %r12, %rdi
callq 0x2100
leaq (%rsp,%rax), %rdx
addq $0xc, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0xa598
leaq 0x7b16(%rip), %rdi      # 0x11300
movq %r15, %rsi
callq 0xb0a8
movq %rax, %r15
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x980c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x7af5(%rip), %rax      # 0x11308
cmpq %rax, %r15
je 0x9875
movq 0x40(%r15), %r12
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movabsq $0x200000002, %rax      # imm = 0x200000002
movq %rax, 0x4(%rdx)
leaq 0x58(%rsp), %rcx
movq %r12, %rdi
xorl %esi, %esi
callq 0x22e0
testl %eax, %eax
je 0x988a
leaq 0x38be(%rip), %rdi      # 0xd115
movl %eax, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x26fc
jmp 0x98b5
leaq 0x36d5(%rip), %rdi      # 0xcf41
xorl %eax, %eax
callq 0x26fc
jmp 0x98de
leaq 0x384a(%rip), %rdi      # 0xd0c6
leaq 0xc(%rsp), %rsi
xorl %eax, %eax
callq 0x26fc
jmp 0x98de
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x98b0
movzbl %r14b, %r12d
movq %rdi, %rax
cmpl $0x2, 0x4(%rax)
jne 0x98a7
cmpl $0x2, 0x8(%rax)
je 0x98f2
movq 0x28(%rax), %rax
testq %rax, %rax
jne 0x989b
callq 0x22f0
movq 0x40(%r15), %rsi
leaq 0x382b(%rip), %rdi      # 0xd0eb
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x26fc
jmp 0x98de
leaq 0x3684(%rip), %rdi      # 0xcf59
movl %ebp, %esi
xorl %eax, %eax
callq 0x26fc
movl %ebx, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rax), %rax
movl 0x4(%rax), %r15d
callq 0x22f0
movl %r15d, %edi
callq 0x2110
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x21e0
cmpb $0x0, (%r15)
je 0x993a
movabsq $0x302e302e302e30, %rax # imm = 0x302E302E302E30
cmpq %rax, 0x60(%rsp)
jne 0x9946
leaq 0x365c(%rip), %rdi      # 0xcf91
jmp 0x986c
leaq 0x3632(%rip), %rdi      # 0xcf73
jmp 0x987c
movl $0x5258, %eax           # imm = 0x5258
xorl 0xc(%rsp), %eax
movzbl 0xe(%rsp), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x9a1d
leaq 0x363c(%rip), %rsi      # 0xcfa3
leaq 0xb0(%rsp), %r15
leaq 0x60(%rsp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
leaq 0x20(%rsp), %rbp
movq %r13, (%rbp)
movq %r15, %rdi
callq 0x2100
leaq (%rsp,%rax), %rdx
addq $0xb0, %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0xa598
leaq 0x78f4(%rip), %rdi      # 0x112a0
leaq 0x20(%rsp), %rsi
callq 0xaa7a
movq %rax, %r15
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x99d0
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x78d1(%rip), %rax      # 0x112a8
cmpq %rax, %r15
je 0x9a36
movq 0x40(%r15), %r14
cmpb $0x0, 0x2b(%r14)
movb 0xb(%rsp), %cl
je 0x9a49
movb $0x20, %al
cmpb $0x58, %cl
jne 0x9a76
addl $-0x41, %r12d
cmpl $0x3, %r12d
ja 0x9b67
leaq 0x26c5(%rip), %rcx      # 0xc0d0
movslq (%rcx,%r12,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movb %al, 0x2c(%r14)
jmp 0x9b6b
leaq 0xc(%rsp), %rsi
leaq 0x60(%rsp), %rcx
movl %r14d, %edi
movl %ebp, %edx
callq 0x9d70
jmp 0x9bff
cmpb $0x58, 0xb(%rsp)
jne 0x9a55
leaq 0x35db(%rip), %rdi      # 0xd01f
jmp 0x986c
leaq 0x355c(%rip), %rdi      # 0xcfac
jmp 0x986c
movq 0x786c(%rip), %rax      # 0x112c8
incq %rax
movl 0x77eb(%rip), %esi      # 0x11250
cmpq %rsi, %rax
jbe 0x9aa8
leaq 0x35e3(%rip), %rdi      # 0xd054
jmp 0x98d7
cmpb %cl, 0x2c(%r14)
je 0x9a9a
cmpb %cl, 0x2d(%r14)
je 0x9a9a
cmpb %cl, 0x2e(%r14)
je 0x9a9a
cmpb %cl, 0x2f(%r14)
je 0x9a9a
movl %ecx, %eax
cmpb %cl, 0x30(%r14)
jne 0x99f6
leaq 0x352f(%rip), %rdi      # 0xcfd0
xorl %ebx, %ebx
jmp 0x986c
leaq 0x60(%rsp), %rdi
callq 0x2220
movl %eax, %ebp
movl $0x40, %edi
callq 0x2310
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x9b3c
movq %rax, %r15
movw $0x3c, 0x28(%rax)
movl $0x214e0002, 0x18(%rax) # imm = 0x214E0002
movl %ebp, 0x1c(%rax)
leaq 0xc(%rsp), %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x10(%r15), %rdi
callq 0x21d0
movb $0x0, 0x9(%r15)
movw $0x120, 0x2a(%r15)      # imm = 0x120
movl $0x20202020, 0x2c(%r15) # imm = 0x20202020
movb $0x20, 0x30(%r15)
movzbl %r14b, %eax
addl $-0x41, %eax
cmpl $0x3, %eax
ja 0x9c24
leaq 0x25bf(%rip), %rcx      # 0xc0e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r15, %rax
addq $0x2c, %rax
movb 0xb(%rsp), %cl
movb %cl, (%rax)
jmp 0x9c2c
leaq 0x3163(%rip), %rdi      # 0xcca6
leaq 0xc(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
jmp 0x98c5
movb %al, 0x2e(%r14)
jmp 0x9b6b
movb %al, 0x2f(%r14)
jmp 0x9b6b
movb %al, 0x2d(%r14)
jmp 0x9b6b
movb %al, 0x30(%r14)
cmpb $0x20, 0x2c(%r14)
jne 0x9bfa
cmpb $0x20, 0x2d(%r14)
jne 0x9bfa
cmpb $0x20, 0x2e(%r14)
jne 0x9bfa
cmpb $0x20, 0x2f(%r14)
jne 0x9bfa
cmpb $0x20, 0x30(%r14)
jne 0x9bfa
leaq 0x345c(%rip), %rdi      # 0xcff5
movq %r14, %rsi
xorl %eax, %eax
callq 0x26fc
movl $0x180005, 0x9b13(%rip) # imm = 0x180005
leaq 0x9b0c(%rip), %rsi      # 0x136c0
movb $0x0, 0x9b09(%rip)      # 0x136c4
movl 0x767f(%rip), %edi      # 0x11240
addq $0x18, %r14
movl $0x5, %edx
xorl %ecx, %ecx
movq %r14, %r8
movl $0x10, %r9d
callq 0x2060
movq 0x40(%r15), %rdi
callq 0x2280
movq $0x0, 0x40(%r15)
leaq 0x76ae(%rip), %rdi      # 0x112a0
movq %r15, %rsi
callq 0xab04
callq 0x3952
xorl %ebx, %ebx
jmp 0x98de
movb 0xb(%rsp), %al
movb %al, 0x2e(%r15)
jmp 0x9c2c
movb 0xb(%rsp), %al
movb %al, 0x2f(%r15)
jmp 0x9c2c
movb 0xb(%rsp), %al
movb %al, 0x2d(%r15)
jmp 0x9c2c
movb 0xb(%rsp), %al
movb %al, 0x30(%r15)
movabsq $0x2020202020464552, %rax # imm = 0x2020202020464552
movq %rax, 0x31(%r15)
movb $0x0, 0x39(%r15)
leaq 0x20(%rsp), %r14
leaq 0xb0(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0xa7ee
leaq 0x7640(%rip), %rdi      # 0x112a0
movq %r14, %rsi
callq 0xab84
movl %edx, %ebp
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x9c81
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x18(%rsp), %rsi
testb $0x1, %bpl
je 0x9d1c
movq 0x7661(%rip), %rcx      # 0x112f8
addq 0x762a(%rip), %rcx      # 0x112c8
leaq 0x33de(%rip), %rdi      # 0xd083
xorl %ebx, %ebx
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x26fc
leaq 0x100(%rsp), %r14
movl $0x180005, (%r14)       # imm = 0x180005
movb $0x1, 0x4(%r14)
movl 0x7570(%rip), %edi      # 0x11240
movq 0x18(%rsp), %r8
addq $0x18, %r8
movl $0x5, %edx
movq %r14, %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x2060
movl 0x754c(%rip), %edi      # 0x11240
movq 0x18(%rsp), %r8
addq $0x18, %r8
movl $0x5, %edx
movq %r14, %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x2060
callq 0x3952
jmp 0x98de
leaq 0x3386(%rip), %rdi      # 0xd0a9
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x26fc
movq 0x18(%rsp), %rdi
callq 0x2280
jmp 0x98de
jmp 0x9d49
jmp 0x9d49
jmp 0x9d65
jmp 0x9d49
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x9d68
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x9d68
movq %rax, %rbx
movq %rbx, %rdi
callq 0x23f0