// File: /workspace/github_c_makefile_star2_O1/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, %r14
movl %esi, %ebp
movq %rdi, 0x8(%rsp)
cmpq $0x0, 0x88(%rdi)
jne 0xbc17
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq 0x8(%rsp), %rcx
movq %rax, 0x88(%rcx)
leaq 0x7011(%rip), %rax      # 0x12c21
movq %rax, 0x90(%rcx)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
movq 0x88(%rax), %r12
addq $0x40, %rax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
testq %rbx, %rbx
setne %r15b
cmpl $0x5, %ebp
jne 0xbce3
movq 0x20(%r12), %rax
testq %rax, %rax
je 0xbc5f
movq $0x0, 0xa0(%rax)
orb $0x8, 0xc9(%rax)
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rsi
testq %rsi, %rsi
je 0xbcda
movq 0x38(%rsp), %rax
movq (%rax), %rdi
leaq 0x90(%rsp), %rdx
movl %r15d, %ecx
callq 0xb64f
testl %eax, %eax
jle 0xbcda
movq %r12, 0x50(%rsp)
xorl %r12d, %r12d
cmpq $0x1, %rbx
adcl $0x64, %r12d
movq 0x8(%rsp), %rbx
movq 0x48(%rbx), %rcx
leaq 0x90(%rsp), %r13
movq %rcx, 0x8(%r13)
addq 0x40(%rbx), %rcx
subq 0x10(%r13), %rcx
movq %rcx, 0x18(%r13)
movq %rbx, %rdi
movq %r13, %rsi
movl %eax, %edx
callq 0xc349
movq %rbx, %rdi
movl %r12d, %esi
movq 0x50(%rsp), %r12
movq %r13, %rdx
callq 0xc38e
movq $0x0, 0x50(%r12)
cmpq $0x0, (%r12)
je 0xbcf4
movq 0x8(%rsp), %rdi
callq 0xc491
movq 0x8(%rsp), %rdi
movq 0x98(%rdi), %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x7a51
cmpl $0x3, %ebp
jne 0xc2f9
movslq (%r14), %rax
addq %rax, 0x50(%r12)
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rdi
movl 0x48(%rax), %esi
leaq 0x90(%rsp), %rdx
movl %r15d, %ecx
callq 0xb64f
movl %eax, %ebp
testl %eax, %eax
movl %eax, 0x1c(%rsp)
jle 0xc060
leaq 0x9cae(%rip), %rsi      # 0x159fa
leaq 0x90(%rsp), %rdi
callq 0xbaa7
testq %rax, %rax
je 0xc060
leaq 0x9ca3(%rip), %rsi      # 0x15a0c
movq %rax, %rdi
callq 0x6c4a
testl %eax, %eax
jne 0xc060
movl %ebp, %eax
movq 0x8(%rsp), %rcx
movq 0x40(%rcx), %rbx
addq %rax, %rbx
movq 0x48(%rcx), %r15
subq %rax, %r15
cmpq $0x0, 0x88(%rcx)
jne 0xbdc1
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq 0x8(%rsp), %rcx
movq %rax, 0x88(%rcx)
leaq 0x6e67(%rip), %rax      # 0x12c21
movq %rax, 0x90(%rcx)
movq 0x8(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x30(%rax), %rdx
movq %r15, 0x60(%rsp)
cmpq %r15, %rdx
ja 0xc30b
movq %rdx, %r15
movq %r14, 0x28(%rsp)
movq %r12, 0x50(%rsp)
movq %rbx, 0x10(%rsp)
movq %r15, 0x58(%rsp)
leaq (%rbx,%rdx), %r12
movq 0x60(%rsp), %rbx
subq %rdx, %rbx
je 0xbe62
movq %rdx, 0x30(%rsp)
callq 0x3660
movq (%rax), %r13
xorl %r15d, %r15d
xorl %r14d, %r14d
movzbl (%r12,%r14), %ebp
testb $0x10, 0x1(%r13,%rbp,2)
je 0xbe5b
leal -0x30(%rbp), %eax
cmpb $0x9, %al
ja 0xbe35
leal -0x30(%rbp), %eax
jmp 0xbe43
callq 0x3650
movq (%rax), %rax
movl (%rax,%rbp,4), %eax
addl $-0x57, %eax
movq %r15, %rcx
shlq $0x4, %rcx
movslq %eax, %r15
addq %rcx, %r15
incq %r14
cmpq %r14, %rbx
jne 0xbe1c
movq %rbx, %r14
movq 0x30(%rsp), %rdx
jmp 0xbe68
xorl %r14d, %r14d
xorl %r15d, %r15d
testq %r14, %r14
je 0xbeab
leaq 0x2(%r14), %rax
cmpq %rbx, %rax
ja 0xbeab
cmpb $0xd, (%r12,%r14)
jne 0xbeab
cmpb $0xa, 0x1(%r12,%r14)
jne 0xbeab
leaq (%r12,%rax), %rcx
movq %rcx, 0x48(%rsp)
addq %r15, %rax
movq 0x28(%rsp), %r14
je 0xbea1
leaq 0x2(%rax), %r13
cmpq %rbx, %r13
jbe 0xbf0d
movq %r15, 0x20(%rsp)
xorl %r13d, %r13d
jmp 0xbeb3
xorl %r13d, %r13d
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %rbx
testq %r13, %r13
movq 0x50(%rsp), %r12
movq 0x58(%rsp), %r15
je 0xbf3c
leaq (%rbx,%r15), %rdi
movq 0x48(%rsp), %rsi
movq %rdx, %rbp
movq %r12, %rbx
movq %r14, %r12
movq 0x20(%rsp), %r14
movq %r14, %rdx
callq 0x3500
movq %rbp, %rdx
addq %r14, %r15
movq %r15, 0xa8(%rsp)
addq %r13, %rdx
testq %r14, %r14
movq %r12, %r14
movq %rbx, %r12
movq 0x10(%rsp), %rbx
jne 0xbdf6
jmp 0xbf3c
cmpb $0xd, (%r12,%rax)
movq 0x10(%rsp), %rbx
jne 0xbf2f
cmpb $0xa, 0x1(%r12,%rax)
movl $0x0, %eax
cmovneq %rax, %r13
movq %r15, 0x20(%rsp)
jmp 0xbeb8
movq %r15, 0x20(%rsp)
xorl %r13d, %r13d
jmp 0xbeb8
movq %rdx, %rdi
subq %r15, %rdi
movl 0x1c(%rsp), %ebp
jbe 0xc060
movq %r15, %rax
movq %rdi, 0x20(%rsp)
movq %r13, 0x48(%rsp)
movq 0x60(%rsp), %r15
movq %r15, %r13
subq %rdx, %r13
jb 0xc32a
movq %rax, 0x58(%rsp)
leaq (%rbx,%rax), %rbp
leaq (%rbx,%rdx), %rsi
negq %rdx
movq %rdx, 0x30(%rsp)
movq %rbp, %rdi
movq %r13, %rdx
callq 0x3500
movq %rbp, 0x68(%rsp)
addq %rbp, %r15
addq 0x30(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %r15
movq %r15, %rdx
callq 0x3240
movq %r13, %rbp
movq 0x8(%rsp), %r13
subq %r15, 0x48(%r13)
movq 0x58(%rsp), %r15
movq 0x40(%rsp), %rax
movq %r15, 0x30(%rax)
andb $-0x21, 0xc9(%r13)
movq 0x98(%r13), %rsi
leaq 0x90(%rsp), %rcx
movq %r13, %rdi
movl $0x66, %edx
callq 0x7a51
testb $0x20, 0xc9(%r13)
movq %rbp, %r13
je 0xc033
movq %r15, %rbp
movq %rbx, %r15
xorl %ebx, %ebx
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3240
movq %r15, %rdi
movq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x3500
movq 0x8(%rsp), %rax
subq %rbp, 0x48(%rax)
movq %rbx, 0xa8(%rsp)
movq 0x40(%rsp), %rax
movq %rbx, 0x30(%rax)
cmpq $0x0, 0x48(%rsp)
movl 0x1c(%rsp), %ebp
je 0xc060
movq 0x40(%rsp), %rax
addq 0x30(%rax), %r13
addq 0xa0(%rsp), %r13
subq 0x90(%rsp), %r13
movq %r13, 0x98(%rsp)
testl %ebp, %ebp
js 0xc166
je 0xc153
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0xc1cd
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0xc1a1
leaq 0x99b4(%rip), %rsi      # 0x15a49
leaq 0x90(%rsp), %rdi
callq 0xbaa7
testq %rax, %rax
je 0xc1a1
movq %rax, %r15
movl %ebp, %esi
movq 0x38(%rsp), %rdi
callq 0x6bb8
leaq 0x5e3(%rip), %rax       # 0xc6a4
movq 0x8(%rsp), %rcx
movq %rax, 0x80(%rcx)
orb $0x1, 0xc9(%rcx)
movq 0x10(%rcx), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xbb2c
testq %rax, %rax
je 0xc0fa
movq 0x28(%rax), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x98(%rcx)
movq 0x8(%rsp), %rbx
movq 0x98(%rbx), %rsi
leaq 0x90(%rsp), %rcx
movq %rbx, %rdi
movl $0x6f, %edx
callq 0x7a51
testb $0xc, 0xc9(%rbx)
jne 0xc2f9
movq 0x8(%rsp), %rax
cmpq $0x0, 0x60(%rax)
jne 0xc149
leaq 0x90(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xc995
movq 0x8(%rsp), %rbx
jmp 0xc210
movq 0x8(%rsp), %rax
cmpq $0x2000, 0x48(%rax)     # imm = 0x2000
jb 0xc2f9
leaq 0x98a7(%rip), %rsi      # 0x15a14
leaq 0x9436(%rip), %rdx      # 0x155aa
movl $0x4, %edi
callq 0x44b4
testl %eax, %eax
je 0xc190
leaq 0x989b(%rip), %rdi      # 0x15a24
xorl %eax, %eax
callq 0x45a0
movq 0x8(%rsp), %rax
orb $0x8, 0xc9(%rax)
jmp 0xc2f9
movq 0x98(%rsp), %rax
cmpq 0x50(%r12), %rax
jbe 0xc23b
leaq 0x90(%rsp), %rsi
movq 0x8(%rsp), %rdi
movl %ebp, %edx
callq 0xc349
jmp 0xc2f9
leaq 0x9860(%rip), %rsi      # 0x15a34
leaq 0x90(%rsp), %rdi
callq 0xbaa7
testq %rax, %rax
je 0xc07e
movl %ebp, %esi
movq 0x38(%rsp), %rdi
callq 0x6bb8
leaq 0x4a7(%rip), %rax       # 0xc6a4
movq 0x8(%rsp), %rbx
movq %rax, 0x80(%rbx)
orb $0x1, 0xc9(%rbx)
movq 0x98(%rbx), %rsi
movq %rbx, %rdi
movl $0x70, %edx
xorl %ecx, %ecx
callq 0x7a51
movq %rbx, %rdi
movl $0x3, %esi
movq %r14, %rdx
callq 0xc6a4
jmp 0xc2f9
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
cmpq $0x1, 0x10(%rax)
adcl $0x64, %ebp
leaq 0x28(%rax), %rdi
leaq 0x70(%rsp), %rsi
movl $0x20, %edx
movl $0x3, %ecx
callq 0xaa56
leaq 0x97ab(%rip), %rsi      # 0x15a14
leaq 0x933a(%rip), %rdx      # 0x155aa
movl $0x4, %edi
callq 0x44b4
testl %eax, %eax
je 0xc2b9
movl 0xb8(%rsp), %ecx
movq 0xb0(%rsp), %r8
movl 0xc8(%rsp), %r9d
movq 0xc0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x97b3(%rip), %rdi      # 0x15a5b
leaq 0x70(%rsp), %rdx
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x45a0
leaq 0x90(%rsp), %r15
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movl 0x1c(%rsp), %edx
callq 0xc349
movq %rbx, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0xc38e
movq 0x8(%r15), %rsi
movq 0x38(%rsp), %rdi
callq 0x6bb8
movq $0x0, 0x50(%r12)
addq $0x608, %rsp            # imm = 0x608
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa188(%rip), %rdi      # 0x1649a
leaq 0x9291(%rip), %rsi      # 0x155aa
leaq 0xa18b(%rip), %rcx      # 0x164ab
movl $0x17d2, %edx           # imm = 0x17D2
callq 0x3220
leaq 0xa1d2(%rip), %rdi      # 0x16503
leaq 0x9272(%rip), %rsi      # 0x155aa
leaq 0xa16c(%rip), %rcx      # 0x164ab
movl $0x17e6, %edx           # imm = 0x17E6
callq 0x3220