// File: /workspace/github_c_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x578, %rsp            # imm = 0x578
movq %rdi, %r14
movq 0xa0(%rdi), %rbx
testq %rbx, %rbx
je 0x13728
cmpl $0x5, %esi
je 0x13735
cmpl $0x3, %esi
jne 0x1386a
testb $0x10, 0xca(%rbx)
je 0x13841
movq 0x40(%r14), %r12
movq 0x48(%r14), %r15
movq %r12, %rdi
movl %r15d, %esi
callq 0xb849
testl %eax, %eax
je 0x1386a
movl %eax, %ebp
setns %al
cmpq $0x2001, %r15           # imm = 0x2001
setb %cl
testb %al, %cl
jne 0x137c6
orb $0x8, 0xc9(%r14)
leaq 0x32db(%rip), %rdx      # 0x169f1
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xcd85
jmp 0x13836
orb $0x8, 0xc9(%r14)
jmp 0x1386a
leaq 0x3308(%rip), %rsi      # 0x16a44
leaq 0x1e67(%rip), %rdx      # 0x155aa
movl $0x4, %edi
callq 0x44b4
testl %eax, %eax
je 0x13762
leaq 0x32fe(%rip), %rdi      # 0x16a56
movq %r14, %rsi
xorl %eax, %eax
callq 0x45a0
cmpq $0x0, 0x88(%rbx)
jne 0x13790
movl $0x1, %edi
movl $0x58, %esi
callq 0x3300
movq %rax, 0x88(%rbx)
leaq -0xb68(%rip), %rax      # 0x12c21
movq %rax, 0x90(%rbx)
movq 0x88(%rbx), %rax
movq 0x20(%rax), %rcx
testq %rcx, %rcx
je 0x137b2
orb $0x8, 0xc9(%rcx)
movq $0x0, 0xa0(%rcx)
movq $0x0, 0x20(%rax)
orb $0x4, 0xc9(%rbx)
jmp 0x1386a
movq %r15, %rsi
addq %r12, %rsi
movq %rsp, %r13
movq %r12, %rdi
movl %r15d, %edx
movq %r13, %rcx
callq 0xb968
leaq 0x3219(%rip), %rsi      # 0x169fd
movq %r13, %rdi
callq 0xbaa7
testq %rax, %rax
je 0x1380b
leaq 0x27fa(%rip), %rsi      # 0x15ff2
leaq 0x3207(%rip), %rdx      # 0x16a06
movq %rbx, %rdi
xorl %eax, %eax
callq 0x80d3
jmp 0x13836
leaq 0x3209(%rip), %rsi      # 0x16a1b
movq %rsp, %rdi
callq 0xbaa7
testq %rax, %rax
je 0x1387c
movl 0x8(%rax), %edx
movq (%rax), %rcx
leaq 0x31f6(%rip), %rsi      # 0x16a22
movq %rbx, %rdi
xorl %eax, %eax
callq 0x80d3
andb $-0x11, 0xca(%rbx)
testl %ebp, %ebp
je 0x1386a
testb $0x10, 0xca(%rbx)
jne 0x1386a
leaq 0x40(%r14), %r15
movq 0x40(%r14), %rsi
movl 0x48(%r14), %edx
movq %rbx, %rdi
callq 0x808e
movq 0x48(%r14), %rsi
movq %r15, %rdi
callq 0x6bb8
addq $0x578, %rsp            # imm = 0x578
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x276f(%rip), %rsi      # 0x15ff2
leaq 0x31a8(%rip), %rdx      # 0x16a32
jmp 0x137ff