// File: /workspace/github_cpp_makefile_star2_O1/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 0x4e3f
movq %rdi, %rbx
cmpl $0x3, 0x10(%rdi)
jl 0x4e3f
movl %edx, %r14d
movq %rsi, %r15
leaq 0x38(%rbx), %r13
cmpl $0x0, 0x48(%rbx)
jle 0x4e00
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x4f07
movl (%rbx), %r8d
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x4e1a
movl 0x4(%rax), %r9d
subl (%rax), %r9d
jmp 0x4e1d
movq %rbx, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x5006
movl %eax, %r12d
testl %eax, %eax
js 0x4e51
movl %r12d, %ecx
jmp 0x4e5f
xorl %r9d, %r9d
movl 0x48(%rbx), %eax
movl %eax, (%rsp)
leaq 0x6500(%rip), %rdx      # 0xb32a
movq %rbx, %rdi
movl $0x3, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x420f
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 0x4eb5
movl %r14d, %r12d
subl %ecx, %r12d
jle 0x4eb0
movl (%rbx), %r8d
movl %r14d, (%rsp)
leaq 0x64fb(%rip), %rdx      # 0xb370
movq %rbx, %rdi
movl $0x3, %esi
movl %ecx, -0x2c(%rbp)
movq %rbx, %rcx
movl %r12d, %r9d
xorl %eax, %eax
callq 0x420f
movl -0x2c(%rbp), %eax
addq %rax, %r15
movq %r13, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x4f07
movq %rbx, %rdi
movl $0x1, %esi
callq 0x49c0
jmp 0x4e3c
movl %ecx, %r12d
jmp 0x4e3f
movl (%rbx), %r14d
movl %edx, %r13d
movl %edx, %edi
movq %rax, %r15
callq 0x20f0
movq %rax, (%rsp)
leaq 0x647e(%rip), %rdx      # 0xb34e
movq %rbx, %rdi
movl $0x1, %esi
movq %rbx, %rcx
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x420f
movq %rbx, %rdi
callq 0x40b7
movl (%r15), %ecx
movq %rbx, %rdi
movl $0x5, %esi
xorl %edx, %edx
callq 0x4d49
jmp 0x4e3f