// File: xocoder[P]m_kcptun/src/kcp_local.cpp
// Function: local_kcpout_callback(char const*, int, IKCPCB*, void*)
int
_local_kcpout_callback(const char *buf, int len, ikcpcb *kcp, void *user) {
   tun_local_t *tun = (tun_local_t*)user;
   chann_t *udpout = tun->udpout[tun->udpout_idx];
   tun->udpout_idx = (tun->udpout_idx + 1) % tun->conf->src_count;

   if (tun && mnet_chann_state(udpout) >= 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, &tun->buf[data_len]);
         data_len += XOR64_CHECKSUM_SIZE;
      }

      if (ret && data_len>0) {
         ret = mnet_chann_send(udpout, 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 (%rcx)
movq -0x30(%rbp), %rax
movl %edx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0xe371
movq -0x38(%rbp), %rdi
callq 0x72d0
cmpl $0x3, %eax
jl 0xe371
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 0xe271
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 0x91b0
movl %eax, -0x40(%rbp)
jmp 0xe282
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x3290
movl $0x0, -0x4c(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0xe30d
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 0xe620
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 0xe710
movl %eax, -0x4c(%rbp)
movl -0x54(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0xe337
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x30(%rbp), %rdx
addq $0x98, %rdx
movslq -0x40(%rbp), %rax
addq %rax, %rdx
callq 0x98d0
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0xe36f
cmpl $0x0, -0x40(%rbp)
jle 0xe36f
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x98, %rsi
movl -0x40(%rbp), %edx
callq 0x7d70
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jne 0xe36d
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xe378
jmp 0xe36f
jmp 0xe371
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)