// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_cgi_ev_handler
static void mg_cgi_ev_handler(struct mg_connection *cgi_nc, int ev,
                              void *ev_data MG_UD_ARG(void *user_data)) {
#if !MG_ENABLE_CALLBACK_USERDATA
  void *user_data = cgi_nc->user_data;
#endif
  struct mg_connection *nc = (struct mg_connection *) user_data;
  (void) ev_data;

  if (nc == NULL) {
    /* The corresponding network connection was closed. */
    cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
    return;
  }

  switch (ev) {
    case MG_EV_RECV:
      /*
       * CGI script does not output reply line, like "HTTP/1.1 CODE XXXXX\n"
       * It outputs headers, then body. Headers might include "Status"
       * header, which changes CODE, and it might include "Location" header
       * which changes CODE to 302.
       *
       * Therefore we do not send the output from the CGI script to the user
       * until all CGI headers are received.
       *
       * Here we parse the output from the CGI script, and if all headers has
       * been received, send appropriate reply line, and forward all
       * received headers to the client.
       */
      if (nc->flags & MG_F_HTTP_CGI_PARSE_HEADERS) {
        struct mbuf *io = &cgi_nc->recv_mbuf;
        int len = mg_http_get_request_len(io->buf, io->len);

        if (len == 0) break;
        if (len < 0 || io->len > MG_MAX_HTTP_REQUEST_SIZE) {
          cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
          mg_http_send_error(nc, 500, "Bad headers");
        } else {
          struct http_message hm;
          struct mg_str *h;
          mg_http_parse_headers(io->buf, io->buf + io->len, io->len, &hm);
          if (mg_get_http_header(&hm, "Location") != NULL) {
            mg_printf(nc, "%s", "HTTP/1.1 302 Moved\r\n");
          } else if ((h = mg_get_http_header(&hm, "Status")) != NULL) {
            mg_printf(nc, "HTTP/1.1 %.*s\r\n", (int) h->len, h->p);
          } else {
            mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\n");
          }
        }
        nc->flags &= ~MG_F_HTTP_CGI_PARSE_HEADERS;
      }
      if (!(nc->flags & MG_F_HTTP_CGI_PARSE_HEADERS)) {
        mg_forward(cgi_nc, nc);
      }
      break;
    case MG_EV_CLOSE:
      DBG(("%p CLOSE", cgi_nc));
      mg_http_free_proto_data_cgi(&mg_http_get_proto_data(nc)->cgi);
      nc->flags |= MG_F_SEND_AND_CLOSE;
      break;
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x580, %rsp            # imm = 0x580
movq %rdi, %r14
movq 0xa0(%rdi), %rbx
testq %rbx, %rbx
je 0x13c26
cmpl $0x5, %esi
je 0x13c33
cmpl $0x3, %esi
jne 0x13d59
movq 0xc8(%rbx), %rax
btl $0x14, %eax
jae 0x13d48
movq 0x40(%r14), %r12
movq 0x48(%r14), %r15
movq %r12, %rdi
movl %r15d, %esi
callq 0xb664
testl %eax, %eax
je 0x13d59
setns %al
cmpq $0x2001, %r15           # imm = 0x2001
setb %cl
testb %al, %cl
jne 0x13cbf
orb $0x8, 0xc9(%r14)
leaq 0x2dfb(%rip), %rdx      # 0x16a0f
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xcb3c
jmp 0x13d33
orb $0x8, 0xc9(%r14)
jmp 0x13d59
leaq 0x2e28(%rip), %rsi      # 0x16a62
leaq 0x198d(%rip), %rdx      # 0x155ce
movl $0x4, %edi
callq 0x4447
testl %eax, %eax
je 0x13c60
leaq 0x2e1e(%rip), %rdi      # 0x16a74
movq %r14, %rsi
xorl %eax, %eax
callq 0x4539
movq 0x88(%rbx), %rax
testq %rax, %rax
jne 0x13c90
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%rbx)
leaq -0xb8b(%rip), %rcx      # 0x130fe
movq %rcx, 0x90(%rbx)
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x13cab
orb $0x8, 0xc9(%rcx)
movq $0x0, 0xa0(%rcx)
movq $0x0, 0x20(%rax)
orb $0x4, 0xc9(%rbx)
jmp 0x13d59
movq %r15, %rsi
addq %r12, %rsi
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movl %r15d, %edx
movq %r13, %rcx
callq 0xb780
leaq 0x2d3c(%rip), %rsi      # 0x16a1b
movq %r13, %rdi
callq 0xb896
testq %rax, %rax
je 0x13d06
leaq 0x2320(%rip), %rsi      # 0x16013
leaq 0x2d2a(%rip), %rdx      # 0x16a24
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7ff5
jmp 0x13d33
leaq 0x2d2c(%rip), %rsi      # 0x16a39
leaq 0x8(%rsp), %rdi
callq 0xb896
testq %rax, %rax
je 0x13d6a
movl 0x8(%rax), %edx
movq (%rax), %rcx
leaq 0x2d17(%rip), %rsi      # 0x16a40
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7ff5
movq $-0x100001, %rax        # imm = 0xFFEFFFFF
andq 0xc8(%rbx), %rax
movq %rax, 0xc8(%rbx)
btl $0x14, %eax
jb 0x13d59
movq %r14, %rdi
movq %rbx, %rsi
callq 0x955a
addq $0x580, %rsp            # imm = 0x580
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x22a2(%rip), %rsi      # 0x16013
leaq 0x2cd8(%rip), %rdx      # 0x16a50
jmp 0x13cfa