// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl %edx, %eax
leaq 0x1cd2(%rdi), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
addq %rsi, %rax
movq %rax, 0x10(%rsp)
movl $0x28, %ebp
movl $0x3, %r15d
xorl %r13d, %r13d
movq 0x20(%rsp), %rdx
movswq 0xc40(%r14,%rdx,2), %rax
movslq %r13d, %rcx
addq %rcx, %rax
movzbl (%rbx,%rax), %eax
movslq 0xc80(%r14,%rdx,4), %rdx
addq %rcx, %rdx
movzbl (%rbx,%rdx), %ecx
addl %eax, %ecx
cmpl $0x3, %ecx
movl %ebp, %esi
sbbl $-0x1, %esi
cmpl $0x21, %ecx
sbbl $-0x1, %esi
addl $0x3, %ebp
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x306b3
testl %eax, %eax
je 0x30a8e
leal 0x1(%r12), %eax
cmpl $0x3, %r12d
cmovgel %r15d, %r12d
addl %ebp, %r12d
movl %r12d, %esi
movl %eax, %r12d
cmpl $0x9, %eax
jne 0x30a61
movl $0x9, %r12d
cmpl $0x9, %r12d
jb 0x30b40
movq 0x10(%r14), %rax
lzcntq %rax, %rsi
shrq $0x22, %rax
jne 0x30c09
movl %esi, %edx
shrl $0x3, %edx
movq (%r14), %rax
movq 0x8(%r14), %r8
movq %rax, %rdi
subq %r8, %rdi
movl %edx, %ecx
cmpq $-0xe, %rdi
vmovdqa (%rsp), %xmm4
jg 0x30d29
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 0x6cbd(%rip), %xmm3 # 0x377ab
vpcmpltub %xmm3, %xmm0, %k1
vptestnmb %xmm1, %xmm1, %k0 {%k1}
vpmovb2m %xmm2, %k1
ktestw %k1, %k0
jne 0x30d3f
addq %rax, %rcx
movq %rcx, (%r14)
vmovq %xmm0, %rax
bswapq %rax
movq 0x18(%r14), %rdx
movl %esi, %ecx
andl $0x38, %ecx
movq %rdx, 0x28(%rsp)
shldq %cl, %rax, %rdx
movq %rdx, 0x18(%r14)
movl %esi, %ecx
andb $0x38, %cl
shlq %cl, 0x10(%r14)
andl $0x7, %esi
jmp 0x30c0e
testl %r12d, %r12d
vmovdqa (%rsp), %xmm4
je 0x30c94
cmpq $0x1ff, 0x10(%r14)      # imm = 0x1FF
ja 0x30bd6
movq (%r14), %rax
movq 0x8(%r14), %rdx
movq %rax, %rcx
subq %rdx, %rcx
cmpq $-0xe, %rcx
jg 0x30dc3
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 0x6c1c(%rip), %xmm3 # 0x377ab
vpcmpltub %xmm3, %xmm0, %k1
vptestnmb %xmm1, %xmm1, %k0 {%k1}
vpmovb2m %xmm2, %k1
ktestw %k1, %k0
jne 0x30dda
addq $0x6, %rax
movq %rax, (%r14)
vmovq %xmm0, %rax
bswapq %rax
movq 0x18(%r14), %rdx
movq %rdx, 0x28(%rsp)
movl $0x30, %ecx
shldq %cl, %rax, %rdx
movq %rdx, 0x18(%r14)
shlq $0x30, 0x10(%r14)
movq 0x10(%r14), %rax
movq 0x18(%r14), %rcx
shrq %rax
movq %rax, 0x10(%r14)
movl %r12d, %edx
negl %edx
cmpq %rax, %rcx
movl $0x0, %esi
cmovaeq %rax, %rsi
cmovbl %r12d, %edx
subq %rsi, %rcx
movq %rcx, 0x18(%r14)
movl %edx, %r12d
jmp 0x30c94
vmovdqa (%rsp), %xmm4
movb $0x37, %al
subb %sil, %al
shrxq %rax, 0x10(%r14), %rcx
movq 0x18(%r14), %rax
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, %r12d
orl %edi, %r12d
leal 0x1(%r12), %edi
movzbl %sil, %r8d
btq %r8, %rax
notl %r12d
cmovael %edi, %r12d
bzhiq %r8, %rax, %rax
imulq %rcx, %rax
addq %rdx, %rax
movq %rax, 0x18(%r14)
shlxq %rsi, %rcx, %rax
movq %rax, 0x10(%r14)
incl %r13d
cmpl $0x2, %r13d
jne 0x30d08
vpinsrw $0x1, %r12d, %xmm4, %xmm0
movzwl 0x1cd0(%r14), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x30d00
movq %rax, %rdi
addq 0x18(%rsp), %rdi
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
movq 0x10(%rsp), %rcx
subq 0x680(%r14), %rcx
addq $-0x70, %rcx
vmovd %xmm4, %eax
movswl %ax, %r8d
leaq 0x9b54(%rip), %rdx      # 0x3a838
movl %r12d, %r9d
xorl %eax, %eax
vmovdqa %xmm0, (%rsp)
callq 0x1070
vmovdqa (%rsp), %xmm0
addw %ax, 0x1cd0(%r14)
vmovdqa %xmm0, 0x30(%rsp)
jmp 0x30d15
movzwl %r12w, %eax
vmovd %eax, %xmm0
movl $0x2f, %ebp
vmovdqa %xmm0, (%rsp)
cmpl $0x2, %r13d
jne 0x30a24
jmp 0x30ed4
testq %rdi, %rdi
js 0x30e49
addq %rcx, %rax
movq %rax, (%r14)
xorl %eax, %eax
jmp 0x30b19
kandw %k1, %k0, %k0
kmovd %k0, %edi
movzwl %di, %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
shlxl %edx, %r8d, %edx
notl %edx
testl %edx, %edi
je 0x30b0b
tzcntl %edi, %r9d
movb (%rax,%r9), %r8b
cmpb $0x2, %r8b
jbe 0x30d84
shll $0x4, %r9d
leaq 0x77a7(%rip), %r10      # 0x38520
vpshufb (%r9,%r10), %xmm0, %xmm0
incq %rax
jmp 0x30da8
leaq (%rax,%r9), %r10
addq $-0x2, %r10
movq %r10, 0x8(%r14)
leaq 0x7809(%rip), %r10      # 0x385a0
subq %r9, %r10
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb 0x10(%r10), %xmm1, %xmm1
vpandn %xmm0, %xmm1, %xmm0
leal 0x7fffffff(%rdi), %r9d
andl %edi, %r9d
shrl %r9d
movl %r9d, %edi
cmpb $0x2, %r8b
ja 0x30d57
jmp 0x30b0b
testq %rcx, %rcx
js 0x30e90
addq $0x6, %rax
movq %rax, (%r14)
xorl %eax, %eax
jmp 0x30bbb
kandw %k1, %k0, %k0
kmovd %k0, %ecx
movzwl %cx, %ecx
testb $0x3f, %cl
je 0x30bac
tzcntl %ecx, %esi
movb (%rax,%rsi), %dl
cmpb $0x2, %dl
jbe 0x30e0f
shll $0x4, %esi
leaq 0x771c(%rip), %rdi      # 0x38520
vpshufb (%rsi,%rdi), %xmm0, %xmm0
incq %rax
jmp 0x30e33
leaq (%rax,%rsi), %rdi
addq $-0x2, %rdi
movq %rdi, 0x8(%r14)
leaq 0x777e(%rip), %rdi      # 0x385a0
subq %rsi, %rdi
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb 0x10(%rdi), %xmm1, %xmm1
vpandn %xmm0, %xmm1, %xmm0
leal 0x7fffffff(%rcx), %esi
andl %ecx, %esi
shrl %esi
movl %esi, %ecx
cmpb $0x2, %dl
ja 0x30de5
jmp 0x30bac
leaq -0x2(%rax), %r9
leaq -0x1(%r8), %r10
andq $-0x10, %r10
cmpq %r10, %r9
cmovsq %r9, %r10
vmovdqu (%r10), %xmm0
subq %r10, %r8
leaq 0x7735(%rip), %r9       # 0x385a0
vpshufb (%r8,%r9), %xmm0, %xmm0
addl $0xe, %edi
cmpl $0x10, %edi
movl $0x10, %r8d
cmovgel %r8d, %edi
movslq %edi, %rdi
vpshufb 0x10(%rdi,%r9), %xmm0, %xmm1
jmp 0x30ad3
leaq -0x2(%rax), %rsi
leaq -0x1(%rdx), %rdi
andq $-0x10, %rdi
cmpq %rdi, %rsi
cmovsq %rsi, %rdi
vmovdqu (%rdi), %xmm0
subq %rdi, %rdx
leaq 0x76ef(%rip), %rsi      # 0x385a0
vpshufb (%rdx,%rsi), %xmm0, %xmm0
addl $0xe, %ecx
cmpl $0x10, %ecx
movl $0x10, %edx
cmovgel %edx, %ecx
movslq %ecx, %rcx
vpshufb 0x10(%rcx,%rsi), %xmm0, %xmm1
jmp 0x30b74
vmovaps 0x30(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq