// File: /workspace/github_c_makefile_star2_O1/akumrao[P]videoAnalyzer/decoder/macroblock.c
// Function: block_residual_read_cavlc(x264_t*, bs_s*, x264_macroblock_t*, int, int*, int)
static int block_residual_read_cavlc(x264_t *h, bs_t *s, x264_macroblock_t *mb,
        int i_idx, int *l, int i_count) {LOG_CALL;
    int i;
    int level[16], run[16];
    int i_coeff;

    int i_total, i_trailing;
    int i_suffix_length;
    int i_zero_left;

    for (i = 0; i < i_count; i++)
    {
        l[i] = 0;
    }

    /* total/trailing */
    if (i_idx == BLOCK_INDEX_CHROMA_DC)
    {
        int i_tt;

        if ((i_tt = bs_read_vlc(s, h->x264_coeff_token_lookup[4])) < 0)// chroma_dc_coeff_token_vlc
        {
            return -1;
        }

        i_total = i_tt / 4;
        i_trailing = i_tt % 4;
    } else
    {
        /* x264_mb_predict_non_zero_code return 0 <-> (16+16+1)>>1 = 16 */
        static const int ct_index[17] = {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
        int nC;
        int i_tt;

        if (i_idx == BLOCK_INDEX_LUMA_DC)
        {
            nC = x264_mb_predict_non_zero_code(h, mb, 0);
        } else
        {
            nC = x264_mb_predict_non_zero_code(h, mb, i_idx);
        }

        if ((i_tt = bs_read_vlc(s, h->x264_coeff_token_lookup[ct_index[nC]])) < 0)
        {
            return -1;
        }

        i_total = i_tt / 4;
        i_trailing = i_tt % 4;
    }

    if (i_idx >= 0)
    {
        mb->block[i_idx].i_non_zero_count = i_total;
    }

    if (i_total <= 0)
    {
        return 0;
    }

    i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;

    for (i = 0; i < i_trailing; i++)
    {
        level[i] = 1 - 2 * bs_read1(s,"level");
    }

    for (; i < i_total; i++)
    {
        int i_prefix;
        int i_level_code;

        i_prefix = bs_read_vlc(s, h->x264_level_prefix_lookup);

        if (i_prefix == -1)
        {
            return -1;
        } else if (i_prefix < 14)
        {
            if (i_suffix_length > 0)
            {
                i_level_code = (i_prefix << i_suffix_length) + bs_read(s, i_suffix_length,"i_suffix_length");
            } else
            {
                i_level_code = i_prefix;
            }
        } else if (i_prefix == 14)
        {
            if (i_suffix_length > 0)
            {
                i_level_code = (i_prefix << i_suffix_length) + bs_read(s, i_suffix_length,"i_suffix_length");
            } else
            {
                i_level_code = i_prefix + bs_read(s, 4,"i_level_code");
            }
        } else /* if( i_prefix == 15 ) */
        {
            i_level_code = (i_prefix << i_suffix_length) + bs_read(s, 12,"i_level_code");
            if (i_suffix_length == 0)
            {
                i_level_code += 15;
            }
        }
        if (i == i_trailing && i_trailing < 3)
        {
            i_level_code += 2;
        }
        /* Optimise */
        level[i] = i_level_code & 0x01 ? -((i_level_code + 1) / 2) : (i_level_code + 2) / 2;

        if (i_suffix_length == 0)
        {
            i_suffix_length++;
        }
        if (abs(level[i]) > (3 << (i_suffix_length - 1)) && i_suffix_length < 6)
        {
            i_suffix_length++;
        }
    }

    if (i_total < i_count)
    {
        if (i_idx == BLOCK_INDEX_CHROMA_DC)
        {
            i_zero_left = bs_read_vlc(s, h->x264_total_zeros_dc_lookup[i_total - 1]);
        } else
        {
            i_zero_left = bs_read_vlc(s, h->x264_total_zeros_lookup[i_total - 1]);
        }
        if (i_zero_left < 0)
        {
            return -1;
        }
    } else
    {
        i_zero_left = 0;
    }

    for (i = 0; i < i_total - 1; i++)
    {
        if (i_zero_left <= 0)
        {
            break;
        }
        run[i] = bs_read_vlc(s, h->x264_run_before_lookup[X264_MIN(i_zero_left - 1, 6)]);

        if (run[i] < 0)
        {
            return -1;
        }
        i_zero_left -= run[i];
    }
    if (i_zero_left < 0)
    {
        return -1;
    }

    for (; i < i_total - 1; i++)
    {
        run[i] = 0;
    }
    run[i_total - 1] = i_zero_left;

    i_coeff = -1;
    for (i = i_total - 1; i >= 0; i--)
    {
        i_coeff += run[i] + 1;
        l[i_coeff] = level[i];
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r9d, %r12d
movq %r8, %r15
movl %ecx, %r13d
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, -0x48(%rbp)
leaq 0x15d21(%rip), %rdi     # 0x251b4
leaq 0x18dc2(%rip), %rsi     # 0x2825c
leaq 0x18f73(%rip), %rdx     # 0x28414
xorl %eax, %eax
callq 0x4c22
testl %r12d, %r12d
movl %r12d, -0x4c(%rbp)
jle 0xf4c2
movl %r12d, %edx
shlq $0x2, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2070
cmpl $-0x1, %r13d
je 0xf4f8
xorl %edx, %edx
cmpl $-0x2, %r13d
cmovnel %r13d, %edx
movq -0x48(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x67eb
cltq
leaq 0x18b76(%rip), %rcx     # 0x28060
movslq (%rcx,%rax,4), %rax
movq 0x10f3c8(%r12,%rax,8), %rsi
jmp 0xf503
movq -0x48(%rbp), %rax
movq 0x10f3e8(%rax), %rsi
movq %r14, %rdi
callq 0x104b0
testl %eax, %eax
js 0xfa64
movl %eax, %edx
shrl $0x2, %edx
testl %r13d, %r13d
js 0xf536
movl %eax, %esi
movl %r13d, %eax
movq %rax, %rcx
shlq $0x7, %rcx
leaq (%rcx,%rax,4), %rax
movl %edx, 0x328(%rbx,%rax)
movl %esi, %eax
cmpl $0x4, %eax
jb 0xfa60
movl %r13d, -0x5c(%rbp)
movq %rdx, -0x58(%rbp)
movq %r15, -0x78(%rbp)
movl %eax, %ecx
andl $0x3, %ecx
movl %eax, -0x60(%rbp)
cmpl $0x2c, %eax
setae %dl
cmpl $0x3, %ecx
setne %sil
movl %ecx, %eax
movq %rax, -0x70(%rbp)
movl %ecx, -0x64(%rbp)
testl %ecx, %ecx
je 0xf60a
movb %sil, -0x38(%rbp)
movb %dl, -0x2c(%rbp)
leaq 0x18569(%rip), %rbx     # 0x27ae8
leaq 0x18ea8(%rip), %r15     # 0x2842e
xorl %r13d, %r13d
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jae 0xf5dc
movl 0x18(%r14), %edx
decl %edx
movl %edx, 0x18(%r14)
movzbl (%rax), %ecx
xorl %r12d, %r12d
btl %edx, %ecx
setb %cl
testl %edx, %edx
jne 0xf5bc
incq %rax
movq %rax, 0x8(%r14)
movl $0x8, 0x18(%r14)
movb %cl, %r12b
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4c22
addl %r12d, %r12d
movl $0x1, %eax
subl %r12d, %eax
jmp 0xf5f0
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4c22
movl $0x1, %eax
movl %eax, -0x100(%rbp,%r13,4)
incq %r13
cmpq %r13, -0x70(%rbp)
jne 0xf589
movb -0x2c(%rbp), %dl
movb -0x38(%rbp), %sil
jmp 0xf60d
xorl %r13d, %r13d
cmpl -0x58(%rbp), %r13d
movl -0x4c(%rbp), %eax
jae 0xf965
andb %sil, %dl
movzbl %dl, %ebx
movl %r13d, %r13d
movl -0x58(%rbp), %r15d
movq %r15, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq 0x10f3f0(%rax), %rsi
movq %r14, %rdi
callq 0x104b0
cmpl $-0x1, %eax
je 0xf94d
cmpl $0xd, %eax
jg 0xf6b3
testl %ebx, %ebx
leaq 0x1897b(%rip), %r8      # 0x27fd0
jle 0xf7ab
movl %eax, %r10d
movl %ebx, %ecx
shll %cl, %r10d
movq 0x8(%r14), %r9
movq 0x10(%r14), %rdx
xorl %r15d, %r15d
movl %ebx, %esi
movl %eax, -0x2c(%rbp)
movl %r10d, -0x40(%rbp)
cmpq %rdx, %r9
jae 0xf719
movslq 0x18(%r14), %rdi
movl %edi, %ecx
subl %esi, %ecx
movzbl (%r9), %r12d
jns 0xf7b3
andl (%r8,%rdi,4), %r12d
negb %cl
shll %cl, %r12d
orl %r12d, %r15d
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
subl %edi, %esi
jg 0xf677
jmp 0xf719
cmpl $0xe, %eax
leaq 0x18913(%rip), %r8      # 0x27fd0
movl %eax, -0x2c(%rbp)
jne 0xf739
testl %ebx, %ebx
jle 0xf7f7
movl $0xe, %edx
movl %ebx, %ecx
shll %cl, %edx
movl %edx, -0x40(%rbp)
movq 0x8(%r14), %r9
movq 0x10(%r14), %rdx
xorl %r15d, %r15d
movl %ebx, %esi
cmpq %rdx, %r9
jae 0xf719
movslq 0x18(%r14), %rdi
movl %edi, %ecx
subl %esi, %ecx
movzbl (%r9), %r12d
jns 0xf7b3
andl (%r8,%rdi,4), %r12d
negb %cl
shll %cl, %r12d
orl %r12d, %r15d
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
subl %edi, %esi
jg 0xf6e3
leaq 0x183b4(%rip), %rdi     # 0x27ad4
movl %r15d, %edx
leaq 0x18d0a(%rip), %rcx     # 0x28434
xorl %eax, %eax
callq 0x4c22
movl %r15d, %r12d
jmp 0xf7ee
movl %eax, %r10d
movl %ebx, %ecx
shll %cl, %r10d
movq 0x8(%r14), %r9
movq 0x10(%r14), %rdx
movl $0xc, %esi
xorl %r15d, %r15d
movq %r10, -0x40(%rbp)
cmpq %rdx, %r9
jae 0xf78b
movslq 0x18(%r14), %rdi
movl %edi, %ecx
subl %esi, %ecx
movzbl (%r9), %r12d
jns 0xf85d
andl (%r8,%rdi,4), %r12d
negb %cl
shll %cl, %r12d
orl %r12d, %r15d
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
subl %edi, %esi
jg 0xf755
leaq 0x18342(%rip), %rdi     # 0x27ad4
movl %r15d, %edx
leaq 0x18ca8(%rip), %rcx     # 0x28444
xorl %eax, %eax
callq 0x4c22
movl %r15d, %r12d
jmp 0xf898
movl %eax, %r12d
jmp 0xf8f3
shrl %cl, %r12d
movl %esi, %edx
andl (%r8,%rdx,4), %r12d
movl %ecx, 0x18(%r14)
cmpl %esi, %edi
jne 0xf7d3
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
orl %r15d, %r12d
leaq 0x182e2(%rip), %rdi     # 0x27abf
movl %r12d, %edx
leaq 0x18c4d(%rip), %rcx     # 0x28434
xorl %eax, %eax
callq 0x4c22
addl -0x40(%rbp), %r12d
jmp 0xf8ec
movq 0x8(%r14), %r9
movq 0x10(%r14), %rdx
movl $0x4, %esi
xorl %r15d, %r15d
cmpq %rdx, %r9
jae 0xf83d
movslq 0x18(%r14), %rdi
movl %edi, %ecx
subl %esi, %ecx
movzbl (%r9), %r12d
jns 0xf8ad
andl (%r8,%rdi,4), %r12d
negb %cl
shll %cl, %r12d
orl %r12d, %r15d
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
subl %edi, %esi
jg 0xf807
leaq 0x18290(%rip), %rdi     # 0x27ad4
movl %r15d, %edx
leaq 0x18bf6(%rip), %rcx     # 0x28444
xorl %eax, %eax
callq 0x4c22
movl %r15d, %r12d
jmp 0xf8e8
shrl %cl, %r12d
movl %esi, %edx
andl (%r8,%rdx,4), %r12d
movl %ecx, 0x18(%r14)
cmpl %esi, %edi
jne 0xf87d
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
orl %r15d, %r12d
leaq 0x18238(%rip), %rdi     # 0x27abf
movl %r12d, %edx
leaq 0x18bb3(%rip), %rcx     # 0x28444
xorl %eax, %eax
callq 0x4c22
movq -0x40(%rbp), %rcx
leal (%r12,%rcx), %eax
testl %ebx, %ebx
leal 0xf(%r12,%rcx), %r12d
cmovnel %eax, %r12d
jmp 0xf8ec
shrl %cl, %r12d
movl %esi, %edx
andl (%r8,%rdx,4), %r12d
movl %ecx, 0x18(%r14)
cmpl %esi, %edi
jne 0xf8cd
incq %r9
movq %r9, 0x8(%r14)
movl $0x8, 0x18(%r14)
orl %r15d, %r12d
leaq 0x181e8(%rip), %rdi     # 0x27abf
movl %r12d, %edx
leaq 0x18b63(%rip), %rcx     # 0x28444
xorl %eax, %eax
callq 0x4c22
addl $0xe, %r12d
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %eax
leal 0x2(%r12), %ecx
cmpq -0x70(%rbp), %r13
cmovnel %r12d, %ecx
cmpl $0x3, -0x64(%rbp)
cmovel %r12d, %ecx
testb $0x1, %cl
jne 0xf914
addl $0x2, %ecx
sarl %ecx
jmp 0xf91a
incl %ecx
sarl %ecx
negl %ecx
movl %ecx, -0x100(%rbp,%r13,4)
cmpl $0x1, %ebx
adcl $0x0, %ebx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
leal -0x1(%rbx), %ecx
movl $0x3, %esi
shll %cl, %esi
cmpl %esi, %edx
setg %dl
cmpl $0x6, %ebx
setl %cl
andb %dl, %cl
movzbl %cl, %ecx
addl %ebx, %ecx
movl %ecx, %ebx
cmpl $-0x1, %eax
movl -0x4c(%rbp), %eax
je 0xfa64
incq %r13
cmpq %r15, %r13
jne 0xf62b
xorl %r15d, %r15d
movl $0x0, %ebx
movq -0x58(%rbp), %r13
cmpl %eax, %r13d
jge 0xf9aa
leal -0x1(%r13), %eax
movq -0x48(%rbp), %rdx
leaq 0x10f3f8(%rdx), %rcx
addq $0x10f470, %rdx         # imm = 0x10F470
cmpl $-0x1, -0x5c(%rbp)
cmovneq %rcx, %rdx
movq (%rdx,%rax,8), %rsi
movq %r14, %rdi
callq 0x104b0
movl %eax, %ebx
testl %eax, %eax
js 0xfa64
leal -0x1(%r13), %r12d
cmpl $0x8, -0x60(%rbp)
jb 0xf9fd
testl %ebx, %ebx
je 0xf9fd
movslq %r12d, %r13
xorl %r15d, %r15d
cmpl $0x7, %ebx
movl $0x7, %eax
cmovll %ebx, %eax
decl %eax
movq -0x48(%rbp), %rcx
movq 0x10f488(%rcx,%rax,8), %rsi
movq %r14, %rdi
callq 0x104b0
movl %eax, -0xc0(%rbp,%r15,4)
testl %eax, %eax
js 0xfa64
subl %eax, %ebx
incq %r15
cmpq %r13, %r15
jge 0xf9f9
testl %ebx, %ebx
jg 0xf9be
movq -0x58(%rbp), %r13
testl %ebx, %ebx
movq -0x78(%rbp), %r14
js 0xfa64
cmpl %r12d, %r15d
jge 0xfa30
movl %r15d, %eax
leaq -0xc0(,%rax,4), %rdi
addq %rbp, %rdi
movl %r13d, %eax
subl %r15d, %eax
addl $-0x2, %eax
leaq 0x4(,%rax,4), %rdx
xorl %esi, %esi
callq 0x2070
movl %r12d, %eax
movl %ebx, -0xc0(%rbp,%rax,4)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl -0xc0(%rbp,%rax,4), %edx
addl %edx, %ecx
incl %ecx
movl -0x100(%rbp,%rax,4), %edx
movslq %ecx, %rsi
movl %edx, (%r14,%rsi,4)
decq %rax
decl %r13d
jg 0xfa3f
xorl %eax, %eax
jmp 0xfa69
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq