// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
cmpl $0x5, %esi
ja 0x69cbc
movq %rdx, %r15
movq %rdi, %rbx
movq 0x80(%rdi), %r14
movl %esi, %eax
leaq 0xe8e5(%rip), %rcx      # 0x78080
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r14, %r14
je 0x69bbc
movq 0x2a8(%r14), %rax
testq %rax, %rax
je 0x69801
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x69801
leaq 0x8(%r14), %rdi
movl $0x64, %esi
callq *%rax
cmpl $0x1, %eax
jne 0x69801
movq (%r14), %rdi
testb $0x10, 0xa2(%rdi)
je 0x697ef
leaq 0x10034(%rip), %rsi     # 0x79819
movl $0x5, %edx
callq 0x6661a
movq %r14, %rdi
callq 0x68a64
movq (%r14), %rax
orl $0x1, 0xa0(%rax)
cmpl $0x1, 0x2b8(%r14)
jne 0x69813
movq %r14, %rdi
callq 0x702d6
movq (%r15), %r15
cmpl $0x0, 0x280(%r14)
je 0x69bbf
movq (%r14), %rax
movq %r15, %rcx
subq 0x90(%rax), %rcx
cmpq $0x6, %rcx
jl 0x69bbf
addq $0x8, %r14
leaq 0xde2c(%rip), %rdx      # 0x77672
movq %r14, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x68637
jmp 0x69bbf
testq %r14, %r14
je 0x698da
movl 0x20(%rbx), %edi
leaq -0x340(%rbp), %r12
movl $0x64, %edx
movq %r12, %rsi
movl $0x7, %ecx
callq 0x66ce6
leaq 0x28(%r14), %rdx
leaq 0x88(%r14), %rcx
leaq 0x10174(%rip), %rsi     # 0x79a01
movq %r12, %rdi
xorl %eax, %eax
callq 0xc100
movl 0x20(%rbx), %edi
movq 0x80(%rbx), %r12
leaq -0x340(%rbp), %r13
movl $0x64, %edx
movq %r13, %rsi
movl $0x3, %ecx
callq 0x66ce6
leaq 0x58(%r12), %rdx
addq $0x8a, %r12
movq %r13, %rdi
leaq 0x10131(%rip), %rsi     # 0x79a01
movq %r12, %rcx
xorl %eax, %eax
callq 0xc100
movl (%r15), %eax
movl %eax, 0x284(%r14)
testl %eax, %eax
jne 0x69cb5
testb $-0x80, 0xa2(%rbx)
jne 0x69cbc
movq 0x2a8(%r14), %rax
testq %rax, %rax
je 0x69cb5
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x69cb5
addq $0x8, %r14
movq %r14, %rdi
movl $0x65, %esi
callq *%rax
testl %eax, %eax
jne 0x69cbc
jmp 0x69cb5
movl 0x4(%r15), %r13d
movq 0x18(%rbx), %r12
movq 0x38(%r12), %r15
leaq -0x340(%rbp), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x6bda1
testq %rax, %rax
je 0x69bf3
movq %rax, %r14
movq %r12, -0x50(%rbp)
bswapl %r13d
xorl %eax, %eax
testq %r15, %r15
setne %al
leal 0x2b(,%rax,2), %r15d
movl %r13d, -0x44(%rbp)
movq -0x340(%rbp), %r13
movzbl (%r13), %r12d
cmpl $0x2d, %r12d
je 0x69992
cmpl $0x2b, %r12d
jne 0x69b66
incq %r13
movl $0x20, -0x2c(%rbp)
movq %r13, %rdi
leaq 0x1003c(%rip), %rsi     # 0x799e2
leaq -0x3c(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x34(%rbp), %r8
leaq -0x30(%rbp), %r9
xorl %eax, %eax
leaq -0x40(%rbp), %r10
pushq %r10
leaq -0x2c(%rbp), %r10
pushq %r10
callq 0xc100
addq $0x10, %rsp
cmpl $0x5, %eax
je 0x69a0a
subq $0x8, %rsp
movq %r13, %rdi
leaq 0x10013(%rip), %rsi     # 0x799f3
leaq -0x3c(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x34(%rbp), %r8
leaq -0x30(%rbp), %r9
xorl %eax, %eax
leaq -0x40(%rbp), %r10
pushq %r10
callq 0xc100
addq $0x10, %rsp
cmpl $0x4, %eax
jne 0x69b66
movl -0x3c(%rbp), %eax
cmpl $0xff, %eax
ja 0x69b66
movl -0x38(%rbp), %esi
cmpl $0xff, %esi
ja 0x69b66
movl -0x34(%rbp), %edx
cmpl $0xff, %edx
ja 0x69b66
movl -0x30(%rbp), %edi
cmpl $0xff, %edi
ja 0x69b66
movl -0x2c(%rbp), %r8d
cmpl $0x21, %r8d
jae 0x69b66
cmpl $0x0, -0x40(%rbp)
je 0x69b66
movsbl %r12b, %r9d
movl %r8d, %ecx
negb %cl
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
shll %cl, %r10d
testl %r8d, %r8d
cmovel %r8d, %r10d
shll $0x10, %esi
shll $0x18, %eax
orl %esi, %eax
shll $0x8, %edx
orl %edi, %edx
orl %eax, %edx
movl -0x44(%rbp), %r13d
andl %r13d, %r10d
cmpl %r10d, %edx
cmovel %r9d, %r15d
movq %r14, %rdi
leaq -0x340(%rbp), %rsi
xorl %edx, %edx
callq 0x6bda1
movq %rax, %r14
testq %rax, %rax
jne 0x69976
cmpl $0x2b, %r15d
movq -0x50(%rbp), %r12
je 0x69bfc
jmp 0x69cb5
testq %r14, %r14
je 0x69ad2
movslq (%r15), %rax
addq %rax, 0x2d0(%r14)
movl 0xa0(%rbx), %eax
btl $0x8, %eax
jb 0x69b6f
cmpq $0x0, 0x10(%rbx)
je 0x69d2d
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6d299
movq $0x0, 0x80(%rbx)
testb $-0x40, 0xa2(%rbx)
je 0x69cce
testq %r14, %r14
je 0x69cbc
movq (%r14), %rax
testq %rax, %rax
je 0x69cbc
movl 0xa0(%rax), %ecx
andl $-0x3, %ecx
cmpq $0x0, 0x60(%rax)
movl $0x10, %edx
movl $0x1, %esi
cmovel %edx, %esi
orl %ecx, %esi
movl %esi, 0xa0(%rax)
movq $0x0, 0x2b0(%r14)
jmp 0x69cbc
movq -0x50(%rbp), %r12
jmp 0x69bfc
leaq -0x338(%rbp), %r14
movl $0x2e0, %edx            # imm = 0x2E0
movq %r14, %rdi
xorl %esi, %esi
callq 0xc360
movq %rbx, -0x8(%r14)
movq 0x18(%rbx), %rax
movq %rax, 0x2a0(%r14)
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
movq %r14, %rdx
callq 0x6e3c6
movq %rax, 0x2d8(%r14)
leaq -0x340(%rbp), %rdi
callq 0x6d299
jmp 0x69cbc
movq (%r15), %r15
cmpq $0x0, 0x10(%rbx)
je 0x69cbc
movl $0x12c, %eax            # imm = 0x12C
addq 0x90(%rbx), %rax
cmpq %r15, %rax
jge 0x69cbc
movq %rbx, %rdi
movl $0x5, %esi
xorl %edx, %edx
callq 0x69768
jmp 0x69cb5
testq %r15, %r15
jne 0x69cb5
movl $0x1, %edi
movl $0x2e8, %esi            # imm = 0x2E8
callq 0xc980
testq %rax, %rax
je 0x69cb5
movq %rax, %r14
movq %rax, 0x80(%rbx)
movq %rbx, (%rax)
movq %r12, 0x2a8(%rax)
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
movq %rax, 0x290(%r14)
movl 0x20(%rbx), %edi
leaq -0x340(%rbp), %r15
movl $0x64, %edx
movq %r15, %rsi
movl $0x7, %ecx
callq 0x66ce6
leaq 0x28(%r14), %rdx
addq $0x88, %r14
leaq 0xfd9c(%rip), %r12      # 0x79a01
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0xc100
movl 0x20(%rbx), %edi
movq 0x80(%rbx), %rbx
leaq -0x340(%rbp), %r14
movl $0x64, %edx
movq %r14, %rsi
movl $0x3, %ecx
callq 0x66ce6
leaq 0x58(%rbx), %rdx
addq $0x8a, %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc100
jmp 0x69cbc
orb $0x10, 0xa0(%rbx)
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
je 0x69cbc
cmpl $0x5, 0x2b8(%r14)
jne 0x69cec
cmpq $0x0, 0x48(%rbx)
je 0x69cec
movq %r14, %rdi
callq 0x70215
movq 0x2a8(%r14), %rax
testq %rax, %rax
je 0x69d0c
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x69d0c
leaq 0x8(%r14), %rdi
movl $0x6a, %esi
callq *%rax
movq %r14, %rdi
callq 0x68a64
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc250
btl $0x16, %eax
jb 0x69d52
btl $0x17, %eax
jb 0x69d6b
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x701ce
movq %rbx, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x70056
testq %r14, %r14
je 0x69cbc
movq (%r14), %rdi
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rdx
callq 0x6661a
cmpq $0x0, 0x48(%rbx)
je 0x69cbc
movq $0x0, 0x48(%rbx)
jmp 0x69cbc