// File: 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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jbe 0x443f
jmp 0x445e
leaq 0xec53(%rip), %rdi      # 0x13099
leaq 0xebbb(%rip), %rsi      # 0x13008
movl $0x1d8, %edx            # imm = 0x1D8
leaq 0xec4d(%rip), %rcx      # 0x130a6
callq 0x3210
cmpl $0x0, -0x1c(%rbp)
jge 0x4470
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4842
movq -0x10(%rbp), %rax
cmpl $0x0, 0xf0(%rax)
je 0x4676
movq -0x10(%rbp), %rax
addq $0x88, %rax
movq -0x10(%rbp), %rcx
cmpq 0x88(%rcx), %rax
je 0x4662
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x4660
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x38(%rbp), %rcx
subl 0x2c(%rcx), %eax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x44e0
movl -0x1c(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x44e6
movl -0x3c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %esi
addl -0x40(%rbp), %esi
callq 0x4850
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x450c
jmp 0x452b
leaq 0xebbe(%rip), %rdi      # 0x130d1
leaq 0xeaee(%rip), %rsi      # 0x13008
movl $0x1e3, %edx            # imm = 0x1E3
leaq 0xeb80(%rip), %rcx      # 0x130a6
callq 0x3210
cmpq $0x0, -0x28(%rbp)
jne 0x453e
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4842
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rcx
addq $0x88, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x28(%rbp), %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rsi
addq $0x40, %rsi
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, %edx
callq 0x3290
cmpq $0x0, -0x18(%rbp)
je 0x45d9
movq -0x28(%rbp), %rdi
addq $0x40, %rdi
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %eax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movslq -0x40(%rbp), %rdx
callq 0x3290
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %ecx
addl -0x40(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x18(%rax)
movl -0x40(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x3f30
jmp 0x4662
cmpl $0x0, -0x1c(%rbp)
jg 0x4674
movl $0x0, -0x4(%rbp)
jmp 0x4842
jmp 0x4676
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jg 0x468b
movl $0x1, -0x2c(%rbp)
jmp 0x46a4
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
addl 0x8(%rcx), %eax
subl $0x1, %eax
movq -0x10(%rbp), %rcx
xorl %edx, %edx
divl 0x8(%rcx)
movl %eax, -0x2c(%rbp)
cmpl $0x80, -0x2c(%rbp)
jb 0x46b9
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4842
cmpl $0x0, -0x2c(%rbp)
jne 0x46c6
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x483b
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jle 0x46f1
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x46f7
movl -0x1c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x4850
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x4716
jmp 0x4735
leaq 0xe9b4(%rip), %rdi      # 0x130d1
leaq 0xe8e4(%rip), %rsi      # 0x13008
movl $0x204, %edx            # imm = 0x204
leaq 0xe976(%rip), %rcx      # 0x130a6
callq 0x3210
cmpq $0x0, -0x28(%rbp)
jne 0x4748
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x4842
cmpq $0x0, -0x18(%rbp)
je 0x476a
cmpl $0x0, -0x1c(%rbp)
jle 0x476a
movq -0x28(%rbp), %rdi
addq $0x40, %rdi
movq -0x18(%rbp), %rsi
movslq -0x44(%rbp), %rdx
callq 0x3290
movl -0x44(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xf0(%rax)
jne 0x478f
movl -0x2c(%rbp), %eax
subl -0x30(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x4796
xorl %eax, %eax
movl %eax, -0x50(%rbp)
jmp 0x4796
movl -0x50(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x28(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rcx
addq $0x88, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x10(%rbp), %rax
movl 0x68(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x68(%rax)
cmpq $0x0, -0x18(%rbp)
je 0x4822
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movl -0x44(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x46cd
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)