// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3260
movq -0x18(%rbp), %rax
movw $0x2, (%rax)
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rdi
leaq 0x13e65(%rip), %rsi     # 0x2024d
movl $0x6, %edx
callq 0x30b0
cmpl $0x0, %eax
jne 0xc40f
movq -0x10(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x10(%rbp)
movq -0x20(%rbp), %rax
movl $0x2, (%rax)
jmp 0xc437
movq -0x10(%rbp), %rdi
leaq 0x13e3a(%rip), %rsi     # 0x20254
movl $0x6, %edx
callq 0x30b0
cmpl $0x0, %eax
jne 0xc435
movq -0x10(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x10(%rbp)
jmp 0xc437
movq -0x10(%rbp), %rdi
leaq 0x13e19(%rip), %rsi     # 0x2025b
leaq -0x34(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x3c(%rbp), %r8
leaq -0x40(%rbp), %r9
leaq -0x44(%rbp), %r10
leaq -0x4c(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x3460
cmpl $0x5, %eax
jne 0xc4b1
movl -0x34(%rbp), %edi
shll $0x18, %edi
movl -0x38(%rbp), %eax
shll $0x10, %eax
orl %eax, %edi
movl -0x3c(%rbp), %eax
shll $0x8, %eax
orl %eax, %edi
orl -0x40(%rbp), %edi
callq 0x3250
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x44(%rbp), %eax
movzwl %ax, %edi
callq 0x31a0
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xc5a2
movq -0x10(%rbp), %rdi
callq 0x3180
cmpq -0x30(%rbp), %rax
jae 0xc53f
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x13d9d(%rip), %rsi     # 0x2026c
leaq -0x44(%rbp), %rcx
leaq -0x4c(%rbp), %r8
movb $0x0, %al
callq 0x3460
cmpl $0x2, %eax
jne 0xc53f
movl -0x44(%rbp), %eax
movzwl %ax, %edi
callq 0x31a0
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x18ce0
cmpl $0x0, %eax
je 0xc53d
movq -0x28(%rbp), %rdi
leaq 0x13d62(%rip), %rsi     # 0x20278
movl $0x9, %edx
callq 0x8bf0
cmpl $0x0, %eax
je 0xc531
movl $0x0, -0x4(%rbp)
jmp 0xc5fa
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc5fa
jmp 0xc5a0
movq -0x10(%rbp), %rdi
leaq 0x13d28(%rip), %rsi     # 0x20272
leaq -0x44(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
movb $0x0, %al
callq 0x3460
cmpl $0x1, %eax
je 0xc57d
movq -0x10(%rbp), %rdi
leaq 0x13b5f(%rip), %rsi     # 0x200c8
leaq -0x44(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
movb $0x0, %al
callq 0x3460
cmpl $0x1, %eax
jne 0xc595
movl -0x44(%rbp), %eax
movzwl %ax, %edi
callq 0x31a0
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x2(%rax)
jmp 0xc59e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc5fa
jmp 0xc5a0
jmp 0xc5a2
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, -0x48(%rbp)
movl -0x44(%rbp), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
jae 0xc5ea
cmpl $0x0, -0x48(%rbp)
je 0xc5e2
cmpl $0x2c, -0x48(%rbp)
je 0xc5e2
callq 0x36a0
movq (%rax), %rax
movslq -0x48(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0xc5ea
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0xc5f4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x50(%rbp)
jmp 0xc5f4
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)