// File: xocoder[P]m_kcptun/vendor/m_net/src/mnet_core.c
// Function: mnet_resolve
int
mnet_resolve(char *host, int port, chann_type_t ctype, chann_addr_t *addr) {
   int ret = 0;
   if (host && addr) {
      struct sockaddr_in *valid_in = NULL;
      struct addrinfo hints, *ai=NULL, *curr=NULL;
      char buf[32] = {0};

      memset(&hints, 0, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = (ctype == CHANN_TYPE_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
      if (port > 0) { sprintf(buf, "%d", port); }
      if ( getaddrinfo(host, (port>0 ? buf : NULL), &hints, &ai) ) {
         mm_log(NULL, 0, "fail to resolve host !\n");
         goto fail;
      }

      for ( curr=ai; curr!=NULL; curr=curr->ai_next) {
         char ipstr[16] = {0};
         inet_ntop(AF_INET, &(((struct sockaddr_in *)(curr->ai_addr))->sin_addr), ipstr, 16);
         if (strlen(ipstr) > 7) { // '0.0.0.0'
            valid_in = (struct sockaddr_in*)curr->ai_addr;
            break;
         }
      }

      if (valid_in) {
         if (0 == getnameinfo((struct sockaddr*)valid_in, sizeof(*valid_in),
                              (char*)addr->ip, 16,NULL, 0, NI_NUMERICHOST))
         {
            ret = 1;
            addr->port = port;
         }
      }

     fail:
      freeaddrinfo(ai);
   }
   return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x70c5
cmpq $0x0, -0x18(%rbp)
je 0x70c5
movq $0x0, -0x28(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x31c0
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x31c0
movl $0x2, -0x54(%rbp)
movl -0x10(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x2, %edx
cmovel %ecx, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0xc(%rbp)
jle 0x6f9c
leaq -0x90(%rbp), %rdi
movl -0xc(%rbp), %edx
leaq 0xc289(%rip), %rsi      # 0x1321e
movb $0x0, %al
callq 0x30a0
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpl $0x0, -0xc(%rbp)
jle 0x6fbd
leaq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x6fc8
xorl %eax, %eax
movq %rax, -0xb0(%rbp)
jmp 0x6fc8
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x33f0
cmpl $0x0, %eax
je 0x7001
xorl %eax, %eax
movl %eax, %edi
xorl %esi, %esi
leaq 0xc22c(%rip), %rdx      # 0x13221
movb $0x0, %al
callq 0x6c30
jmp 0x70bc
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x7075
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31c0
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rsi
addq $0x4, %rsi
leaq -0xa0(%rbp), %rdx
movl $0x2, %edi
movl $0x10, %ecx
callq 0x3360
leaq -0xa0(%rbp), %rdi
callq 0x3150
cmpq $0x7, %rax
jbe 0x7065
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x7075
jmp 0x7067
movq -0x68(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x7009
cmpq $0x0, -0x28(%rbp)
je 0x70ba
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x10, %ecx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
movl %ecx, %esi
movl $0x1, (%rsp)
callq 0x3200
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x70b8
movl $0x1, -0x1c(%rbp)
movl -0xc(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x70ba
jmp 0x70bc
movq -0x60(%rbp), %rdi
callq 0x3420
movl -0x1c(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)