// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x610, %rsp            # imm = 0x610
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x590(%rbp), %rax
movq %rax, -0x598(%rbp)
movq -0x8(%rbp), %rdi
callq 0x109b0
movq %rax, -0x5a0(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x5a8(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x5b0(%rbp)
cmpl $0x5, -0xc(%rbp)
jne 0x10b9d
movq -0x5a0(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x10ab8
movq -0x5a0(%rbp), %rax
movq 0x20(%rax), %rax
movq $0x0, 0xa0(%rax)
movq -0x5a0(%rbp), %rax
movq 0x20(%rax), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
movq -0x5a8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x10b8e
movq -0x5a8(%rbp), %rax
movq (%rax), %rdi
movq -0x5a8(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %esi
movq -0x598(%rbp), %rdx
movl -0x5b0(%rbp), %ecx
callq 0x10150
movl %eax, -0x5ac(%rbp)
cmpl $0x0, %eax
jle 0x10b8e
movl -0x5b0(%rbp), %edx
movl $0x65, %eax
movl $0x64, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x5bc(%rbp)
movq -0x5a8(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x598(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x5a8(%rbp), %rax
movq (%rax), %rcx
movq -0x5a8(%rbp), %rax
addq 0x8(%rax), %rcx
movq -0x598(%rbp), %rax
movq 0x10(%rax), %rax
subq %rax, %rcx
movq -0x598(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x598(%rbp), %rsi
movl -0x5ac(%rbp), %edx
callq 0x11060
movq -0x8(%rbp), %rdi
movl -0x5bc(%rbp), %esi
movq -0x598(%rbp), %rdx
callq 0x11100
movq -0x5a0(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x5a0(%rbp), %rax
cmpq $0x0, (%rax)
je 0x10bb3
movq -0x8(%rbp), %rdi
callq 0x11200
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdx
movl -0xc(%rbp), %ecx
movq -0x18(%rbp), %r8
callq 0xa8e0
cmpl $0x3, -0xc(%rbp)
jne 0x11053
movq -0x18(%rbp), %rax
movslq (%rax), %rcx
movq -0x5a0(%rbp), %rax
addq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x5a8(%rbp), %rax
movq (%rax), %rdi
movq -0x5a8(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %esi
movq -0x598(%rbp), %rdx
movl -0x5b0(%rbp), %ecx
callq 0x10150
movl %eax, -0x5ac(%rbp)
cmpl $0x0, -0x5ac(%rbp)
jle 0x10ca2
movq -0x598(%rbp), %rdi
leaq 0xed5b(%rip), %rsi      # 0x1f99a
callq 0x10830
movq %rax, -0x5c8(%rbp)
cmpq $0x0, %rax
je 0x10ca2
movq -0x5c8(%rbp), %rdi
leaq 0xed4d(%rip), %rsi      # 0x1f9ac
callq 0x8b60
cmpl $0x0, %eax
jne 0x10ca2
movq -0x8(%rbp), %rdi
movq -0x598(%rbp), %rsi
movq -0x5a8(%rbp), %rax
movq (%rax), %rdx
movslq -0x5ac(%rbp), %rax
addq %rax, %rdx
movq -0x5a8(%rbp), %rax
movq 0x8(%rax), %rcx
movslq -0x5ac(%rbp), %rax
subq %rax, %rcx
callq 0x11570
cmpl $0x0, -0x5ac(%rbp)
jl 0x10cc5
cmpl $0x0, -0x5ac(%rbp)
jne 0x10d12
movq -0x5a8(%rbp), %rax
cmpq $0x2000, 0x8(%rax)      # imm = 0x2000
jb 0x10d12
jmp 0x10cc7
movl $0x4, %edi
leaq 0xece1(%rip), %rsi      # 0x1f9b4
leaq 0xe870(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x10cf2
leaq 0xecd9(%rip), %rdi      # 0x1f9c4
movb $0x0, %al
callq 0x47a0
jmp 0x10cf4
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
jmp 0x11051
cmpl $0x0, -0x5ac(%rbp)
jne 0x10d20
jmp 0x1104f
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x10dc6
movq -0x598(%rbp), %rdi
leaq 0xec97(%rip), %rsi      # 0x1f9d4
callq 0x10830
cmpq $0x0, %rax
je 0x10dc6
movq -0x5a8(%rbp), %rdi
movslq -0x5ac(%rbp), %rsi
callq 0x89f0
movq -0x8(%rbp), %rax
leaq 0xa8a(%rip), %rcx       # 0x117f0
movq %rcx, 0x80(%rax)
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x100, %rcx            # imm = 0x100
movq %rcx, 0xc8(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdx
movl $0x70, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0xa8e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x3, %esi
callq 0x117f0
jmp 0x1104d
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x10f1a
movq -0x598(%rbp), %rdi
leaq 0xec06(%rip), %rsi      # 0x1f9e9
callq 0x10830
movq %rax, -0x5b8(%rbp)
cmpq $0x0, %rax
je 0x10f1a
movq -0x5a8(%rbp), %rdi
movslq -0x5ac(%rbp), %rsi
callq 0x89f0
movq -0x8(%rbp), %rax
leaq 0x9d9(%rip), %rcx       # 0x117f0
movq %rcx, 0x80(%rax)
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x100, %rcx            # imm = 0x100
movq %rcx, 0xc8(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x598(%rbp), %rsi
addq $0x30, %rsi
callq 0x10900
movq %rax, -0x5d0(%rbp)
cmpq $0x0, -0x5d0(%rbp)
je 0x10e76
movq -0x5d0(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdx
movq -0x598(%rbp), %r8
movl $0x6f, %ecx
callq 0xa8e0
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0xc00, %rax            # imm = 0xC00
cmpq $0x0, %rax
jne 0x10f15
movq -0x8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x10eda
movq -0x8(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
movq -0x598(%rbp), %rdx
callq 0x118b0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdx
movl $0x70, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0xa8e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x3, %esi
callq 0x117f0
jmp 0x1104b
movq -0x598(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x5a0(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jbe 0x10f4d
movq -0x8(%rbp), %rdi
movq -0x598(%rbp), %rsi
movl -0x5ac(%rbp), %edx
callq 0x11060
jmp 0x11049
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x65, %eax
movl $0x64, %ecx
cmpq $0x0, %rdx
cmovnel %ecx, %eax
movl %eax, -0x5d4(%rbp)
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
leaq -0x600(%rbp), %rsi
movl $0x20, %edx
movl $0x3, %ecx
callq 0xeec0
movl $0x4, %edi
leaq 0xea1e(%rip), %rsi      # 0x1f9b4
leaq 0xe5ad(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x10ff5
movq -0x8(%rbp), %rsi
leaq -0x600(%rbp), %rdx
movq -0x598(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %ecx
movq -0x598(%rbp), %rax
movq 0x20(%rax), %r8
movq -0x598(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %r9d
movq -0x598(%rbp), %rax
movq 0x30(%rax), %rax
leaq 0xea11(%rip), %rdi      # 0x1f9fb
movq %rax, (%rsp)
movb $0x0, %al
callq 0x47a0
jmp 0x10ff7
movq -0x8(%rbp), %rdi
movq -0x598(%rbp), %rsi
movl -0x5ac(%rbp), %edx
callq 0x11060
movq -0x8(%rbp), %rdi
movl -0x5d4(%rbp), %esi
movq -0x598(%rbp), %rdx
callq 0x11100
movq -0x5a8(%rbp), %rdi
movq -0x598(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x89f0
movq -0x5a0(%rbp), %rax
movq $0x0, 0x50(%rax)
jmp 0x1104b
jmp 0x1104d
jmp 0x1104f
jmp 0x11051
jmp 0x11053
addq $0x610, %rsp            # imm = 0x610
popq %rbp
retq
nopl (%rax)