// File: /workspace/github_c_makefile_star2_O2/akumrao[P]videoAnalyzer/encoder/macroblock.c
// Function: block_residual_write_cabac(x264_t*, x264_macroblock_t*, int, int, int*, int)
static void block_residual_write_cabac(x264_t *h, x264_macroblock_t *mb, int i_ctxBlockCat, int i_idx, int *l, int i_count) {LOG_CALL;
    static const int significant_coeff_flag_offset[5] = {0, 15, 29, 44, 47};
    static const int last_significant_coeff_flag_offset[5] = {0, 15, 29, 44, 47};
    static const int coeff_abs_level_m1_offset[5] = {0, 10, 20, 30, 39};

    int i_coeff_abs_m1[16];
    int i_coeff_sign[16];
    int i_coeff = 0;
    int i_last = 0;

    int i_abslevel1 = 0;
    int i_abslevelgt1 = 0;

    int i;

    /* i_ctxBlockCat: 0-> DC 16x16  i_idx = 0
     *                1-> AC 16x16  i_idx = luma4x4idx
     *                2-> Luma4x4   i_idx = luma4x4idx
     *                3-> DC Chroma i_idx = iCbCr
     *                4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx
     */

    //fprintf( stderr, "l[] = " );
    for (i = 0; i < i_count; i++)
    {
        //fprintf( stderr, "%d ", l[i] );
        if (l[i] != 0)
        {
            i_coeff_abs_m1[i_coeff] = abs(l[i]) - 1;
            i_coeff_sign[i_coeff] = (l[i] < 0 ? 1 : 0);
            i_coeff++;

            i_last = i;
        }
    }
    //fprintf( stderr, "\n" );

    if (i_coeff == 0)
    {
        /* codec block flag */
        x264_cabac_encode_decision(&h->cabac, 85 + x264_cabac_mb_cbf_ctxidxinc(mb, i_ctxBlockCat, i_idx), 0);
        return;
    }

    /* block coded */
    x264_cabac_encode_decision(&h->cabac, 85 + x264_cabac_mb_cbf_ctxidxinc(mb, i_ctxBlockCat, i_idx), 1);
    for (i = 0; i < i_count - 1; i++)
    {
        int i_ctxIdxInc;

        i_ctxIdxInc = X264_MIN(i, i_count - 2);

        if (l[i] != 0)
        {
            x264_cabac_encode_decision(&h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 1);
            x264_cabac_encode_decision(&h->cabac, 166 + last_significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, i == i_last ? 1 : 0);
        } else
        {
            x264_cabac_encode_decision(&h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 0);
        }
        if (i == i_last)
        {
            break;
        }
    }

    for (i = i_coeff - 1; i >= 0; i--)
    {
        int i_prefix;
        int i_ctxIdxInc;

        /* write coeff_abs - 1 */

        /* prefix */
        i_prefix = X264_MIN(i_coeff_abs_m1[i], 14);

        i_ctxIdxInc = (i_abslevelgt1 != 0 ? 0 : X264_MIN(4, i_abslevel1 + 1)) + coeff_abs_level_m1_offset[i_ctxBlockCat];
        if (i_prefix == 0)
        {
            x264_cabac_encode_decision(&h->cabac, 227 + i_ctxIdxInc, 0);
        } else
        {
            int j;
            x264_cabac_encode_decision(&h->cabac, 227 + i_ctxIdxInc, 1);
            i_ctxIdxInc = 5 + X264_MIN(4, i_abslevelgt1) + coeff_abs_level_m1_offset[i_ctxBlockCat];
            for (j = 0; j < i_prefix - 1; j++)
            {
                x264_cabac_encode_decision(&h->cabac, 227 + i_ctxIdxInc, 1);
            }
            if (i_prefix < 14)
            {
                x264_cabac_encode_decision(&h->cabac, 227 + i_ctxIdxInc, 0);
            }
        }
        /* suffix */
        if (i_coeff_abs_m1[i] >= 14)
        {
            int k = 0;
            int i_suffix = i_coeff_abs_m1[i] - 14;

            while (i_suffix >= (1 << k))
            {
                x264_cabac_encode_bypass(&h->cabac, 1);
                i_suffix -= 1 << k;
                k++;
            }
            x264_cabac_encode_bypass(&h->cabac, 0);
            while (k--)
            {
                x264_cabac_encode_bypass(&h->cabac, (i_suffix >> k)&0x01);
            }
        }

        /* write sign */
        x264_cabac_encode_bypass(&h->cabac, i_coeff_sign[i]);


        if (i_coeff_abs_m1[i] == 0)
        {
            i_abslevel1++;
        } else
        {
            i_abslevelgt1++;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %r9d, %r15d
movq %r8, %r12
movl %ecx, -0x30(%rbp)
movl %edx, -0x38(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdi, %rbx
leaq 0x795c(%rip), %rdi      # 0x1b1b4
leaq 0xe109(%rip), %rsi      # 0x21968
leaq 0xe713(%rip), %rdx      # 0x21f79
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x39ba
testl %r15d, %r15d
movl $0x0, %eax
movq %r15, -0x40(%rbp)
cmovgl %r15d, %eax
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpq %r14, %rax
je 0x138bd
movl (%r12,%r14,4), %ecx
testl %ecx, %ecx
je 0x138b8
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
decl %edx
movslq %r13d, %r13
movl %edx, -0xe0(%rbp,%r13,4)
shrl $0x1f, %ecx
movl %ecx, -0xa0(%rbp,%r13,4)
incl %r13d
movl %r14d, %r15d
incq %r14
jmp 0x13886
addq $0x10db60, %rbx         # imm = 0x10DB60
movq -0x48(%rbp), %rdi
movl -0x38(%rbp), %r14d
movl %r14d, %esi
movl -0x30(%rbp), %edx
callq 0x13dc7
addl $0x55, %eax
testl %r13d, %r13d
je 0x13b12
movq %r12, -0x48(%rbp)
movq %rbx, %rdi
pushq $0x1
popq %rbx
movq %rdi, -0x30(%rbp)
movl %eax, %esi
movl %ebx, %edx
callq 0x30bc
movl %r14d, %eax
movl %r15d, %ecx
movq -0x40(%rbp), %r14
cmpl $0x2, %r14d
cmovll %ebx, %r14d
movq %rax, %rbx
decl %r14d
movq %rcx, -0x40(%rbp)
leaq 0x1(%rcx), %rax
movq %rax, -0x38(%rbp)
xorl %r15d, %r15d
leaq 0xdf58(%rip), %r12      # 0x21880
movq %rbx, -0x50(%rbp)
cmpq %r15, %r14
je 0x13993
movq -0x48(%rbp), %rax
cmpl $0x0, (%rax,%r15,4)
movl (%r12,%rbx,4), %eax
leaq 0x69(%r15,%rax), %rsi
je 0x13978
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0x30bc
movq -0x50(%rbp), %rax
movl (%r12,%rax,4), %eax
leal (%r15,%rax), %esi
addl $0xa6, %esi
xorl %edx, %edx
cmpq %r15, -0x40(%rbp)
sete %dl
movq %rbx, %rdi
movq -0x50(%rbp), %rbx
jmp 0x1397e
movq -0x30(%rbp), %rdi
xorl %edx, %edx
callq 0x30bc
incq %r15
cmpq %r15, -0x38(%rbp)
leaq 0xdeef(%rip), %r12      # 0x21880
jne 0x1392c
movl %r13d, %r12d
xorl %eax, %eax
xorl %ecx, %ecx
pushq $0x1
popq %r13
movq -0x30(%rbp), %rbx
testl %r12d, %r12d
jle 0x13b00
decq %r12
cmpl $0x3, %eax
pushq $0x3
popq %r15
movl %eax, -0x40(%rbp)
cmovll %eax, %r15d
incl %r15d
testl %ecx, %ecx
movl $0x0, %eax
cmovnel %eax, %r15d
movq %r12, -0x48(%rbp)
movl -0xe0(%rbp,%r12,4), %edx
movq -0x50(%rbp), %rax
movl %ecx, %r14d
leaq 0xdebc(%rip), %rcx      # 0x218a0
movl (%rcx,%rax,4), %eax
addl %eax, %r15d
testl %edx, %edx
movq %rdx, -0x38(%rbp)
movl %r14d, -0x54(%rbp)
je 0x13a8f
cmpl $0xe, %edx
pushq $0xe
popq %r12
cmovll %edx, %r12d
addl $0xe3, %r15d
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
movq %rax, %r15
callq 0x30bc
cmpl $0x4, %r14d
pushq $0x4
popq %rax
cmovll %r14d, %eax
addl %eax, %r15d
addl $0xe8, %r15d
cmpl $0x2, %r12d
cmovll %r13d, %r12d
decl %r12d
subl $0x1, %r12d
jb 0x13a53
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x30bc
jmp 0x13a3d
movq -0x38(%rbp), %rax
cmpl $0xe, %eax
jl 0x13a96
leal -0xe(%rax), %r14d
xorl %r15d, %r15d
pushq $-0x1
popq %r12
movl %r13d, %ebx
movl %r14d, %r13d
movl %ebx, %eax
movl %r15d, %ecx
shll %cl, %eax
subl %eax, %r13d
jl 0x13ada
movq -0x30(%rbp), %rdi
movl %ebx, %esi
callq 0x31cc
incl %r15d
incl %r12d
movl %r13d, %r14d
jmp 0x13a6d
addl $0xe3, %r15d
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x30bc
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpl $0x0, -0x38(%rbp)
setne %r14b
sete %r15b
movq -0x48(%rbp), %r12
movl -0xa0(%rbp,%r12,4), %esi
movq %rbx, %rdi
callq 0x31cc
movl -0x40(%rbp), %eax
addl %r15d, %eax
movl -0x54(%rbp), %ecx
addl %r14d, %ecx
jmp 0x139a2
xorl %esi, %esi
movl %ebx, %r13d
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x31cc
cmpl $-0x1, %r12d
je 0x13aa3
xorl %esi, %esi
btl %r12d, %r14d
setb %sil
decl %r12d
jmp 0x13ae3
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30bc