// File: /workspace/github_cpp_makefile_star2_O1/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, -0x30(%rbp)
testq %rdi, %rdi
je 0x2c1e
movq %rdi, %rbx
leaq 0x98(%rdi), %rax
movq %rax, -0x48(%rbp)
cmpq 0x98(%rdi), %rax
je 0x2c03
movq %rsi, %r12
movq %rbx, %rdi
callq 0x2c5c
testl %eax, %eax
js 0x2c0a
movl %eax, %edx
movl -0x30(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
cmpl %ecx, %edx
jg 0x2c0f
movl %edx, -0x3c(%rbp)
movl 0x3c(%rbx), %eax
movl %eax, -0x38(%rbp)
movl 0x64(%rbx), %eax
movl %eax, -0x34(%rbp)
movq 0x98(%rbx), %r13
movl $0x0, -0x2c(%rbp)
cmpq -0x48(%rbp), %r13
je 0x2b71
movq (%r13), %r15
testq %r12, %r12
je 0x2aeb
leaq 0x40(%r13), %rsi
movl 0x2c(%r13), %edx
movq %r12, %rdi
callq 0x2220
movl 0x2c(%r13), %eax
addq %rax, %r12
jmp 0x2aee
xorl %r12d, %r12d
movl 0x18(%r13), %r14d
movl -0x2c(%rbp), %eax
addl 0x2c(%r13), %eax
movl %eax, -0x2c(%rbp)
testb $0x8, 0xf4(%rbx)
je 0x2b29
cmpq $0x0, 0x100(%rbx)
je 0x2b29
movl 0x24(%r13), %ecx
movq %rbx, %rdi
movl $0x8, %esi
leaq 0x8575(%rip), %rdx      # 0xb097
xorl %eax, %eax
callq 0x2620
cmpl $0x0, -0x30(%rbp)
js 0x2b65
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%r13), %rcx
movq %rax, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq 0xd73f(%rip), %rax      # 0x10290
movq %r13, %rdi
testq %rax, %rax
je 0x2b5d
callq *%rax
jmp 0x2b62
callq 0x2310
decl 0x64(%rbx)
movq %r15, %r13
testl %r14d, %r14d
jne 0x2abf
movl -0x2c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jne 0x2c3d
leaq 0xb8(%rbx), %r9
movq 0xb8(%rbx), %rdx
cmpq %rdx, %r9
je 0x2bed
movl 0x18(%rbx), %ecx
xorps %xmm0, %xmm0
cmpl 0x24(%rdx), %ecx
jne 0x2bed
movl 0x64(%rbx), %esi
cmpl 0x3c(%rbx), %esi
jae 0x2bed
movq (%rdx), %rdi
movq 0x8(%rdx), %r8
movq %r8, 0x8(%rdi)
movq 0x8(%rdx), %r8
movq %rdi, (%r8)
movups %xmm0, (%rdx)
decl 0x5c(%rbx)
movq 0xa0(%rbx), %rdi
movq %rdi, 0x8(%rdx)
movq -0x48(%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, %r9
jne 0x2b96
movl -0x34(%rbp), %ecx
cmpl -0x38(%rbp), %ecx
jb 0x2c0f
movl 0x3c(%rbx), %ecx
cmpl %ecx, 0x64(%rbx)
jae 0x2c0f
orb $0x2, 0x48(%rbx)
jmp 0x2c0f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2c0f
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x841b(%rip), %rdi      # 0xb040
leaq 0x8418(%rip), %rsi      # 0xb044
leaq 0x843f(%rip), %rcx      # 0xb072
movl $0x16b, %edx            # imm = 0x16B
callq 0x21b0
leaq 0x845f(%rip), %rdi      # 0xb0a3
leaq 0x83f9(%rip), %rsi      # 0xb044
leaq 0x8420(%rip), %rcx      # 0xb072
movl $0x199, %edx            # imm = 0x199
callq 0x21b0