// File: /workspace/github_c_makefile_star2_O2/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 $0x638, %rsp            # imm = 0x638
movq %rdx, %r13
movl %esi, %r14d
movq %rdi, %rbx
callq 0xb106
movq %rax, %rcx
leaq 0x40(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rbx), %r12
xorl %ebp, %ebp
testq %r12, %r12
setne %bpl
cmpl $0x5, %r14d
movq %rcx, 0x20(%rsp)
jne 0xb20e
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0xb198
andq $0x0, 0xa0(%rax)
orb $0x8, 0xc9(%rax)
movq 0x48(%rbx), %rsi
testq %rsi, %rsi
je 0xb204
movq 0x18(%rsp), %rax
movq (%rax), %rdi
leaq 0xc0(%rsp), %rdx
movl %ebp, %ecx
callq 0xac2c
testl %eax, %eax
jle 0xb204
xorl %r15d, %r15d
cmpq $0x1, %r12
adcl $0x64, %r15d
movq 0x48(%rbx), %rcx
movq %r13, %r12
leaq 0xc0(%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 0xb88a
movq %rbx, %rdi
movl %r15d, %esi
movq %r13, %rdx
movq %r12, %r13
callq 0xb8cd
movq 0x20(%rsp), %rcx
andq $0x0, 0x50(%rcx)
cmpq $0x0, (%rcx)
je 0xb21c
movq %rbx, %rdi
callq 0xb973
movq 0x98(%rbx), %rsi
movq %rbx, %rdi
movl %r14d, %edx
movq %r13, %rcx
callq 0x75bd
cmpl $0x3, %r14d
jne 0xb83a
movq %r13, 0x38(%rsp)
movslq (%r13), %rax
movq 0x20(%rsp), %rcx
addq %rax, 0x50(%rcx)
movq 0x40(%rbx), %rdi
movl 0x48(%rbx), %esi
leaq 0xc0(%rsp), %rdx
movl %ebp, %ecx
callq 0xac2c
movl %eax, %ebp
movl %eax, %r12d
testl %eax, %eax
jle 0xb4e4
leaq 0x9787(%rip), %rsi      # 0x149fe
leaq 0xc0(%rsp), %rdi
callq 0xb03c
testq %rax, %rax
je 0xb4f2
leaq 0x977c(%rip), %rsi      # 0x14a10
movq %rax, %rdi
callq 0x68d7
testl %eax, %eax
jne 0xb4f2
movl %ebp, 0x44(%rsp)
movq 0x40(%rbx), %r15
addq %r12, %r15
movq 0x48(%rbx), %r13
movq %r12, 0x58(%rsp)
subq %r12, %r13
movq %rbx, %rdi
callq 0xb106
movq %rax, 0x28(%rsp)
movq 0x30(%rax), %rcx
cmpq %r13, %rcx
ja 0xb84c
movq %rcx, %rbp
movq %r15, 0x30(%rsp)
movq %r13, 0x50(%rsp)
movq %rbp, 0x10(%rsp)
leaq (%r15,%rcx), %r14
movq %r13, %rbp
movq %rcx, 0x8(%rsp)
subq %rcx, %rbp
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq %r12, %rbp
je 0xb341
callq 0x3660
movq (%rax), %rax
movzbl (%r14,%r12), %r15d
testb $0x10, 0x1(%rax,%r15,2)
je 0xb344
leal -0x30(%r15), %eax
cmpb $0x9, %al
ja 0xb324
leal -0x30(%r15), %eax
jmp 0xb333
callq 0x3650
movq (%rax), %rax
movl (%rax,%r15,4), %eax
addl $-0x57, %eax
shlq $0x4, %r13
cltq
addq %rax, %r13
incq %r12
jmp 0xb2fc
movq %rbp, %r12
movb $0x1, %dl
testq %r12, %r12
je 0xb3ca
leaq 0x2(%r12), %rax
cmpq %rbp, %rax
ja 0xb3ca
cmpb $0xd, (%r14,%r12)
jne 0xb3ca
cmpb $0xa, 0x1(%r14,%r12)
jne 0xb3ca
movq %rax, %rcx
addq %r13, %rcx
je 0xb3ca
leaq 0x2(%rcx), %r12
cmpq %rbp, %r12
ja 0xb3ca
cmpb $0xd, (%r14,%rcx)
movq 0x10(%rsp), %rbp
jne 0xb3cf
testq %r12, %r12
je 0xb3cf
cmpb $0xa, 0x1(%r14,%rcx)
jne 0xb3cf
addq %rax, %r14
movq 0x30(%rsp), %r15
leaq (%r15,%rbp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x3500
addq %r13, %rbp
movq %rbp, 0xd8(%rsp)
movq 0x8(%rsp), %rcx
addq %r12, %rcx
testq %r13, %r13
movq 0x50(%rsp), %r13
jne 0xb2e2
xorl %edx, %edx
jmp 0xb3d4
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %rcx
movq %rcx, %r13
movq %rbp, %rax
subq %rbp, %r13
movl 0x44(%rsp), %ebp
movq 0x58(%rsp), %r12
jbe 0xb4e4
movq %rcx, %r15
movl %edx, 0x40(%rsp)
movq 0x50(%rsp), %r14
movq %r14, %rcx
subq %r15, %rcx
jb 0xb86b
movq 0x30(%rsp), %rdx
movq %rax, 0x10(%rsp)
leaq (%rdx,%rax), %rdi
movq %rdi, 0x8(%rsp)
leaq (%rdx,%r15), %rsi
negq %r15
movq %rcx, 0x48(%rsp)
movq %rcx, %rdx
callq 0x3500
addq 0x8(%rsp), %r14
addq %r15, %r14
movq %r14, %rdi
movq 0x10(%rsp), %r14
xorl %esi, %esi
movq %r13, %rdx
callq 0x3240
subq %r13, 0x48(%rbx)
movq 0x28(%rsp), %rax
movq %r14, 0x30(%rax)
andb $-0x21, 0xc9(%rbx)
movq 0x98(%rbx), %rsi
pushq $0x66
popq %rdx
leaq 0xc0(%rsp), %rcx
movq %rbx, %rdi
callq 0x75bd
testb $0x20, 0xc9(%rbx)
je 0xb4b7
movq 0x30(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3240
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x3500
subq %r14, 0x48(%rbx)
andq $0x0, 0xd8(%rsp)
movq 0x28(%rsp), %rax
andq $0x0, 0x30(%rax)
cmpb $0x0, 0x40(%rsp)
jne 0xb4e4
movq 0x28(%rsp), %rax
movq 0x48(%rsp), %rcx
addq 0x30(%rax), %rcx
addq 0xd0(%rsp), %rcx
subq 0xc0(%rsp), %rcx
movq %rcx, 0xc8(%rsp)
testl %ebp, %ebp
js 0xb5d6
je 0xb5c8
cmpq $0x0, 0x10(%rbx)
je 0xb565
leaq 0x954d(%rip), %rsi      # 0x14a4d
leaq 0xc0(%rsp), %rdi
callq 0xb03c
testq %rax, %rax
je 0xb762
movq %rax, %r15
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x6845
leaq 0x5f2(%rip), %rax       # 0xbb1f
movq %rax, 0x80(%rbx)
orb $0x1, 0xc9(%rbx)
movq 0x10(%rbx), %rdi
leaq 0xf0(%rsp), %rsi
callq 0xb09e
testq %rax, %rax
je 0xb60a
movq 0x28(%rax), %rsi
movq %rsi, 0x98(%rbx)
jmp 0xb611
leaq 0x94cc(%rip), %rsi      # 0x14a38
leaq 0xc0(%rsp), %rdi
callq 0xb03c
testq %rax, %rax
je 0xb757
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x6845
leaq 0x589(%rip), %rax       # 0xbb1f
movq %rax, 0x80(%rbx)
orb $0x1, 0xc9(%rbx)
movq 0x98(%rbx), %rsi
pushq $0x70
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x75bd
pushq $0x3
popq %rsi
movq %rbx, %rdi
movq 0x38(%rsp), %rdx
jmp 0xb74d
cmpq $0x2000, 0x48(%rbx)     # imm = 0x2000
jb 0xb83a
leaq 0x943b(%rip), %rsi      # 0x14a18
leaq 0x8fca(%rip), %rdx      # 0x145ae
pushq $0x4
popq %rdi
callq 0x4227
testl %eax, %eax
je 0xb5fe
leaq 0x9431(%rip), %rdi      # 0x14a28
xorl %eax, %eax
callq 0x430a
orb $0x8, 0xc9(%rbx)
jmp 0xb83a
movq 0x98(%rbx), %rsi
movq 0x38(%rsp), %r12
pushq $0x6f
popq %rdx
leaq 0xc0(%rsp), %rcx
movq %rbx, %rdi
callq 0x75bd
testb $0xc, 0xc9(%rbx)
jne 0xb83a
cmpq $0x0, 0x60(%rbx)
jne 0xb730
movq (%r15), %rax
leaq 0x90(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xaee5(%rip), %rax      # 0x1653b
movq %rax, 0x8(%rsi)
movq 0x8(%r15), %rax
leaq 0x80(%rsp), %rdx
movq %rax, (%rdx)
movq $0x24, 0x8(%rdx)
pushq $0x2
popq %rdi
leaq 0xa0(%rsp), %r14
movq %r14, %rcx
callq 0xfc02
pushq $0x14
popq %rsi
leaq 0x60(%rsp), %rdx
movq %r14, %rdi
callq 0x4509
leaq 0x995b(%rip), %rsi      # 0x14ff6
leaq 0xaebe(%rip), %rdx      # 0x16560
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
leaq 0xaef9(%rip), %rsi      # 0x165ac
leaq 0xc0(%rsp), %rdi
callq 0xb03c
testq %rax, %rax
je 0xb6dc
movl 0x8(%rax), %edx
movq (%rax), %rcx
leaq 0x99bf(%rip), %rsi      # 0x15091
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
leaq 0xaee0(%rip), %rsi      # 0x165c3
leaq 0xa0e2(%rip), %rcx      # 0x157cc
leaq 0x60(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
leaq 0xaede(%rip), %rsi      # 0x165de
leaq 0x8ea7(%rip), %rdx      # 0x145ae
pushq $0x4
popq %rdi
callq 0x4227
testl %eax, %eax
je 0xb730
movl 0x8(%r15), %edx
movq (%r15), %rcx
leaq 0xaecd(%rip), %rdi      # 0x165ee
leaq 0x60(%rsp), %r8
movq %rbx, %rsi
xorl %eax, %eax
callq 0x430a
movq 0x98(%rbx), %rsi
pushq $0x70
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x75bd
pushq $0x3
popq %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0xbb1f
jmp 0xb83a
cmpq $0x0, 0x10(%rbx)
jne 0xb4f9
movq 0xc8(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq 0x50(%rcx), %rax
jbe 0xb78c
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0xb88a
jmp 0xb83a
xorl %r14d, %r14d
cmpq $0x1, 0x10(%rbx)
adcl $0x64, %r14d
leaq 0x28(%rbx), %rdi
leaq 0x60(%rsp), %rsi
pushq $0x20
popq %rdx
pushq $0x3
popq %rcx
callq 0xa13d
leaq 0x9265(%rip), %rsi      # 0x14a18
leaq 0x8df4(%rip), %rdx      # 0x145ae
pushq $0x4
popq %rdi
callq 0x4227
testl %eax, %eax
je 0xb7ff
movl 0xe8(%rsp), %ecx
movq 0xe0(%rsp), %r8
movl 0xf8(%rsp), %r9d
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x926f(%rip), %rdi      # 0x14a5f
leaq 0x60(%rsp), %rdx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x430a
leaq 0xc0(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0xb88a
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0xb8cd
movq 0x8(%r15), %rsi
movq 0x18(%rsp), %rdi
callq 0x6845
movq 0x20(%rsp), %rax
andq $0x0, 0x50(%rax)
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9c48(%rip), %rdi      # 0x1549b
leaq 0x8d54(%rip), %rsi      # 0x145ae
leaq 0x9c4b(%rip), %rcx      # 0x154ac
movl $0x17d2, %edx           # imm = 0x17D2
callq 0x3220
leaq 0x9c92(%rip), %rdi      # 0x15504
leaq 0x8d35(%rip), %rsi      # 0x145ae
leaq 0x9c2c(%rip), %rcx      # 0x154ac
movl $0x17e6, %edx           # imm = 0x17E6
callq 0x3220