// File: xocoder[P]m_kcptun/vendor/m_net/src/mnet_core.c
// Function: evt_add(s_mchann*, int)
static int
_evt_add(chann_t *n, int set) {
   mnet_t *ss = _gmnet();
   struct s_event *chg = &ss->chg;
   if ( _evt_check_expand(chg) ) {
      mevent_t *kev = chg->array;
      memset(kev, 0, sizeof(mevent_t));
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
      kev->ident = n->fd;
      if (set == MNET_SET_READ) {
         kev->filter = EVFILT_READ;
      } else if (set == MNET_SET_WRITE) {
         kev->filter = EVFILT_WRITE;
      }
      kev->flags = EV_ADD | EV_EOF;
      kev->udata = (void*)n;
      if (kevent(ss->kq, chg->array, 1, NULL, 0, NULL) < 0) {
         mm_log(n, MNET_LOG_ERR, "kq fail to add fd:%d filter %x, errno %d:%s\n",
                n->fd, kev->filter, errno, strerror(errno));
         return 0;
      }
#else
      uint32_t events = 0;
      kev->data.ptr = (void*)n;
      if (set == MNET_SET_READ) {
         events = n->epoll_events | EPOLLIN | EPOLLRDHUP | EPOLLHUP;
      } else if (set == MNET_SET_WRITE) {
         events = n->epoll_events | EPOLLOUT | EPOLLRDHUP | EPOLLHUP;
      }
      kev->events = events;
      if (epoll_ctl(ss->kq, (n->epoll_events ? EPOLL_CTL_MOD : EPOLL_CTL_ADD), n->fd, kev) < 0) {
         mm_log(n, MNET_LOG_ERR, "epoll fail to add fd:%d filter %x, errno %d:%s\n",
                n->fd, n->epoll_events, errno, strerror(errno));
         return 0;
      }
      n->epoll_events = events;
#endif
      mm_log(n, MNET_LOG_VERBOSE, "add chann:%p fd:%d events\n", n, n->fd);
      return 1;
   }
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
callq 0x67b0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x8c90
cmpl $0x0, %eax
je 0x784e
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x31c0
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x4(%rax)
cmpl $0x0, -0x14(%rbp)
jne 0x7755
movq -0x10(%rbp), %rax
movl 0x88(%rax), %eax
orl $0x1, %eax
orl $0x2000, %eax           # imm = 0x2000
orl $0x10, %eax
movl %eax, -0x34(%rbp)
jmp 0x7775
cmpl $0x1, -0x14(%rbp)
jne 0x7773
movq -0x10(%rbp), %rax
movl 0x88(%rax), %eax
orl $0x4, %eax
orl $0x2000, %eax           # imm = 0x2000
orl $0x10, %eax
movl %eax, -0x34(%rbp)
jmp 0x7775
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x10(%rbp), %rax
movl 0x88(%rax), %ecx
movl $0x1, %esi
movl $0x3, %eax
cmpl $0x0, %ecx
cmovnel %eax, %esi
movq -0x10(%rbp), %rax
movl (%rax), %edx
movq -0x30(%rbp), %rcx
callq 0x3450
cmpl $0x0, %eax
jge 0x7816
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x88(%rax), %eax
movl %eax, -0x3c(%rbp)
callq 0x3040
movl (%rax), %eax
movl %eax, -0x38(%rbp)
callq 0x3040
movl (%rax), %edi
callq 0x3110
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %ecx
movl -0x3c(%rbp), %r8d
movl -0x38(%rbp), %r9d
movl $0x1, %esi
leaq 0xbc25(%rip), %rdx      # 0x13427
movq %rax, (%rsp)
movb $0x0, %al
callq 0x6c30
movl $0x0, -0x4(%rbp)
jmp 0x7855
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x88(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r8d
movl $0x3, %esi
leaq 0xbc19(%rip), %rdx      # 0x13457
movb $0x0, %al
callq 0x6c30
movl $0x1, -0x4(%rbp)
jmp 0x7855
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop