// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_parse_uri
int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme,
                 struct mg_str *user_info, struct mg_str *host,
                 unsigned int *port, struct mg_str *path, struct mg_str *query,
                 struct mg_str *fragment) {
  struct mg_str rscheme = {0, 0}, ruser_info = {0, 0}, rhost = {0, 0},
                rpath = {0, 0}, rquery = {0, 0}, rfragment = {0, 0};
  unsigned int rport = 0;
  enum {
    P_START,
    P_SCHEME_OR_PORT,
    P_USER_INFO,
    P_HOST,
    P_PORT,
    P_REST
  } state = P_START;

  const char *p = uri.p, *end = p + uri.len;
  while (p < end) {
    switch (state) {
      case P_START:
        /*
         * expecting on of:
         * - `scheme://xxxx`
         * - `xxxx:port`
         * - `[a:b:c]:port`
         * - `xxxx/path`
         */
        if (*p == '[') {
          state = P_HOST;
          break;
        }
        for (; p < end; p++) {
          if (*p == ':') {
            state = P_SCHEME_OR_PORT;
            break;
          } else if (*p == '/') {
            state = P_REST;
            break;
          }
        }
        if (state == P_START || state == P_REST) {
          rhost.p = uri.p;
          rhost.len = p - uri.p;
        }
        break;
      case P_SCHEME_OR_PORT:
        if (end - p >= 3 && strncmp(p, "://", 3) == 0) {
          rscheme.p = uri.p;
          rscheme.len = p - uri.p;
          state = P_USER_INFO;
          p += 3;
        } else {
          rhost.p = uri.p;
          rhost.len = p - uri.p;
          state = P_PORT;
        }
        break;
      case P_USER_INFO:
        ruser_info.p = p;
        for (; p < end; p++) {
          if (*p == '@' || *p == '[') {
            break;
          }
        }
        if (p == end || *p == '/' || *p == '[') {
          /* backtrack and parse as host */
          p = ruser_info.p;
        }
        ruser_info.len = p - ruser_info.p;
        state = P_HOST;
        break;
      case P_HOST:
        if (*p == '@') p++;
        rhost.p = p;
        if (*p == '[') {
          int found = 0;
          for (; !found && p < end; p++) {
            found = (*p == ']');
          }
          if (!found) return -1;
        } else {
          for (; p < end; p++) {
            if (*p == ':' || *p == '/') break;
          }
        }
        rhost.len = p - rhost.p;
        if (p < end) {
          if (*p == ':') {
            state = P_PORT;
            break;
          } else if (*p == '/') {
            state = P_REST;
            break;
          }
        }
        break;
      case P_PORT:
        p++;
        for (; p < end; p++) {
          if (*p == '/') {
            state = P_REST;
            break;
          }
          rport *= 10;
          rport += *p - '0';
        }
        break;
      case P_REST:
        /* `p` points to separator. `path` includes the separator */
        parse_uri_component(&p, end, "?#", &rpath);
        if (p < end && *(p - 1) == '?') {
          parse_uri_component(&p, end, "#", &rquery);
        }
        parse_uri_component(&p, end, "", &rfragment);
        break;
    }
  }

  if (scheme != 0) *scheme = rscheme;
  if (user_info != 0) *user_info = ruser_info;
  if (host != 0) *host = rhost;
  if (port != 0) *port = rport;
  if (path != 0) *path = rpath;
  if (query != 0) *query = rquery;
  if (fragment != 0) *fragment = rfragment;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, %r13
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0x80(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0x70(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0x8(%rsp), %r8
movq %rdi, (%r8)
addq %rdi, %r13
movq %rdi, %rax
negq %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x9e5f(%rip), %r15      # 0x14380
pushq $0x3
popq %r9
pushq $0x5
popq %r10
pushq $0x4
popq %rbx
movq %rdi, 0x10(%rsp)
movq %rdi, %rax
movq $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %edx, %edx
cmpq %r13, %rax
jae 0xa7fd
movq %rax, %rcx
movl %edx, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
cmpb $0x5b, (%rcx)
movq %rcx, %rax
movl %r9d, %edx
je 0xa559
movq 0x58(%rsp), %rax
leaq (%rax,%rcx), %rsi
movq %rcx, %rax
cmpq %r13, %rax
jae 0xa7d7
movzbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xa7db
cmpl $0x3a, %ecx
je 0xa7f0
incq %rax
incq %rsi
jmp 0xa587
incq %rcx
movq %rcx, %rax
cmpq %r13, %rax
jae 0xa742
movsbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xa7ca
imull $0xa, %r12d, %edx
leal (%rdx,%rcx), %r12d
addl $-0x30, %r12d
incq %rax
jmp 0xa5b3
movq %rcx, %rax
cmpq %r13, %rax
jae 0xa5f3
movzbl (%rax), %edx
cmpl $0x40, %edx
je 0xa5f3
cmpl $0x5b, %edx
je 0xa5f3
incq %rax
jmp 0xa5dc
movq %rax, 0x8(%rsp)
cmpq %r13, %rax
je 0xa60a
movzbl (%rax), %edx
cmpl $0x2f, %edx
je 0xa60a
cmpl $0x5b, %edx
jne 0xa612
movq %rcx, 0x8(%rsp)
movq %rcx, %rax
movq %rax, %rdx
subq %rcx, %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movl %r9d, %edx
jmp 0xa559
movb (%rcx), %dl
cmpb $0x40, %dl
jne 0xa749
leaq 0x1(%rcx), %rbp
movq %rbp, 0x8(%rsp)
movb 0x1(%rcx), %dl
jmp 0xa74c
movq %r13, %rax
subq %rcx, %rax
cmpq $0x3, %rax
jl 0xa72d
cmpb $0x3a, (%rcx)
jne 0xa72d
cmpb $0x2f, 0x1(%rcx)
jne 0xa72d
cmpb $0x2f, 0x2(%rcx)
jne 0xa72d
movq %rcx, %rax
movq 0x10(%rsp), %rdx
subq %rdx, %rax
movq %rax, 0x18(%rsp)
addq $0x3, %rcx
movq %rcx, 0x8(%rsp)
movq %rcx, %rax
movq %rdx, 0x20(%rsp)
pushq $0x2
popq %rdx
jmp 0xa559
movq %r14, 0x60(%rsp)
movq %rbp, 0x68(%rsp)
movl %r10d, %r14d
movl %r9d, %ebp
movq %r8, %rdi
movq %r13, %rsi
leaq 0xa332(%rip), %rdx      # 0x149eb
leaq 0x90(%rsp), %rcx
movq %r8, %rbx
callq 0xa8b0
movq 0x8(%rsp), %rax
cmpq %r13, %rax
jae 0xa6f3
cmpb $0x3f, -0x1(%rax)
jne 0xa6f3
movq %rbx, %rdi
movq %r13, %rsi
leaq 0xbab5(%rip), %rdx      # 0x1619b
leaq 0x80(%rsp), %rcx
callq 0xa8b0
movq %rbx, %rdi
movq %r13, %rsi
leaq 0xb0d0(%rip), %rdx      # 0x157d0
leaq 0x70(%rsp), %rcx
callq 0xa8b0
movq 0x8(%rsp), %rax
movl %r14d, %r10d
movl %r14d, %edx
movq %rbx, %r8
movl %ebp, %r9d
pushq $0x4
popq %rbx
movq 0x68(%rsp), %rbp
movq 0x60(%rsp), %r14
jmp 0xa559
movq %rcx, %r14
movq 0x10(%rsp), %rbp
subq %rbp, %r14
movq %rcx, %rax
movl %ebx, %edx
jmp 0xa559
movl %ebx, %edx
jmp 0xa7cd
movq %rcx, %rbp
movq %rbp, %rax
cmpb $0x5b, %dl
jne 0xa76f
xorl %ecx, %ecx
movq %rbp, %rax
testl %ecx, %ecx
jne 0xa786
cmpq %r13, %rax
jae 0xa786
xorl %ecx, %ecx
cmpb $0x5d, (%rax)
sete %cl
incq %rax
jmp 0xa759
cmpq %r13, %rax
jae 0xa794
movzbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xa794
cmpl $0x3a, %ecx
je 0xa794
incq %rax
jmp 0xa76f
movq %rax, 0x8(%rsp)
testl %ecx, %ecx
jne 0xa799
jmp 0xa8ab
movq %rax, 0x8(%rsp)
movq %rax, %r14
subq %rbp, %r14
movl %r9d, %edx
cmpq %r13, %rax
jae 0xa559
movzbl (%rax), %ecx
movl %ebx, %edx
cmpl $0x3a, %ecx
je 0xa559
cmpl $0x2f, %ecx
jne 0xa622
movl %r10d, %edx
jmp 0xa559
movl %r10d, %edx
movq %rax, 0x8(%rsp)
jmp 0xa559
xorl %edx, %edx
jmp 0xa7de
movl %r10d, %edx
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rbp
movq %rsi, %r14
jmp 0xa559
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rdx
jmp 0xa559
movq 0x38(%rsp), %rax
testq %rax, %rax
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
je 0xa822
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
testq %rdi, %rdi
movq 0xf0(%rsp), %r8
movq 0xe8(%rsp), %rcx
movq 0x50(%rsp), %rdx
je 0xa84d
movq 0x28(%rsp), %rax
movq %rax, (%rdi)
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rdi)
testq %rsi, %rsi
je 0xa859
movq %rbp, (%rsi)
movq %r14, 0x8(%rsi)
testq %rdx, %rdx
je 0xa861
movl %r12d, (%rdx)
movq 0xe0(%rsp), %rdx
testq %rdx, %rdx
je 0xa879
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%rdx)
testq %rcx, %rcx
je 0xa889
movaps 0x80(%rsp), %xmm0
movups %xmm0, (%rcx)
xorl %eax, %eax
testq %r8, %r8
je 0xa899
movaps 0x70(%rsp), %xmm0
movups %xmm0, (%r8)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rax
jmp 0xa899