// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_parse_http
int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req) {
  const char *end, *qs;
  int len = mg_http_get_request_len(s, n);

  if (len <= 0) return len;

  memset(hm, 0, sizeof(*hm));
  hm->message.p = s;
  hm->body.p = s + len;
  hm->message.len = hm->body.len = (size_t) ~0;
  end = s + len;

  /* Request is fully buffered. Skip leading whitespaces. */
  while (s < end && isspace(*(unsigned char *) s)) s++;

  if (is_req) {
    /* Parse request line: method, URI, proto */
    s = mg_skip(s, end, " ", &hm->method);
    s = mg_skip(s, end, " ", &hm->uri);
    s = mg_skip(s, end, "\r\n", &hm->proto);
    if (hm->uri.p <= hm->method.p || hm->proto.p <= hm->uri.p) return -1;

    /* If URI contains '?' character, initialize query_string */
    if ((qs = (char *) memchr(hm->uri.p, '?', hm->uri.len)) != NULL) {
      hm->query_string.p = qs + 1;
      hm->query_string.len = &hm->uri.p[hm->uri.len] - (qs + 1);
      hm->uri.len = qs - hm->uri.p;
    }
  } else {
    s = mg_skip(s, end, " ", &hm->proto);
    if (end - s < 4 || s[3] != ' ') return -1;
    hm->resp_code = atoi(s);
    if (hm->resp_code < 100 || hm->resp_code >= 600) return -1;
    s += 4;
    s = mg_skip(s, end, "\r\n", &hm->resp_status_msg);
  }

  s = mg_http_parse_headers(s, end, len, hm);

  /*
   * mg_parse_http() is used to parse both HTTP requests and HTTP
   * responses. If HTTP response does not have Content-Length set, then
   * body is read until socket is closed, i.e. body.len is infinite (~0).
   *
   * For HTTP requests though, according to
   * http://tools.ietf.org/html/rfc7231#section-8.1.3,
   * only POST and PUT methods have defined body semantics.
   * Therefore, if Content-Length is not specified and methods are
   * not one of PUT or POST, set body length to 0.
   *
   * So,
   * if it is HTTP request, and Content-Length is not set,
   * and method is not (PUT or POST) then reset body length to zero.
   */
  if (hm->body.len == (size_t) ~0 && is_req &&
      mg_vcasecmp(&hm->method, "PUT") != 0 &&
      mg_vcasecmp(&hm->method, "POST") != 0) {
    hm->body.len = 0;
    hm->message.len = len;
  }

  return len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r14d
movq %rdx, %rbx
movq %rdi, %r13
callq 0xae20
movl %eax, %ebp
testl %eax, %eax
jle 0xae0b
leaq 0x20(%rbx), %r15
movl $0x558, %edx            # imm = 0x558
movq %r15, %rdi
xorl %esi, %esi
callq 0x3240
movq %r13, (%rbx)
movl %ebp, %eax
movq %rax, 0x10(%rsp)
leaq (%rax,%r13), %r12
movq %r12, 0x10(%rbx)
orq $-0x1, 0x18(%rbx)
orq $-0x1, 0x8(%rbx)
cmpq %r12, %r13
jae 0xac9f
callq 0x3660
movq (%rax), %rax
movzbl (%r13), %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0xac9f
incq %r13
jmp 0xac81
movl %r14d, 0x4(%rsp)
testl %r14d, %r14d
je 0xad4d
leaq 0xb959(%rip), %r14      # 0x1660d
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, 0x8(%rsp)
movq %r15, %rcx
callq 0xaec4
leaq 0x30(%rbx), %rcx
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0xaec4
leaq 0x40(%rbx), %r15
leaq 0xaae7(%rip), %rdx      # 0x157ce
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0xaec4
movq 0x30(%rbx), %r14
pushq $-0x1
popq %r13
cmpq 0x20(%rbx), %r14
jbe 0xae0e
cmpq %r14, (%r15)
jbe 0xae0e
movq %rax, %r15
movq 0x38(%rbx), %r13
pushq $0x3f
popq %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0x32c0
testq %rax, %rax
je 0xad43
leaq 0x1(%rax), %rcx
movq %rcx, 0x68(%rbx)
addq %r14, %r13
subq %rcx, %r13
movq %r13, 0x70(%rbx)
subq %r14, %rax
movq %rax, 0x38(%rbx)
movq 0x8(%rsp), %r14
movq %r15, %rdi
jmp 0xadbc
leaq 0x40(%rbx), %rcx
leaq 0xb8b5(%rip), %rdx      # 0x1660d
movq %r13, %rdi
movq %r12, %rsi
callq 0xaec4
movq %rax, %r14
movq %r12, %rax
subq %r14, %rax
pushq $-0x1
popq %r13
cmpq $0x4, %rax
jl 0xae0e
cmpb $0x20, 0x3(%r14)
jne 0xae0e
movq %r14, %rdi
callq 0x3580
movl %eax, 0x50(%rbx)
addl $0xfffffda8, %eax       # imm = 0xFFFFFDA8
cmpl $0xfffffe0c, %eax       # imm = 0xFFFFFE0C
jb 0xae0e
addq $0x4, %r14
leaq 0x58(%rbx), %rcx
leaq 0xaa23(%rip), %rdx      # 0x157ce
movq %r14, %rdi
movq %r12, %rsi
callq 0xaec4
movq %rax, %rdi
movq %r15, %r14
movq %r12, %rsi
movl %ebp, %edx
movq %rbx, %rcx
callq 0xaf33
cmpl $0x0, 0x4(%rsp)
je 0xae0b
cmpq $-0x1, 0x18(%rbx)
jne 0xae0b
leaq 0x9c17(%rip), %rsi      # 0x149f5
movq %r14, %rdi
callq 0x68d7
testl %eax, %eax
je 0xae0b
leaq 0x9c08(%rip), %rsi      # 0x149f9
movq %r14, %rdi
callq 0x68d7
testl %eax, %eax
je 0xae0b
andq $0x0, 0x18(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rbx)
movl %ebp, %r13d
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq