// File: /workspace/github_c_makefile_star2_O2/tvlabs[P]edge264/./edge264_slice.c
// Function: parse_mvd_pair_cabac
static i16x8 CAFUNC(parse_mvd_pair, const uint8_t *absMvd_lx, int i4x4) {
	#if !CABAC
		int x = get_se32(&ctx->t._gb, -32768, 32767);
		int y = get_se32(&ctx->t._gb, -32768, 32767);
		log_mb(ctx, "[%d,%d],", x, y);
		return (i16x8){x, y};
	#else
		i16x8 res;
		for (int ctxBase = 40, i = 0;;) {
			int sum = absMvd_lx[ctx->absMvd_A[i4x4] + i] + absMvd_lx[ctx->absMvd_B[i4x4] + i];
			int ctxIdx = ctxBase + (sum >= 3) + (sum > 32);
			int mvd = 0;
			ctxBase += 3;
			while (mvd < 9 && get_ae(ctx, ctxIdx))
				ctxIdx = ctxBase + min(mvd++, 3);
			if (mvd >= 9) {
				// we need at least 35 (or 21) bits in codIOffset to get 26 (or 12) bypass bits
				int zeros = clz(ctx->t._gb.codIRange);
				if (zeros > (SIZE_BIT == 64 ? 64 - 35 : 32 - 21)) {
					ctx->t._gb.codIOffset = shld(get_bytes(&ctx->t._gb, zeros >> 3), ctx->t._gb.codIOffset, zeros & -8);
					ctx->t._gb.codIRange <<= zeros & -8;
					zeros &= 7;
				}
				// for 64-bit we could shift codIOffset down to 37 bits to help iterative hardware dividers, but that would make the code harder to maintain
				ctx->t._gb.codIRange >>= SIZE_BIT - 9 - zeros;
				size_t quo = ctx->t._gb.codIOffset / ctx->t._gb.codIRange; // requested bits are in lsb and zeros+9 empty bits above
				size_t rem = ctx->t._gb.codIOffset % ctx->t._gb.codIRange;
				int k = 3 + clz(~quo << (zeros + 9) | (size_t)1 << (SIZE_BIT - 12));
				int unused = SIZE_BIT - 9 - zeros - k * 2 + 1;
				#if SIZE_BIT == 32
					if (__builtin_expect(unused < 0, 0)) { // FIXME needs testing
						// refill codIOffset with 16 bits then make a new division
						ctx->t._gb.codIOffset = shld(get_bytes(&ctx->t._gb, 2), rem, 16);
						quo = shld((ctx->t._gb.codIOffset / ctx->t._gb.codIRange) << (SIZE_BIT - 16), quo, 16);
						rem = ctx->t._gb.codIOffset % ctx->t._gb.codIRange;
						unused += 16;
					}
				#endif
				mvd = 1 + (1 << k | (quo >> unused >> 1 & (((size_t)1 << k) - 1)));
				mvd = (quo & (size_t)1 << unused) ? -mvd : mvd;
				ctx->t._gb.codIOffset = (quo & (((size_t)1 << unused) - 1)) * ctx->t._gb.codIRange + rem;
				ctx->t._gb.codIRange <<= unused;
			} else if (mvd > 0) {
				mvd = get_bypass(ctx) ? -mvd : mvd;
			}
			
			if (++i == 2) {
				res[1] = mvd;
				log_mb(ctx, "    \"%tu\": [%d,%d]\n", absMvd_lx - mb->absMvd + i4x4, res[0], mvd);
				return res;
			}
			ctxBase = 47;
			res = (i16x8){mvd};
		}
	#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movl %edx, %eax
movq %rax, 0x8(%rsp)
pushq $0x28
popq %r14
xorl %r13d, %r13d
pushq $0x3
popq %rbp
movq 0x8(%rsp), %rdx
movswq 0xc40(%rbx,%rdx,2), %rax
movslq %r13d, %rcx
addq %rcx, %rax
movzbl (%r15,%rax), %eax
movslq 0xc80(%rbx,%rdx,4), %rdx
addq %rcx, %rdx
movzbl (%r15,%rdx), %ecx
addl %eax, %ecx
cmpl $0x3, %ecx
movl %r14d, %esi
sbbl $-0x1, %esi
cmpl $0x21, %ecx
sbbl $-0x1, %esi
addl $0x3, %r14d
xorl %r12d, %r12d
cmpl $0x9, %r12d
je 0x2b0b9
movq %rbx, %rdi
callq 0x2aced
testl %eax, %eax
je 0x2b101
leal 0x1(%r12), %eax
cmpl $0x3, %r12d
cmovael %ebp, %r12d
movl %r12d, %esi
addl %r14d, %esi
movl %eax, %r12d
jmp 0x2b08f
movq 0x10(%rbx), %rcx
lzcntq %rcx, %r12
movq %rcx, %rax
shrq $0x22, %rax
jne 0x2b123
movl %r12d, %esi
shrl $0x3, %esi
movq %rbx, %rdi
callq 0x21e4a
movq %rax, %rdx
movq 0x18(%rbx), %rax
movl %r12d, %ecx
andl $0x38, %ecx
movq %rax, 0x20(%rsp)
shldq %cl, %rdx, %rax
movl %r12d, %ecx
andb $0x38, %cl
shlxq %rcx, 0x10(%rbx), %rcx
andl $0x7, %r12d
jmp 0x2b127
testl %r12d, %r12d
je 0x2b1a8
movq %rbx, %rdi
callq 0x2b2e5
movl %r12d, %r14d
negl %r14d
testl %eax, %eax
cmovel %r12d, %r14d
jmp 0x2b1ab
movq 0x18(%rbx), %rax
movb $0x37, %dl
subb %r12b, %dl
shrxq %rdx, %rcx, %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
notq %rsi
leal 0x9(%r12), %edi
shlxq %rdi, %rsi, %rsi
btsq $0x34, %rsi
lzcntq %rsi, %rsi
leal 0x3(%rsi), %edi
leal (%r12,%rsi,2), %r8d
addl $0x6, %r8d
pushq $0x8
popq %r9
shlxl %esi, %r9d, %r9d
movb $0x38, %sil
subb %r8b, %sil
shrxq %rsi, %rax, %r8
shrq %r8
bzhil %edi, %r8d, %r14d
orl %r9d, %r14d
leal 0x1(%r14), %edi
movzbl %sil, %r8d
btq %r8, %rax
notl %r14d
cmovael %edi, %r14d
bzhiq %r8, %rax, %rax
imulq %rcx, %rax
addq %rdx, %rax
movq %rax, 0x18(%rbx)
shlxq %rsi, %rcx, %rax
movq %rax, 0x10(%rbx)
jmp 0x2b1ab
xorl %r14d, %r14d
incl %r13d
cmpl $0x2, %r13d
je 0x2b1cb
movzwl %r14w, %eax
vmovd %eax, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
pushq $0x2f
popq %r14
jmp 0x2b04e
movzwl 0x1cd0(%rbx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x2b228
leaq (%rbx,%rax), %rdi
addq $0x1cd2, %rdi           # imm = 0x1CD2
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
movq 0x8(%rsp), %rcx
addq %r15, %rcx
subq 0x680(%rbx), %rcx
addq $-0x70, %rcx
vmovdqa 0x10(%rsp), %xmm0
vmovd %xmm0, %eax
movswl %ax, %r8d
leaq 0x9651(%rip), %rdx      # 0x34868
movl %r14d, %r9d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
vmovdqa 0x10(%rsp), %xmm0
vpinsrw $0x1, %r14d, %xmm0, %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq