// File: /workspace/github_c_makefile_star2_O3/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
movq %rdi, %rbx
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movl %eax, 0xc(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x1, (%rdx)
leaq 0xd825(%rip), %rsi      # 0x162cb
movl $0x6, %edx
callq 0x30c0
testl %eax, %eax
je 0x8ad4
leaq 0xd817(%rip), %rsi      # 0x162d2
movl $0x6, %edx
movq %rbx, %rdi
callq 0x30c0
leaq 0x6(%rbx), %rcx
testl %eax, %eax
cmoveq %rcx, %rbx
jmp 0x8ae0
addq $0x6, %rbx
movl $0x2, (%r12)
leaq 0xc(%rsp), %r10
leaq 0x8(%rsp), %r11
leaq 0xd7e8(%rip), %rsi      # 0x162d9
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
addq $0x10, %rsp
cmpl $0x5, %eax
jne 0x8b44
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 0x8bfd
movq %rbx, %rdi
callq 0x3180
cmpq $0xc7, %rax
ja 0x8bb8
leaq 0xd78f(%rip), %rsi      # 0x162ea
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3420
cmpl $0x2, %eax
jne 0x8bb8
movzwl 0x8(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0x12a5b
testl %eax, %eax
je 0x8c4f
leaq 0xd757(%rip), %rsi      # 0x162f6
movl $0x9, %edx
movq %r15, %rdi
callq 0x6bc6
xorl %ebp, %ebp
cmpl $0x1, %eax
sbbl %ebp, %ebp
jmp 0x8c40
leaq 0xd731(%rip), %rsi      # 0x162f0
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3420
cmpl $0x1, %eax
je 0x8bfd
leaq 0xd567(%rip), %rsi      # 0x16146
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3420
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x1, %eax
jne 0x8c40
movl 0x8(%rsp), %eax
movl %eax, %ecx
rolw $0x8, %cx
movw %cx, 0x2(%r14)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0xfffe, %eax           # imm = 0xFFFE
ja 0x8c40
movslq 0xc(%rsp), %r14
movsbq (%rbx,%r14), %rbx
testb %bl, %bl
je 0x8c3d
movzbl %bl, %eax
cmpl $0x2c, %eax
je 0x8c3d
callq 0x3660
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbx,2)
je 0x8c40
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 0x8c0c