// File: /workspace/github_cpp_makefile_star2_O2/xocoder[P]m_kcptun/vendor/kcp/ikcp.c
// Function: ikcp_recv
int ikcp_recv(ikcpcb *kcp, char *buffer, int len)
{
	struct IQUEUEHEAD *p;
	int ispeek = (len < 0)? 1 : 0;
	int peeksize;
	int recover = 0;
	IKCPSEG *seg;
	assert(kcp);

	if (iqueue_is_empty(&kcp->rcv_queue))
		return -1;

	if (len < 0) len = -len;

	peeksize = ikcp_peeksize(kcp);

	if (peeksize < 0) 
		return -2;

	if (peeksize > len) 
		return -3;

	if (kcp->nrcv_que >= kcp->rcv_wnd)
		recover = 1;

	// merge fragment
	for (len = 0, p = kcp->rcv_queue.next; p != &kcp->rcv_queue; ) {
		int fragment;
		seg = iqueue_entry(p, IKCPSEG, node);
		p = p->next;

		if (buffer) {
			memcpy(buffer, seg->data, seg->len);
			buffer += seg->len;
		}

		len += seg->len;
		fragment = seg->frg;

		if (ikcp_canlog(kcp, IKCP_LOG_RECV)) {
			ikcp_log(kcp, IKCP_LOG_RECV, "recv sn=%lu", seg->sn);
		}

		if (ispeek == 0) {
			iqueue_del(&seg->node);
			ikcp_segment_delete(kcp, seg);
			kcp->nrcv_que--;
		}

		if (fragment == 0) 
			break;
	}

	assert(len == peeksize);

	// move available data from rcv_buf -> rcv_queue
	while (! iqueue_is_empty(&kcp->rcv_buf)) {
		IKCPSEG *seg = iqueue_entry(kcp->rcv_buf.next, IKCPSEG, node);
		if (seg->sn == kcp->rcv_nxt && kcp->nrcv_que < kcp->rcv_wnd) {
			iqueue_del(&seg->node);
			kcp->nrcv_buf--;
			iqueue_add_tail(&seg->node, &kcp->rcv_queue);
			kcp->nrcv_que++;
			kcp->rcv_nxt++;
		}	else {
			break;
		}
	}

	// fast recover
	if (kcp->nrcv_que < kcp->rcv_wnd && recover) {
		// ready to send back IKCP_CMD_WINS in ikcp_flush
		// tell remote my window size
		kcp->probe |= IKCP_ASK_TELL;
	}

	return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, -0x2c(%rbp)
testq %rdi, %rdi
je 0x2b4b
movq %rdi, %r14
leaq 0x98(%rdi), %rax
movq %rax, -0x40(%rbp)
cmpq 0x98(%rdi), %rax
je 0x29d9
movq %rsi, %r12
movq %r14, %rdi
callq 0x2b89
testl %eax, %eax
js 0x29dd
movl -0x2c(%rbp), %edx
movl %edx, %ecx
negl %ecx
cmovsl %edx, %ecx
cmpl %ecx, %eax
jbe 0x29ef
pushq $-0x3
jmp 0x29df
pushq $-0x1
jmp 0x29df
pushq $-0x2
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, -0x38(%rbp)
movl 0x3c(%r14), %eax
movl %eax, -0x34(%rbp)
movl 0x64(%r14), %eax
movl %eax, -0x30(%rbp)
movq 0x98(%r14), %r13
xorl %r15d, %r15d
cmpq -0x40(%rbp), %r13
je 0x2ab7
movq (%r13), %rax
movq %rax, -0x50(%rbp)
testq %r12, %r12
je 0x2a3e
leaq 0x40(%r13), %rsi
movl 0x2c(%r13), %edx
movq %r12, %rdi
callq 0x2220
movl 0x2c(%r13), %ebx
addq %rbx, %r12
movq %r12, -0x48(%rbp)
jmp 0x2a4a
movl 0x2c(%r13), %ebx
movq $0x0, -0x48(%rbp)
movl 0x18(%r13), %r12d
testb $0x8, 0xf4(%r14)
je 0x2a7a
cmpq $0x0, 0x100(%r14)
je 0x2a7a
movl 0x24(%r13), %ecx
movq %r14, %rdi
pushq $0x8
popq %rsi
leaq 0x7624(%rip), %rdx      # 0xa097
xorl %eax, %eax
callq 0x2600
cmpl $0x0, -0x2c(%rbp)
js 0x2aa3
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq %r13, %rdi
callq 0x2838
decl 0x64(%r14)
addl %ebx, %r15d
movq -0x50(%rbp), %r13
testl %r12d, %r12d
movq -0x48(%rbp), %r12
jne 0x2a0a
movl -0x38(%rbp), %eax
cmpl %eax, %r15d
jne 0x2b6a
leaq 0xb8(%r14), %r9
xorps %xmm0, %xmm0
movq (%r9), %rsi
cmpq %rsi, %r9
je 0x2b27
movl 0x24(%rsi), %edx
movl 0x64(%r14), %ecx
cmpl 0x18(%r14), %edx
jne 0x2b2b
cmpl 0x3c(%r14), %ecx
jae 0x2b2b
movq (%rsi), %rdi
movq 0x8(%rsi), %r8
movq %r8, 0x8(%rdi)
movq %rdi, (%r8)
movups %xmm0, (%rsi)
decl 0x5c(%r14)
movq 0xa0(%r14), %rdi
movq %rdi, 0x8(%rsi)
movq -0x40(%rbp), %r8
movq %r8, (%rsi)
movq %rsi, (%rdi)
movq %rsi, 0xa0(%r14)
incl %ecx
movl %ecx, 0x64(%r14)
incl %edx
movl %edx, 0x18(%r14)
jmp 0x2acd
movl 0x64(%r14), %ecx
movl -0x30(%rbp), %edx
cmpl -0x34(%rbp), %edx
jb 0x29e0
cmpl 0x3c(%r14), %ecx
jae 0x29e0
orb $0x2, 0x48(%r14)
jmp 0x29e0
leaq 0x74ee(%rip), %rdi      # 0xa040
leaq 0x74eb(%rip), %rsi      # 0xa044
leaq 0x7512(%rip), %rcx      # 0xa072
movl $0x16b, %edx            # imm = 0x16B
callq 0x21b0
leaq 0x7532(%rip), %rdi      # 0xa0a3
leaq 0x74cc(%rip), %rsi      # 0xa044
leaq 0x74f3(%rip), %rcx      # 0xa072
movl $0x199, %edx            # imm = 0x199
callq 0x21b0