// File: /workspace/github_c_makefile_star2_O1/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 0x6b7d1
movq %rdx, %r15
movq %rdi, %r13
movq 0x80(%rdi), %r14
movl %esi, %eax
leaq 0xecfd(%rip), %rcx      # 0x7a080
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r14, %r14
je 0x6b3f7
movq 0x2a8(%r14), %rax
testq %rax, %rax
je 0x6b3e5
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x6b3e5
leaq 0x8(%r14), %rdi
movl $0x64, %esi
callq *%rax
cmpl $0x1, %eax
jne 0x6b3e5
movq (%r14), %rdi
testb $0x10, 0xa2(%rdi)
je 0x6b3d3
leaq 0x10450(%rip), %rsi     # 0x7b819
movl $0x5, %edx
callq 0x681c0
movq %r14, %rdi
callq 0x6a615
movq (%r14), %rax
orl $0x1, 0xa0(%rax)
cmpl $0x1, 0x2b8(%r14)
jne 0x6b3f7
movq %r14, %rdi
callq 0x7200a
movq (%r15), %rbx
testq %r14, %r14
je 0x6b436
cmpl $0x0, 0x280(%r14)
je 0x6b436
movq (%r14), %rax
movq %rbx, %rcx
subq 0x90(%rax), %rcx
cmpq $0x6, %rcx
jl 0x6b436
addq $0x8, %r14
leaq 0xe240(%rip), %rdx      # 0x79667
movq %r14, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x6a1f9
cmpq $0x0, 0x10(%r13)
je 0x6b7d1
movl $0x12c, %eax            # imm = 0x12C
addq 0x90(%r13), %rax
cmpq %rbx, %rax
jge 0x6b7d1
movq %r13, %rdi
movl $0x5, %esi
xorl %edx, %edx
callq 0x6b350
jmp 0x6b7c9
testq %r14, %r14
je 0x6b486
movq %r13, %rdi
movl $0x1, %esi
callq 0x71d1f
movq %r13, %rdi
xorl %esi, %esi
callq 0x71d1f
movl (%r15), %eax
movl %eax, 0x284(%r14)
testl %eax, %eax
jne 0x6b7c9
testb $-0x80, 0xa2(%r13)
jne 0x6b7d1
testq %r14, %r14
je 0x6b7c9
movq 0x2a8(%r14), %rax
testq %rax, %rax
je 0x6b7c9
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x6b7c9
addq $0x8, %r14
movq %r14, %rdi
movl $0x65, %esi
callq *%rax
testl %eax, %eax
jne 0x6b7d1
jmp 0x6b7c9
movq 0x18(%r13), %rax
movq %rax, -0x58(%rbp)
movq 0x38(%rax), %r12
movl 0x4(%r15), %r15d
testq %r12, %r12
sete %bl
leaq -0x340(%rbp), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x6d92e
testq %rax, %rax
je 0x6b676
movq %rax, %r14
movq %r13, -0x50(%rbp)
bswapl %r15d
movl %r15d, -0x48(%rbp)
xorl %eax, %eax
testq %r12, %r12
setne %al
leal 0x2b(,%rax,2), %r12d
movq -0x340(%rbp), %r13
movzbl (%r13), %ebx
cmpl $0x2d, %ebx
je 0x6b54f
cmpl $0x2b, %ebx
jne 0x6b774
incq %r13
movl $0x20, -0x2c(%rbp)
movq %r13, %rdi
leaq 0x1047f(%rip), %rsi     # 0x7b9e2
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 0x6b5c5
subq $0x8, %rsp
movq %r13, %rdi
leaq 0x10456(%rip), %rsi     # 0x7b9f3
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
movb $0x1, %cl
cmpl $0x4, %eax
jne 0x6b636
movl -0x3c(%rbp), %edi
movb $0x1, %cl
cmpl $0xff, %edi
ja 0x6b636
movl -0x38(%rbp), %edx
cmpl $0xff, %edx
ja 0x6b636
movl -0x34(%rbp), %eax
cmpl $0xff, %eax
ja 0x6b636
movl -0x30(%rbp), %r8d
cmpl $0x100, %r8d            # imm = 0x100
setb %r9b
movl -0x2c(%rbp), %esi
cmpl $0x21, %esi
setb %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x6b636
shll $0x18, %edi
shll $0x10, %edx
orl %edi, %edx
shll $0x8, %eax
orl %r8d, %eax
movl %esi, %ecx
negb %cl
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
shll %cl, %r15d
orl %edx, %eax
testl %esi, %esi
cmovel %esi, %r15d
cmpl $0x0, -0x40(%rbp)
sete %cl
movl %eax, -0x44(%rbp)
testb %cl, %cl
jne 0x6b774
movsbl %bl, %eax
movl %r15d, %ecx
andl -0x48(%rbp), %ecx
cmpl %ecx, -0x44(%rbp)
cmovel %eax, %r12d
movq %r14, %rdi
leaq -0x340(%rbp), %rsi
xorl %edx, %edx
callq 0x6d92e
movq %rax, %r14
testq %rax, %rax
jne 0x6b535
cmpl $0x2b, %r12d
sete %bl
movq -0x50(%rbp), %r13
testb %bl, %bl
jne 0x6b778
jmp 0x6b7c9
testq %r14, %r14
je 0x6b692
movslq (%r15), %rax
addq %rax, 0x2d0(%r14)
movl 0xa0(%r13), %eax
btl $0x8, %eax
jb 0x6b728
cmpq $0x0, 0x10(%r13)
je 0x6b849
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6ee5f
movq $0x0, 0x80(%r13)
testb $-0x40, 0xa2(%r13)
je 0x6b7e3
testq %r14, %r14
je 0x6b7d1
movq (%r14), %rax
testq %rax, %rax
je 0x6b7d1
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 0x6b7d1
leaq -0x340(%rbp), %r14
movl $0x2e8, %edx            # imm = 0x2E8
movq %r14, %rdi
xorl %esi, %esi
callq 0xc360
movq %r13, (%r14)
movq 0x18(%r13), %rax
movq %rax, 0x2a8(%r14)
movq 0x40(%r13), %rdi
movq 0x48(%r13), %rsi
leaq -0x338(%rbp), %rbx
movq %rbx, %rdx
callq 0x70033
movq %rax, 0x2d8(%rbx)
movq %r14, %rdi
callq 0x6ee5f
jmp 0x6b7d1
movq -0x50(%rbp), %r13
movl $0x1, %edi
movl $0x2e8, %esi            # imm = 0x2E8
callq 0xc980
testq %rax, %rax
je 0x6b7c9
movq %rax, 0x80(%r13)
movq %r13, (%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x2a8(%rax)
movq 0x18(%r13), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, 0x290(%rax)
movq %r13, %rdi
movl $0x1, %esi
callq 0x71d1f
movq %r13, %rdi
xorl %esi, %esi
callq 0x71d1f
jmp 0x6b7d1
orb $0x10, 0xa0(%r13)
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
je 0x6b7d1
cmpl $0x5, 0x2b8(%r14)
jne 0x6b801
cmpq $0x0, 0x48(%r13)
je 0x6b801
movq %r14, %rdi
callq 0x71f44
movq 0x2a8(%r14), %rax
testq %rax, %rax
je 0x6b821
movq 0x30(%rax), %rax
testq %rax, %rax
je 0x6b821
leaq 0x8(%r14), %rdi
movl $0x6a, %esi
callq *%rax
movq %r14, %rdi
callq 0x6a615
movq $0x0, (%r14)
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 0x6b86e
btl $0x17, %eax
jb 0x6b887
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x71f01
movq %r13, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x71d99
testq %r14, %r14
je 0x6b7d1
movq (%r14), %rsi
movq %r13, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6ffee