// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
testq %rsi, %rsi
jle 0xb19b
movq %rsi, %r13
addq %rdi, %r13
xorl %eax, %eax
leaq 0xa569(%rip), %r9       # 0x15394
leaq 0xabb5(%rip), %rbx      # 0x159e7
xorl %r15d, %r15d
movq $0x0, (%rsp)
xorl %r10d, %r10d
xorl %r11d, %r11d
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq %rdi, %rbp
movq $0x0, 0x28(%rsp)
xorl %r14d, %r14d
xorl %r8d, %r8d
movq $0x0, 0x8(%rsp)
xorl %esi, %esi
movq $0x0, 0x20(%rsp)
movq %rdi, 0x50(%rsp)
movl %eax, %eax
movslq (%r9,%rax,4), %rax
addq %r9, %rax
jmpq *%rax
movl $0x3, %eax
cmpb $0x5b, (%rbp)
je 0xb161
xorl %edx, %edx
cmpq %r13, %rbp
setae %cl
jae 0xaeca
movzbl (%rbp), %eax
cmpl $0x2f, %eax
je 0xb141
cmpl $0x3a, %eax
je 0xb138
incq %rbp
cmpq %r13, %rbp
setae %cl
jb 0xaea7
xorl %edx, %edx
xorl %eax, %eax
jmp 0xb148
incq %rbp
movl $0x4, %eax
cmpq %r13, %rbp
jae 0xb161
movsbl (%rbp), %ecx
cmpl $0x2f, %ecx
je 0xb0a5
leal (%r15,%r15,4), %edx
leal (%rcx,%rdx,2), %r15d
addl $-0x30, %r15d
incq %rbp
jmp 0xaed9
movq %rbp, %rcx
cmpq %r13, %rbp
jae 0xaf1d
movzbl (%rcx), %eax
cmpl $0x40, %eax
je 0xaf1d
cmpl $0x5b, %eax
je 0xaf1d
incq %rcx
cmpq %r13, %rcx
jb 0xaf08
cmpq %r13, %rcx
je 0xaf2f
movzbl (%rcx), %eax
cmpl $0x5b, %eax
je 0xaf2f
cmpl $0x2f, %eax
jne 0xaf32
movq %rbp, %rcx
movq %rcx, %r11
subq %rbp, %r11
movl $0x3, %eax
movq %rbp, %r12
jmp 0xb18d
xorl %eax, %eax
cmpb $0x40, (%rbp)
sete %al
leaq (%rax,%rbp), %r10
cmpb $0x5b, (%rbp,%rax)
jne 0xb051
movb $0x1, %al
movq %r10, %rcx
cmpq %r13, %r10
jae 0xaf7b
movq %r10, %rcx
cmpb $0x5d, (%rcx)
leaq 0x1(%rcx), %rcx
setne %al
je 0xaf7b
cmpq %r13, %rcx
jb 0xaf6a
testb %al, %al
je 0xb06e
jmp 0xb28e
movq %r13, %rax
subq %rbp, %rax
cmpq $0x3, %rax
jl 0xafea
movl $0x3, %edx
movq %rbp, %rdi
movq %rsi, 0x60(%rsp)
leaq 0xb30f(%rip), %rsi      # 0x162b7
movq %r12, 0x58(%rsp)
movq %r8, %r12
movq %r10, 0x70(%rsp)
movq %r11, 0x68(%rsp)
callq 0x30c0
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %r11
movq 0x70(%rsp), %r10
leaq 0xa3bf(%rip), %r9       # 0x15394
movq %r12, %r8
movq 0x58(%rsp), %r12
movq 0x50(%rsp), %rdi
testl %eax, %eax
je 0xb113
movq %rbp, %rax
subq %rdi, %rax
movq %rax, (%rsp)
movl $0x4, %eax
jmp 0xb159
movb (%rbp), %cl
movl $0x1, %eax
movq %rbp, %r14
cmpb $0x3f, %cl
je 0xb046
movb $0x1, %al
movq %rbp, %r14
xorl %edx, %edx
cmpq $0x1, %rdx
je 0xb02d
movb 0x1(%rdx,%rbx), %sil
incq %rdx
cmpb %sil, %cl
jne 0xb015
testb %sil, %sil
jne 0xb040
incq %r14
cmpq %r13, %r14
setb %al
jae 0xb0af
movb (%r14), %cl
cmpb $0x3f, %cl
jne 0xb013
movzbl %al, %eax
andl $0x1, %eax
movq %r14, %rsi
subq %rbp, %rsi
addq %rax, %r14
jmp 0xb0b5
movq %r10, %rcx
cmpq %r13, %r10
jae 0xb06e
movzbl (%rcx), %eax
cmpl $0x2f, %eax
je 0xb06e
cmpl $0x3a, %eax
je 0xb06e
incq %rcx
cmpq %r13, %rcx
jb 0xb059
movq %rcx, %rax
subq %r10, %rax
movq %rax, (%rsp)
movl $0x3, %eax
cmpq %r13, %rcx
jae 0xb18d
movzbl (%rcx), %edx
cmpl $0x3a, %edx
je 0xb131
cmpl $0x2f, %edx
jne 0xb18d
movl $0x5, %eax
jmp 0xb18d
movl $0x5, %eax
jmp 0xb161
movq %r14, %rsi
subq %rbp, %rsi
cmpq %r13, %r14
jae 0xb16e
cmpb $0x3f, -0x1(%r14)
jne 0xb16e
movl $0x1, %ecx
cmpb $0x23, (%r14)
movq %r14, %rax
je 0xb100
leaq 0x1(%r14), %rax
movl $0x1, %r8d
movq %rax, %rdx
cmpq %r13, %rax
jae 0xb166
leaq 0x1(%rdx), %rax
incq %r8
cmpb $0x23, (%rdx)
jne 0xb0e1
xorl %ecx, %ecx
cmpq %r13, %rdx
setb %cl
decq %rax
movq %rax, %r8
subq %r14, %r8
addq %rax, %rcx
movq %r14, 0x8(%rsp)
movq %rcx, %r14
jmp 0xb16e
movq %rbp, %rax
subq %rdi, %rax
movq %rax, 0x18(%rsp)
addq $0x3, %rbp
movl $0x2, %eax
movq %rbp, %rcx
movq %rdi, 0x10(%rsp)
jmp 0xb18d
movl $0x4, %eax
jmp 0xb18d
movl $0x1, %eax
xorl %edx, %edx
jmp 0xb148
movl $0x5, %eax
movb $0x1, %dl
orb %dl, %cl
testb $0x1, %cl
je 0xb161
movq %rbp, %rcx
subq %rdi, %rcx
movq %rcx, (%rsp)
movq %rbp, %rcx
movq %rdi, %r10
jmp 0xb18d
movq %rbp, %rcx
jmp 0xb18d
movq %r14, 0x8(%rsp)
movq %rdx, %r14
cmpq %r13, %r14
movq %r14, %rcx
cmovbq %r13, %rcx
movq %rcx, %rax
subq %r14, %rax
movq %rax, 0x28(%rsp)
movl $0x5, %eax
movq %rbp, 0x20(%rsp)
movq %rcx, %rbp
cmpq %r13, %rcx
jb 0xae83
jmp 0xb1e4
movq $0x0, 0x20(%rsp)
xorl %esi, %esi
movq $0x0, 0x8(%rsp)
xorl %r8d, %r8d
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
xorl %r11d, %r11d
xorl %r10d, %r10d
movq $0x0, (%rsp)
xorl %r15d, %r15d
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0xb1ff
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
je 0xb21a
movq %r12, (%rdx)
movq %r11, 0x8(%rdx)
testq %rcx, %rcx
je 0xb22a
movq %r10, (%rcx)
movq (%rsp), %rdx
movq %rdx, 0x8(%rcx)
movq 0xb0(%rsp), %rcx
testq %rax, %rax
je 0xb23a
movl %r15d, (%rax)
movq 0xb8(%rsp), %rax
testq %rcx, %rcx
je 0xb253
movq 0x20(%rsp), %rdx
movq %rdx, (%rcx)
movq %rsi, 0x8(%rcx)
movq 0xc0(%rsp), %rcx
testq %rax, %rax
je 0xb26c
movq 0x8(%rsp), %rdx
movq %rdx, (%rax)
movq %r8, 0x8(%rax)
xorl %eax, %eax
testq %rcx, %rcx
je 0xb27f
movq %r14, (%rcx)
movq 0x28(%rsp), %rdx
movq %rdx, 0x8(%rcx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xb27f