// File: /workspace/github_c_makefile_star2_O1/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, %r13d
movq %rdx, %rbx
movq %rdi, %r12
callq 0xb849
movl %eax, %ebp
testl %eax, %eax
jle 0xb834
movl $0x578, %edx            # imm = 0x578
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3240
movq %r12, (%rbx)
movl %ebp, %r14d
leaq (%r12,%r14), %r15
movq %r15, 0x10(%rbx)
movq $-0x1, %rax
movq %rax, 0x18(%rbx)
movq %rax, 0x8(%rbx)
callq 0x3660
movq (%rax), %rax
movzbl (%r12), %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0xb6bd
incq %r12
cmpq %r15, %r12
jb 0xb6a9
movl %r13d, 0xc(%rsp)
testl %r13d, %r13d
je 0xb76b
movq %r14, 0x10(%rsp)
leaq 0x20(%rbx), %rcx
leaq 0xbf31(%rip), %r14      # 0x1760c
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xb8ec
leaq 0x30(%rbx), %rcx
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xb8ec
leaq 0x40(%rbx), %r14
leaq 0xb0c7(%rip), %rdx      # 0x167cd
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0xb8ec
movq 0x30(%rbx), %r13
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq 0x20(%rbx), %r13
jbe 0xb837
cmpq %r13, (%r14)
jbe 0xb837
movq %rax, %r14
movq 0x38(%rbx), %r12
movq %r13, %rdi
movl $0x3f, %esi
movq %r12, %rdx
callq 0x32c0
testq %rax, %rax
je 0xb766
leaq 0x1(%rax), %rcx
movq %rcx, 0x68(%rbx)
addq %r13, %r12
subq %rcx, %r12
movq %r12, 0x70(%rbx)
subq %r13, %rax
movq %rax, 0x38(%rbx)
movq %r14, %rdi
jmp 0xb7de
leaq 0x40(%rbx), %rcx
leaq 0xbe96(%rip), %rdx      # 0x1760c
movq %r12, %rdi
movq %r15, %rsi
callq 0xb8ec
movq %rax, %r13
movq %r15, %rax
subq %r13, %rax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq $0x4, %rax
jl 0xb837
cmpb $0x20, 0x3(%r13)
jne 0xb837
movq %r14, 0x10(%rsp)
movq %r13, %rdi
callq 0x3580
movl %eax, 0x50(%rbx)
addl $0xfffffda8, %eax       # imm = 0xFFFFFDA8
cmpl $0xfffffe0c, %eax       # imm = 0xFFFFFE0C
jb 0xb837
addq $0x4, %r13
leaq 0x58(%rbx), %rcx
leaq 0xaffd(%rip), %rdx      # 0x167cd
movq %r13, %rdi
movq %r15, %rsi
callq 0xb8ec
movq %rax, %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %rbx, %rcx
callq 0xb968
cmpl $0x0, 0xc(%rsp)
je 0xb834
cmpq $-0x1, 0x18(%rbx)
jne 0xb834
leaq 0x20(%rbx), %r14
leaq 0xa1ed(%rip), %rsi      # 0x159f1
movq %r14, %rdi
callq 0x6c4a
testl %eax, %eax
je 0xb834
leaq 0xa1de(%rip), %rsi      # 0x159f5
movq %r14, %rdi
callq 0x6c4a
testl %eax, %eax
je 0xb834
movq $0x0, 0x18(%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rbx)
movl %ebp, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq