// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $-0x1
popq %r12
orb %cl, %al
jne 0x4a70
movq %rdi, %rbx
cmpl $0x3, 0x10(%rdi)
jl 0x4a70
movl %edx, %r14d
movq %rsi, %r15
leaq 0x38(%rbx), %r13
cmpl $0x0, 0x48(%rbx)
jle 0x4a33
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x4b30
movl (%rbx), %r8d
movq 0x40(%rbx), %rax
testq %rax, %rax
je 0x4a4d
movl 0x4(%rax), %r9d
subl (%rax), %r9d
jmp 0x4a50
movq %rbx, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0x4c25
movl %eax, %r12d
testl %eax, %eax
js 0x4a82
movl %r12d, %ecx
jmp 0x4a90
xorl %r9d, %r9d
movl 0x48(%rbx), %eax
movl %eax, (%rsp)
leaq 0x58cd(%rip), %rdx      # 0xa32a
pushq $0x3
popq %rsi
movq %rbx, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3eb5
movl %r14d, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2040
movl (%rax), %edx
xorl %ecx, %ecx
cmpl $0xb, %edx
jne 0x4ae2
movl %r14d, %r12d
subl %ecx, %r12d
jbe 0x4add
movl (%rbx), %r8d
movl %r14d, (%rsp)
leaq 0x58ca(%rip), %rdx      # 0xa370
pushq $0x3
popq %rsi
movq %rbx, %rdi
movl %ecx, -0x2c(%rbp)
movq %rbx, %rcx
movl %r12d, %r9d
xorl %eax, %eax
callq 0x3eb5
movl -0x2c(%rbp), %eax
addq %rax, %r15
movq %r13, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x4b30
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x461f
jmp 0x4a6d
movl %ecx, %r12d
jmp 0x4a70
movl (%rbx), %r14d
movl %edx, %r13d
movl %edx, %edi
movq %rax, %r15
callq 0x20e0
movq %rax, (%rsp)
leaq 0x5851(%rip), %rdx      # 0xa34e
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %rbx, %rcx
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x3eb5
movq %rbx, %rdi
callq 0x3da8
movl (%r15), %ecx
pushq $0x5
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x4980
jmp 0x4a70