// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movl %r9d, %r15d
movq %r8, %r14
movl %ecx, -0x40(%rbp)
movl %edx, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdi, %rbx
leaq 0xa361(%rip), %rdi      # 0x251b4
leaq 0x10bbe(%rip), %rsi     # 0x2ba18
leaq 0x111c8(%rip), %rdx     # 0x2c029
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x4c22
testl %r15d, %r15d
jle 0x1aec5
movl %r15d, %eax
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %r8d, %r8d
movl -0x30(%rbp), %esi
movl -0x40(%rbp), %edx
movq -0x38(%rbp), %rdi
movl (%r14,%rcx,4), %r9d
testl %r9d, %r9d
je 0x1aeb8
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
decl %r8d
movslq %r13d, %r13
movl %r8d, -0xd0(%rbp,%r13,4)
shrl $0x1f, %r9d
movl %r9d, -0x90(%rbp,%r13,4)
incl %r13d
movl %ecx, %r8d
incq %rcx
cmpq %rcx, %rax
jne 0x1ae85
movl %r8d, %r12d
jmp 0x1aed2
xorl %r12d, %r12d
movl -0x30(%rbp), %esi
movl -0x40(%rbp), %edx
movq -0x38(%rbp), %rdi
addq $0x10db60, %rbx         # imm = 0x10DB60
callq 0x1b2eb
addl $0x55, %eax
testl %r13d, %r13d
je 0x1b152
movq %r14, -0x40(%rbp)
movq %rbx, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x40ca
movl -0x30(%rbp), %eax
movq %rax, -0x30(%rbp)
cmpl $0x2, %r15d
jl 0x1afb7
leal -0x1(%r15), %eax
addl $-0x2, %r15d
decq %rax
movq %rax, -0x48(%rbp)
xorl %r14d, %r14d
movq %r15, -0x50(%rbp)
movq %r12, -0x38(%rbp)
cmpl %r15d, %r14d
movl %r15d, %r12d
cmovll %r14d, %r12d
leal 0x69(%r12), %esi
movq -0x40(%rbp), %rax
cmpl $0x0, (%rax,%r14,4)
je 0x1af85
movq -0x30(%rbp), %rax
leaq 0x109e3(%rip), %rcx     # 0x2b930
movl (%rcx,%rax,4), %r15d
addl %r15d, %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x40ca
leal (%r12,%r15), %esi
addl $0xa6, %esi
movq -0x50(%rbp), %r15
xorl %edx, %edx
movq -0x38(%rbp), %r12
cmpq %r14, %r12
sete %dl
movq %rbx, %rdi
callq 0x40ca
jmp 0x1afa1
movq -0x30(%rbp), %rax
leaq 0x109a0(%rip), %rcx     # 0x2b930
addl (%rcx,%rax,4), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x40ca
movq -0x38(%rbp), %r12
cmpq %r14, %r12
je 0x1afb7
leaq 0x1(%r14), %rax
cmpq %r14, -0x48(%rbp)
movq %rax, %r14
jne 0x1af28
testl %r13d, %r13d
jle 0x1b140
leaq 0x10989(%rip), %rax     # 0x2b950
movq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl %r13d, %r14d
xorl %r13d, %r13d
xorl %ecx, %ecx
movq %rdx, -0x48(%rbp)
movq %r14, -0x30(%rbp)
movl -0xd4(%rbp,%r14,4), %r15d
cmpl $0x3, %ecx
movl $0x3, %r14d
movl %ecx, -0x38(%rbp)
cmovll %ecx, %r14d
incl %r14d
testl %r13d, %r13d
movl $0x0, %eax
cmovnel %eax, %r14d
addl %edx, %r14d
testl %r15d, %r15d
movl %r13d, -0x40(%rbp)
je 0x1b07d
cmpl $0xe, %r15d
movl $0xe, %r12d
cmovll %r15d, %r12d
addl $0xe3, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movq %rdx, %r14
movl $0x1, %edx
callq 0x40ca
cmpl $0x4, %r13d
movl $0x4, %eax
cmovll %r13d, %eax
addl %eax, %r14d
addl $0x5, %r14d
cmpl $0x2, %r15d
movq %r15, %r13
jl 0x1b075
leal 0xe3(%r14), %r15d
decl %r12d
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x40ca
decl %r12d
jne 0x1b060
cmpl $0xd, %r13d
jle 0x1b080
jmp 0x1b094
movq %r15, %r13
addl $0xe3, %r14d
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x40ca
cmpl $0xe, %r13d
jl 0x1b0ff
leal -0xe(%r13), %r15d
xorl %r12d, %r12d
cmpl $0xe, %r13d
je 0x1b0d5
xorl %r14d, %r14d
movl $0x1, %eax
subl %eax, %r15d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4314
movl $0x2, %eax
movl %r14d, %ecx
shll %cl, %eax
leal 0x1(%r14), %r12d
movl %r12d, %r14d
cmpl %eax, %r15d
jge 0x1b0af
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4314
testl %r12d, %r12d
je 0x1b0ff
decl %r12d
xorl %esi, %esi
btl %r12d, %r15d
setb %sil
movq %rbx, %rdi
callq 0x4314
addl $-0x1, %r12d
jb 0x1b0e7
movq -0x30(%rbp), %r15
leaq -0x1(%r15), %r14
movl -0x90(%rbp,%r14,4), %esi
movq %rbx, %rdi
callq 0x4314
xorl %eax, %eax
xorl %ecx, %ecx
testl %r13d, %r13d
setne %al
sete %cl
movl -0x38(%rbp), %edx
addl %ecx, %edx
movl %edx, %ecx
movl -0x40(%rbp), %r13d
addl %eax, %r13d
cmpq $0x1, %r15
movq -0x48(%rbp), %rdx
jg 0x1afda
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x40ca