// File: /workspace/github_c_makefile_star2_O3/tvlabs[P]edge264/./edge264_bitstream.c
// Function: refill
static inline size_t get_bytes(Edge264GetBits *gb, int nbytes)
{
	static const i8x16 shuf[8] = {
		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1},
		{0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, -1},
	};
	
	// load 16 bytes without reading past aligned buffer boundaries
	const uint8_t *CPB = gb->CPB;
	const uint8_t *end = gb->end;
	intptr_t diff = CPB - end;
	u8x16 v;
	if (__builtin_expect(diff <= -14, 1)) {
		v = load128(CPB - 2);
	} else if (diff < 0) {
		const uint8_t *p = minp(CPB - 2, (uint8_t *)((uintptr_t)(end - 1) & -16));
		v = shrv128(shlv128(load128(p), p + 16 - end), min(14 + diff, 16));
	} else {
		gb->CPB = CPB + nbytes;
		return 0;
	}
	
	// make a bitmask for the positions of 00n escape sequences and iterate on it
	u8x16 eq0 = v == 0;
	u8x16 x = shr128(v, 2);
	#if defined(__SSE2__)
		unsigned esc = movemask(eq0 & shr128(eq0, 1) & (x <= 3));
		if (__builtin_expect(esc, 0)) {
			while (esc & ((1 << nbytes) - 1)) {
				int i = __builtin_ctz(esc);
				esc = (esc & (esc - 1)) >> 1;
				if (CPB[i] <3) { // delimiter sequence, set end to its first byte
					gb->end = CPB + i - 2;
					x &= ~shlv128(set8(-1), i);
					break; // we cannot iterate more since now x differs from esc
				}
				// otherwise this is an emulation_prevention_three_byte -> remove it
				x = shuffle(x, shuf[i]);
				CPB++;
			}
		}
	#elif defined(__ARM_NEON)
		uint64_t esc = (uint64_t)vshrn_n_u16(eq0 & shr128(eq0, 1) & (x <= 3), 4);
		if (__builtin_expect(esc, 0)) {
			esc &= 0x8888888888888888ULL;
			while (esc & ((1ULL << (nbytes << 2)) - 1)) {
				int i = __builtin_ctzll(esc) >> 2;
				esc = (esc & (esc - 1)) >> 4;
				if (CPB[i] <3) { // delimiter sequence, set end to its first byte
					gb->end = CPB + i - 2;
					x &= ~shlv128(set8(-1), i);
					break; // we cannot iterate more since now x differs from esc
				}
				// otherwise this is an emulation_prevention_three_byte -> remove it
				x = shuffle(x, shuf[i]);
				CPB++;
			}
		}
	#endif
	
	// increment CPB and return the requested bytes in upper part of the result
	gb->CPB = CPB + nbytes;
	#if SIZE_BIT == 32
		return big_endian32(((i32x4)x)[0]);
	#elif SIZE_BIT == 64
		return big_endian64(((i64x2)x)[0]);
	#endif
}

// Assembly:
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movq %rax, %rcx
subq %rdx, %rcx
cmpq $-0xe, %rcx
jg 0xef8b
vmovdqu -0x2(%rax), %xmm1
vpxor %xmm0, %xmm0, %xmm0
vpcmpeqb %xmm0, %xmm1, %xmm2
vpsrldq $0x2, %xmm1, %xmm0      # xmm0 = xmm1[2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero,zero
vpsrldq $0x1, %xmm2, %xmm2      # xmm2 = xmm2[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],zero
vpbroadcastb 0x24bc0(%rip), %xmm3 # 0x33af6
vpcmpltub %xmm3, %xmm0, %k1
vptestnmb %xmm1, %xmm1, %k0 {%k1}
vpmovb2m %xmm2, %k1
ktestw %k1, %k0
jne 0xef9b
addq $0x8, %rax
movq %rax, (%rdi)
vmovq %xmm0, %rax
bswapq %rax
movq 0x18(%rdi), %rcx
tzcntq %rcx, %rdx
btcq %rdx, %rcx
leaq 0x1(,%rax,2), %rsi
shlxq %rdx, %rsi, %rsi
notb %dl
shrxq %rdx, %rax, %rax
orq %rcx, %rax
movq %rax, 0x18(%rdi)
movq %rsi, 0x10(%rdi)
retq
testq %rcx, %rcx
js 0xefe4
addq $0x8, %rax
movq %rax, (%rdi)
xorl %eax, %eax
jmp 0xef5e
kandw %k1, %k0, %k0
kmovd %k0, %ecx
testb %cl, %cl
je 0xef4f
movzwl %cx, %edx
leaq 0x280af(%rip), %rcx     # 0x37060
tzcntl %edx, %esi
cmpb $0x2, (%rax,%rsi)
jbe 0xf018
leal 0x7fffffff(%rdx), %r8d
andl %edx, %r8d
movl %r8d, %edx
shrl %edx
shll $0x4, %esi
vpshufb (%rsi,%rcx), %xmm0, %xmm0
incq %rax
testl $0x1fe, %r8d            # imm = 0x1FE
jne 0xefb1
jmp 0xef4f
leaq -0x2(%rax), %rsi
leaq -0x1(%rdx), %r8
andq $-0x10, %r8
cmpq %r8, %rsi
cmovsq %rsi, %r8
vmovdqu (%r8), %xmm0
subq %r8, %rdx
leaq 0x2525a(%rip), %rsi     # 0x34260
vpshufb (%rdx,%rsi), %xmm0, %xmm0
vpshufb 0x1e(%rcx,%rsi), %xmm0, %xmm1
jmp 0xef1b
leaq (%rax,%rsi), %rcx
addq $-0x2, %rcx
movq %rcx, 0x8(%rdi)
leaq 0x25235(%rip), %rcx     # 0x34260
subq %rsi, %rcx
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb 0x10(%rcx), %xmm1, %xmm1
vpandn %xmm0, %xmm1, %xmm0
jmp 0xef4f