// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movl %ecx, %eax
movq %rdi, %rbx
movl %ecx, %r15d
andl $0x3, %r15d
movq 0x18(%rdi), %rdi
movq %rdi, %rcx
notq %rcx
vpbroadcastq %rcx, %xmm0
vpsrlvq 0x57da(%rip), %xmm0, %xmm0 # 0x2d7a0
vpshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
vpandd 0x92c3(%rip){1to4}, %xmm0, %xmm0 # 0x31298
vpcmpeqd %xmm1, %xmm1, %xmm1
vpaddd %xmm1, %xmm0, %xmm0
vmovq %xmm0, 0x30(%rsp)
shrq $0x3c, %rcx
andl $0x2, %ecx
decl %ecx
movl %ecx, 0x38(%rsp)
movq 0x10(%rbx), %r8
movq %rdi, 0x18(%rsp)
movl %r15d, %ecx
shldq %cl, %r8, %rdi
movq %rdi, 0x18(%rbx)
shlq %cl, 0x10(%rbx)
movq %rdx, 0x20(%rsp)
movl %esi, 0xc(%rsp)
movl %eax, %r13d
shrl $0x2, %r13d
cmpl $0x2c, %eax
setae %al
cmpl $0x3, %r15d
setne %cl
andb %al, %cl
pushq $0x1
popq %rbp
xorl %r14d, %r14d
xorb $0x1, %cl
movb %cl, 0xb(%rsp)
movq %r15, 0x10(%rsp)
movq %r13, 0x28(%rsp)
cmpq %r13, %r15
jae 0x2810f
movq 0x18(%rbx), %rcx
movq %rcx, %rax
btsq $0x26, %rax
lzcntq %rax, %rax
movq %rcx, %rdx
shrq $0x31, %rdx
jne 0x28079
leal -0x2(,%rax,2), %esi
pushq $0xf
popq %rax
shlxl %ebp, %eax, %r12d
addl $0xfffff000, %r12d      # imm = 0xFFFFF000
jmp 0x280aa
cmpq 0x10(%rsp), %r15
setbe %dl
testb %dl, 0xb(%rsp)
jne 0x28095
leal (%rax,%rbp), %esi
incl %esi
decl %eax
shlxl %ebp, %eax, %r12d
jmp 0x280aa
shrq $0x32, %rcx
pushq $-0x2
popq %r12
pushq $0x13
popq %rsi
je 0x280aa
leal 0x1(%rax), %esi
decl %eax
movl %eax, %r12d
cmpl $0x3, 0x10(%rsp)
setne %r13b
movq %rbx, %rdi
callq 0x1539a
addl %r12d, %eax
subq $0x1, %r14
setb %cl
andb %r13b, %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
movl %esi, 0x30(%rsp,%r15,4)
pushq $0x3
popq %rax
shlxl %ebp, %eax, %eax
xorl %ecx, %ecx
cmpl %eax, %edx
setge %cl
addl %ebp, %ecx
cmpl $0x6, %ecx
pushq $0x6
popq %rax
cmovgel %eax, %ecx
incq %r15
movl %ecx, %ebp
movq 0x28(%rsp), %r13
jmp 0x2803e
movq 0x20(%rsp), %r12
movl %r12d, %ecx
movl 0xc(%rsp), %ebp
subl %ebp, %ecx
xorl %eax, %eax
cmpl %ecx, %r13d
jg 0x2817f
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 (%r12,%r13), %ecx
addl $-0x4, %ecx
movslq %ecx, %rcx
imulq $0x24, %rcx, %rcx
leaq 0xa9f1(%rip), %rdi      # 0x32b50
addq %rcx, %rdi
movzbl (%rdi,%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 %ebp, %ecx
addq %rbx, %rcx
movl %eax, %edx
addq %rcx, %rdx
addq %r13, %rdx
addq $0x1a9f, %rdx           # imm = 0x1A9F
movl 0x30(%rsp), %ecx
movsbq (%rdx), %rsi
movl %ecx, 0x1b60(%rbx,%rsi,4)
pushq $0x1
popq %rsi
pushq $0x7
popq %rdi
leaq 0xa961(%rip), %r8       # 0x32b10
cmpq %r13, %rsi
jae 0x2824a
decq %rdx
testl %eax, %eax
jle 0x28232
movq 0x18(%rbx), %r9
cmpl $0x7, %eax
setb %r10b
movq %r9, %rcx
shrq $0x3d, %rcx
setne %r11b
orb %r10b, %r11b
cmpb $0x1, %r11b
jne 0x28200
cmpl $0x7, %eax
movl %edi, %r10d
cmovbl %eax, %r10d
decl %r10d
leaq (%r8,%r10,8), %r10
movsbl (%rcx,%r10), %r10d
movl %r10d, %ecx
sarl $0x3, %ecx
andl $0x7, %r10d
jmp 0x28214
lzcntq %r9, %r10
leal 0x1(%r10), %ecx
addl $0x4, %r10d
cmpl %eax, %r10d
cmovael %eax, %r10d
movl %r10d, %r11d
subq %r11, %rdx
subl %r10d, %eax
movq 0x10(%rbx), %r10
movq %r9, 0x18(%rsp)
shldq %cl, %r10, %r9
movq %r9, 0x18(%rbx)
shlq %cl, 0x10(%rbx)
movl 0x30(%rsp,%rsi,4), %ecx
movsbq (%rdx), %r9
movl %ecx, 0x1b60(%rbx,%r9,4)
incq %rsi
jmp 0x281af
leaq 0x1cd2(%rbx), %r14
movslq %ebp, %r15
movslq %r12d, %r12
leaq 0xbf34(%rip), %r13      # 0x34192
leaq 0xc469(%rip), %rbp      # 0x346ce
cmpq %r12, %r15
jg 0x282b6
movzwl 0x1cd0(%rbx), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
ja 0x282b1
movq %rax, %rdi
addq %r14, %rdi
movl $0x1000, %ecx           # imm = 0x1000
subl %eax, %ecx
movzwl %cx, %esi
cmpq %r12, %r15
movq %rbp, %rdx
cmovlq %r13, %rdx
movsbq 0x1aa0(%rbx,%r15), %rax
movl 0x1b60(%rbx,%rax,4), %ecx
xorl %eax, %eax
callq 0x1070
addw %ax, 0x1cd0(%rbx)
incq %r15
jmp 0x28265
cmpq $0x0, 0x10(%rbx)
jne 0x282c5
movq %rbx, %rdi
callq 0x15327
pushq $0x1
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq