// File: /workspace/github_c_makefile_star2_O3/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
vpbroadcastq %rcx, %xmm0
vpsrlvq 0x5745(%rip), %xmm0, %xmm0 # 0x338b0
vpshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
vpandd 0x91fe(%rip){1to4}, %xmm0, %xmm0 # 0x37378
vpcmpeqd %xmm1, %xmm1, %xmm1
vpaddd %xmm1, %xmm0, %xmm0
vmovq %xmm0, 0x40(%rsp)
shrq $0x3c, %rcx
andl $0x2, %ecx
decl %ecx
movl %ecx, 0x48(%rsp)
movq 0x10(%rdi), %rdi
movq %rax, 0x20(%rsp)
movl %r12d, %ecx
shldq %cl, %rdi, %rax
movq %rax, 0x18(%rbx)
shlq %cl, 0x10(%rbx)
movq %rdx, 0x28(%rsp)
movl %esi, 0x14(%rsp)
movl %r8d, 0x10(%rsp)
movl %r8d, %eax
shrl $0x2, %eax
movq %rax, 0x18(%rsp)
cmpl %eax, %r12d
jae 0x2e2e4
cmpl $0x2c, 0x10(%rsp)
setae %al
cmpq $0x3, %r12
setne %cl
andb %al, %cl
movl 0x18(%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, 0xf(%rsp)
movq 0x18(%rbx), %rax
movq %rax, %rcx
btsq $0x26, %rcx
lzcntq %rcx, %rbp
movq %rax, %rcx
shrq $0x31, %rcx
jne 0x2e23f
leal -0x2(,%rbp,2), %esi
movl $0xf, %eax
shlxl %r13d, %eax, %ebp
addl $0xfffff000, %ebp       # imm = 0xFFFFF000
jmp 0x2e274
leaq (%r12,%r14), %rcx
cmpq %r12, %rcx
setbe %cl
testb %cl, 0xf(%rsp)
jne 0x2e25d
leal 0x1(%r13,%rbp), %esi
decl %ebp
shlxl %r13d, %ebp, %ebp
jmp 0x2e274
shrq $0x32, %rax
je 0x2e26a
leal 0x1(%rbp), %esi
decl %ebp
jmp 0x2e274
movl $0x13, %esi
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl $0x3, %r12d
setne %r15b
movq %rbx, %rdi
callq 0x18ad7
addl %ebp, %eax
testq %r14, %r14
sete %cl
andb %r15b, %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 0x2e20c
movq 0x28(%rsp), %r11
movl %r11d, %ecx
movl 0x14(%rsp), %r15d
subl %r15d, %ecx
xorl %eax, %eax
movq 0x18(%rsp), %r14
cmpl %ecx, %r14d
jg 0x2e35c
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 0xa955(%rip), %rdi      # 0x38c90
leaq (%rdi,%rcx,4), %rcx
movzbl (%rcx,%rax), %eax
movl %eax, %ecx
shrl $0x4, %ecx
movq %rdx, 0x20(%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, 0x10(%rsp)
jb 0x2e436
leaq (%r14,%rcx), %rdx
addq $0x1a9f, %rdx           # imm = 0x1A9F
movl $0x1, %esi
leaq 0xa8b3(%rip), %rdi      # 0x38c50
decq %rdx
testl %eax, %eax
jle 0x2e41a
movq 0x18(%rbx), %r8
cmpl $0x7, %eax
setb %r9b
movq %r8, %rcx
shrq $0x3d, %rcx
setne %r10b
orb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x2e3e8
cmpl $0x7, %eax
movl $0x7, %r9d
cmovbl %eax, %r9d
decl %r9d
leaq (%rdi,%r9,8), %r9
movsbl (%rcx,%r9), %r9d
movl %r9d, %ecx
sarl $0x3, %ecx
andl $0x7, %r9d
jmp 0x2e3fc
lzcntq %r8, %r9
leal 0x1(%r9), %ecx
addl $0x4, %r9d
cmpl %eax, %r9d
cmovael %eax, %r9d
movl %r9d, %r10d
subq %r10, %rdx
subl %r9d, %eax
movq 0x10(%rbx), %r9
movq %r8, 0x20(%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 0x2e39d
movl %r11d, %ebp
subl %r15d, %ebp
jl 0x2e4b0
leaq 0x1cd2(%rbx), %r14
movslq %r15d, %r15
movslq %r11d, %r12
movzwl 0x1cd0(%rbx), %eax
incl %ebp
leaq 0xc3c3(%rip), %r13      # 0x3a81e
movzwl %ax, %edi
cmpl $0xfff, %edi            # imm = 0xFFF
ja 0x2e4a9
movl $0x1000, %eax           # imm = 0x1000
subl %edi, %eax
addq %r14, %rdi
movzwl %ax, %esi
cmpq %r12, %r15
movq %r13, %rdx
leaq 0xbe62(%rip), %rax      # 0x3a2e2
cmovlq %rax, %rdx
movsbq 0x1aa0(%rbx,%r15), %rax
movl 0x1b60(%rbx,%rax,4), %ecx
xorl %eax, %eax
callq 0x1070
addw 0x1cd0(%rbx), %ax
movw %ax, 0x1cd0(%rbx)
incq %r15
decl %ebp
jne 0x2e45b
cmpq $0x0, 0x10(%rbx)
jne 0x2e4bf
movq %rbx, %rdi
callq 0x18999
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq