// File: /workspace/github_c_makefile_star2_O2/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 0x5ab37
movq %rdx, %r15
movq %rdi, %rbx
movq 0x80(%rdi), %r14
movl %esi, %eax
leaq 0xd992(%rip), %rcx      # 0x68060
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r14, %r14
je 0x5a9fb
pushq $0x64
popq %rsi
movq %r14, %rdi
callq 0x5d354
cmpl $0x1, %eax
jne 0x5a716
movq (%r14), %rax
testb $0x10, 0xa2(%rax)
je 0x5a704
movq %r14, %rdi
callq 0x5d5a0
movq %r14, %rdi
callq 0x59b05
movq (%r14), %rax
orl $0x1, 0xa0(%rax)
cmpl $0x1, 0x2b8(%r14)
jne 0x5a728
movq %r14, %rdi
callq 0x60b7b
movq (%r15), %r15
cmpl $0x0, 0x280(%r14)
je 0x5a9fe
movq (%r14), %rax
movq %r15, %rcx
subq 0x90(%rax), %rcx
cmpq $0x6, %rcx
jl 0x5a9fe
addq $0x8, %r14
leaq 0xcef7(%rip), %rdx      # 0x67652
pushq $0x9
popq %rsi
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x5974d
jmp 0x5a9fe
testq %r14, %r14
je 0x5a787
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6091f
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6091f
movl (%r15), %eax
movl %eax, 0x284(%r14)
testl %eax, %eax
jne 0x5ab30
testb $-0x80, 0xa2(%rbx)
jne 0x5ab37
pushq $0x65
popq %rsi
movq %r14, %rdi
callq 0x5d354
testl %eax, %eax
jne 0x5ab37
jmp 0x5ab30
movq 0x18(%rbx), %rax
movq %rax, -0x50(%rbp)
movq 0x38(%rax), %r14
movl 0x4(%r15), %eax
bswapl %eax
movl %eax, -0x44(%rbp)
xorl %eax, %eax
testq %r14, %r14
setne %al
leal 0x2b(,%rax,2), %r13d
movq %r14, %rdi
leaq -0x338(%rbp), %rsi
xorl %edx, %edx
callq 0x5c8fa
testq %rax, %rax
je 0x5aadd
movq %rax, %r14
movq -0x338(%rbp), %r12
movzbl (%r12), %r15d
cmpl $0x2d, %r15d
je 0x5a81c
cmpl $0x2b, %r15d
jne 0x5aae3
incq %r12
movl $0x20, -0x2c(%rbp)
movq %r12, %rdi
leaq 0xf192(%rip), %rsi      # 0x699c2
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 0xc110
popq %rcx
popq %rdx
cmpl $0x5, %eax
je 0x5a890
subq $0x8, %rsp
movq %r12, %rdi
leaq 0xf16b(%rip), %rsi      # 0x699d3
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 0xc110
popq %rcx
popq %rdx
cmpl $0x4, %eax
jne 0x5aae3
movl -0x3c(%rbp), %eax
cmpl $0xff, %eax
ja 0x5aae3
movl -0x38(%rbp), %esi
cmpl $0xff, %esi
ja 0x5aae3
movl -0x34(%rbp), %edx
cmpl $0xff, %edx
ja 0x5aae3
movl -0x30(%rbp), %edi
cmpl $0xff, %edi
ja 0x5aae3
movl -0x2c(%rbp), %r8d
cmpl $0x21, %r8d
jae 0x5aae3
cmpl $0x0, -0x40(%rbp)
je 0x5aae3
movsbl %r15b, %r9d
movl %r8d, %ecx
negb %cl
pushq $-0x1
popq %r10
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
andl -0x44(%rbp), %r10d
cmpl %r10d, %edx
cmovel %r9d, %r13d
jmp 0x5a7e3
testq %r14, %r14
je 0x5a928
movslq (%r15), %rax
addq %rax, 0x2d0(%r14)
movl 0xa0(%rbx), %eax
btl $0x8, %eax
jb 0x5a9ae
cmpq $0x0, 0x10(%rbx)
je 0x5aa7e
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5ddba
andq $0x0, 0x80(%rbx)
testb $-0x40, 0xa2(%rbx)
je 0x5aa30
testq %r14, %r14
je 0x5ab37
movq (%r14), %rax
testq %rax, %rax
je 0x5ab37
movl 0xa0(%rax), %ecx
andl $-0x3, %ecx
cmpq $0x0, 0x60(%rax)
pushq $0x10
popq %rdx
pushq $0x1
popq %rsi
cmovel %edx, %esi
orl %ecx, %esi
movl %esi, 0xa0(%rax)
andq $0x0, 0x2b0(%r14)
jmp 0x5ab37
leaq -0x330(%rbp), %r14
movl $0x2e0, %edx            # imm = 0x2E0
movq %r14, %rdi
xorl %esi, %esi
callq 0xc370
movq %rbx, -0x8(%r14)
movq 0x18(%rbx), %rax
movq %rax, 0x2a0(%r14)
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rsi
movq %r14, %rdx
callq 0x5f251
movq %rax, 0x2d8(%r14)
leaq -0x338(%rbp), %rdi
callq 0x5ddba
jmp 0x5ab37
movq (%r15), %r15
cmpq $0x0, 0x10(%rbx)
je 0x5ab37
movl $0x12c, %eax            # imm = 0x12C
addq 0x90(%rbx), %rax
cmpq %r15, %rax
jge 0x5ab37
pushq $0x5
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5a69b
jmp 0x5ab30
testq %r14, %r14
je 0x5ab37
cmpl $0x5, 0x2b8(%r14)
jne 0x5aa52
cmpq $0x0, 0x48(%rbx)
je 0x5aa52
movq %r14, %rdi
callq 0x60af2
pushq $0x6a
popq %rsi
movq %r14, %rdi
callq 0x5d354
movq %r14, %rdi
callq 0x59b05
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 0x5ab49
btl $0x17, %eax
jb 0x5ab62
movq (%r14), %rbx
movq %r14, %rdi
callq 0x5e4c0
cmpq $0x0, 0x2e0(%r14)
js 0x5aac4
sete %cl
movq 0x48(%rbx), %rax
cmpq $0x4001, %rax           # imm = 0x4001
setae %dl
testb %dl, %cl
jne 0x5aac4
cmpq 0x2d8(%r14), %rax
jl 0x5ab37
movq %r14, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x60af2
cmpl $0x2b, %r13d
jne 0x5ab30
pushq $0x1
popq %rdi
movl $0x2e8, %esi            # imm = 0x2E8
callq 0xc9a0
testq %rax, %rax
je 0x5ab30
movq %rax, 0x80(%rbx)
movq %rbx, (%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x2a8(%rax)
movq 0x18(%rbx), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, 0x290(%rax)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6091f
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6091f
jmp 0x5ab37
orb $0x10, 0xa0(%rbx)
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x60997
testq %r14, %r14
je 0x5ab37
movq (%r14), %rsi
movq %rbx, %rdi
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5f220