// File: /workspace/github_c_makefile_star2_O2/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 $0xc8, %rsp
movl %r9d, %r13d
movq %r8, %rbx
movl %ecx, %r12d
movq %rdx, %r15
movq %rsi, -0x30(%rbp)
movq %rdi, -0x38(%rbp)
leaq 0xfb7e(%rip), %rdi      # 0x1b1b4
leaq 0x12b2f(%rip), %rsi     # 0x1e16c
leaq 0x12ce0(%rip), %rdx     # 0x1e324
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x39ba
testl %r13d, %r13d
movl $0x0, %eax
movl %r13d, -0x54(%rbp)
cmovgl %r13d, %eax
cmpq %r14, %rax
je 0xb66d
andl $0x0, (%rbx,%r14,4)
incq %r14
jmp 0xb65e
pushq $-0x1
popq %r13
cmpl $-0x1, %r12d
je 0xb6db
xorl %edx, %edx
cmpl $-0x2, %r12d
cmovnel %r12d, %edx
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x5523
cltq
leaq 0x128d7(%rip), %rcx     # 0x1df70
movslq (%rcx,%rax,4), %rax
movq 0x10f3c8(%r14,%rax,8), %rsi
movq -0x30(%rbp), %rdi
callq 0xc3ad
testl %eax, %eax
js 0xb9a3
movl %r12d, %edx
movl %eax, %r12d
movl %eax, %ecx
shrl $0x2, %ecx
testl %edx, %edx
movl %edx, -0x3c(%rbp)
js 0xb703
movl %edx, %eax
imulq $0x84, %rax, %rax
movl %ecx, 0x328(%r15,%rax)
jmp 0xb703
movl %r12d, -0x3c(%rbp)
movq -0x38(%rbp), %rax
movq 0x10f3e8(%rax), %rsi
movq -0x30(%rbp), %rdi
callq 0xc3ad
testl %eax, %eax
js 0xb9a3
movl %eax, %r12d
movl %eax, %ecx
shrl $0x2, %ecx
testl %ecx, %ecx
je 0xb9a0
movq %rbx, -0x60(%rbp)
andl $0x3, %r12d
movq %rcx, -0x48(%rbp)
cmpl $0xb, %ecx
setae %al
cmpl $0x3, %r12d
setne %cl
andb %al, %cl
movb %cl, -0x50(%rbp)
xorl %r13d, %r13d
leaq 0x12c0b(%rip), %r15     # 0x1e33e
pushq $0x1
popq %r14
movq -0x30(%rbp), %rbx
cmpq %r13, %r12
je 0xb75f
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb467
addl %eax, %eax
movl %r14d, %ecx
subl %eax, %ecx
movl %ecx, -0xf0(%rbp,%r13,4)
incq %r13
jmp 0xb73b
movzbl -0x50(%rbp), %r13d
movl -0x48(%rbp), %eax
movq %rax, -0x50(%rbp)
leaq -0xf0(,%r12,4), %rax
addq %rbp, %rax
movq %rax, -0x68(%rbp)
xorl %r14d, %r14d
leaq (%r12,%r14), %rax
cmpq -0x50(%rbp), %rax
jae 0xb8b0
movq -0x38(%rbp), %rax
movq 0x10f3f0(%rax), %rsi
movq %rbx, %rdi
callq 0xc3ad
cmpl $-0x1, %eax
je 0xb93e
movl %eax, %r15d
cmpl $0xd, %eax
jg 0xb7d8
testl %r13d, %r13d
jle 0xb84d
movl %r13d, %ecx
shll %cl, %r15d
movq %rbx, %rdi
movl %r13d, %esi
leaq 0x12b79(%rip), %rdx     # 0x1e344
callq 0xb4ba
addl %r15d, %eax
movl %eax, %r15d
jmp 0xb84d
cmpl $0xe, %r15d
jne 0xb80a
testl %r13d, %r13d
jle 0xb834
pushq $0xe
popq %rbx
movl %r13d, %ecx
shll %cl, %ebx
movq -0x30(%rbp), %rdi
movl %r13d, %esi
leaq 0x12b4b(%rip), %rdx     # 0x1e344
callq 0xb4ba
movl %eax, %r15d
addl %ebx, %r15d
movq -0x30(%rbp), %rbx
jmp 0xb84d
movl %r13d, %ecx
shll %cl, %r15d
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x12b37(%rip), %rdx     # 0x1e354
callq 0xb4ba
leal (%rax,%r15), %ecx
testl %r13d, %r13d
leal 0xf(%rax,%r15), %r15d
cmovnel %ecx, %r15d
jmp 0xb84d
movq %rbx, %rdi
pushq $0x4
popq %rsi
leaq 0x12b13(%rip), %rdx     # 0x1e354
callq 0xb4ba
movl %eax, %r15d
addl $0xe, %r15d
leal 0x2(%r15), %eax
testq %r14, %r14
cmovnel %r15d, %eax
cmpl $0x3, %r12d
cmovel %r15d, %eax
testb $0x1, %al
jne 0xb86b
addl $0x2, %eax
sarl %eax
jmp 0xb871
incl %eax
sarl %eax
negl %eax
cmpl $0x1, %r13d
adcl $0x0, %r13d
movl %eax, %edx
negl %edx
cmovsl %eax, %edx
movq -0x68(%rbp), %rcx
movl %eax, (%rcx,%r14,4)
leal -0x1(%r13), %ecx
pushq $0x3
popq %rax
shll %cl, %eax
cmpl %eax, %edx
setg %al
cmpl $0x6, %r13d
setl %cl
andb %al, %cl
movzbl %cl, %eax
addl %r13d, %eax
incq %r14
movl %eax, %r13d
jmp 0xb77d
xorl %r12d, %r12d
movq -0x48(%rbp), %r14
cmpl -0x54(%rbp), %r14d
jge 0xb8ee
leal -0x1(%r14), %eax
movq -0x38(%rbp), %rdx
leaq 0x10f3f8(%rdx), %rcx
addq $0x10f470, %rdx         # imm = 0x10F470
cmpl $-0x1, -0x3c(%rbp)
cmovneq %rcx, %rdx
movq (%rdx,%rax,8), %rsi
movq %rbx, %rdi
callq 0xc3ad
movl %eax, %r12d
testl %eax, %eax
js 0xb93e
decl %r14d
movq %r14, -0x48(%rbp)
movslq %r14d, %r15
xorl %r14d, %r14d
pushq $0x7
popq %r13
cmpq %r15, %r14
jge 0xb944
testl %r12d, %r12d
jle 0xb944
cmpl $0x7, %r12d
movl %r13d, %eax
cmovbl %r12d, %eax
decl %eax
movq -0x38(%rbp), %rcx
movq 0x10f488(%rcx,%rax,8), %rsi
movq %rbx, %rdi
callq 0xc3ad
movl %eax, -0xb0(%rbp,%r14,4)
testl %eax, %eax
js 0xb93e
subl %eax, %r12d
incq %r14
jmp 0xb8ff
pushq $-0x1
popq %r13
jmp 0xb9a3
testl %r12d, %r12d
pushq $-0x1
popq %r13
js 0xb9a3
movq -0x48(%rbp), %rdi
cmpl %edi, %r14d
movl %edi, %eax
cmovgl %r14d, %eax
movq -0x60(%rbp), %rsi
cmpq %r14, %rax
je 0xb971
andl $0x0, -0xb0(%rbp,%r14,4)
incq %r14
jmp 0xb95e
movl %edi, %eax
movl %r12d, -0xb0(%rbp,%rax,4)
pushq $-0x1
popq %rax
testl %edi, %edi
js 0xb9a0
movl %edi, %ecx
movl -0xb0(%rbp,%rcx,4), %edx
addl %edx, %eax
incl %eax
movl -0xf0(%rbp,%rcx,4), %ecx
movslq %eax, %rdx
movl %ecx, (%rsi,%rdx,4)
decl %edi
jmp 0xb97e
xorl %r13d, %r13d
movl %r13d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq