// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_http_handler
void mg_http_handler(struct mg_connection *nc, int ev,
                     void *ev_data MG_UD_ARG(void *user_data)) {
  struct http_message shm, *hm = &shm;
#endif /* __XTENSA__ */
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
  struct mbuf *io = &nc->recv_mbuf;
  int req_len;
  const int is_req = (nc->listener != NULL);
#if MG_ENABLE_HTTP_WEBSOCKET
  struct mg_str *vec;
#endif
  if (ev == MG_EV_CLOSE) {
#if MG_ENABLE_HTTP_CGI
    /* Close associated CGI forwarder connection */
    if (pd->cgi.cgi_nc != NULL) {
      pd->cgi.cgi_nc->user_data = NULL;
      pd->cgi.cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
    }
#endif
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
    if (pd->mp_stream.boundary != NULL) {
      /*
       * Multipart message is in progress, but connection is closed.
       * Finish part and request with an error flag.
       */
      struct mg_http_multipart_part mp;
      memset(&mp, 0, sizeof(mp));
      mp.status = -1;
      mp.var_name = pd->mp_stream.var_name;
      mp.file_name = pd->mp_stream.file_name;
      mg_call(nc, (pd->endpoint_handler ? pd->endpoint_handler : nc->handler),
              nc->user_data, MG_EV_HTTP_PART_END, &mp);
      mp.var_name = NULL;
      mp.file_name = NULL;
      mg_call(nc, (pd->endpoint_handler ? pd->endpoint_handler : nc->handler),
              nc->user_data, MG_EV_HTTP_MULTIPART_REQUEST_END, &mp);
    } else
#endif
        if (io->len > 0 &&
            (req_len = mg_parse_http(io->buf, io->len, hm, is_req)) > 0) {
      /*
      * For HTTP messages without Content-Length, always send HTTP message
      * before MG_EV_CLOSE message.
      */
      int ev2 = is_req ? MG_EV_HTTP_REQUEST : MG_EV_HTTP_REPLY;
      hm->message.len = io->len;
      hm->body.len = io->buf + io->len - hm->body.p;
      deliver_chunk(nc, hm, req_len);
      mg_http_call_endpoint_handler(nc, ev2, hm);
    }
    pd->rcvd = 0;
  }

#if MG_ENABLE_FILESYSTEM
  if (pd->file.fp != NULL) {
    mg_http_transfer_file_data(nc);
  }
#endif

  mg_call(nc, nc->handler, nc->user_data, ev, ev_data);

  if (ev == MG_EV_RECV) {
    struct mg_str *s;
    pd->rcvd += *(int *) ev_data;

#if MG_ENABLE_HTTP_STREAMING_MULTIPART
    if (pd->mp_stream.boundary != NULL) {
      mg_http_multipart_continue(nc);
      return;
    }
#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */

    req_len = mg_parse_http(io->buf, io->len, hm, is_req);

    if (req_len > 0 &&
        (s = mg_get_http_header(hm, "Transfer-Encoding")) != NULL &&
        mg_vcasecmp(s, "chunked") == 0) {
      mg_handle_chunked(nc, hm, io->buf + req_len, io->len - req_len);
    }

#if MG_ENABLE_HTTP_STREAMING_MULTIPART
    if (req_len > 0 && (s = mg_get_http_header(hm, "Content-Type")) != NULL &&
        s->len >= 9 && strncmp(s->p, "multipart", 9) == 0) {
      mg_http_multipart_begin(nc, hm, req_len);
      mg_http_multipart_continue(nc);
      return;
    }
#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */

    /* TODO(alashkin): refactor this ifelseifelseifelseifelse */
    if ((req_len < 0 ||
         (req_len == 0 && io->len >= MG_MAX_HTTP_REQUEST_SIZE))) {
      DBG(("invalid request"));
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
    } else if (req_len == 0) {
      /* Do nothing, request is not yet fully buffered */
    }
#if MG_ENABLE_HTTP_WEBSOCKET
    else if (nc->listener == NULL &&
             mg_get_http_header(hm, "Sec-WebSocket-Accept")) {
      /* We're websocket client, got handshake response from server. */
      /* TODO(lsm): check the validity of accept Sec-WebSocket-Accept */
      mbuf_remove(io, req_len);
      nc->proto_handler = mg_ws_handler;
      nc->flags |= MG_F_IS_WEBSOCKET;
      mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_DONE,
              NULL);
      mg_ws_handler(nc, MG_EV_RECV, ev_data MG_UD_ARG(user_data));
    } else if (nc->listener != NULL &&
               (vec = mg_get_http_header(hm, "Sec-WebSocket-Key")) != NULL) {
      struct mg_http_endpoint *ep;

      /* This is a websocket request. Switch protocol handlers. */
      mbuf_remove(io, req_len);
      nc->proto_handler = mg_ws_handler;
      nc->flags |= MG_F_IS_WEBSOCKET;

      /*
       * If we have a handler set up with mg_register_http_endpoint(),
       * deliver subsequent websocket events to this handler after the
       * protocol switch.
       */
      ep = mg_http_get_endpoint_handler(nc->listener, &hm->uri);
      if (ep != NULL) {
        nc->handler = ep->handler;
#if MG_ENABLE_CALLBACK_USERDATA
        nc->user_data = ep->user_data;
#endif
      }

      /* Send handshake */
      mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_REQUEST,
              hm);
      if (!(nc->flags & (MG_F_CLOSE_IMMEDIATELY | MG_F_SEND_AND_CLOSE))) {
        if (nc->send_mbuf.len == 0) {
          mg_ws_handshake(nc, vec, hm);
        }
        mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_DONE,
                NULL);
        mg_ws_handler(nc, MG_EV_RECV, ev_data MG_UD_ARG(user_data));
      }
    }
#endif /* MG_ENABLE_HTTP_WEBSOCKET */
    else if (hm->message.len > pd->rcvd) {
      /* Not yet received all HTTP body, deliver MG_EV_HTTP_CHUNK */
      deliver_chunk(nc, hm, req_len);
    } else {
      /* We did receive all HTTP body. */
      int trigger_ev = nc->listener ? MG_EV_HTTP_REQUEST : MG_EV_HTTP_REPLY;
      char addr[32];
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
      DBG(("%p %s %.*s %.*s", nc, addr, (int) hm->method.len, hm->method.p,
           (int) hm->uri.len, hm->uri.p));
      deliver_chunk(nc, hm, req_len);
      /* Whole HTTP message is fully buffered, call event handler */
      mg_http_call_endpoint_handler(nc, trigger_ev, hm);
      mbuf_remove(io, hm->message.len);
      pd->rcvd = 0;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x608, %rsp            # imm = 0x608
movq %rdx, 0x40(%rsp)
movl %esi, %ebp
movq %rdi, %r15
movq 0x88(%rdi), %r13
testq %r13, %r13
jne 0xb9e8
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, %r13
movq %rax, 0x88(%r15)
leaq 0x771d(%rip), %rax      # 0x130fe
movq %rax, 0x90(%r15)
leaq 0x40(%r15), %rbx
movq 0x10(%r15), %r14
xorl %r12d, %r12d
testq %r14, %r14
setne %r12b
cmpl $0x5, %ebp
jne 0xba98
movq 0x20(%r13), %rax
testq %rax, %rax
je 0xba1e
movq $0x0, 0xa0(%rax)
orb $0x8, 0xc9(%rax)
movq 0x48(%r15), %rsi
testq %rsi, %rsi
je 0xba90
movq (%rbx), %rdi
leaq 0x90(%rsp), %rdx
movl %r12d, %ecx
callq 0xb464
testl %eax, %eax
jle 0xba90
movq %rbx, 0x30(%rsp)
movq %r13, %rbx
xorl %r13d, %r13d
cmpq $0x1, %r14
adcl $0x64, %r13d
movq 0x48(%r15), %rcx
leaq 0x90(%rsp), %r14
movq %rcx, 0x8(%r14)
addq 0x40(%r15), %rcx
subq 0x10(%r14), %rcx
movq %rcx, 0x18(%r14)
movq %r15, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0xc0da
movq %r15, %rdi
movl %r13d, %esi
movq %rbx, %r13
movq 0x30(%rsp), %rbx
movq %r14, %rdx
callq 0xc11f
movq $0x0, 0x50(%r13)
cmpq $0x0, (%r13)
je 0xbaa7
movq %r15, %rdi
callq 0xc220
movq 0x98(%r15), %rsi
movq %r15, %rdi
movl %ebp, %edx
movq 0x40(%rsp), %r14
movq %r14, %rcx
callq 0x7979
cmpl $0x3, %ebp
jne 0xc068
movslq (%r14), %rax
addq %rax, 0x50(%r13)
movq 0x40(%r15), %rdi
movl 0x48(%r15), %esi
leaq 0x90(%rsp), %rdx
movl %r12d, %ecx
callq 0xb464
testl %eax, %eax
movl %eax, 0x14(%rsp)
jle 0xbdf0
leaq 0x9f20(%rip), %rsi      # 0x15a1b
leaq 0x90(%rsp), %rdi
callq 0xb896
testq %rax, %rax
je 0xbdfe
leaq 0x9f15(%rip), %rsi      # 0x15a2d
movq %rax, %rdi
callq 0x6b88
testl %eax, %eax
jne 0xbdfe
movl 0x14(%rsp), %ecx
movq 0x40(%r15), %r12
addq %rcx, %r12
movq 0x48(%r15), %rbp
movq 0x88(%r15), %rax
subq %rcx, %rbp
testq %rax, %rax
jne 0xbb6a
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%r15)
leaq 0x759b(%rip), %rcx      # 0x130fe
movq %rcx, 0x90(%r15)
movq 0x30(%rax), %rcx
movq %rbp, %r14
movq %rcx, 0x20(%rsp)
subq %rcx, %r14
jb 0xc09c
je 0xbdfe
movq %rbp, 0x60(%rsp)
movq %rax, 0x58(%rsp)
movq %r15, 0x68(%rsp)
callq 0x3660
movq %rax, 0x48(%rsp)
movq 0x20(%rsp), %rbp
movq %r13, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq %rbp, 0x18(%rsp)
addq %r12, %rbp
movq 0x48(%rsp), %rax
movq (%rax), %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
movzbl (%rbp,%r13), %ebx
testb $0x10, 0x1(%r15,%rbx,2)
je 0xbc0a
leal -0x30(%rbx), %eax
cmpb $0x9, %al
ja 0xbbe2
leal -0x30(%rbx), %eax
jmp 0xbbf0
callq 0x3650
movq (%rax), %rax
movl (%rax,%rbx,4), %eax
addl $-0x57, %eax
movq %r12, %rcx
shlq $0x4, %rcx
movslq %eax, %r12
addq %rcx, %r12
incq %r13
cmpq %r13, %r14
jne 0xbbc8
movq %r14, %r13
jmp 0xbc13
testq %r13, %r13
je 0xc087
leaq 0x2(%r13), %rax
cmpq %r14, %rax
movq 0x30(%rsp), %rbx
ja 0xbcc6
cmpb $0xd, (%rbp,%r13)
jne 0xbcc6
cmpb $0xa, 0x1(%rbp,%r13)
jne 0xbcc6
movq %rax, %rcx
addq %r12, %rcx
movq 0x28(%rsp), %r13
je 0xbcbe
leaq 0x2(%rcx), %r15
cmpq %r14, %r15
ja 0xbcbe
cmpb $0xd, (%rbp,%rcx)
jne 0xbcbe
testq %r15, %r15
je 0xbcbe
movb $0x1, %dl
movl %edx, 0x10(%rsp)
cmpb $0xa, 0x1(%rbp,%rcx)
jne 0xbcd1
addq %rax, %rbp
movq 0x38(%rsp), %rax
movq 0x20(%rsp), %r14
leaq (%rax,%r14), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3500
addq %r12, %r14
movq %r14, 0x20(%rsp)
movq %r14, 0xa8(%rsp)
movq 0x18(%rsp), %rbp
addq %r15, %rbp
testq %r12, %r12
je 0xc07a
movq 0x60(%rsp), %r14
subq %rbp, %r14
movq 0x38(%rsp), %r12
jne 0xbbb2
jmp 0xbcd6
movb $0x1, %al
movl %eax, 0x10(%rsp)
jmp 0xbcd1
movb $0x1, %al
movl %eax, 0x10(%rsp)
movq 0x28(%rsp), %r13
movq 0x18(%rsp), %rbp
movq %rbp, %rcx
subq 0x20(%rsp), %rcx
movq 0x68(%rsp), %r15
movl 0x14(%rsp), %eax
jbe 0xbdf0
movq %rcx, 0x48(%rsp)
movq 0x60(%rsp), %r12
movq %r12, %rax
subq %rbp, %rax
jb 0xc0bb
movq 0x38(%rsp), %rcx
movq 0x20(%rsp), %rdx
leaq (%rcx,%rdx), %rdi
movq %rdi, 0x18(%rsp)
leaq (%rcx,%rbp), %rsi
negq %rbp
movq %rax, 0x50(%rsp)
movq %rax, %rdx
callq 0x3500
addq 0x18(%rsp), %r12
addq %rbp, %r12
movq %r12, %rdi
movq 0x20(%rsp), %r12
xorl %esi, %esi
movq 0x48(%rsp), %rbp
movq %rbp, %rdx
callq 0x3240
subq %rbp, 0x48(%r15)
movq 0x58(%rsp), %rbp
movq %r12, 0x30(%rbp)
andb $-0x21, 0xc9(%r15)
movq 0x98(%r15), %rsi
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movl $0x66, %edx
callq 0x7979
testb $0x20, 0xc9(%r15)
je 0xbdbf
xorl %ebx, %ebx
movq 0x38(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x3240
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x3500
subq %r12, 0x48(%r15)
movq %rbx, 0xa8(%rsp)
movq %rbx, 0x30(%rbp)
movq 0x30(%rsp), %rbx
cmpb $0x0, 0x10(%rsp)
movl 0x14(%rsp), %eax
jne 0xbdf0
movq 0x58(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq 0x30(%rcx), %rdx
addq 0xa0(%rsp), %rdx
subq 0x90(%rsp), %rdx
movq %rdx, 0x98(%rsp)
testl %eax, %eax
js 0xbf50
je 0xbf42
cmpq $0x0, 0x10(%r15)
je 0xbe6a
leaq 0x9c5e(%rip), %rsi      # 0x15a6a
leaq 0x90(%rsp), %rdi
callq 0xb896
testq %rax, %rax
je 0xbf92
movq %rax, %r12
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0x6af6
leaq 0x5d4(%rip), %rax       # 0xc40c
movq %rax, 0x80(%r15)
orb $0x1, 0xc9(%r15)
movq 0x10(%r15), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xb902
testq %rax, %rax
je 0xbece
movq 0x28(%rax), %rsi
movq %rsi, 0x98(%r15)
jmp 0xbed5
leaq 0x9be4(%rip), %rsi      # 0x15a55
leaq 0x90(%rsp), %rdi
callq 0xb896
testq %rax, %rax
je 0xbf87
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0x6af6
leaq 0x572(%rip), %rax       # 0xc40c
movq %rax, 0x80(%r15)
orb $0x1, 0xc9(%r15)
movq 0x98(%r15), %rsi
movq %r15, %rdi
movl $0x70, %edx
xorl %ecx, %ecx
callq 0x7979
movq %r15, %rdi
movl $0x3, %esi
movq 0x40(%rsp), %rdx
jmp 0xbf38
movq 0x98(%r15), %rsi
movq 0x40(%rsp), %rbx
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movl $0x6f, %edx
callq 0x7979
testb $0xc, 0xc9(%r15)
jne 0xc068
cmpq $0x0, 0x60(%r15)
jne 0xbf17
leaq 0x90(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0xc703
movq 0x98(%r15), %rsi
movq %r15, %rdi
movl $0x70, %edx
xorl %ecx, %ecx
callq 0x7979
movq %r15, %rdi
movl $0x3, %esi
movq %rbx, %rdx
callq 0xc40c
jmp 0xc068
cmpq $0x2000, 0x48(%r15)     # imm = 0x2000
jb 0xc068
leaq 0x9ade(%rip), %rsi      # 0x15a35
leaq 0x9670(%rip), %rdx      # 0x155ce
movl $0x4, %edi
callq 0x4447
testl %eax, %eax
je 0xbf7a
leaq 0x9ad2(%rip), %rdi      # 0x15a45
xorl %eax, %eax
callq 0x4539
orb $0x8, 0xc9(%r15)
jmp 0xc068
cmpq $0x0, 0x10(%r15)
jne 0xbe05
movq 0x98(%rsp), %rax
cmpq 0x50(%r13), %rax
jbe 0xbfb9
leaq 0x90(%rsp), %rsi
movq %r15, %rdi
movl 0x14(%rsp), %edx
callq 0xc0da
jmp 0xc068
xorl %ebp, %ebp
cmpq $0x1, 0x10(%r15)
adcl $0x64, %ebp
leaq 0x28(%r15), %rdi
leaq 0x70(%rsp), %rsi
movl $0x20, %edx
movl $0x3, %ecx
callq 0xa911
leaq 0x9a53(%rip), %rsi      # 0x15a35
leaq 0x95e5(%rip), %rdx      # 0x155ce
movl $0x4, %edi
callq 0x4447
testl %eax, %eax
je 0xc030
movl 0xb8(%rsp), %ecx
movq 0xb0(%rsp), %r8
movl 0xc8(%rsp), %r9d
movq 0xc0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x9a5b(%rip), %rdi      # 0x15a7c
leaq 0x70(%rsp), %rdx
movq %r15, %rsi
xorl %eax, %eax
callq 0x4539
leaq 0x90(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
movl 0x14(%rsp), %edx
callq 0xc0da
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xc11f
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6af6
movq $0x0, 0x50(%r13)
addq $0x608, %rsp            # imm = 0x608
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x10(%rsp)
jmp 0xbcd6
movb $0x1, %al
movl %eax, 0x10(%rsp)
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %rbx
jmp 0xbcd1
leaq 0xa415(%rip), %rdi      # 0x164b8
leaq 0x9524(%rip), %rsi      # 0x155ce
leaq 0xa418(%rip), %rcx      # 0x164c9
movl $0x17d2, %edx           # imm = 0x17D2
callq 0x3220
leaq 0xa45f(%rip), %rdi      # 0x16521
leaq 0x9505(%rip), %rsi      # 0x155ce
leaq 0xa3f9(%rip), %rcx      # 0x164c9
movl $0x17e6, %edx           # imm = 0x17E6
callq 0x3220