// File: /workspace/github_cpp_makefile_star2_O1/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 0x9762
leaq 0x378d(%rip), %rdi      # 0xcedb
movl %r14d, %esi
xorl %eax, %eax
callq 0x26fc
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x9921
movq %rdi, %r15
leaq 0xc(%rsp), %rbx
movb $0x20, 0x8(%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 0xa632
leaq 0x7b7e(%rip), %rdi      # 0x11330
leaq 0x20(%rsp), %rsi
callq 0xabc6
movq %rax, %r12
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x97d6
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x7b5b(%rip), %rax      # 0x11338
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %rax, %r12
jne 0x9921
movl $0x4552, %eax           # imm = 0x4552
xorl 0xc(%rsp), %eax
movzbl 0xe(%rsp), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x9823
movl $0x5258, %eax           # imm = 0x5258
xorl 0xc(%rsp), %eax
movzbl 0xe(%rsp), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x9823
leaq 0x36e6(%rip), %rdi      # 0xcf04
jmp 0x991a
leaq 0xb2ae(%rip), %rsi      # 0x14ad8
leaq 0xc(%rsp), %rdi
callq 0x2330
testl %eax, %eax
je 0x9913
movb 0x7(%r15), %bpl
movsbl %bpl, %esi
movzbl %sil, %eax
leal -0x41(%rax), %ecx
cmpl $0x4, %ecx
jb 0x9859
cmpl $0x58, %eax
jne 0x996d
movl %esi, 0x54(%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 0xa632
leaq 0x7a6f(%rip), %rdi      # 0x11300
movq %r15, %rsi
callq 0xb130
movq %rax, %r15
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x98b3
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x7a4e(%rip), %rax      # 0x11308
cmpq %rax, %r15
je 0x9935
movb %bpl, 0xb(%rsp)
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 0x9941
leaq 0x37e6(%rip), %rdi      # 0xd0e9
xorl %ebp, %ebp
movl %eax, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x26fc
jmp 0x998c
leaq 0x35fb(%rip), %rdi      # 0xcf15
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
leaq 0x375e(%rip), %rdi      # 0xd09a
jmp 0x99f0
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
setne %bpl
je 0x997d
movq %rdi, %rax
cmpl $0x2, 0x4(%rax)
jne 0x995e
cmpl $0x2, 0x8(%rax)
je 0x997f
movq 0x28(%rax), %rax
testq %rax, %rax
setne %bpl
jne 0x9952
jmp 0x9987
leaq 0x35b9(%rip), %rdi      # 0xcf2d
xorl %eax, %eax
callq 0x26fc
jmp 0x9921
jmp 0x9987
movq 0x18(%rax), %rax
movl 0x4(%rax), %r12d
callq 0x22f0
testb $0x1, %bpl
je 0x99cd
movl %r12d, %edi
callq 0x2110
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x21e0
cmpb $0x0, (%r15)
je 0x99e9
movabsq $0x302e302e302e30, %rax # imm = 0x302E302E302E30
cmpq %rax, 0x60(%rsp)
jne 0x9a01
leaq 0x359d(%rip), %rdi      # 0xcf65
jmp 0x991a
movq 0x40(%r15), %rsi
leaq 0x36e7(%rip), %rdi      # 0xd0bf
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x26fc
jmp 0x9921
leaq 0x3557(%rip), %rdi      # 0xcf47
leaq 0xc(%rsp), %rsi
xorl %eax, %eax
callq 0x26fc
jmp 0x9921
movl $0x5258, %eax           # imm = 0x5258
xorl 0xc(%rsp), %eax
movzbl 0xe(%rsp), %ecx
xorl $0x46, %ecx
orw %ax, %cx
je 0x9adf
leaq 0x3555(%rip), %rsi      # 0xcf77
leaq 0xb0(%rsp), %r15
leaq 0x60(%rsp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
leaq 0x20(%rsp), %r12
movq %r13, (%r12)
movq %r15, %rdi
callq 0x2100
leaq (%rsp,%rax), %rdx
addq $0xb0, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0xa632
leaq 0x7839(%rip), %rdi      # 0x112a0
leaq 0x20(%rsp), %rsi
callq 0xab02
movb 0xb(%rsp), %bpl
movq %rax, %r15
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x9a90
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x7811(%rip), %rax      # 0x112a8
cmpq %rax, %r15
je 0x9afa
movq 0x40(%r15), %rsi
movq %rsi, 0x18(%rsp)
cmpb $0x0, 0x2b(%rsi)
je 0x9b0c
movb $0x20, %al
cmpb $0x58, %bpl
jne 0x9b39
movzbl %r14b, %ecx
addl $-0x41, %ecx
cmpl $0x3, %ecx
ja 0x9c28
leaq 0x25d6(%rip), %rdx      # 0xc0a4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movb %al, 0x2c(%rsi)
jmp 0x9c2b
leaq 0xc(%rsp), %rsi
leaq 0x60(%rsp), %rcx
movl %r14d, %edi
movl 0x54(%rsp), %edx
callq 0x9e0a
jmp 0x9da6
cmpb $0x58, %bpl
jne 0x9b18
leaq 0x34ec(%rip), %rdi      # 0xcff3
jmp 0x991a
leaq 0x346d(%rip), %rdi      # 0xcf80
jmp 0x991a
movq 0x77a9(%rip), %rax      # 0x112c8
incq %rax
movl 0x7728(%rip), %esi      # 0x11250
cmpq %rsi, %rax
jbe 0x9b6b
leaq 0x34f4(%rip), %rdi      # 0xd028
jmp 0x9974
cmpb %bpl, 0x2c(%rsi)
je 0x9b5d
cmpb %bpl, 0x2d(%rsi)
je 0x9b5d
cmpb %bpl, 0x2e(%rsi)
je 0x9b5d
cmpb %bpl, 0x2f(%rsi)
je 0x9b5d
movl %ebp, %eax
cmpb %bpl, 0x30(%rsi)
jne 0x9ab7
leaq 0x3440(%rip), %rdi      # 0xcfa4
xorl %ebx, %ebx
jmp 0x991a
leaq 0x60(%rsp), %rdi
callq 0x2220
movl %eax, %ebp
movl $0x40, %edi
callq 0x2310
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x9c00
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
addq $0x10, %r15
movq %r15, %rdi
callq 0x21d0
movq 0x18(%rsp), %rax
movb $0x0, 0x9(%rax)
movw $0x120, 0x2a(%rax)      # imm = 0x120
movb $0x20, 0x30(%rax)
movl $0x20202020, 0x2c(%rax) # imm = 0x20202020
movzbl %r14b, %ecx
addl $-0x41, %ecx
cmpl $0x3, %ecx
ja 0x9cd8
leaq 0x24cc(%rip), %rdx      # 0xc0b4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
movb 0xb(%rsp), %dl
jmpq *%rcx
leaq 0x2c(%rax), %rcx
movb %dl, (%rcx)
jmp 0x9cdf
leaq 0x3073(%rip), %rdi      # 0xcc7a
leaq 0xc(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
jmp 0x99dd
movb %al, 0x2e(%rsi)
jmp 0x9c2b
movb %al, 0x2f(%rsi)
jmp 0x9c2b
movb %al, 0x2d(%rsi)
jmp 0x9c2b
movb %al, 0x30(%rsi)
cmpb $0x20, 0x2c(%rsi)
jne 0x9da1
cmpb $0x20, 0x2d(%rsi)
jne 0x9da1
cmpb $0x20, 0x2e(%rsi)
jne 0x9da1
cmpb $0x20, 0x2f(%rsi)
jne 0x9da1
cmpb $0x20, 0x30(%rsi)
jne 0x9da1
leaq 0x3365(%rip), %rdi      # 0xcfc9
xorl %eax, %eax
callq 0x26fc
movl $0x180005, 0x9a4b(%rip) # imm = 0x180005
leaq 0x9a44(%rip), %rsi      # 0x136c0
movb $0x0, 0x9a41(%rip)      # 0x136c4
movl 0x75b7(%rip), %edi      # 0x11240
movq 0x18(%rsp), %r8
addq $0x18, %r8
movl $0x5, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x2060
movq 0x40(%r15), %rdi
callq 0x2280
movq $0x0, 0x40(%r15)
leaq 0x75e4(%rip), %rdi      # 0x112a0
movq %r15, %rsi
callq 0xab8c
jmp 0x9da1
movb %dl, 0x2e(%rax)
jmp 0x9cdf
movb %dl, 0x2f(%rax)
jmp 0x9cdf
movb %dl, 0x2d(%rax)
jmp 0x9cdf
movb 0xb(%rsp), %cl
movb %cl, 0x30(%rax)
movabsq $0x2020202020464552, %rcx # imm = 0x2020202020464552
movq %rcx, 0x31(%rax)
movb $0x0, 0x39(%rax)
leaq 0x20(%rsp), %r14
leaq 0xb0(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0xa882
leaq 0x758e(%rip), %rdi      # 0x112a0
movq %r14, %rsi
callq 0xac0c
movl %edx, %ebp
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x9d33
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x18(%rsp), %rsi
testb $0x1, %bpl
je 0x9dad
movq 0x75b3(%rip), %rcx      # 0x112f8
addq 0x757c(%rip), %rcx      # 0x112c8
leaq 0x3304(%rip), %rdi      # 0xd057
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x26fc
leaq 0x100(%rsp), %rbx
movl $0x180005, (%rbx)       # imm = 0x180005
movb $0x1, 0x4(%rbx)
movw $0x1, %bp
movl 0x74c2(%rip), %edi      # 0x11240
movq 0x18(%rsp), %r8
addq $0x18, %r8
movl $0x5, %edx
movq %rbx, %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x2060
decw %bp
je 0x9d78
callq 0x3834
xorl %ebx, %ebx
jmp 0x9921
leaq 0x32c9(%rip), %rdi      # 0xd07d
leaq 0xb0(%rsp), %rdx
xorl %eax, %eax
callq 0x26fc
movq 0x18(%rsp), %rdi
callq 0x2280
movq $0x0, 0x18(%rsp)
jmp 0x9921
jmp 0x9de3
jmp 0x9de3
jmp 0x9dff
jmp 0x9de3
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r13, %rdi
je 0x9e02
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x9e02
movq %rax, %rbx
movq %rbx, %rdi
callq 0x23f0