// File: /workspace/github_c_makefile_star2_O1/tvlabs[P]edge264/./edge264_slice.c
// Function: parse_residual_coeffs_cavlc
static noinline int parse_residual_coeffs_cavlc(Edge264Context *ctx, int startIdx, int endIdx, int token) {
		int TrailingOnes = token & 3;
		int TotalCoeff = token >> 2;
		
		// parse all level values from end to start
		int32_t level[16];
		size_t signs = ~ctx->t._gb.msb_cache;
		level[0] = (signs >> (SIZE_BIT - 2) & 2) - 1;
		level[1] = (signs >> (SIZE_BIT - 3) & 2) - 1;
		level[2] = (signs >> (SIZE_BIT - 4) & 2) - 1;
		ctx->t._gb.msb_cache = shld(ctx->t._gb.lsb_cache, ctx->t._gb.msb_cache, TrailingOnes);
		ctx->t._gb.lsb_cache <<= TrailingOnes;
		for (int i = TrailingOnes, suffixLength = 1, v, offset; i < TotalCoeff; i++) {
			int level_prefix = clz(ctx->t._gb.msb_cache | (size_t)1 << (SIZE_BIT - 26)); // limit given in 9.2.2.1
			if (level_prefix >= 15) {
				v = level_prefix * 2 - 2;
				offset = (15 << suffixLength) - 4096;
			} else if (i > TrailingOnes || (TotalCoeff > 10 && TrailingOnes < 3)) {
				v = level_prefix + suffixLength + 1;
				offset = (level_prefix - 1) << suffixLength;
			} else if (level_prefix < 14) {
				v = level_prefix + 1;
				offset = level_prefix - 1;
			} else {
				v = 19;
				offset = -2;
			}
			#if SIZE_BIT == 32
				v -= level_prefix;
				ctx->t._gb.msb_cache = shld(ctx->t._gb.lsb_cache, ctx->t._gb.msb_cache, level_prefix);
				if (!(ctx->t._gb.lsb_cache <<= level_prefix))
					refill(&ctx->t._gb, 0);
			#endif
			int levelCode = get_uv(&ctx->t._gb, v) + offset;
			levelCode += (i == TrailingOnes && TrailingOnes < 3) * 2;
			level[i] = (levelCode % 2) ? (-levelCode - 1) >> 1 : (levelCode + 2) >> 1;
			suffixLength = min(suffixLength + (levelCode >= (3 << suffixLength)), 6);
		}
		
		// store level values at proper positions in memory
		int zerosLeft = 0;
		if (TotalCoeff <= endIdx - startIdx)
			zerosLeft = parse_total_zeros(ctx, endIdx, TotalCoeff);
		int8_t *scan = ctx->scan + startIdx + zerosLeft + TotalCoeff - 1;
		ctx->c[*scan] = level[0];
		for (int i = 1, v, run_before; i < TotalCoeff; i++) {
			scan--;
			if (zerosLeft > 0) {
				int threeBits = ctx->t._gb.msb_cache >> (SIZE_BIT - 3);
				if (zerosLeft <= 6 || threeBits > 0) {
					static int8_t run_before_codes[7][8] = {
						{9, 9, 9, 9, 8, 8, 8, 8},
						{18, 18, 17, 17, 8, 8, 8, 8},
						{19, 19, 18, 18, 17, 17, 16, 16},
						{28, 27, 18, 18, 17, 17, 16, 16},
						{29, 28, 27, 26, 17, 17, 16, 16},
						{25, 26, 28, 27, 30, 29, 16, 16},
						{0, 30, 29, 28, 27, 26, 25, 24}};
					int code = run_before_codes[min(zerosLeft, 7) - 1][threeBits];
					v = code >> 3;
					run_before = code & 7;
				} else {
					v = clz(ctx->t._gb.msb_cache) + 1;
					run_before = min(v + 3, zerosLeft);
				}
				scan -= run_before;
				zerosLeft -= run_before;
				ctx->t._gb.msb_cache = shld(ctx->t._gb.lsb_cache, ctx->t._gb.msb_cache, v);
				ctx->t._gb.lsb_cache <<= v;
			}
			ctx->c[*scan] = level[i];
		}
		for (int i = startIdx; i <= endIdx; i++)
			log_mb(ctx, (i < endIdx) ? "%d," : "%d]}\n", ctx->c[ctx->scan[i]]);
		
		// trailing_ones_sign_flags+total_zeros+run_before consumed at most 31 bits, so we can delay refill here
		return ctx->t._gb.lsb_cache ? 1 : refill(&ctx->t._gb, 1);
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %ecx, %r8d
movq %rdi, %rbx
movl %ecx, %r12d
andl $0x3, %r12d
movq 0x18(%rdi), %rax
movq %rax, %rcx
notq %rcx
movq %rcx, %rdi
shrq $0x3e, %rdi
andl $-0x2, %edi
decl %edi
movl %edi, 0x40(%rsp)
movq %rcx, %rdi
shrq $0x3d, %rdi
andl $0x2, %edi
decl %edi
movl %edi, 0x44(%rsp)
shrq $0x3c, %rcx
andl $0x2, %ecx
decl %ecx
movl %ecx, 0x48(%rsp)
movq 0x10(%rbx), %rdi
movq %rax, 0x18(%rsp)
movl %r12d, %ecx
shldq %cl, %rdi, %rax
movq %rax, 0x18(%rbx)
shlq %cl, 0x10(%rbx)
movq %rdx, 0x20(%rsp)
movl %esi, 0xc(%rsp)
movl %r8d, 0x8(%rsp)
movl %r8d, %eax
shrl $0x2, %eax
movq %rax, 0x10(%rsp)
cmpl %eax, %r12d
jae 0x2d9d5
cmpl $0x2c, 0x8(%rsp)
setae %al
cmpl $0x3, %r12d
setne %cl
andb %al, %cl
movl %r12d, %eax
movq %rax, 0x28(%rsp)
movl 0x10(%rsp), %eax
leaq (%rsp,%r12,4), %rdx
addq $0x40, %rdx
movq %rdx, 0x38(%rsp)
movq %r12, %rdx
subq %rax, %rdx
movq %rdx, 0x30(%rsp)
movl $0x1, %r13d
xorl %r14d, %r14d
xorb $0x1, %cl
movb %cl, 0x7(%rsp)
movq 0x18(%rbx), %rax
movq %rax, %rcx
btsq $0x26, %rcx
lzcntq %rcx, %r15
movq %rax, %rcx
shrq $0x31, %rcx
jne 0x2d928
leal -0x2(,%r15,2), %esi
movl $0xf, %eax
shlxl %r13d, %eax, %r15d
addl $0xfffff000, %r15d      # imm = 0xFFFFF000
jmp 0x2d964
leaq (%r12,%r14), %rcx
cmpq 0x28(%rsp), %rcx
setbe %cl
testb %cl, 0x7(%rsp)
jne 0x2d94a
leal (%r15,%r13), %esi
incl %esi
decl %r15d
shlxl %r13d, %r15d, %r15d
jmp 0x2d964
shrq $0x32, %rax
je 0x2d959
leal 0x1(%r15), %esi
decl %r15d
jmp 0x2d964
movl $0x13, %esi
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl $0x3, %r12d
setne %bpl
movq %rbx, %rdi
callq 0x1875d
addl %r15d, %eax
testq %r14, %r14
sete %cl
andb %bpl, %cl
movzbl %cl, %ecx
leal (%rax,%rcx,2), %edx
movl %edx, %esi
sarl %esi
leal (%rax,%rcx,2), %ecx
addl $0x2, %ecx
sarl %ecx
testb $0x1, %al
notl %esi
cmovel %ecx, %esi
movq 0x38(%rsp), %rax
movl %esi, (%rax,%r14,4)
movl $0x3, %eax
shlxl %r13d, %eax, %eax
xorl %ecx, %ecx
cmpl %eax, %edx
setge %cl
addl %r13d, %ecx
cmpl $0x6, %ecx
movl $0x6, %eax
cmovgel %eax, %ecx
incq %r14
movq 0x30(%rsp), %rax
movl %ecx, %r13d
addq %r14, %rax
jne 0x2d8f3
movq 0x20(%rsp), %r11
movl %r11d, %ecx
movl 0xc(%rsp), %r15d
subl %r15d, %ecx
xorl %eax, %eax
movq 0x10(%rsp), %r14
cmpl %ecx, %r14d
jg 0x2da4d
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
movq %rdx, %rax
btsq $0x37, %rax
lzcntq %rax, %rax
leal 0x2(%rax), %ecx
notb %cl
shrxq %rcx, %rdx, %rcx
andl $0x3, %ecx
leaq (%rcx,%rax,4), %rax
leal (%r11,%r14), %ecx
addl $-0x4, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,8), %rcx
leaq 0xb0f4(%rip), %rdi      # 0x38b20
leaq (%rdi,%rcx,4), %rcx
movzbl (%rcx,%rax), %eax
movl %eax, %ecx
shrl $0x4, %ecx
movq %rdx, 0x18(%rsp)
shldq %cl, %rsi, %rdx
movq %rdx, 0x18(%rbx)
shlq %cl, 0x10(%rbx)
andl $0xf, %eax
movl %r15d, %edx
addq %rbx, %rdx
movl %eax, %ecx
addq %rdx, %rcx
movl 0x40(%rsp), %edx
movsbq 0x1a9f(%r14,%rcx), %rsi
movl %edx, 0x1b60(%rbx,%rsi,4)
cmpl $0x8, 0x8(%rsp)
jb 0x2db2b
leaq (%r14,%rcx), %rdx
addq $0x1a9f, %rdx           # imm = 0x1A9F
movl $0x1, %esi
leaq 0xb052(%rip), %rdi      # 0x38ae0
decq %rdx
testl %eax, %eax
jle 0x2db0f
movq 0x18(%rbx), %r8
cmpl $0x7, %eax
setl %r9b
movq %r8, %rcx
shrq $0x3d, %rcx
setne %r10b
orb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x2dada
cmpl $0x7, %eax
movl $0x7, %r9d
cmovll %eax, %r9d
movslq %r9d, %r9
leaq (%rdi,%r9,8), %r9
movsbl -0x8(%rcx,%r9), %r9d
movl %r9d, %ecx
sarl $0x3, %ecx
andl $0x7, %r9d
jmp 0x2daee
lzcntq %r8, %r9
leal 0x1(%r9), %ecx
addl $0x4, %r9d
cmpl %eax, %r9d
cmovgel %eax, %r9d
subl %r9d, %eax
negl %r9d
movslq %r9d, %r9
addq %r9, %rdx
movq 0x10(%rbx), %r9
movq %r8, 0x18(%rsp)
shldq %cl, %r9, %r8
movq %r8, 0x18(%rbx)
shlq %cl, 0x10(%rbx)
movl 0x40(%rsp,%rsi,4), %ecx
movsbq (%rdx), %r8
movl %ecx, 0x1b60(%rbx,%r8,4)
incq %rsi
cmpq %rsi, %r14
jne 0x2da8e
movl %r11d, %ebp
subl %r15d, %ebp
jl 0x2db9e
leaq 0x1cd2(%rbx), %r14
movslq %r15d, %r15
movslq %r11d, %r12
incl %ebp
leaq 0xcb55(%rip), %r13      # 0x3a69e
movzwl 0x1cd0(%rbx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x2db97
movq %rax, %rdi
addq %r14, %rdi
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
cmpq %r12, %r15
movq %r13, %rdx
leaq 0xc5ed(%rip), %rax      # 0x3a162
cmovlq %rax, %rdx
movsbq 0x1aa0(%rbx,%r15), %rax
movl 0x1b60(%rbx,%rax,4), %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
incq %r15
decl %ebp
jne 0x2db49
cmpq $0x0, 0x10(%rbx)
jne 0x2dbad
movq %rbx, %rdi
callq 0x18605
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq