// File: Alcaro[P]bridges/arlib/bytepipe.cpp
// Function: bytepipe::pull_begin(unsigned long)
bytesw bytepipe::pull_begin(size_t nbytes)
{
	// - if the requested amount doesn't exist, just say so
	// - if buf1 is empty, swap the buffers
	// - if the requested amount exists in buf1 (after the swap, if applicable), return that
	// - if the requested amount fits in buf1, memmove+memcpy and set buf2st nonzero
	// - else an allocation must be made
	if (size() < nbytes)
	{
		return {};
	}
	try_swap();
	size_t size1 = buf1end - buf1st;
	if (size1 >= nbytes)
	{
		return buf1.slice(buf1st, size1);
	}
	if (nbytes <= buf1.size())
	{
		size_t size2 = nbytes - size1;
		if (buf1st != 0)
			memmove(buf1.ptr(), buf1.ptr()+buf1st, size1);
		memcpy(buf1.ptr()+size1, buf2.ptr()+buf2st, size2);
		buf1st = 0;
		buf1end = nbytes;
		buf2st += size2;
		return buf1.slice(0, nbytes);
	}
	else
	{
		size_t size2 = buf2end-buf2st;
		bytearray newbuf;
		newbuf.resize(bitround(size()));
		memcpy(newbuf.ptr(), buf1.ptr()+buf1st, size1);
		memcpy(newbuf.ptr()+size1, buf2.ptr()+buf2st, size2);
		buf1 = std::move(newbuf);
		buf1st = 0;
		buf1end = size1+size2;
		buf2st = 0;
		buf2end = 0;
		return buf1.slice(0, buf1end);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x68(%rbp)
movq %rdi, %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x58(%rbp)
callq 0xc4a0
cmpq -0x18(%rbp), %rax
jae 0xbda3
movq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4170
movq -0x68(%rbp), %rdi
callq 0x7530
jmp 0xbfbd
movq -0x58(%rbp), %rdi
callq 0xbb80
movq -0x58(%rbp), %rcx
movq 0x18(%rcx), %rax
subq 0x10(%rcx), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jb 0xbde0
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdi
movq 0x10(%rsi), %rdx
movq -0x20(%rbp), %rcx
callq 0xc4c0
jmp 0xbfbd
movq -0x58(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0x6290
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
ja 0xbeb2
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
subq -0x20(%rbp), %rcx
movq %rcx, -0x28(%rbp)
cmpq $0x0, 0x10(%rax)
je 0xbe46
movq -0x58(%rbp), %rdi
callq 0x7490
movq -0x58(%rbp), %rdi
movq %rax, -0x78(%rbp)
callq 0x7490
movq -0x78(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rax
addq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x45f0
movq -0x58(%rbp), %rdi
callq 0x7490
movq -0x58(%rbp), %rdi
addq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
addq $0x20, %rdi
callq 0x7490
movq -0x80(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rax
addq 0x30(%rax), %rsi
movq -0x28(%rbp), %rdx
callq 0x4280
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq $0x0, 0x10(%rsi)
movq -0x18(%rbp), %rax
movq %rax, 0x18(%rsi)
movq -0x28(%rbp), %rax
addq 0x30(%rsi), %rax
movq %rax, 0x30(%rsi)
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0xc4c0
jmp 0xbfbd
movq -0x58(%rbp), %rcx
movq 0x38(%rcx), %rax
subq 0x30(%rcx), %rax
movq %rax, -0x30(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x74b0
movq -0x58(%rbp), %rdi
callq 0xc4a0
movq %rax, %rdi
callq 0xbfd0
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x1ce60
leaq -0x40(%rbp), %rdi
callq 0x7490
movq -0x58(%rbp), %rdi
movq %rax, -0x90(%rbp)
callq 0x7490
movq -0x90(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rax
addq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x4280
leaq -0x40(%rbp), %rdi
callq 0x7490
movq -0x58(%rbp), %rdi
addq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
addq $0x20, %rdi
callq 0x7490
movq -0x88(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rax
addq 0x30(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x4280
leaq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1d320
movq -0x58(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1d440
leaq -0x50(%rbp), %rdi
callq 0x1d720
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq $0x0, 0x10(%rsi)
movq -0x20(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, 0x18(%rsi)
movq $0x0, 0x30(%rsi)
movq $0x0, 0x38(%rsi)
movq 0x18(%rsi), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0xc4c0
leaq -0x40(%rbp), %rdi
callq 0x1d720
movq -0x60(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)