// File: /workspace/github_c_makefile_star2_O1/tvlabs[P]edge264/./edge264_slice.c
// Function: parse_ref_idx_cabac
static inline void CAFUNC(parse_ref_idx, unsigned f) {
	u8x16 v = {f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f};
	u8x16 bits = {1, 2, 4, 8, 16, 32, 64, 128};
	mb->refIdx_l = ((i64x2){mb->refIdx_l} & ~(i64x2)((v & bits) == bits))[0]; // set to 0 if parsed
	log_mb(ctx, "    ref_idx: {");
	for (unsigned u = f & ctx->num_ref_idx_mask; u; u &= u - 1) {
		int i = __builtin_ctz(u);
		int ref_idx = 0;
		#if !CABAC
			if (ctx->clip_ref_idx[i] == 1)
				ref_idx = get_u1(&ctx->t._gb) ^ 1;
			else
				ref_idx = get_ue16(&ctx->t._gb, ctx->clip_ref_idx[i]);
		#else
			if (get_ae(ctx, 54 + (mb->bits[0] >> inc8x8[4 + i] & 3))) {
				ref_idx = 1;
				mb->bits[0] |= 1 << bit8x8[4 + i];
				int ctxIdx = 58;
				while (ref_idx < 32 && get_ae(ctx, ctxIdx))
					ref_idx++, ctxIdx = 59;
			}
		#endif
		mb->refIdx[i] = ref_idx;
		log_mb(ctx, "\"%u\":%u,", i, ref_idx);
	}
	log_mb(ctx, "}\n");
	
	// clip and broadcast the values
	i8x16 refIdx_v = (i64x2){mb->refIdx_l};
	#if CABAC
		refIdx_v = min8(refIdx_v, (i64x2){(int64_t)ctx->clip_ref_idx_v});
	#endif
	if (!(f & 0x122)) { // 16xN
		refIdx_v = shuffle(refIdx_v, ((i8x16){0, 0, 2, 2, 4, 4, 6, 6, -1, -1, -1, -1, -1, -1, -1, -1}));
		#if CABAC
			mb->bits[0] |= (mb->bits[0] >> 2 & 0x080800) | (mb->bits[0] << 5 & 0x808000);
		#endif
	}
	if (!(f & 0x144)) { // Nx16
		refIdx_v = shuffle(refIdx_v, ((i8x16){0, 1, 0, 1, 4, 5, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1}));
		#if CABAC
			mb->bits[0] |= (mb->bits[0] >> 3 & 0x040400) | (mb->bits[0] << 4 & 0x808000);
		#endif
	}
	mb->refIdx_l = ((i64x2)refIdx_v)[0];
	
	// compute reference picture numbers
	mb->refPic_s[0] = ((i32x4)shufflen(ctx->t.RefPicList_v[0], refIdx_v))[0];
	mb->refPic_s[1] = ((i32x4)shufflen(ctx->t.RefPicList_v[2], refIdx_v))[1];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
vpbroadcastb %esi, %xmm0
movq 0x680(%rdi), %rax
vmovq 0x2a71(%rip), %xmm1     # 0x33200
vpand %xmm1, %xmm0, %xmm0
vpcmpeqb %xmm1, %xmm0, %xmm0
vpternlogq $0xf, %xmm0, %xmm0, %xmm0
vmovq %xmm0, %rcx
andq %rcx, 0x10(%rax)
movzwl 0x1cd0(%rdi), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x307e0
leaq (%rbx,%rax), %rdi
addq $0x1cd2, %rdi           # imm = 0x1CD2
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x9e22(%rip), %rdx      # 0x3a5f4
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
movzbl 0xdc1(%rbx), %r13d
movl %ebp, 0x4(%rsp)
andl %ebp, %r13d
je 0x308c9
leaq 0x1cd2(%rbx), %rbp
leaq 0x8b2d(%rip), %r14      # 0x39330
tzcntl %r13d, %r15d
movq 0x680(%rbx), %rax
movb 0x4(%r15,%r14), %cl
shrxl %ecx, 0x8(%rax), %esi
andl $0x3, %esi
addl $0x36, %esi
movq %rbx, %rdi
callq 0x306b3
movl $0x0, %r12d
testl %eax, %eax
je 0x30878
leaq 0x7fb7(%rip), %rax      # 0x387f0
movb 0x4(%r15,%rax), %al
movl $0x1, %r12d
shlxl %eax, %r12d, %eax
movq 0x680(%rbx), %rcx
orl %eax, 0x8(%rcx)
movl $0x3a, %esi
movq %rbx, %rdi
callq 0x306b3
testl %eax, %eax
je 0x30878
incl %r12d
movl $0x3b, %esi
cmpl $0x20, %r12d
jne 0x30858
movl $0x20, %r12d
movq 0x680(%rbx), %rax
movb %r12b, 0x10(%rax,%r15)
movzwl 0x1cd0(%rbx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x308be
movq %rax, %rdi
addq %rbp, %rdi
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x9d59(%rip), %rdx      # 0x3a603
movl %r15d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
blsrl %r13d, %r13d
jne 0x30803
movzwl 0x1cd0(%rbx), %eax
cmpq $0x1000, %rax           # imm = 0x1000
jae 0x30902
leaq (%rbx,%rax), %rdi
addq $0x1cd2, %rdi           # imm = 0x1CD2
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
leaq 0xa07d(%rip), %rdx      # 0x3a971
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
movq 0x680(%rbx), %rax
vmovq 0x10(%rax), %xmm0
vmovq 0xe08(%rbx), %xmm1
vpminsb %xmm1, %xmm0, %xmm0
movl 0x4(%rsp), %edi
testl $0x122, %edi            # imm = 0x122
jne 0x30950
vpshufb 0x28e0(%rip), %xmm0, %xmm0 # 0x33210
movl 0x8(%rax), %ecx
movl %ecx, %edx
shrl $0x2, %edx
andl $0x80800, %edx          # imm = 0x80800
movl %ecx, %esi
shll $0x5, %esi
andl $0x808000, %esi         # imm = 0x808000
orl %edx, %esi
orl %ecx, %esi
movl %esi, 0x8(%rax)
testl $0x144, %edi            # imm = 0x144
jne 0x30988
vpshufb 0x28bf(%rip), %xmm0, %xmm0 # 0x33220
movq 0x680(%rbx), %rax
movl 0x8(%rax), %ecx
movl %ecx, %edx
shrl $0x3, %edx
andl $0x40400, %edx          # imm = 0x40400
movl %ecx, %esi
shll $0x4, %esi
andl $0x808000, %esi         # imm = 0x808000
orl %edx, %esi
orl %ecx, %esi
movl %esi, 0x8(%rax)
movq 0x680(%rbx), %rax
vmovq %xmm0, 0x10(%rax)
vmovdqa 0x1a0(%rbx), %xmm1
vpshufb %xmm0, %xmm1, %xmm1
vpxor %xmm2, %xmm2, %xmm2
vpcmpgtb %xmm0, %xmm2, %xmm2
vpor %xmm2, %xmm1, %xmm1
movq 0x680(%rbx), %rax
vmovd %xmm1, 0x18(%rax)
vmovdqa 0x1c0(%rbx), %xmm1
vpshufb %xmm0, %xmm1, %xmm0
vpor %xmm2, %xmm0, %xmm0
movq 0x680(%rbx), %rax
vpextrd $0x1, %xmm0, 0x1c(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq