// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movq %rdi, %rbx
movl %edx, %r14d
movl $0x28, %r15d
xorl %r13d, %r13d
movl $0x3, %ebp
movswq 0xc40(%rbx,%r14,2), %rax
movslq %r13d, %rcx
addq %rcx, %rax
movq (%rsp), %rsi
movzbl (%rsi,%rax), %eax
movslq 0xc80(%rbx,%r14,4), %rdx
addq %rcx, %rdx
movzbl (%rsi,%rdx), %ecx
addl %eax, %ecx
cmpl $0x3, %ecx
movl %r15d, %esi
sbbl $-0x1, %esi
cmpl $0x21, %ecx
sbbl $-0x1, %esi
addl $0x3, %r15d
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x3102b
testl %eax, %eax
je 0x31495
leal 0x1(%r12), %eax
cmpl $0x3, %r12d
cmovael %ebp, %r12d
addl %r15d, %r12d
movl %r12d, %esi
movl %eax, %r12d
cmpl $0x9, %eax
jne 0x313c6
movq 0x10(%rbx), %rcx
lzcntq %rcx, %rsi
movq %rcx, %rax
shrq $0x22, %rax
jne 0x31531
movl %esi, %ecx
shrl $0x3, %ecx
movq (%rbx), %rax
movq 0x8(%rbx), %rdi
movq %rax, %rdx
subq %rdi, %rdx
cmpq $-0xe, %rdx
jg 0x3160a
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 0x64d8(%rip), %xmm3 # 0x3791b
vpcmpltub %xmm3, %xmm0, %k1
vptestnmb %xmm1, %xmm1, %k0 {%k1}
vpmovb2m %xmm2, %k1
ktestw %k1, %k0
jne 0x31622
movl %ecx, %ecx
addq %rax, %rcx
movq %rcx, (%rbx)
vmovq %xmm0, %rdx
bswapq %rdx
movq 0x18(%rbx), %rax
movl %esi, %ecx
andl $0x38, %ecx
movq %rax, 0x8(%rsp)
shldq %cl, %rdx, %rax
movl %esi, %ecx
andb $0x38, %cl
shlxq %rcx, 0x10(%rbx), %rcx
andl $0x7, %esi
jmp 0x31535
testl %r12d, %r12d
je 0x315b7
movq 0x10(%rbx), %rcx
cmpq $0x200, %rcx            # imm = 0x200
jae 0x315bc
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rax, %rcx
subq %rdx, %rcx
cmpq $-0xe, %rcx
jg 0x31682
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 0x6435(%rip), %xmm3 # 0x3791b
vpcmpltub %xmm3, %xmm0, %k1
vptestnmb %xmm1, %xmm1, %k0 {%k1}
vpmovb2m %xmm2, %k1
ktestw %k1, %k0
jne 0x31699
addq $0x6, %rax
movq %rax, (%rbx)
vmovq %xmm0, %rdx
bswapq %rdx
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rsp)
movl $0x30, %ecx
shldq %cl, %rdx, %rax
movq 0x10(%rbx), %rcx
shlq $0x30, %rcx
jmp 0x315c0
movq 0x18(%rbx), %rax
movb $0x37, %dl
subb %sil, %dl
shrxq %rdx, %rcx, %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %rdi
notq %rdi
leal 0x9(%rsi), %r8d
shlxq %r8, %rdi, %rdi
btsq $0x34, %rdi
lzcntq %rdi, %rdi
leal 0x3(%rdi), %r8d
leal (%rsi,%rdi,2), %r9d
addl $0x6, %r9d
movl $0x8, %esi
shlxl %edi, %esi, %edi
movb $0x38, %sil
subb %r9b, %sil
shrxq %rsi, %rax, %r9
shrq %r9
bzhil %r8d, %r9d, %r15d
orl %edi, %r15d
leal 0x1(%r15), %edi
movzbl %sil, %r8d
btq %r8, %rax
notl %r15d
cmovael %edi, %r15d
bzhiq %r8, %rax, %rax
imulq %rcx, %rax
addq %rdx, %rax
movq %rax, 0x18(%rbx)
shlxq %rsi, %rcx, %rax
movq %rax, 0x10(%rbx)
jmp 0x315e4
xorl %r15d, %r15d
jmp 0x315e4
movq 0x18(%rbx), %rax
shrq %rcx
movq %rcx, 0x10(%rbx)
movl %r12d, %r15d
negl %r15d
cmpq %rcx, %rax
movl $0x0, %edx
cmovaeq %rcx, %rdx
cmovbl %r12d, %r15d
subq %rdx, %rax
movq %rax, 0x18(%rbx)
incl %r13d
cmpl $0x2, %r13d
je 0x3179e
movzwl %r15w, %eax
vmovd %eax, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movl $0x2f, %r15d
jmp 0x31388
testq %rdx, %rdx
js 0x316e5
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, (%rbx)
xorl %edx, %edx
jmp 0x31470
kandw %k1, %k0, %k0
kmovd %k0, %edx
movzwl %dx, %edi
bzhil %ecx, %edi, %edx
je 0x31460
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
shlxl %ecx, %edx, %edx
notl %edx
tzcntl %edi, %r8d
cmpb $0x2, (%rax,%r8)
jbe 0x31719
leal 0x7fffffff(%rdi), %r9d
andl %edi, %r9d
shrl %r9d
shll $0x4, %r8d
leaq 0x7024(%rip), %rdi      # 0x38690
vpshufb (%r8,%rdi), %xmm0, %xmm0
incq %rax
movl %r9d, %edi
testl %edx, %r9d
jne 0x31644
jmp 0x31460
testq %rcx, %rcx
js 0x31742
addq $0x6, %rax
movq %rax, (%rbx)
xorl %edx, %edx
jmp 0x31512
kandw %k1, %k0, %k0
kmovd %k0, %ecx
movzwl %cx, %ecx
testb $0x3f, %cl
je 0x31503
tzcntl %ecx, %edx
cmpb $0x2, (%rax,%rdx)
jbe 0x31775
leal 0x7fffffff(%rcx), %esi
andl %ecx, %esi
movl %esi, %ecx
shrl %ecx
shll $0x4, %edx
leaq 0x6fbf(%rip), %rdi      # 0x38690
vpshufb (%rdx,%rdi), %xmm0, %xmm0
incq %rax
testb $0x7e, %sil
jne 0x316ad
jmp 0x31503
leaq -0x2(%rax), %r8
leaq -0x1(%rdi), %r9
andq $-0x10, %r9
cmpq %r9, %r8
cmovsq %r8, %r9
vmovdqu (%r9), %xmm0
subq %r9, %rdi
leaq 0x7009(%rip), %r8       # 0x38710
vpshufb (%rdi,%r8), %xmm0, %xmm0
vpshufb 0x1e(%rdx,%r8), %xmm0, %xmm1
jmp 0x31428
leaq (%rax,%r8), %rdx
addq $-0x2, %rdx
movq %rdx, 0x8(%rbx)
leaq 0x6fe4(%rip), %rdx      # 0x38710
subq %r8, %rdx
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb 0x10(%rdx), %xmm1, %xmm1
vpandn %xmm0, %xmm1, %xmm0
jmp 0x31460
leaq -0x2(%rax), %rsi
leaq -0x1(%rdx), %rdi
andq $-0x10, %rdi
cmpq %rdi, %rsi
cmovsq %rsi, %rdi
vmovdqu (%rdi), %xmm0
subq %rdi, %rdx
leaq 0x6fad(%rip), %rsi      # 0x38710
vpshufb (%rdx,%rsi), %xmm0, %xmm0
vpshufb 0x1e(%rcx,%rsi), %xmm0, %xmm1
jmp 0x314cb
leaq (%rax,%rdx), %rcx
addq $-0x2, %rcx
movq %rcx, 0x8(%rbx)
leaq 0x6f88(%rip), %rcx      # 0x38710
subq %rdx, %rcx
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb 0x10(%rcx), %xmm1, %xmm1
vpandn %xmm0, %xmm1, %xmm0
jmp 0x31503
movzwl 0x1cd0(%rbx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x317fa
leaq (%rbx,%rax), %rdi
addq $0x1cd2, %rdi           # imm = 0x1CD2
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
addq (%rsp), %r14
subq 0x680(%rbx), %r14
addq $-0x70, %r14
vmovdqa 0x10(%rsp), %xmm0
vmovd %xmm0, %eax
movswl %ax, %r8d
leaq 0x91d2(%rip), %rdx      # 0x3a9b8
movq %r14, %rcx
movl %r15d, %r9d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
vmovdqa 0x10(%rsp), %xmm0
vpinsrw $0x1, %r15d, %xmm0, %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq