// File: /workspace/github_c_makefile_star2_O3/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
movq %r9, -0x8(%rsp)
movq %r8, -0x10(%rsp)
movq %rcx, -0x18(%rsp)
movq %rdx, -0x20(%rsp)
testq %rsi, %rsi
jle 0xb00f
movq %rdi, %r14
addq %rdi, %rsi
leaq 0xa62b(%rip), %rbp      # 0x153a0
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %r9d, %r9d
xorl %edi, %edi
xorl %r12d, %r12d
movq $0x0, -0x28(%rsp)
movq $0x0, -0x30(%rsp)
movq $0x0, -0x38(%rsp)
movq %r14, %rdx
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r10d, %r10d
movq $0x0, -0x40(%rsp)
xorl %r11d, %r11d
xorl %r8d, %r8d
movl %ecx, %eax
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movb (%rdx), %al
movl $0x3, %ecx
cmpb $0x5b, %al
je 0xafe8
xorl %ecx, %ecx
cmpq %rsi, %rdx
jae 0xaff5
incq %rdx
cmpb $0x2f, %al
je 0xafed
movzbl %al, %eax
cmpl $0x3a, %eax
je 0xafe0
cmpq %rsi, %rdx
jae 0xaff5
movb (%rdx), %al
jmp 0xaddd
incq %rdx
movl $0x4, %ecx
cmpq %rsi, %rdx
jae 0xafe8
movsbl (%rdx), %eax
cmpl $0x2f, %eax
je 0xafa7
leal (%rbx,%rbx,4), %ebx
leal (%rax,%rbx,2), %ebx
addl $-0x30, %ebx
incq %rdx
jmp 0xae09
movq %rdx, %rax
cmpq %rsi, %rdx
jae 0xae49
movzbl (%rax), %ecx
cmpl $0x40, %ecx
je 0xae49
cmpl $0x5b, %ecx
je 0xae49
incq %rax
cmpq %rsi, %rax
jb 0xae34
cmpq %rsi, %rax
je 0xae5b
movzbl (%rax), %ecx
cmpl $0x5b, %ecx
je 0xae5b
cmpl $0x2f, %ecx
jne 0xae5e
movq %rdx, %rax
movq %rax, %r12
subq %rdx, %r12
movl $0x3, %ecx
movq %rdx, -0x28(%rsp)
jmp 0xb001
movb (%rdx), %al
cmpb $0x40, %al
jne 0xae7f
movb 0x1(%rdx), %al
incq %rdx
movq %rdx, %rdi
cmpb $0x5b, %al
jne 0xaf66
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rsi, %rdi
jae 0xb0d4
movq %rdi, %rax
movb (%rax), %dl
incq %rax
cmpb $0x5d, %dl
je 0xaeaa
cmpq %rsi, %rax
jb 0xae9b
cmpb $0x5d, %dl
je 0xaf83
jmp 0xb0d4
movq %rsi, %rax
subq %rdx, %rax
cmpq $0x3, %rax
jl 0xaf1d
cmpb $0x3a, (%rdx)
jne 0xaf1d
cmpb $0x2f, 0x1(%rdx)
jne 0xaf1d
cmpb $0x2f, 0x2(%rdx)
jne 0xaf1d
movq %rdx, %rax
subq %r14, %rax
movq %rax, -0x30(%rsp)
addq $0x3, %rdx
movl $0x2, %ecx
movq %rdx, %rax
movq %r14, -0x38(%rsp)
jmp 0xb001
xorl %r11d, %r11d
movzbl (%rdx,%r11), %eax
cmpl $0x23, %eax
je 0xaf2d
cmpl $0x3f, %eax
je 0xaf2d
leaq (%rdx,%r11), %rcx
incq %rcx
incq %r11
cmpq %rsi, %rcx
jb 0xaef9
leaq (%rdx,%r11), %r15
jmp 0xaf34
movq %rdx, %r9
subq %r14, %r9
movl $0x4, %ecx
jmp 0xaffb
leaq (%rdx,%r11), %r15
incq %r15
cmpq %rsi, %r15
setae %cl
cmpb $0x3f, %al
setne %al
orb %cl, %al
jne 0xafb9
xorl %r10d, %r10d
cmpb $0x23, (%r15,%r10)
je 0xafae
leaq (%r15,%r10), %rax
incq %rax
incq %r10
cmpq %rsi, %rax
jb 0xaf46
movq %r15, -0x40(%rsp)
addq %r10, %r15
jmp 0xafb9
movq %rdi, %rax
cmpq %rsi, %rdi
jae 0xaf83
movzbl (%rax), %ecx
cmpl $0x2f, %ecx
je 0xaf83
cmpl $0x3a, %ecx
je 0xaf83
incq %rax
cmpq %rsi, %rax
jb 0xaf6e
movq %rax, %r9
subq %rdi, %r9
movl $0x3, %ecx
cmpq %rsi, %rax
jae 0xb001
movzbl (%rax), %edx
cmpl $0x3a, %edx
je 0xafd9
cmpl $0x2f, %edx
jne 0xb001
movl $0x5, %ecx
jmp 0xb001
movl $0x5, %ecx
jmp 0xafe8
movq %r15, -0x40(%rsp)
addq %r10, %r15
incq %r15
movq %rsi, %r13
subq %r15, %r13
movq %r15, %rax
cmovaq %rsi, %rax
movl $0x0, %ecx
cmovbeq %rcx, %r13
movl $0x5, %ecx
movq %rdx, %r8
jmp 0xb001
movl $0x4, %ecx
jmp 0xb001
decq %rdx
movl $0x1, %ecx
movq %rdx, %rax
jmp 0xb001
decq %rdx
movl $0x5, %ecx
movq %rdx, %r9
subq %r14, %r9
movq %rdx, %rax
movq %r14, %rdi
movq %rax, %rdx
cmpq %rsi, %rax
jb 0xadb7
jmp 0xb04c
xorl %r8d, %r8d
xorl %r11d, %r11d
movq $0x0, -0x40(%rsp)
xorl %r10d, %r10d
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, -0x38(%rsp)
movq $0x0, -0x30(%rsp)
movq $0x0, -0x28(%rsp)
xorl %r12d, %r12d
xorl %edi, %edi
xorl %r9d, %r9d
xorl %ebx, %ebx
movq -0x20(%rsp), %rax
testq %rax, %rax
je 0xb067
movq -0x38(%rsp), %rcx
movq %rcx, (%rax)
movq -0x30(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rsp), %rcx
testq %rcx, %rcx
movq -0x10(%rsp), %rax
je 0xb082
movq -0x28(%rsp), %rdx
movq %rdx, (%rcx)
movq %r12, 0x8(%rcx)
testq %rax, %rax
je 0xb08e
movq %rdi, (%rax)
movq %r9, 0x8(%rax)
movq 0x38(%rsp), %rcx
movq -0x8(%rsp), %rax
testq %rax, %rax
je 0xb09f
movl %ebx, (%rax)
movq 0x40(%rsp), %rax
testq %rcx, %rcx
je 0xb0b0
movq %r8, (%rcx)
movq %r11, 0x8(%rcx)
movq 0x48(%rsp), %rdx
testq %rax, %rax
je 0xb0c6
movq -0x40(%rsp), %rcx
movq %rcx, (%rax)
movq %r10, 0x8(%rax)
xorl %ecx, %ecx
testq %rdx, %rdx
je 0xb0d4
movq %r15, (%rdx)
movq %r13, 0x8(%rdx)
movl %ecx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq