// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: mg_ev_handler
static void mg_ev_handler(struct ns_connection *nc, int ev, void *p) {
  struct connection *conn = (struct connection *) nc->user_data;

  // Send NS event to the handler. Note that call_user won't send an event
  // if conn == NULL. Therefore, repeat this for NS_ACCEPT event as well.
  send_ns_event(nc, ev, p);

  switch (ev) {
    case NS_ACCEPT:
      on_accept(nc, (union socket_address *) p);
      send_ns_event(nc, ev, p);
      break;

    case NS_CONNECT:
      if (nc->user_data != NULL) {
        set_ips(nc, 1);
        set_ips(nc, 0);
      }
      conn->mg_conn.status_code = * (int *) p;
      if (conn->mg_conn.status_code != 0 ||
          (!(nc->flags & MG_PROXY_CONN) &&
           call_user(conn, MG_CONNECT) == MG_FALSE)) {
        nc->flags |= NSF_CLOSE_IMMEDIATELY;
      }
      break;

    case NS_RECV:
      if (conn != NULL) {
        conn->num_bytes_recv += * (int *) p;
      }

      if (nc->flags & NSF_UDP) {
        process_udp(nc);
      } else if (nc->listener != NULL) {
        on_recv_data(conn);
#ifndef MONGOOSE_NO_CGI
      } else if (nc->flags & MG_CGI_CONN) {
        on_cgi_data(nc);
#endif
      } else if (nc->flags & MG_PROXY_CONN) {
        if (conn != NULL) {
          ns_forward(nc, conn->ns_conn);
        }
      } else {
        process_response(conn);
      }
      break;

    case NS_SEND:
      break;

    case NS_CLOSE:
      nc->user_data = NULL;
      if (nc->flags & (MG_CGI_CONN | MG_PROXY_CONN)) {
        DBG(("%p %p closing cgi/proxy conn", conn, nc));
        if (conn && conn->ns_conn) {
          conn->ns_conn->flags &= ~NSF_BUFFER_BUT_DONT_SEND;
          conn->ns_conn->flags |= conn->ns_conn->send_iobuf.len > 0 ?
            NSF_FINISHED_SENDING_DATA : NSF_CLOSE_IMMEDIATELY;
          conn->endpoint.nc = NULL;
        }
      } else if (conn != NULL) {
        DBG(("%p %p %d closing", conn, nc, conn->endpoint_type));

        if (conn->endpoint_type == EP_CLIENT && nc->recv_iobuf.len > 0) {
          call_http_client_handler(conn);
        }

        call_user(conn, MG_CLOSE);
        close_local_endpoint(conn);
        conn->ns_conn = NULL;
        NS_FREE(conn);
      }
      break;

    case NS_POLL:
      if (conn != NULL) {
        if (call_user(conn, MG_POLL) == MG_TRUE) {
          if (conn->ns_conn->flags & MG_HEADERS_SENT) {
            write_terminating_chunk(conn);
          }
          close_local_endpoint(conn);
          /*
           * MG_POLL callback returned MG_TRUE,
           * i.e. data is sent, set corresponding flag
           */
          conn->ns_conn->flags |= NSF_FINISHED_SENDING_DATA;
        }

        if (conn->endpoint_type == EP_FILE) {
          transfer_file_data(conn);
        }
      }

      // Expire idle connections
      {
        time_t current_time = * (time_t *) p;

        if (conn != NULL && conn->mg_conn.is_websocket) {
          ping_idle_websocket_connection(conn, current_time);
        }

        if (nc->listener != NULL &&
            nc->last_io_time + MONGOOSE_IDLE_TIMEOUT_SECONDS < current_time) {
          mg_ev_handler(nc, NS_CLOSE, NULL);
          nc->flags |= NSF_CLOSE_IMMEDIATELY;
        }
      }
      break;

    default:
      break;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x80(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x18(%rbp), %rdx
callq 0xbdcd0
movl -0xc(%rbp), %eax
movq %rax, -0x30(%rbp)
subq $0x5, %rax
ja 0xb3790
movq -0x30(%rbp), %rax
leaq 0x1356a(%rip), %rcx     # 0xc69c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xbdcf0
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x18(%rbp), %rdx
callq 0xbdcd0
jmp 0xb3792
movq -0x8(%rbp), %rax
cmpq $0x0, 0x80(%rax)
je 0xb34b0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0xbddd0
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xbddd0
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x284(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x284(%rax)
jne 0xb34f4
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x800000, %eax         # imm = 0x800000
cmpl $0x0, %eax
jne 0xb3507
movq -0x20(%rbp), %rdi
movl $0x65, %esi
callq 0xb7300
cmpl $0x0, %eax
jne 0xb3507
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %ecx
orl $0x10, %ecx
movl %ecx, 0xa0(%rax)
jmp 0xb3792
cmpq $0x0, -0x20(%rbp)
je 0xb352c
movq -0x18(%rbp), %rax
movslq (%rax), %rcx
movq -0x20(%rbp), %rax
addq 0x2d0(%rax), %rcx
movq %rcx, 0x2d0(%rax)
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0xb354b
movq -0x8(%rbp), %rdi
callq 0xbdec0
jmp 0xb35bc
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xb3561
movq -0x20(%rbp), %rdi
callq 0xb9310
jmp 0xb35ba
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x400000, %eax         # imm = 0x400000
cmpl $0x0, %eax
je 0xb3580
movq -0x8(%rbp), %rdi
callq 0xbdf40
jmp 0xb35b8
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x800000, %eax         # imm = 0x800000
cmpl $0x0, %eax
je 0xb35ad
cmpq $0x0, -0x20(%rbp)
je 0xb35ab
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
callq 0xb9ca0
jmp 0xb35b6
movq -0x20(%rbp), %rdi
callq 0xbe170
jmp 0xb35b8
jmp 0xb35ba
jmp 0xb35bc
jmp 0xb3792
jmp 0xb3792
movq -0x8(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0xc00000, %eax         # imm = 0xC00000
cmpl $0x0, %eax
je 0xb3650
cmpq $0x0, -0x20(%rbp)
je 0xb364e
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
je 0xb364e
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0xa0(%rax), %ecx
andl $-0x3, %ecx
movl %ecx, 0xa0(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x60(%rax), %rdx
movl $0x10, %ecx
movl $0x1, %eax
cmpq $0x0, %rdx
cmoval %eax, %ecx
movq -0x20(%rbp), %rax
movq (%rax), %rax
orl 0xa0(%rax), %ecx
movl %ecx, 0xa0(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x2b0(%rax)
jmp 0xb36a5
cmpq $0x0, -0x20(%rbp)
je 0xb36a3
movq -0x20(%rbp), %rax
cmpl $0x5, 0x2b8(%rax)
jne 0xb3678
movq -0x8(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jbe 0xb3678
movq -0x20(%rbp), %rdi
callq 0xbe1f0
movq -0x20(%rbp), %rdi
movl $0x6a, %esi
callq 0xb7300
movq -0x20(%rbp), %rdi
callq 0xb23e0
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x20(%rbp), %rdi
callq 0xd2c0
jmp 0xb36a5
jmp 0xb3792
cmpq $0x0, -0x20(%rbp)
je 0xb371b
movq -0x20(%rbp), %rdi
movl $0x64, %esi
callq 0xb7300
cmpl $0x1, %eax
jne 0xb3703
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0xa0(%rax), %eax
andl $0x100000, %eax         # imm = 0x100000
cmpl $0x0, %eax
je 0xb36e4
movq -0x20(%rbp), %rdi
callq 0xb79c0
movq -0x20(%rbp), %rdi
callq 0xb23e0
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0xa0(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0xa0(%rax)
movq -0x20(%rbp), %rax
cmpl $0x1, 0x2b8(%rax)
jne 0xb3719
movq -0x20(%rbp), %rdi
callq 0xbe2e0
jmp 0xb371b
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb3747
movq -0x20(%rbp), %rax
cmpl $0x0, 0x280(%rax)
je 0xb3747
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xbe3f0
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xb378e
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
addq $0x12c, %rax            # imm = 0x12C
cmpq -0x28(%rbp), %rax
jge 0xb378e
movq -0x8(%rbp), %rdi
movl $0x5, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0xb3410
movq -0x8(%rbp), %rax
movl 0xa0(%rax), %ecx
orl $0x10, %ecx
movl %ecx, 0xa0(%rax)
jmp 0xb3792
jmp 0xb3792
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)