// File: /workspace/github_c_makefile_star2_O2/tvlabs[P]edge264/./edge264_slice.c
// Function: parse_residual_coeffs_cabac
static noinline void parse_residual_coeffs_cabac(Edge264Context *ctx, int startIdx, int endIdx) {
		// significant_coeff_flags are stored as a bit mask
		uint64_t significant_coeff_flags = 0;
		int i = startIdx;
		do {
			if (get_ae(ctx, ctx->ctxIdxOffsets[1] + ctx->sig_inc[i])) {
				significant_coeff_flags |= (uint64_t)1 << i;
				if (get_ae(ctx, ctx->ctxIdxOffsets[2] + ctx->last_inc[i]))
					break;
			}
		} while (++i < endIdx);
		significant_coeff_flags |= (uint64_t)1 << i;
		
		// Now loop on set bits to parse all non-zero coefficients.
		int ctxIdx0 = ctx->ctxIdxOffsets[3] + 1;
		int ctxIdx1 = ctx->ctxIdxOffsets[3] + 5;
		do {
			int coeff_level = 1;
			if (!get_ae(ctx, ctxIdx0)) {
				static const int8_t trans[5] = {0, 2, 3, 4, 4};
				ctxIdx0 = ctx->ctxIdxOffsets[3] + trans[ctxIdx0 - ctx->ctxIdxOffsets[3]];
				coeff_level = get_bypass(ctx) ? -coeff_level : coeff_level;
			} else {
				coeff_level++;
				while (coeff_level < 15 && get_ae(ctx, ctxIdx1))
					coeff_level++;
				ctxIdx0 = ctx->ctxIdxOffsets[3];
				ctxIdx1 = ctxIdx0 + ctx->coeff_abs_inc[ctxIdx1 - ctxIdx0 - 5];
				#if SIZE_BIT == 32
					if (coeff_level >= 15) {
						// the biggest value to encode is 2^(14+7)-14, for which k=20 (see 9.3.2.3)
						int k = 0;
						while (get_bypass(ctx) && k < 20)
							k++;
						coeff_level = 1;
						while (k--)
							coeff_level += coeff_level + get_bypass(ctx);
						coeff_level += 14;
					}
					coeff_level = get_bypass(ctx) ? -coeff_level : coeff_level;
				#elif SIZE_BIT == 64
					if (coeff_level >= 15) {
						// we need at least 51 bits in codIOffset to get 42 bits with a division by 9 bits
						int zeros = clz(ctx->t._gb.codIRange);
						if (zeros > 64 - 51) {
							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;
						}
						ctx->t._gb.codIRange >>= 64 - 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 = clz(~quo << (zeros + 9) | (size_t)1 << (SIZE_BIT - 21));
						int unused = 64 - 9 - zeros - k * 2 - 2;
						coeff_level = 14 + (1 << k | (quo >> unused >> 1 & (((size_t)1 << k) - 1)));
						coeff_level = (quo & (size_t)1 << unused) ? -coeff_level : coeff_level;
						ctx->t._gb.codIOffset = (quo & (((size_t)1 << unused) - 1)) * ctx->t._gb.codIRange + rem;
						ctx->t._gb.codIRange <<= unused;
					} else {
						coeff_level = get_bypass(ctx) ? -coeff_level : coeff_level;
					}
				#endif
			}
		
			// scale and store
			int i = 63 - __builtin_clzll(significant_coeff_flags);
			ctx->c[ctx->scan[i]] = coeff_level; // beware, scan is transposed already
			significant_coeff_flags &= ~((uint64_t)1 << i);
		} while (significant_coeff_flags != 0);
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movslq %esi, %r12
movslq %edx, %r13
leal 0x1(%r12), %r14d
cmpl %r14d, %r13d
cmovgl %r13d, %r14d
xorl %r15d, %r15d
movswl 0x1a12(%rbx), %eax
movsbl 0x1a20(%rbx,%r12), %esi
addl %eax, %esi
movq %rbx, %rdi
callq 0x13a0c
testl %eax, %eax
je 0x145ad
btsq %r12, %r15
movswl 0x1a14(%rbx), %eax
movsbl 0x1a60(%rbx,%r12), %esi
addl %eax, %esi
movq %rbx, %rdi
callq 0x13a0c
testl %eax, %eax
jne 0x145b7
incq %r12
cmpq %r13, %r12
jl 0x1456d
jmp 0x145ba
movl %r12d, %r14d
btsq %r14, %r15
movswl 0x1a16(%rbx), %r14d
leal 0x1(%r14), %ebp
addl $0x5, %r14d
pushq $-0x2
popq %r13
movq %rbx, %rdi
movl %ebp, %esi
callq 0x13a0c
testl %eax, %eax
je 0x1466c
pushq $0x2
popq %r12
cmpl $0xf, %r12d
je 0x14602
movq %rbx, %rdi
movl %r14d, %esi
callq 0x13a0c
testl %eax, %eax
je 0x14602
incl %r12d
jmp 0x145e8
movswl 0x1a16(%rbx), %ebp
subl %ebp, %r14d
addl $-0x5, %r14d
movslq %r14d, %rax
movsbl 0x1a18(%rbx,%rax), %r14d
addl %ebp, %r14d
cmpl $0xf, %r12d
jb 0x1469e
movq 0x10(%rbx), %rcx
lzcntq %rcx, %r12
movq %rcx, %rax
shrq $0x32, %rax
jne 0x146b3
movl %r12d, %esi
shrl $0x3, %esi
movq %rbx, %rdi
callq 0x13e93
movq %rax, %rdx
movq 0x18(%rbx), %rax
movl %r12d, %ecx
andl $0x38, %ecx
movq %rax, (%rsp)
shldq %cl, %rdx, %rax
movl %r12d, %ecx
andb $0x38, %cl
shlxq %rcx, 0x10(%rbx), %rcx
andl $0x7, %r12d
jmp 0x146b7
movswl 0x1a16(%rbx), %eax
subl %eax, %ebp
movslq %ebp, %rcx
leaq 0x1cae1(%rip), %rdx     # 0x31160
movsbl (%rcx,%rdx), %ebp
addl %eax, %ebp
movq %rbx, %rdi
callq 0x13fae
negl %eax
movl $0x0, %esi
sbbl %esi, %esi
orl $0x1, %esi
jmp 0x14729
movq %rbx, %rdi
callq 0x13fae
movl %r12d, %esi
negl %esi
testl %eax, %eax
cmovel %r12d, %esi
jmp 0x14729
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 $0x2b, %rsi
lzcntq %rsi, %rdi
leal (%r12,%rdi,2), %esi
movb $0x35, %r8b
subb %sil, %r8b
shrxq %r8, %rax, %rsi
shrq %rsi
bzhil %edi, %esi, %esi
btsl %edi, %esi
movzbl %r8b, %edi
pushq $-0xe
popq %r9
subl %esi, %r9d
addl $0xe, %esi
btq %rdi, %rax
cmovbl %r9d, %esi
bzhiq %rdi, %rax, %rax
imulq %rcx, %rax
addq %rdx, %rax
movq %rax, 0x18(%rbx)
shlxq %r8, %rcx, %rax
movq %rax, 0x10(%rbx)
bsrq %r15, %rcx
movsbq 0x1aa0(%rbx,%rcx), %rax
movl %esi, 0x1b60(%rbx,%rax,4)
movq %r13, %rax
rolq %cl, %rax
andq %rax, %r15
jne 0x145d2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq