// File: /workspace/github_c_makefile_star2_O1/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 0xd6ac(%rip), %rsi      # 0x162ad
movl $0x6, %edx
callq 0x30c0
testl %eax, %eax
je 0x8c2f
leaq 0xd69e(%rip), %rsi      # 0x162b4
movl $0x6, %edx
movq %rbx, %rdi
callq 0x30c0
leaq 0x6(%rbx), %rcx
testl %eax, %eax
cmoveq %rcx, %rbx
jmp 0x8c3b
addq $0x6, %rbx
movl $0x2, (%r12)
leaq 0xc(%rsp), %r10
leaq 0x8(%rsp), %r11
leaq 0xd66f(%rip), %rsi      # 0x162bb
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 0x8c9f
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 0x8d54
movq %rbx, %rdi
callq 0x3180
cmpq $0xc7, %rax
ja 0x8d0f
leaq 0xd616(%rip), %rsi      # 0x162cc
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3420
cmpl $0x2, %eax
jne 0x8d0f
movzwl 0x8(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1257c
testl %eax, %eax
je 0x8d62
leaq 0xd5e2(%rip), %rsi      # 0x162d8
movl $0x9, %edx
movq %r15, %rdi
callq 0x6c88
xorl %ebp, %ebp
cmpl $0x1, %eax
sbbl %ebp, %ebp
jmp 0x8d99
leaq 0xd5bc(%rip), %rsi      # 0x162d2
leaq 0x8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3420
cmpl $0x1, %eax
je 0x8d54
leaq 0xd3f2(%rip), %rsi      # 0x16128
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 0x8d99
movzwl 0x8(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0xfffe, 0x8(%rsp)      # imm = 0xFFFE
ja 0x8d99
movslq 0xc(%rsp), %r14
movsbq (%rbx,%r14), %rbx
testb %bl, %bl
je 0x8d96
movzbl %bl, %eax
cmpl $0x2c, %eax
je 0x8d96
callq 0x3660
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbx,2)
je 0x8d99
movl %r14d, %ebp
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq