// File: /workspace/github_cpp_makefile_star2_O3/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 0x2c74
movq %rdi, %rbx
leaq 0x98(%rdi), %rax
movq %rax, -0x40(%rbp)
cmpq 0x98(%rdi), %rax
je 0x2c59
movq %rsi, %r12
movq %rbx, %rdi
callq 0x2cb2
testl %eax, %eax
js 0x2c60
movl %eax, %edx
movl -0x2c(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
cmpl %ecx, %edx
ja 0x2c65
movl %edx, -0x38(%rbp)
movl 0x3c(%rbx), %eax
movl %eax, -0x34(%rbp)
movl 0x64(%rbx), %eax
movl %eax, -0x30(%rbp)
movq 0x98(%rbx), %r13
xorl %r15d, %r15d
cmpq -0x40(%rbp), %r13
je 0x2bc7
movq (%r13), %rax
movq %rax, -0x50(%rbp)
testq %r12, %r12
je 0x2b3e
leaq 0x40(%r13), %rsi
movl 0x2c(%r13), %edx
movq %r12, %rdi
callq 0x2230
movl 0x2c(%r13), %r14d
addq %r14, %r12
movq %r12, -0x48(%rbp)
jmp 0x2b4a
movl 0x2c(%r13), %r14d
movq $0x0, -0x48(%rbp)
movl 0x18(%r13), %r12d
testb $0x8, 0xf4(%rbx)
je 0x2b7b
cmpq $0x0, 0x100(%rbx)
je 0x2b7b
movl 0x24(%r13), %ecx
movq %rbx, %rdi
movl $0x8, %esi
leaq 0x8523(%rip), %rdx      # 0xb097
xorl %eax, %eax
callq 0x2630
cmpl $0x0, -0x2c(%rbp)
js 0x2bb3
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq 0xd6f1(%rip), %rax      # 0x10290
movq %r13, %rdi
testq %rax, %rax
je 0x2bab
callq *%rax
jmp 0x2bb0
callq 0x2320
decl 0x64(%rbx)
addl %r14d, %r15d
movq -0x50(%rbp), %r13
testl %r12d, %r12d
movq -0x48(%rbp), %r12
jne 0x2b0a
movl -0x38(%rbp), %r9d
cmpl %r9d, %r15d
jne 0x2c93
leaq 0xb8(%rbx), %rax
movq 0xb8(%rbx), %rdx
cmpq %rdx, %rax
je 0x2c40
movl 0x18(%rbx), %ecx
xorps %xmm0, %xmm0
cmpl 0x24(%rdx), %ecx
jne 0x2c40
movl 0x64(%rbx), %esi
cmpl 0x3c(%rbx), %esi
jae 0x2c40
movq (%rdx), %rdi
movq 0x8(%rdx), %r8
movq %r8, 0x8(%rdi)
movq %rdi, (%r8)
movups %xmm0, (%rdx)
decl 0x5c(%rbx)
movq 0xa0(%rbx), %rdi
movq %rdi, 0x8(%rdx)
movq -0x40(%rbp), %r8
movq %r8, (%rdx)
movq %rdx, (%rdi)
movq %rdx, 0xa0(%rbx)
incl %esi
movl %esi, 0x64(%rbx)
incl %ecx
movl %ecx, 0x18(%rbx)
movq 0xb8(%rbx), %rdx
cmpq %rdx, %rax
jne 0x2bed
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jb 0x2c54
movl 0x3c(%rbx), %eax
cmpl %eax, 0x64(%rbx)
jae 0x2c54
orb $0x2, 0x48(%rbx)
movl %r9d, %eax
jmp 0x2c65
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2c65
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x83c5(%rip), %rdi      # 0xb040
leaq 0x83c2(%rip), %rsi      # 0xb044
leaq 0x83e9(%rip), %rcx      # 0xb072
movl $0x16b, %edx            # imm = 0x16B
callq 0x21b0
leaq 0x8409(%rip), %rdi      # 0xb0a3
leaq 0x83a3(%rip), %rsi      # 0xb044
leaq 0x83ca(%rip), %rcx      # 0xb072
movl $0x199, %edx            # imm = 0x199
callq 0x21b0