// File: xocoder[P]m_kcptun/src/kcp_remote.cpp
// Function: remote_kcpin_callback(char const*, int, IKCPCB*, void*)
static int
_remote_kcpin_callback(const char *buf, int len, ikcpcb *kcp, void *user) {
   tun_remote_t *tun = (tun_remote_t*)user;
   chann_t *udpin = tun->udpin[tun->udpin_idx];
   tun->udpin_idx = (tun->udpin_idx + 1) % tun->conf->dest_count;

   if (tun && mnet_chann_state(udpin) >= CHANN_STATE_CONNECTED) {
      const int rs_offset = tun->rt ? sizeof(uint16_t) : 0;
      int data_len = len;
      uint8_t *data = tun->buf + rs_offset;

      if ( tun->conf->crypto ) {
         data_len = rc4_encrypt(buf, len,
                                (char*)data, MKCP_BUF_SIZE - XOR64_CHECKSUM_SIZE,
                                tun->ukey, (tun->ti>>20));
      } else {
         memcpy(data, buf, len);
      }

      int ret = 0;

      if ( tun->rt ) {
         int plen = 0;
         *((uint16_t*)tun->buf) = data_len;
         int raw_len = rskcp_enc_info(tun->rt, (data_len + rs_offset), &plen);
         ret = rskcp_encode(tun->rt, tun->buf, (data_len + rs_offset), &tun->buf[raw_len]);
         data_len = raw_len + plen;
      } else {
         ret = xor64_checksum_gen((uint8_t*)data, data_len, &data[data_len]);
         data_len += XOR64_CHECKSUM_SIZE;
      }

      if (ret && data_len>0  && mnet_chann_cached(udpin) < tun->conf->tcp_cache) {
         ret = mnet_chann_send(udpin, tun->buf, data_len);
         if (ret == data_len) {
            return len;
         }
      }
   }
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rcx
movslq (%rcx), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x78(%rcx), %rcx
cltd
idivl 0x94(%rcx)
movq -0x30(%rbp), %rax
movl %edx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0xe277
movq -0x38(%rbp), %rdi
callq 0x72c0
cmpl $0x3, %eax
jl 0xe277
movq -0x30(%rbp), %rax
movq 0x90(%rax), %rdx
xorl %eax, %eax
movl $0x2, %ecx
cmpq $0x0, %rdx
cmovneq %rcx, %rax
movl %eax, -0x3c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
addq $0x98, %rax
movslq -0x3c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rax
cmpl $0x0, 0x148(%rax)
je 0xe165
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x48(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x60(%rax), %r8
movq -0x30(%rbp), %rax
movq 0x68(%rax), %r9
shrq $0x14, %r9
movl $0xfff8, %ecx           # imm = 0xFFF8
callq 0x91a0
movl %eax, -0x40(%rbp)
jmp 0xe176
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x3280
movl $0x0, -0x4c(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0xe201
movl $0x0, -0x50(%rbp)
movl -0x40(%rbp), %eax
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x98(%rax)
movq -0x30(%rbp), %rax
movq 0x90(%rax), %rdi
movl -0x40(%rbp), %esi
addl -0x3c(%rbp), %esi
leaq -0x50(%rbp), %rdx
callq 0xe7b0
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x98, %rsi
movl -0x40(%rbp), %edx
addl -0x3c(%rbp), %edx
movq -0x30(%rbp), %rcx
addq $0x98, %rcx
movslq -0x54(%rbp), %rax
addq %rax, %rcx
callq 0xe8a0
movl %eax, -0x4c(%rbp)
movl -0x54(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0xe224
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x48(%rbp), %rdx
movslq -0x40(%rbp), %rax
addq %rax, %rdx
callq 0x98c0
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xe275
cmpl $0x0, -0x40(%rbp)
jle 0xe275
movq -0x38(%rbp), %rdi
callq 0x81c0
movq -0x30(%rbp), %rcx
movq 0x78(%rcx), %rcx
cmpl 0x154(%rcx), %eax
jge 0xe275
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x98, %rsi
movl -0x40(%rbp), %edx
callq 0x7d60
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jne 0xe273
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xe27e
jmp 0xe275
jmp 0xe277
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)