// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movl %esi, %r14d
movq %rdi, %rbx
vpbroadcastb %esi, %xmm0
movq 0x680(%rdi), %rax
vmovq 0x2747(%rip), %xmm1     # 0x2d510
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 0x2ae1f
leaq (%rbx,%rax), %rdi
addq $0x1cd2, %rdi           # imm = 0x1CD2
movl $0x1000, %esi           # imm = 0x1000
subq %rax, %rsi
leaq 0x981a(%rip), %rdx      # 0x34624
xorl %eax, %eax
callq 0x1070
addw 0x1cd0(%rbx), %ax
movw %ax, 0x1cd0(%rbx)
movzbl 0xdc1(%rbx), %ebp
movl %r14d, 0xc(%rsp)
andl %r14d, %ebp
leaq 0x1cd2(%rbx), %rcx
movq %rcx, 0x10(%rsp)
pushq $0x1
popq %r14
pushq $0x3b
popq %r15
testl %ebp, %ebp
je 0x2af16
tzcntl %ebp, %r12d
movq 0x680(%rbx), %rax
leaq 0x8503(%rip), %rcx      # 0x33360
movb 0x4(%r12,%rcx), %cl
shrxl %ecx, 0x8(%rax), %esi
andl $0x3, %esi
addl $0x36, %esi
movq %rbx, %rdi
callq 0x2aced
testl %eax, %eax
je 0x2aeb5
leaq 0x799f(%rip), %rax      # 0x32820
movb 0x4(%r12,%rax), %al
shlxl %eax, %r14d, %eax
movq 0x680(%rbx), %rcx
orl %eax, 0x8(%rcx)
pushq $0x3a
popq %rsi
movl %r14d, %r13d
cmpl $0x20, %r13d
je 0x2aeba
movq %rbx, %rdi
callq 0x2aced
testl %eax, %eax
je 0x2aebe
incl %r13d
movl %r15d, %esi
jmp 0x2ae9b
xorl %r13d, %r13d
jmp 0x2aebe
pushq $0x20
popq %r13
movq 0x680(%rbx), %rax
movb %r13b, 0x10(%rax,%r12)
movzwl 0x1cd0(%rbx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x2af0c
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movl $0x1000, %esi           # imm = 0x1000
subq %rax, %rsi
leaq 0x9742(%rip), %rdx      # 0x34633
movl %r12d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x1070
addw 0x1cd0(%rbx), %ax
movw %ax, 0x1cd0(%rbx)
blsrl %ebp, %ebp
jmp 0x2ae42
cmpw $0x1000, %ax            # imm = 0x1000
jae 0x2af46
movzwl %ax, %ecx
movq 0x10(%rsp), %rdi
addq %rcx, %rdi
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
leaq 0x9a69(%rip), %rdx      # 0x349a1
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 0xc(%rsp), %edi
testw $0x122, %di             # imm = 0x122
jne 0x2af9a
vpshufb 0x25ad(%rip), %xmm0, %xmm0 # 0x2d520
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)
movq 0x680(%rbx), %rax
testw $0x144, %di             # imm = 0x144
jne 0x2afd1
vpshufb 0x2586(%rip), %xmm0, %xmm0 # 0x2d530
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 $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq