// File: /workspace/github_cpp_makefile_star2_O2/xocoder[P]m_kcptun/vendor/kcp/ikcp.c
// Function: ikcp_input
int ikcp_input(ikcpcb *kcp, const char *data, long size)
{
	IUINT32 una = kcp->snd_una;
	IUINT32 maxack = 0;
	int flag = 0;

	if (ikcp_canlog(kcp, IKCP_LOG_INPUT)) {
		ikcp_log(kcp, IKCP_LOG_INPUT, "[RI] %d bytes", size);
	}

	if (data == NULL || (int)size < (int)IKCP_OVERHEAD) return -1;

	while (1) {
		IUINT32 ts, sn, len, una, conv;
		IUINT16 wnd;
		IUINT8 cmd, frg;
		IKCPSEG *seg;

		if (size < (int)IKCP_OVERHEAD) break;

		data = ikcp_decode32u(data, &conv);
		if (conv != kcp->conv) return -1;

		data = ikcp_decode8u(data, &cmd);
		data = ikcp_decode8u(data, &frg);
		data = ikcp_decode16u(data, &wnd);
		data = ikcp_decode32u(data, &ts);
		data = ikcp_decode32u(data, &sn);
		data = ikcp_decode32u(data, &una);
		data = ikcp_decode32u(data, &len);

		size -= IKCP_OVERHEAD;

		if ((long)size < (long)len) return -2;

		if (cmd != IKCP_CMD_PUSH && cmd != IKCP_CMD_ACK &&
			cmd != IKCP_CMD_WASK && cmd != IKCP_CMD_WINS) 
			return -3;

		kcp->rmt_wnd = wnd;
		ikcp_parse_una(kcp, una);
		ikcp_shrink_buf(kcp);

		if (cmd == IKCP_CMD_ACK) {
			if (_itimediff(kcp->current, ts) >= 0) {
				ikcp_update_ack(kcp, _itimediff(kcp->current, ts));
			}
			ikcp_parse_ack(kcp, sn);
			ikcp_shrink_buf(kcp);
			if (flag == 0) {
				flag = 1;
				maxack = sn;
			}	else {
				if (_itimediff(sn, maxack) > 0) {
					maxack = sn;
				}
			}
			if (ikcp_canlog(kcp, IKCP_LOG_IN_ACK)) {
				ikcp_log(kcp, IKCP_LOG_IN_DATA, 
					"input ack: sn=%lu rtt=%ld rto=%ld", sn, 
					(long)_itimediff(kcp->current, ts),
					(long)kcp->rx_rto);
			}
		}
		else if (cmd == IKCP_CMD_PUSH) {
			if (ikcp_canlog(kcp, IKCP_LOG_IN_DATA)) {
				ikcp_log(kcp, IKCP_LOG_IN_DATA, 
					"input psh: sn=%lu ts=%lu", sn, ts);
			}
			if (_itimediff(sn, kcp->rcv_nxt + kcp->rcv_wnd) < 0) {
				ikcp_ack_push(kcp, sn, ts);
				if (_itimediff(sn, kcp->rcv_nxt) >= 0) {
					seg = ikcp_segment_new(kcp, len);
					seg->conv = conv;
					seg->cmd = cmd;
					seg->frg = frg;
					seg->wnd = wnd;
					seg->ts = ts;
					seg->sn = sn;
					seg->una = una;
					seg->len = len;

					if (len > 0) {
						memcpy(seg->data, data, len);
					}

					ikcp_parse_data(kcp, seg);
				}
			}
		}
		else if (cmd == IKCP_CMD_WASK) {
			// ready to send back IKCP_CMD_WINS in ikcp_flush
			// tell remote my window size
			kcp->probe |= IKCP_ASK_TELL;
			if (ikcp_canlog(kcp, IKCP_LOG_IN_PROBE)) {
				ikcp_log(kcp, IKCP_LOG_IN_PROBE, "input probe");
			}
		}
		else if (cmd == IKCP_CMD_WINS) {
			// do nothing
			if (ikcp_canlog(kcp, IKCP_LOG_IN_WINS)) {
				ikcp_log(kcp, IKCP_LOG_IN_WINS,
					"input wins: %lu", (IUINT32)(wnd));
			}
		}
		else {
			return -3;
		}

		data += len;
		size -= len;
	}

	if (flag != 0) {
		ikcp_parse_fastack(kcp, maxack);
	}

	if (_itimediff(kcp->snd_una, una) > 0) {
		if (kcp->cwnd < kcp->rmt_wnd) {
			IUINT32 mss = kcp->mss;
			if (kcp->cwnd < kcp->ssthresh) {
				kcp->cwnd++;
				kcp->incr += mss;
			}	else {
				if (kcp->incr < mss) kcp->incr = mss;
				kcp->incr += (mss * mss) / kcp->incr + (mss / 16);
				if ((kcp->cwnd + 1) * mss <= kcp->incr) {
					kcp->cwnd++;
				}
			}
			if (kcp->cwnd > kcp->rmt_wnd) {
				kcp->cwnd = kcp->rmt_wnd;
				kcp->incr = kcp->rmt_wnd * mss;
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, %r8
movq %rdi, %rbx
movl 0x10(%rdi), %r14d
testb $0x2, 0xf4(%rdi)
je 0x2fa7
cmpq $0x0, 0x100(%rbx)
je 0x2fa7
leaq 0x7180(%rip), %rdx      # 0xa111
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
movq %r8, %r12
callq 0x2600
movq %r12, %r8
testq %r8, %r8
sete %al
cmpl $0x18, %r15d
setl %cl
orb %al, %cl
pushq $-0x1
popq %r9
jne 0x3450
movl %r14d, -0x64(%rbp)
leaq 0xa8(%rbx), %r13
leaq 0x14(%rbx), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x34(%rbp)
cmpq $0x18, %r15
jl 0x3405
movl (%r8), %edx
cmpl (%rbx), %edx
jne 0x3448
movl 0x14(%r8), %ecx
addq $-0x18, %r15
subq %rcx, %r15
jb 0x344c
movzbl 0x4(%r8), %r14d
leal -0x55(%r14), %eax
cmpb $-0x4, %al
jb 0x3401
movl %edx, -0x6c(%rbp)
movq %rcx, -0x40(%rbp)
movq %r15, -0x48(%rbp)
movzbl 0x5(%r8), %eax
movl %eax, -0x68(%rbp)
movzwl 0x6(%r8), %ecx
movl 0x8(%r8), %eax
movl %eax, -0x4c(%rbp)
movl 0xc(%r8), %eax
movl %eax, -0x54(%rbp)
movl 0x10(%r8), %r12d
addq $0x18, %r8
movq %r8, -0x30(%rbp)
movl %ecx, -0x58(%rbp)
movl %ecx, 0x40(%rbx)
movq 0xa8(%rbx), %rdi
cmpq %r13, %rdi
je 0x3087
movl %r12d, %eax
subl 0x24(%rdi), %eax
testl %eax, %eax
jle 0x3087
movq (%rdi), %r15
movq 0x8(%rdi), %rax
movq %rax, 0x8(%r15)
movq %r15, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
callq 0x2838
decl 0x60(%rbx)
movq %r15, %rdi
jmp 0x3057
movq 0xa8(%rbx), %r8
leaq 0x24(%r8), %rax
cmpq %r13, %r8
cmoveq -0x60(%rbp), %rax
movl (%rax), %r9d
movl %r9d, 0x10(%rbx)
addl $-0x51, %r14d
cmpl $0x3, %r14d
ja 0x3401
leaq 0x6f7a(%rip), %rcx      # 0xa030
movslq (%rcx,%r14,4), %rax
addq %rcx, %rax
jmpq *%rax
testb $0x10, 0xf4(%rbx)
movl -0x54(%rbp), %r14d
je 0x30f1
cmpq $0x0, 0x100(%rbx)
je 0x30f1
movq %rbx, %rdi
pushq $0x10
popq %rsi
leaq 0x705e(%rip), %rdx      # 0xa141
movl %r14d, %ecx
movl -0x4c(%rbp), %r8d
xorl %eax, %eax
callq 0x2600
movl 0x18(%rbx), %eax
addl 0x3c(%rbx), %eax
cmpl %eax, %r14d
jns 0x33c8
movl 0xd0(%rbx), %eax
leal 0x1(%rax), %ecx
cmpl 0xd4(%rbx), %ecx
jbe 0x331d
pushq $0x8
popq %rax
movq %rax, %r15
addq %rax, %rax
cmpq %rcx, %r15
jb 0x3118
leaq (,%r15,8), %rdi
callq 0x281f
testq %rax, %rax
je 0x34cb
movq %rax, %r14
movq 0xc8(%rbx), %rdi
testq %rdi, %rdi
je 0x332b
xorl %eax, %eax
movl 0xd0(%rbx), %ecx
cmpq %rcx, %rax
jae 0x3326
movl (%rdi,%rax,8), %ecx
movl %ecx, (%r14,%rax,8)
movl 0x4(%rdi,%rax,8), %ecx
movl %ecx, 0x4(%r14,%rax,8)
incq %rax
jmp 0x314e
orb $0x2, 0x48(%rbx)
testb $0x40, 0xf4(%rbx)
je 0x33c8
cmpq $0x0, 0x100(%rbx)
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r12
je 0x3314
movq %rbx, %rdi
pushq $0x40
popq %rsi
leaq 0x6fb4(%rip), %rdx      # 0xa15a
xorl %eax, %eax
callq 0x2600
jmp 0x3314
testb $-0x80, 0xf4(%rbx)
je 0x33c8
cmpq $0x0, 0x100(%rbx)
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r12
je 0x3314
movq %rbx, %rdi
movl $0x80, %esi
leaq 0x6f82(%rip), %rdx      # 0xa166
movl -0x58(%rbp), %ecx
xorl %eax, %eax
callq 0x2600
jmp 0x3314
movl 0x4c(%rbx), %ecx
subl -0x4c(%rbp), %ecx
movl -0x54(%rbp), %r14d
js 0x3273
movl 0x2c(%rbx), %edi
testl %edi, %edi
je 0x3247
movl %ecx, %eax
subl %edi, %eax
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
movslq 0x28(%rbx), %rax
leaq (%rax,%rax,2), %rax
addq %rdx, %rax
cqto
pushq $0x4
popq %rsi
idivq %rsi
movq %rax, %rsi
movl %esi, 0x28(%rbx)
imull $0x7, %edi, %edi
addl %ecx, %edi
movl %edi, %eax
cltd
pushq $0x8
popq %rcx
idivl %ecx
cmpl $0x8, %edi
movl %eax, %ecx
pushq $0x1
popq %rax
cmovll %eax, %ecx
movl %ecx, 0x2c(%rbx)
jmp 0x3251
movl %ecx, 0x2c(%rbx)
movl %ecx, %esi
shrl %esi
movl %esi, 0x28(%rbx)
movl 0x34(%rbx), %eax
movl 0x50(%rbx), %edx
shll $0x2, %esi
cmpl %esi, %edx
cmoval %edx, %esi
addl %ecx, %esi
cmpl %esi, %eax
cmoval %eax, %esi
movl $0xea60, %eax           # imm = 0xEA60
cmpl %eax, %esi
cmovael %eax, %esi
movl %esi, 0x30(%rbx)
cmpl %r9d, %r14d
js 0x32a5
movq -0x60(%rbp), %rax
cmpl (%rax), %r14d
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r12
jns 0x32ad
movq %r8, %rdi
cmpq %r13, %rdi
je 0x32ad
movq (%rdi), %rax
cmpl 0x24(%rdi), %r14d
je 0x33dc
movq %rax, %rdi
jns 0x328c
jmp 0x32ad
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r12
leaq 0x24(%r8), %rax
cmpq %r13, %r8
cmoveq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, 0x10(%rbx)
movl %r14d, %eax
movl -0x50(%rbp), %ecx
subl %ecx, %eax
testl %eax, %eax
cmovgl %r14d, %ecx
cmpl $0x0, -0x34(%rbp)
cmovel %r14d, %ecx
testb $0x20, 0xf4(%rbx)
movl %ecx, -0x50(%rbp)
je 0x330e
cmpq $0x0, 0x100(%rbx)
je 0x330e
movl 0x4c(%rbx), %eax
subl -0x4c(%rbp), %eax
movslq %eax, %r8
movslq 0x30(%rbx), %r9
movq %rbx, %rdi
pushq $0x10
popq %rsi
leaq 0x6e1b(%rip), %rdx      # 0xa11f
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2600
pushq $0x1
popq %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %r8
jmp 0x33d4
movq 0xc8(%rbx), %r14
jmp 0x333f
callq 0x2838
movq %r14, 0xc8(%rbx)
movl %r15d, 0xd4(%rbx)
movl 0xd0(%rbx), %eax
addl %eax, %eax
movl -0x54(%rbp), %ecx
movl %ecx, (%r14,%rax,4)
movl -0x4c(%rbp), %r15d
movl %r15d, 0x4(%r14,%rax,4)
incl 0xd0(%rbx)
cmpl 0x18(%rbx), %ecx
js 0x33c8
movl %ecx, %r14d
movq -0x40(%rbp), %rdi
callq 0x2e7a
movl %r14d, %ecx
movq %rax, %r14
movl -0x6c(%rbp), %eax
movl %eax, 0x10(%r14)
movl $0x51, 0x14(%r14)
movl -0x68(%rbp), %eax
movl %eax, 0x18(%r14)
movl -0x58(%rbp), %eax
movl %eax, 0x1c(%r14)
movl %r15d, 0x20(%r14)
movl %ecx, 0x24(%r14)
movl %r12d, 0x28(%r14)
movq -0x40(%rbp), %r12
movl %r12d, 0x2c(%r14)
testl %r12d, %r12d
je 0x33b4
leaq 0x40(%r14), %rdi
movq -0x30(%rbp), %rsi
movq %r12, %rdx
callq 0x2220
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e8b
movq -0x48(%rbp), %r15
jmp 0x3314
movq -0x48(%rbp), %r15
movq -0x30(%rbp), %r8
movq -0x40(%rbp), %r12
addq %r12, %r8
jmp 0x2fe1
movq 0x8(%rdi), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
callq 0x2838
decl 0x60(%rbx)
movq 0xa8(%rbx), %r8
jmp 0x32ad
pushq $-0x3
jmp 0x344e
movl 0x10(%rbx), %eax
cmpl $0x0, -0x34(%rbp)
movl -0x50(%rbp), %esi
je 0x341f
movl %esi, %ecx
subl %eax, %ecx
js 0x341f
movq -0x60(%rbp), %rcx
cmpl (%rcx), %esi
js 0x3462
subl -0x64(%rbp), %eax
xorl %r9d, %r9d
testl %eax, %eax
jle 0x3450
movl 0x40(%rbx), %ecx
movl 0x44(%rbx), %edi
cmpl %ecx, %edi
jae 0x3450
movl 0x8(%rbx), %esi
cmpl 0x24(%rbx), %edi
jae 0x347d
incl %edi
movl %edi, 0x44(%rbx)
addl %esi, 0x80(%rbx)
jmp 0x34b9
pushq $-0x1
jmp 0x344e
pushq $-0x2
popq %r9
movl %r9d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rdx
cmpq %r13, %rdx
je 0x341f
movq %rdx, %rcx
cmpl 0x24(%rdx), %esi
js 0x341f
movq (%rcx), %rdx
je 0x3466
incl 0x38(%rcx)
jmp 0x3466
movl 0x80(%rbx), %r8d
cmpl %esi, %r8d
cmovbel %esi, %r8d
movl %esi, %eax
imull %esi, %eax
xorl %edx, %edx
divl %r8d
movl %esi, %edx
shrl $0x4, %edx
addl %r8d, %edx
addl %eax, %edx
movl %edx, 0x80(%rbx)
leal 0x1(%rdi), %eax
movl %esi, %r8d
imull %eax, %r8d
cmpl %edx, %r8d
ja 0x34b9
movl %eax, 0x44(%rbx)
movl %eax, %edi
cmpl %ecx, %edi
jbe 0x3450
movl %ecx, 0x44(%rbx)
imull %ecx, %esi
movl %esi, 0x80(%rbx)
jmp 0x3450
leaq 0x6ca4(%rip), %rdi      # 0xa176
leaq 0x6b6b(%rip), %rsi      # 0xa044
leaq 0x6ca6(%rip), %rcx      # 0xa186
movl $0x283, %edx            # imm = 0x283
callq 0x21b0