// File: /workspace/github_cpp_makefile_star2_O3/xocoder[P]m_kcptun/vendor/kcp/ikcp.c
// Function: ikcp_send
int ikcp_send(ikcpcb *kcp, const char *buffer, int len)
{
	IKCPSEG *seg;
	int count, i;

	assert(kcp->mss > 0);
	if (len < 0) return -1;

	// append to previous segment in streaming mode (if possible)
	if (kcp->stream != 0) {
		if (!iqueue_is_empty(&kcp->snd_queue)) {
			IKCPSEG *old = iqueue_entry(kcp->snd_queue.prev, IKCPSEG, node);
			if (old->len < kcp->mss) {
				int capacity = kcp->mss - old->len;
				int extend = (len < capacity)? len : capacity;
				seg = ikcp_segment_new(kcp, old->len + extend);
				assert(seg);
				if (seg == NULL) {
					return -2;
				}
				iqueue_add_tail(&seg->node, &kcp->snd_queue);
				memcpy(seg->data, old->data, old->len);
				if (buffer) {
					memcpy(seg->data + old->len, buffer, extend);
					buffer += extend;
				}
				seg->len = old->len + extend;
				seg->frg = 0;
				len -= extend;
				iqueue_del_init(&old->node);
				ikcp_segment_delete(kcp, old);
			}
		}
		if (len <= 0) {
			return 0;
		}
	}

	if (len <= (int)kcp->mss) count = 1;
	else count = (len + kcp->mss - 1) / kcp->mss;

	if (count >= IKCP_WND_RCV) return -2;

	if (count == 0) count = 1;

	// fragment
	for (i = 0; i < count; i++) {
		int size = len > (int)kcp->mss ? (int)kcp->mss : len;
		seg = ikcp_segment_new(kcp, size);
		assert(seg);
		if (seg == NULL) {
			return -2;
		}
		if (buffer && len > 0) {
			memcpy(seg->data, buffer, size);
		}
		seg->len = size;
		seg->frg = (kcp->stream == 0)? (count - i - 1) : 0;
		iqueue_init(&seg->node);
		iqueue_add_tail(&seg->node, &kcp->snd_queue);
		kcp->nsnd_que++;
		if (buffer) {
			buffer += size;
		}
		len -= size;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x8(%rdi), %r14d
testl %r14d, %r14d
je 0x2f98
movl %edx, %ebx
testl %edx, %edx
js 0x2d9e
movq %rsi, %r15
cmpl $0x0, 0xf0(%rdi)
movq %rdi, -0x40(%rbp)
je 0x2e72
leaq 0x88(%rdi), %r12
cmpq 0x88(%rdi), %r12
je 0x2e66
movq 0x90(%rdi), %rax
movq %rax, -0x38(%rbp)
movl 0x2c(%rax), %eax
subl %eax, %r14d
jbe 0x2e66
cmpl %ebx, %r14d
cmovgel %ebx, %r14d
addl %r14d, %eax
movslq %eax, %rdi
addq $0x48, %rdi
movq 0xd4f3(%rip), %rax      # 0x10288
testq %rax, %rax
je 0x2da8
callq *%rax
jmp 0x2dad
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2f6a
callq 0x2360
movq %rax, %r13
testq %rax, %rax
movq -0x40(%rbp), %rcx
je 0x2fb7
movq 0x90(%rcx), %rax
movq %rax, 0x8(%r13)
movq %r12, (%r13)
movq %r13, (%rax)
movq %r13, 0x90(%rcx)
leaq 0x40(%r13), %rdi
movq -0x38(%rbp), %r12
leaq 0x40(%r12), %rsi
movl 0x2c(%r12), %edx
movq %rdi, -0x30(%rbp)
callq 0x2230
testq %r15, %r15
je 0x2e17
movl 0x2c(%r12), %eax
movq -0x30(%rbp), %rdi
addq %rax, %rdi
movslq %r14d, %rdx
movq %rdx, -0x30(%rbp)
movq %r15, %rsi
callq 0x2230
addq -0x30(%rbp), %r15
jmp 0x2e1a
xorl %r15d, %r15d
movl 0x2c(%r12), %eax
addl %r14d, %eax
movl %eax, 0x2c(%r13)
movl $0x0, 0x18(%r13)
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %r12, (%r12)
movq %r12, 0x8(%r12)
movq 0xd442(%rip), %rax      # 0x10290
movq %r12, %rdi
testq %rax, %rax
je 0x2e5a
callq *%rax
jmp 0x2e5f
callq 0x2320
subl %r14d, %ebx
movq -0x40(%rbp), %rdi
testl %ebx, %ebx
jle 0x2f68
movl 0x8(%rdi), %r14d
movl $0x1, %r12d
cmpl %r14d, %ebx
jle 0x2e9a
leal (%rbx,%r14), %eax
decl %eax
xorl %edx, %edx
divl %r14d
movl %eax, %r12d
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpl $0x7f, %r12d
ja 0x2f6a
cmpl $0x1, %r12d
adcl $-0x1, %r12d
leaq 0x88(%rdi), %rax
movq %rax, -0x38(%rbp)
movq 0xd3d4(%rip), %rsi      # 0x10288
movl 0x8(%rdi), %eax
cmpl %eax, %ebx
cmovll %ebx, %eax
movslq %eax, %r13
leaq 0x48(%r13), %rdi
testq %rsi, %rsi
je 0x2ed6
callq *%rsi
movq %rax, %r14
movq 0xd3b4(%rip), %rsi      # 0x10288
jmp 0x2ee0
callq 0x2360
movq %rax, %r14
xorl %esi, %esi
testq %r14, %r14
je 0x2f79
testq %r15, %r15
je 0x2f0f
testl %ebx, %ebx
jle 0x2f0f
leaq 0x40(%r14), %rdi
movq %rbx, -0x30(%rbp)
movq %rsi, %rbx
movq %r15, %rsi
movq %r13, %rdx
callq 0x2230
movq %rbx, %rsi
movq -0x30(%rbp), %rbx
movl %r13d, 0x2c(%r14)
xorl %eax, %eax
movq -0x40(%rbp), %rdi
cmpl $0x0, 0xf0(%rdi)
movl $0x0, %ecx
cmovel %r12d, %ecx
movl %ecx, 0x18(%r14)
movq 0x90(%rdi), %rcx
movq %rcx, 0x8(%r14)
movq -0x38(%rbp), %rdx
movq %rdx, (%r14)
movq %r14, (%rcx)
incl 0x68(%rdi)
movq %r13, %rcx
addq %r15, %rcx
testq %r15, %r15
movq %r14, 0x90(%rdi)
cmovneq %rcx, %r15
subl %r13d, %ebx
addl $-0x1, %r12d
jb 0x2eb4
jmp 0x2f6a
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x818d(%rip), %rdi      # 0xb10d
leaq 0x80bd(%rip), %rsi      # 0xb044
leaq 0x8154(%rip), %rcx      # 0xb0e2
movl $0x204, %edx            # imm = 0x204
callq 0x21b0
leaq 0x8136(%rip), %rdi      # 0xb0d5
leaq 0x809e(%rip), %rsi      # 0xb044
leaq 0x8135(%rip), %rcx      # 0xb0e2
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x21b0
leaq 0x814f(%rip), %rdi      # 0xb10d
leaq 0x807f(%rip), %rsi      # 0xb044
leaq 0x8116(%rip), %rcx      # 0xb0e2
movl $0x1e3, %edx            # imm = 0x1E3
callq 0x21b0