// File: /workspace/github_cpp_makefile_star2_O1/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 0x2f3a
movl %edx, %ebx
testl %edx, %edx
js 0x2d4a
movq %rsi, %r15
cmpl $0x0, 0xf0(%rdi)
movq %rdi, -0x38(%rbp)
je 0x2e27
leaq 0x88(%rdi), %rcx
cmpq 0x88(%rdi), %rcx
je 0x2e1f
movq 0x90(%rdi), %r12
movl 0x2c(%r12), %eax
subl %eax, %r14d
jbe 0x2e1f
movq %rcx, -0x30(%rbp)
cmpl %ebx, %r14d
cmovgel %ebx, %r14d
addl %r14d, %eax
movslq %eax, %rdi
addq $0x48, %rdi
movq 0xd547(%rip), %rax      # 0x10288
testq %rax, %rax
je 0x2d54
callq *%rax
jmp 0x2d59
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2f0c
callq 0x2350
movq %rax, %r13
testq %rax, %rax
movq -0x38(%rbp), %rcx
je 0x2f59
movq 0x90(%rcx), %rax
movq %rax, 0x8(%r13)
movq -0x30(%rbp), %rdx
movq %rdx, (%r13)
movq %r13, (%rax)
movq %r13, 0x90(%rcx)
leaq 0x40(%r13), %rdi
leaq 0x40(%r12), %rsi
movl 0x2c(%r12), %edx
movq %rdi, -0x30(%rbp)
callq 0x2220
testq %r15, %r15
je 0x2dc3
movl 0x2c(%r12), %eax
movq -0x30(%rbp), %rdi
addq %rax, %rdi
movslq %r14d, %rdx
movq %rdx, -0x30(%rbp)
movq %r15, %rsi
callq 0x2220
addq -0x30(%rbp), %r15
jmp 0x2dc6
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 0x8(%r12), %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq %r12, (%r12)
movq %r12, 0x8(%r12)
movq 0xd489(%rip), %rax      # 0x10290
movq %r12, %rdi
testq %rax, %rax
je 0x2e13
callq *%rax
jmp 0x2e18
callq 0x2310
subl %r14d, %ebx
movq -0x38(%rbp), %rdi
testl %ebx, %ebx
jle 0x2f0a
movl 0x8(%rdi), %ecx
movl $0x1, %r12d
cmpl %ecx, %ebx
jle 0x2e40
leal (%rbx,%rcx), %eax
decl %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r12d
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpl $0x7f, %r12d
ja 0x2f0c
cmpl $0x1, %r12d
adcl $-0x1, %r12d
leaq 0x88(%rdi), %rax
movq %rax, -0x30(%rbp)
movl 0x8(%rdi), %eax
cmpl %eax, %ebx
cmovll %ebx, %eax
movslq %eax, %r13
leaq 0x48(%r13), %rdi
movq 0xd410(%rip), %rax      # 0x10288
testq %rax, %rax
je 0x2e81
callq *%rax
jmp 0x2e86
callq 0x2350
movq %rax, %r14
testq %rax, %rax
je 0x2f1b
testq %r15, %r15
je 0x2eaa
testl %ebx, %ebx
jle 0x2eaa
leaq 0x40(%r14), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2220
movl %r13d, 0x2c(%r14)
xorl %eax, %eax
movq -0x38(%rbp), %rdi
cmpl $0x0, 0xf0(%rdi)
movl $0x0, %ecx
cmovel %r12d, %ecx
movl %ecx, 0x18(%r14)
movq %r14, (%r14)
movq %r14, 0x8(%r14)
movq 0x90(%rdi), %rcx
movq %rcx, 0x8(%r14)
movq -0x30(%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 0x2e62
jmp 0x2f0c
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x81eb(%rip), %rdi      # 0xb10d
leaq 0x811b(%rip), %rsi      # 0xb044
leaq 0x81b2(%rip), %rcx      # 0xb0e2
movl $0x204, %edx            # imm = 0x204
callq 0x21b0
leaq 0x8194(%rip), %rdi      # 0xb0d5
leaq 0x80fc(%rip), %rsi      # 0xb044
leaq 0x8193(%rip), %rcx      # 0xb0e2
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x21b0
leaq 0x81ad(%rip), %rdi      # 0xb10d
leaq 0x80dd(%rip), %rsi      # 0xb044
leaq 0x8174(%rip), %rcx      # 0xb0e2
movl $0x1e3, %edx            # imm = 0x1E3
callq 0x21b0