// File: /workspace/github_c_makefile_star2_O2/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 0x12122
cmpl $0x5, %esi
je 0x1212f
cmpl $0x3, %esi
jne 0x12211
movq 0xc8(%rbx), %rax
btl $0x14, %eax
jae 0x12200
movq 0x40(%r14), %r12
movq 0x48(%r14), %r15
movq %r12, %rdi
movl %r15d, %esi
callq 0xae20
testl %eax, %eax
je 0x12211
setns %al
cmpq $0x2001, %r15           # imm = 0x2001
setb %cl
testb %al, %cl
jne 0x12177
orb $0x8, 0xc9(%r14)
leaq 0x38e2(%rip), %rdx      # 0x159f2
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc060
jmp 0x121eb
orb $0x8, 0xc9(%r14)
jmp 0x12211
leaq 0x390f(%rip), %rsi      # 0x15a45
leaq 0x2471(%rip), %rdx      # 0x145ae
pushq $0x4
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x1215a
leaq 0x3907(%rip), %rdi      # 0x15a57
movq %r14, %rsi
xorl %eax, %eax
callq 0x430a
movq %rbx, %rdi
callq 0xb106
leaq 0x20(%rax), %rdi
callq 0x11e14
orb $0x4, 0xc9(%rbx)
jmp 0x12211
movq %r15, %rsi
addq %r12, %rsi
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movl %r15d, %edx
movq %r13, %rcx
callq 0xaf33
leaq 0x3867(%rip), %rsi      # 0x159fe
movq %r13, %rdi
callq 0xb03c
testq %rax, %rax
je 0x121be
leaq 0x2e4b(%rip), %rsi      # 0x14ff6
leaq 0x3855(%rip), %rdx      # 0x15a07
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
jmp 0x121eb
leaq 0x3857(%rip), %rsi      # 0x15a1c
leaq 0x8(%rsp), %rdi
callq 0xb03c
testq %rax, %rax
je 0x12222
movl 0x8(%rax), %edx
movq (%rax), %rcx
leaq 0x3842(%rip), %rsi      # 0x15a23
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
movq $-0x100001, %rax        # imm = 0xFFEFFFFF
andq 0xc8(%rbx), %rax
movq %rax, 0xc8(%rbx)
btl $0x14, %eax
jb 0x12211
movq %r14, %rdi
movq %rbx, %rsi
callq 0x904b
addq $0x580, %rsp            # imm = 0x580
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x2dcd(%rip), %rsi      # 0x14ff6
leaq 0x3803(%rip), %rdx      # 0x15a33
jmp 0x121b2