// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_parse_address
MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa,
                                 int *proto, char *host, size_t host_len) {
  unsigned int a, b, c, d, port = 0;
  int ch, len = 0;
#if MG_ENABLE_IPV6
  char buf[100];
#endif

  /*
   * MacOS needs that. If we do not zero it, subsequent bind() will fail.
   * Also, all-zeroes in the socket address means binding to all addresses
   * for both IPv4 and IPv6 (INADDR_ANY and IN6ADDR_ANY_INIT).
   */
  memset(sa, 0, sizeof(*sa));
  sa->sin.sin_family = AF_INET;

  *proto = SOCK_STREAM;

  if (strncmp(str, "udp://", 6) == 0) {
    str += 6;
    *proto = SOCK_DGRAM;
  } else if (strncmp(str, "tcp://", 6) == 0) {
    str += 6;
  }

  if (sscanf(str, "%u.%u.%u.%u:%u%n", &a, &b, &c, &d, &port, &len) == 5) {
    /* Bind to a specific IPv4 address, e.g. 192.168.1.5:8080 */
    sa->sin.sin_addr.s_addr =
        htonl(((uint32_t) a << 24) | ((uint32_t) b << 16) | c << 8 | d);
    sa->sin.sin_port = htons((uint16_t) port);
#if MG_ENABLE_IPV6
  } else if (sscanf(str, "[%99[^]]]:%u%n", buf, &port, &len) == 2 &&
             inet_pton(AF_INET6, buf, &sa->sin6.sin6_addr)) {
    /* IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 */
    sa->sin6.sin6_family = AF_INET6;
    sa->sin.sin_port = htons((uint16_t) port);
#endif
#if MG_ENABLE_ASYNC_RESOLVER
  } else if (strlen(str) < host_len &&
             sscanf(str, "%[^ :]:%u%n", host, &port, &len) == 2) {
    sa->sin.sin_port = htons((uint16_t) port);
    if (mg_resolve_from_hosts_file(host, sa) != 0) {
      /*
       * if resolving from hosts file failed and the host
       * we are trying to resolve is `localhost` - we should
       * try to resolve it using `gethostbyname` and do not try
       * to resolve it via DNS server if gethostbyname has failed too
       */
      if (mg_ncasecmp(host, "localhost", 9) != 0) {
        return 0;
      }

#if MG_ENABLE_SYNC_RESOLVER
      if (!mg_resolve2(host, &sa->sin.sin_addr)) {
        return -1;
      }
#else
      return -1;
#endif
    }
#endif
  } else if (sscanf(str, ":%u%n", &port, &len) == 1 ||
             sscanf(str, "%u%n", &port, &len) == 1) {
    /* If only port is specified, bind to IPv4, INADDR_ANY */
    sa->sin.sin_port = htons((uint16_t) port);
  } else {
    return -1;
  }

  /* Required for MG_ENABLE_ASYNC_RESOLVER=0 */
  (void) host;
  (void) host_len;

  ch = str[len]; /* Character that follows the address */
  return port < 0xffffUL && (ch == '\0' || ch == ',' || isspace(ch)) ? len : -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
andl $0x0, 0x8(%rsp)
movq %rdi, %rbx
andl $0x0, 0xc(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x1, (%rdx)
leaq 0xccad(%rip), %rsi      # 0x152ae
pushq $0x6
popq %rdx
callq 0x30c0
testl %eax, %eax
je 0x862b
leaq 0xcca1(%rip), %rsi      # 0x152b5
pushq $0x6
popq %rdx
movq %rbx, %rdi
callq 0x30c0
leaq 0x6(%rbx), %rcx
testl %eax, %eax
cmoveq %rcx, %rbx
jmp 0x8637
addq $0x6, %rbx
movl $0x2, (%r12)
leaq 0xc(%rsp), %r10
leaq 0x8(%rsp), %r11
leaq 0xcc74(%rip), %rsi      # 0x152bc
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x10(%rsp), %r9
movq %rbx, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x3420
popq %rcx
popq %rdx
pushq $-0x1
popq %rbp
cmpl $0x5, %eax
jne 0x869c
movl 0x1c(%rsp), %eax
shll $0x18, %eax
movl 0x18(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movl 0x14(%rsp), %eax
shll $0x8, %eax
orl %ecx, %eax
orl 0x10(%rsp), %eax
bswapl %eax
movl %eax, 0x4(%r14)
jmp 0x874b
movq %rbx, %rdi
callq 0x3180
cmpq $0xc7, %rax
ja 0x870b
leaq 0xcc1a(%rip), %rsi      # 0x152cd
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3420
cmpl $0x2, %eax
jne 0x870b
movzwl 0x8(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0x117f2
testl %eax, %eax
je 0x8798
leaq 0xcbe2(%rip), %rsi      # 0x152d9
pushq $0x9
popq %rdx
movq %r15, %rdi
callq 0x6915
xorl %ebp, %ebp
cmpl $0x1, %eax
sbbl %ebp, %ebp
jmp 0x8789
leaq 0xcbc1(%rip), %rsi      # 0x152d3
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3420
cmpl $0x1, %eax
je 0x874b
leaq 0xc9f7(%rip), %rsi      # 0x15129
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3420
cmpl $0x1, %eax
jne 0x8789
movl 0x8(%rsp), %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x2(%r14)
cmpl $0xfffe, %eax           # imm = 0xFFFE
ja 0x8789
movslq 0xc(%rsp), %r14
movsbq (%rbx,%r14), %rbx
testb %bl, %bl
je 0x8786
movzbl %bl, %eax
cmpl $0x2c, %eax
je 0x8786
callq 0x3660
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbx,2)
je 0x8789
movl %r14d, %ebp
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%rsp), %eax
jmp 0x875a