// File: /workspace/github_c_makefile_star2_O2/akumrao[P]videoAnalyzer/encoder/macroblock.c
// Function: block_residual_write_cavlc(x264_t*, bs_s*, x264_macroblock_t*, int, int*, int)
static void block_residual_write_cavlc(x264_t *h, bs_t *s, x264_macroblock_t *mb, int i_idx, int *l, int i_count) {LOG_CALL;
    int level[16], run[16];
    int i_total, i_trailing;
    int i_total_zero;
    int i_last;
    unsigned int i_sign;

    int i;
    int i_zero_left;
    int i_suffix_length;

    /* first find i_last */
    i_last = i_count - 1;
    while (i_last >= 0 && l[i_last] == 0)
    {
        i_last--;
    }

    i_sign = 0;
    i_total = 0;
    i_trailing = 0;
    i_total_zero = 0;

    if (i_last >= 0)
    {
        int b_trailing = 1;
        int idx = 0;

        /* level and run and total */
        while (i_last >= 0)
        {
            level[idx] = l[i_last--];

            run[idx] = 0;
            while (i_last >= 0 && l[i_last] == 0)
            {
                run[idx]++;
                i_last--;
            }

            i_total++;
            i_total_zero += run[idx];

            if (b_trailing && abs(level[idx]) == 1 && i_trailing < 3)
            {
                i_sign <<= 1;
                if (level[idx] < 0)
                {
                    i_sign |= 0x01;
                }

                i_trailing++;
            } else
            {
                b_trailing = 0;
            }

            idx++;
        }
    }

    /* total/trailing */
    if (i_idx == BLOCK_INDEX_CHROMA_DC)
    {
        bs_write_vlc(s, x264_coeff_token[4][i_total * 4 + i_trailing], "vlc BLOCK_INDEX_CHROMA_DC x264_coeff_token");
    } 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;

        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);
        }

        bs_write_vlc(s, x264_coeff_token[ct_index[nC]][i_total * 4 + i_trailing],"vlc x264_coeff_token");
    }

    if (i_total <= 0)
    {
        return;
    }

    i_suffix_length = i_total > 10 && i_trailing < 3 ? 1 : 0;
    if (i_trailing > 0)
    {
        bs_write(s, i_trailing, i_sign,"i_trailing");
    }
    for (i = i_trailing; i < i_total; i++)
    {
        int i_level_code;

        /* calculate level code */
        if (level[i] < 0)
        {
            i_level_code = -2 * level[i] - 1;
        } else /* if( level[i] > 0 ) */
        {
            i_level_code = 2 * level[i] - 2;
        }
        if (i == i_trailing && i_trailing < 3)
        {
            i_level_code -= 2; /* as level[i] can't be 1 for the first one if i_trailing < 3 */
        }

        if ((i_level_code >> i_suffix_length) < 14)
        {
            bs_write_vlc(s, x264_level_prefix[i_level_code >> i_suffix_length],"vlc x264_level_prefix");
            if (i_suffix_length > 0)
            {
                bs_write(s, i_suffix_length, i_level_code,"i_suffix_length");
            }
        } else if (i_suffix_length == 0 && i_level_code < 30)
        {
            bs_write_vlc(s, x264_level_prefix[14],"vlc x264_level_prefix");
            bs_write(s, 4, i_level_code - 14,"i_level_code");
        } else if (i_suffix_length > 0 && (i_level_code >> i_suffix_length) == 14)
        {
            bs_write_vlc(s, x264_level_prefix[14],"vlc x264_level_prefix");
            bs_write(s, i_suffix_length, i_level_code,"i_suffix_length");
        } else
        {
            bs_write_vlc(s, x264_level_prefix[15],"vlc x264_level_prefix");
            i_level_code -= 15 << i_suffix_length;
            if (i_suffix_length == 0)
            {
                i_level_code -= 15;
            }

            if (i_level_code >= (1 << 12) || i_level_code < 0)
            {
                fprintf(stderr, "OVERFLOW levelcode=%d\n", i_level_code);
            }

            bs_write(s, 12, i_level_code,"i_level_code"); /* check overflow ?? */
        }

        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)
        {
            bs_write_vlc(s, x264_total_zeros_dc[i_total - 1][i_total_zero],"vlc x264_total_zeros_dc");
        } else
        {
            bs_write_vlc(s, x264_total_zeros[i_total - 1][i_total_zero],"vlc x264_total_zeros");
        }
    }

    for (i = 0, i_zero_left = i_total_zero; i < i_total - 1; i++)
    {
        int i_zl;

        if (i_zero_left <= 0)
        {
            break;
        }

        i_zl = X264_MIN(i_zero_left - 1, 6);

        bs_write_vlc(s, x264_run_before[i_zl][run[i]],"vlc x264_run_before");

        i_zero_left -= run[i];
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %r9d, %r15d
movq %r8, %r14
movl %ecx, -0x34(%rbp)
movq %rdx, -0x30(%rbp)
movq %rsi, %rbx
movq %rdi, -0x40(%rbp)
leaq 0x8dcf(%rip), %rdi      # 0x1b1b4
leaq 0xf57c(%rip), %rsi      # 0x21968
leaq 0xf97f(%rip), %rdx      # 0x21d72
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x39ba
movl %r15d, %eax
leaq (%r14,%rax,4), %rcx
addq $-0x4, %rcx
movl %r15d, -0x4c(%rbp)
movl %r15d, %eax
testl %eax, %eax
jle 0x124bf
decl %eax
cmpl $0x0, (%rcx)
leaq -0x4(%rcx), %rcx
je 0x1240f
xorl %r12d, %r12d
pushq $-0x1
popq %rcx
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %edx, %edx
testl %eax, %eax
js 0x124c8
movl %eax, %r8d
movl (%r14,%r8,4), %esi
movl %esi, -0xf0(%rbp,%r12,4)
movq %r8, %r9
negq %r9
decq %r8
xorl %edi, %edi
movq %r9, %r10
addq %rdi, %r10
je 0x1246f
movl %r8d, %r10d
cmpl $0x0, (%r14,%r10,4)
jne 0x12475
incq %rdi
decq %r8
jmp 0x12455
movl %eax, %edi
movl %ecx, %eax
jmp 0x1247e
movl %edi, %r8d
notl %r8d
addl %r8d, %eax
movl %edi, -0xb0(%rbp,%r12,4)
incq %r12
addl %edi, %r15d
testb $0x1, %dl
movb $0x1, %dl
jne 0x12433
cmpl $0x3, %r11d
setb %dl
leal 0x1(%rsi), %edi
testl $0xfffffffd, %edi       # imm = 0xFFFFFFFD
sete %dil
andb %dl, %dil
movb $0x1, %dl
cmpb $0x1, %dil
jne 0x12433
shldl $0x1, %esi, %r13d
incl %r11d
jmp 0x12431
xorl %r15d, %r15d
xorl %r11d, %r11d
xorl %r12d, %r12d
movl -0x34(%rbp), %eax
cmpl $-0x1, %eax
movq %r11, -0x48(%rbp)
je 0x1251e
xorl %edx, %edx
cmpl $-0x2, %eax
cmovnel %eax, %edx
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq %r11, %r14
callq 0x5523
cltq
leaq 0xdceb(%rip), %rcx      # 0x201e0
movslq (%rcx,%rax,4), %rax
leal (%r14,%r12,4), %ecx
movslq %ecx, %rcx
imulq $0x220, %rax, %rax      # imm = 0x220
leaq 0xde02(%rip), %rdx      # 0x20310
addq %rax, %rdx
leaq (%rdx,%rcx,8), %rax
leaq 0xf89c(%rip), %rdx      # 0x21db8
jmp 0x1253c
leal (%r11,%r12,4), %eax
cltq
leaq 0xdde5(%rip), %rcx      # 0x20310
leaq (%rcx,%rax,8), %rax
addq $0x880, %rax            # imm = 0x880
leaq 0xf851(%rip), %rdx      # 0x21d8d
movq (%rax), %rsi
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
callq 0x13c54
testl %r12d, %r12d
je 0x127fa
cmpl $0xb, %r12d
setae %al
movq -0x48(%rbp), %rsi
cmpl $0x3, %esi
setb %bl
andb %al, %bl
testl %esi, %esi
je 0x12582
leaq 0xf85b(%rip), %rcx      # 0x21dcd
movq -0x30(%rbp), %rdi
movl %r13d, %edx
callq 0x1221b
movq -0x48(%rbp), %rsi
movzbl %bl, %r14d
movl %esi, %eax
movslq %r12d, %rcx
movq %rcx, -0x68(%rbp)
movq %rax, -0x40(%rbp)
leaq -0xf0(,%rax,4), %rax
addq %rbp, %rax
movq %rax, -0x58(%rbp)
xorl %ebx, %ebx
movl %r15d, -0x38(%rbp)
movq %r12, -0x60(%rbp)
movq -0x40(%rbp), %rax
addq %rbx, %rax
cmpq -0x68(%rbp), %rax
jge 0x1274b
movq -0x58(%rbp), %rax
movl (%rax,%rbx,4), %r12d
leal (%r12,%r12), %eax
testl %r12d, %r12d
notl %eax
leal -0x2(%r12,%r12), %ecx
cmovsl %eax, %ecx
leal -0x2(%rcx), %r13d
testq %rbx, %rbx
cmovnel %ecx, %r13d
cmpl $0x3, %esi
cmovael %ecx, %r13d
movl %r13d, %eax
movl %r14d, %ecx
sarl %cl, %eax
cmpl $0xd, %eax
jg 0x12621
cltq
leaq 0xdc32(%rip), %rcx      # 0x20230
movq (%rcx,%rax,8), %rsi
movq -0x30(%rbp), %rdi
leaq 0xf7cb(%rip), %rdx      # 0x21dd8
callq 0x13c54
testl %r14d, %r14d
jle 0x126ed
movq -0x30(%rbp), %rdi
jmp 0x1268d
testl %r14d, %r14d
jne 0x1265f
cmpl $0x1d, %r13d
jg 0x1265f
movq -0x30(%rbp), %r15
movq %r15, %rdi
movabsq $0xf00000001, %rsi      # imm = 0xF00000001
leaq 0xf794(%rip), %rdx      # 0x21dd8
callq 0x13c54
addl $-0xe, %r13d
movq %r15, %rdi
movl -0x38(%rbp), %r15d
pushq $0x4
popq %rsi
movl %r13d, %edx
jmp 0x126e1
testl %r14d, %r14d
jle 0x1269c
cmpl $0xe, %eax
jne 0x1269c
movq -0x30(%rbp), %r15
movq %r15, %rdi
movabsq $0xf00000001, %rsi      # imm = 0xF00000001
leaq 0xf757(%rip), %rdx      # 0x21dd8
callq 0x13c54
movq %r15, %rdi
movl -0x38(%rbp), %r15d
movl %r14d, %esi
movl %r13d, %edx
leaq 0xbcaa(%rip), %rcx      # 0x1e344
jmp 0x126e8
movq -0x30(%rbp), %rdi
movabsq $0x1000000001, %rsi     # imm = 0x1000000001
leaq 0xf727(%rip), %rdx      # 0x21dd8
callq 0x13c54
pushq $-0xf
popq %rax
movl %r14d, %ecx
shll %cl, %eax
leal (%rax,%r13), %ecx
testl %r14d, %r14d
leal -0xf(%r13,%rax), %r13d
cmovnel %ecx, %r13d
cmpl $0x1000, %r13d          # imm = 0x1000
jae 0x1272e
movq -0x30(%rbp), %rdi
pushq $0xc
popq %rsi
movl %r13d, %edx
leaq 0xbc6c(%rip), %rcx      # 0x1e354
callq 0x1221b
cmpl $0x1, %r14d
adcl $0x0, %r14d
movl %r12d, %eax
negl %eax
cmovsl %r12d, %eax
leal -0x1(%r14), %ecx
pushq $0x3
popq %rdx
shll %cl, %edx
cmpl %edx, %eax
setg %al
cmpl $0x6, %r14d
setl %cl
andb %al, %cl
movzbl %cl, %eax
addl %r14d, %eax
incq %rbx
movl %eax, %r14d
movq -0x60(%rbp), %r12
movq -0x48(%rbp), %rsi
jmp 0x125ac
movq 0x1589b(%rip), %rax     # 0x27fd0
movq (%rax), %rdi
leaq 0xf6af(%rip), %rsi      # 0x21dee
movl %r13d, %edx
xorl %eax, %eax
callq 0x2170
jmp 0x126d7
leal -0x1(%r12), %ebx
cmpl -0x4c(%rbp), %r12d
jge 0x127a8
movl %ebx, %eax
movq %rax, %rcx
shlq $0x5, %rcx
leaq 0xdb4a(%rip), %rdx      # 0x202b0
addq %rcx, %rdx
shlq $0x7, %rax
leaq 0xe63c(%rip), %rcx      # 0x20db0
addq %rax, %rcx
cmpl $-0x1, -0x34(%rbp)
movslq %r15d, %rax
leaq (%rdx,%rax,8), %rdx
leaq (%rcx,%rax,8), %rax
cmoveq %rdx, %rax
leaq 0xf674(%rip), %rcx      # 0x21e05
leaq 0xf685(%rip), %rdx      # 0x21e1d
cmoveq %rcx, %rdx
movq (%rax), %rsi
movq -0x30(%rbp), %rdi
callq 0x13c54
movslq %ebx, %rbx
xorl %r12d, %r12d
leaq 0xf67d(%rip), %r14      # 0x21e32
cmpq %rbx, %r12
jge 0x127fa
testl %r15d, %r15d
jle 0x127fa
cmpl $0x7, %r15d
pushq $0x7
popq %rax
cmovbl %r15d, %eax
decl %eax
movslq -0xb0(%rbp,%r12,4), %r13
imulq $0x78, %rax, %rax
leaq 0xed51(%rip), %rcx      # 0x21530
addq %rcx, %rax
movq (%rax,%r13,8), %rsi
movq -0x30(%rbp), %rdi
movq %r14, %rdx
callq 0x13c54
subl %r13d, %r15d
incq %r12
jmp 0x127b5
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq