// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x5c0, %rsp            # imm = 0x5C0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1b1e2
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
jmp 0x1b400
movl -0xc(%rbp), %eax
movl %eax, -0x5bc(%rbp)
subl $0x3, %eax
je 0x1b206
jmp 0x1b1f2
movl -0x5bc(%rbp), %eax
subl $0x5, %eax
je 0x1b39f
jmp 0x1b400
movq -0x28(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x100000, %rax         # imm = 0x100000
cmpq $0x0, %rax
je 0x1b379
movq -0x8(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %esi
callq 0x10460
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x1b251
jmp 0x1b400
cmpl $0x0, -0x34(%rbp)
jl 0x1b265
movq -0x30(%rbp), %rax
cmpq $0x2000, 0x8(%rax)      # imm = 0x2000
jbe 0x1b298
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
movq -0x28(%rbp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
leaq 0x574b(%rip), %rdx      # 0x209d9
callq 0x11e30
jmp 0x1b360
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rax
addq 0x8(%rax), %rsi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq -0x5b0(%rbp), %rcx
callq 0x10680
leaq -0x5b0(%rbp), %rdi
leaq 0x5713(%rip), %rsi      # 0x209e5
callq 0x10830
cmpq $0x0, %rax
je 0x1b2f8
movq -0x28(%rbp), %rdi
leaq 0x4caa(%rip), %rsi      # 0x1ff92
leaq 0x56ff(%rip), %rdx      # 0x209ee
movb $0x0, %al
callq 0xb2f0
jmp 0x1b35e
leaq -0x5b0(%rbp), %rdi
leaq 0x56fd(%rip), %rsi      # 0x20a03
callq 0x10830
movq %rax, -0x5b8(%rbp)
cmpq $0x0, %rax
je 0x1b343
movq -0x28(%rbp), %rdi
movq -0x5b8(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x5b8(%rbp), %rax
movq (%rax), %rcx
leaq 0x56d0(%rip), %rsi      # 0x20a0a
movb $0x0, %al
callq 0xb2f0
jmp 0x1b35c
movq -0x28(%rbp), %rdi
leaq 0x4c44(%rip), %rsi      # 0x1ff92
leaq 0x56c5(%rip), %rdx      # 0x20a1a
movb $0x0, %al
callq 0xb2f0
jmp 0x1b35e
jmp 0x1b360
movq -0x28(%rbp), %rax
movq 0xc8(%rax), %rcx
andq $-0x100001, %rcx        # imm = 0xFFEFFFFF
movq %rcx, 0xc8(%rax)
movq -0x28(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x100000, %rax         # imm = 0x100000
cmpq $0x0, %rax
jne 0x1b39d
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xd1e0
jmp 0x1b400
jmp 0x1b3a1
movl $0x4, %edi
leaq 0x567f(%rip), %rsi      # 0x20a2c
leaq 0x4196(%rip), %rdx      # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0x1b3d0
movq -0x8(%rbp), %rsi
leaq 0x5675(%rip), %rdi      # 0x20a3e
movb $0x0, %al
callq 0x47a0
jmp 0x1b3d2
movq -0x28(%rbp), %rdi
callq 0x109b0
movq %rax, %rdi
addq $0x20, %rdi
callq 0x19b10
movq -0x28(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x400, %rcx            # imm = 0x400
movq %rcx, 0xc8(%rax)
addq $0x5c0, %rsp            # imm = 0x5C0
popq %rbp
retq
nopl (%rax)