// File: /workspace/github_cpp_makefile_star2_O2/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 0x2e3c
testl %edx, %edx
js 0x2cd5
movq %rdi, %rcx
movq %rdx, %r13
cmpl $0x0, 0xf0(%rdi)
movq %rdi, -0x30(%rbp)
je 0x2d26
leaq 0x88(%rcx), %rbx
cmpq 0x88(%rcx), %rbx
je 0x2d19
movq 0x90(%rcx), %r12
movl 0x2c(%r12), %edi
subl %edi, %r14d
jbe 0x2d19
movq %rsi, %r15
movq %r13, %rax
cmpl %eax, %r14d
cmovgel %eax, %r14d
addl %r14d, %edi
callq 0x2e7a
testq %rax, %rax
je 0x2e5b
movq -0x30(%rbp), %rcx
movq 0x90(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq %rbx, (%rax)
movq %rax, (%rdx)
movq %rax, 0x90(%rcx)
movq %rax, -0x38(%rbp)
movq %rax, %rbx
addq $0x40, %rbx
leaq 0x40(%r12), %rsi
movl 0x2c(%r12), %edx
movq %rbx, %rdi
callq 0x2220
testq %r15, %r15
je 0x2cd9
movl 0x2c(%r12), %eax
addq %rax, %rbx
movslq %r14d, %rax
movq %rbx, %rdi
movq %r15, %rbx
movq %r15, %rsi
movq %rax, %r15
movq %rax, %rdx
callq 0x2220
addq %r15, %rbx
jmp 0x2cdb
pushq $-0x1
jmp 0x2d42
xorl %ebx, %ebx
movl 0x2c(%r12), %eax
addl %r14d, %eax
movq -0x38(%rbp), %rcx
movl %eax, 0x2c(%rcx)
andl $0x0, 0x18(%rcx)
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
movq %r12, (%r12)
movq %r12, 0x8(%r12)
movq %r12, %rdi
callq 0x2838
subl %r14d, %r13d
movq -0x30(%rbp), %rcx
movq %rbx, %rsi
testl %r13d, %r13d
jle 0x2e0c
movl 0x8(%rcx), %r14d
movq %r13, %rax
cmpl %r14d, %eax
jle 0x2d48
addl %r14d, %eax
decl %eax
xorl %edx, %edx
divl %r14d
movl %eax, %r12d
cmpl $0x7f, %eax
jbe 0x2d4c
pushq $-0x2
popq %rax
jmp 0x2e0e
pushq $0x1
popq %r12
cmpl $0x1, %r12d
adcl $-0x1, %r12d
leaq 0x88(%rcx), %rax
movq %rax, -0x40(%rbp)
movq %r13, %r14
cmpl $-0x1, %r12d
je 0x2e0c
movq %rsi, %r15
movl 0x8(%rcx), %r13d
cmpl %r13d, %r14d
cmovll %r14d, %r13d
movl %r13d, %edi
callq 0x2e7a
testq %rax, %rax
je 0x2e1d
movq %rax, %rbx
movq %r15, %rsi
testq %r15, %r15
movq %r14, -0x38(%rbp)
je 0x2db6
testl %r14d, %r14d
jle 0x2db6
leaq 0x40(%rbx), %rdi
movslq %r13d, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x2220
movq %r15, %rsi
jmp 0x2db9
movslq %r13d, %r14
movq -0x30(%rbp), %rcx
movl %r13d, 0x2c(%rbx)
cmpl $0x0, 0xf0(%rcx)
movl $0x0, %eax
cmovel %r12d, %eax
movl %eax, 0x18(%rbx)
movq 0x90(%rcx), %rax
movq %rax, 0x8(%rbx)
movq -0x40(%rbp), %rdx
movq %rdx, (%rbx)
movq %rbx, (%rax)
incl 0x68(%rcx)
addq %rsi, %r14
testq %rsi, %rsi
movq %rbx, 0x90(%rcx)
cmovneq %r14, %rsi
movq -0x38(%rbp), %r14
subl %r13d, %r14d
decl %r12d
jmp 0x2d62
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x72e9(%rip), %rdi      # 0xa10d
leaq 0x7219(%rip), %rsi      # 0xa044
leaq 0x72b0(%rip), %rcx      # 0xa0e2
movl $0x204, %edx            # imm = 0x204
callq 0x21b0
leaq 0x7292(%rip), %rdi      # 0xa0d5
leaq 0x71fa(%rip), %rsi      # 0xa044
leaq 0x7291(%rip), %rcx      # 0xa0e2
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x21b0
leaq 0x72ab(%rip), %rdi      # 0xa10d
leaq 0x71db(%rip), %rsi      # 0xa044
leaq 0x7272(%rip), %rcx      # 0xa0e2
movl $0x1e3, %edx            # imm = 0x1E3
callq 0x21b0