// File: /workspace/github_cpp_makefile_star2_O3/xocoder[P]m_kcptun/vendor/m_net/src/mnet_core.c
// Function: mnet_chann_send
int
mnet_chann_send(chann_t *n, void *buf, int len) {
   if (n && buf && len>0 && n->state>=CHANN_STATE_CONNECTED) {
      int ret = len;
      rwb_head_t *prh = &n->rwb_send;

      if (_rwb_count(prh) > 0) {
         _rwb_cache(prh, (unsigned char*)buf, len);
         mm_log(n, MNET_LOG_VERBOSE, "chann %p fd:%d still cache %d(%d)!\n",
                n, n->fd, _rwb_buffered(prh->tail), _rwb_count(prh));
      } else {
         ret = _chann_send(n, buf, len);
         do {
            if (ret < 0) {
               if (errno == EWOULDBLOCK) {
                  ret = 0;
               } else {
                  mnet_t *ss = _gmnet();
                  mm_log(n, MNET_LOG_ERR, "chann %p fd:%d, send errno %d:%s\n",
                         n, n->fd, errno, strerror(errno));
                  _chann_disconnect_socket(ss, n);
                  _chann_msg(n, CHANN_EVENT_DISCONNECT, NULL, errno);
                  break;
               }
            }
            if (ret < len) {
               mm_log(n, MNET_LOG_VERBOSE, "chann %p fd:%d cache %d of %d!\n", n, n->fd, len - ret, len);
               _rwb_cache(prh, ((unsigned char*)buf) + ret, len - ret);
               ret = len;
               _evt_add(n, MNET_SET_WRITE);
            }
         } while (0);
      }
      return ret;
   }
   return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
testl %edx, %edx
setle %al
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
orb %cl, %al
jne 0x4de0
movq %rdi, %rbx
cmpl $0x3, 0x10(%rdi)
jl 0x4de0
movl %edx, %r14d
movq %rsi, %r15
leaq 0x38(%rbx), %r13
cmpl $0x0, 0x48(%rbx)
jle 0x4da1
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x4ea8
movl (%rbx), %r8d
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x4dbb
movl 0x4(%rax), %r9d
subl (%rax), %r9d
jmp 0x4dbe
movq %rbx, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x4fa1
movl %eax, %r12d
testl %eax, %eax
js 0x4df2
movl %r12d, %ecx
jmp 0x4e00
xorl %r9d, %r9d
movl 0x48(%rbx), %eax
movl %eax, (%rsp)
leaq 0x655f(%rip), %rdx      # 0xb32a
movq %rbx, %rdi
movl $0x3, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x41bd
movl %r14d, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2050
movl (%rax), %edx
xorl %ecx, %ecx
cmpl $0xb, %edx
jne 0x4e56
movl %r14d, %r12d
subl %ecx, %r12d
jbe 0x4e51
movl (%rbx), %r8d
movl %r14d, (%rsp)
leaq 0x655a(%rip), %rdx      # 0xb370
movq %rbx, %rdi
movl $0x3, %esi
movl %ecx, -0x2c(%rbp)
movq %rbx, %rcx
movl %r12d, %r9d
xorl %eax, %eax
callq 0x41bd
movl -0x2c(%rbp), %eax
addq %rax, %r15
movq %r13, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x4ea8
movq %rbx, %rdi
movl $0x1, %esi
callq 0x496f
jmp 0x4ddd
movl %ecx, %r12d
jmp 0x4de0
movl (%rbx), %r14d
movl %edx, %r13d
movl %edx, %edi
movq %rax, %r15
callq 0x20f0
movq %rax, (%rsp)
leaq 0x64dd(%rip), %rdx      # 0xb34e
movq %rbx, %rdi
movl $0x1, %esi
movq %rbx, %rcx
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x41bd
movq %rbx, %rdi
callq 0x4069
movl (%r15), %ecx
movq %rbx, %rdi
movl $0x5, %esi
xorl %edx, %edx
callq 0x4cea
jmp 0x4de0