// File: xocoder[P]m_kcptun/src/kcp_local.cpp
// Function: local_udpout_callback(chann_msg_t*)
void
_local_udpout_callback(chann_msg_t *e) {
   tun_local_t *tun = (tun_local_t*)e->opaque;   

   switch (e->event) {

      case CHANN_EVENT_RECV: {
         long ret = mnet_chann_recv(e->n, tun->buf, MKCP_BUF_SIZE);

         if (ret > MKCP_OVERHEAD) {
            const int rs_offset = tun->rt ? sizeof(uint16_t) : 0;
            uint8_t *data = tun->buf + rs_offset;
            int data_len = tun->rt ? rskcp_dec_info(tun->rt, ret) : (ret - XOR64_CHECKSUM_SIZE);
            
            if ( tun->rt ) {
               ret = rskcp_decode(tun->rt, tun->buf, ret, &tun->buf[data_len]);
               data_len = *((uint16_t*)tun->buf); // restore data_len
            } else {
               ret = xor64_checksum_check(data, data_len, &tun->buf[data_len]);
            }

            if ( ret ) {
               if ( tun->conf->crypto ) {
                  data_len = rc4_decrypt((const char*)data, data_len,
                                         (char*)tun->buf, MKCP_BUF_SIZE,
                                         tun->ukey, (tun->ti>>20));
                  if (data_len < 0) {
                     return;
                  }
                  data = tun->buf;
               }

               if (data_len >= MKCP_OVERHEAD) {
                  ikcp_input(tun->kcpout, (const char*)data, data_len);
               }
            }
         }

         break;
      }

      case CHANN_EVENT_DISCONNECT:  {
         cout << "local udp disconnect !" << endl;
         break;
      }

      default: {
         break;
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
subl $0x1, %eax
je 0xdf99
jmp 0xdf88
movl -0x30(%rbp), %eax
subl $0x5, %eax
je 0xe135
jmp 0xe159
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x98, %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x7b70
cltq
movq %rax, -0x18(%rbp)
cmpq $0x18, -0x18(%rbp)
jle 0xe133
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdx
xorl %eax, %eax
movl $0x2, %ecx
cmpq $0x0, %rdx
cmovneq %rcx, %rax
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
addq $0x98, %rax
movslq -0x1c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0xe025
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x18(%rbp), %rax
movl %eax, %esi
callq 0xe6a0
cltq
movq %rax, -0x38(%rbp)
jmp 0xe031
movq -0x18(%rbp), %rax
subq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0xe08f
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x98, %rsi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %rcx
addq $0x98, %rcx
movslq -0x2c(%rbp), %rax
addq %rax, %rcx
callq 0xe880
cltq
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x98(%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0xe0b3
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x10(%rbp), %rdx
addq $0x98, %rdx
movslq -0x2c(%rbp), %rax
addq %rax, %rdx
callq 0x99e0
cltq
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xe131
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
cmpl $0x0, 0x148(%rax)
je 0xe114
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x10(%rbp), %rdx
addq $0x98, %rdx
movq -0x10(%rbp), %rax
movq 0x60(%rax), %r8
movq -0x10(%rbp), %rax
movq 0x68(%rax), %r9
shrq $0x14, %r9
movl $0x10000, %ecx          # imm = 0x10000
callq 0x9650
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0xe106
jmp 0xe15b
movq -0x10(%rbp), %rax
addq $0x98, %rax
movq %rax, -0x28(%rbp)
cmpl $0x18, -0x2c(%rbp)
jl 0xe12f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x28(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x4b00
jmp 0xe131
jmp 0xe133
jmp 0xe15b
movq 0x9e94(%rip), %rdi      # 0x17fd0
leaq 0x5ea9(%rip), %rsi      # 0x13fec
callq 0x32f0
movq %rax, %rdi
movq 0x9e6e(%rip), %rsi      # 0x17fc0
callq 0x3340
jmp 0xe15b
jmp 0xe15b
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)