// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %r8, %r15
movl %ecx, -0x30(%rbp)
movl %edx, -0x38(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdi, %rbx
leaq 0xb632(%rip), %rdi      # 0x271b4
leaq 0x11bdb(%rip), %rsi     # 0x2d764
leaq 0x121e5(%rip), %rdx     # 0x2dd75
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x4a56
movq %r14, -0x40(%rbp)
testl %r14d, %r14d
jle 0x1be33
movl -0x40(%rbp), %r12d
xorl %eax, %eax
xorl %r14d, %r14d
movl (%r15,%rax,4), %ecx
testl %ecx, %ecx
je 0x1bbdd
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
decl %edx
movslq %r13d, %r13
movl %edx, -0xd0(%rbp,%r13,4)
shrl $0x1f, %ecx
movl %ecx, -0x90(%rbp,%r13,4)
incl %r13d
movl %eax, %r14d
incq %rax
cmpq %rax, %r12
jne 0x1bbb0
testl %r13d, %r13d
je 0x1be33
addq $0x10db60, %rbx         # imm = 0x10DB60
movq -0x48(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x30(%rbp), %edx
callq 0x1bff2
leal 0x55(%rax), %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x3efb
movl -0x38(%rbp), %eax
movq %rax, -0x30(%rbp)
cmpl $0x1, %r12d
je 0x1bcaa
movl %r14d, %r12d
movq -0x40(%rbp), %rax
decl %eax
leaq 0x11afb(%rip), %rdx     # 0x2d730
movq -0x30(%rbp), %rcx
movl (%rdx,%rcx,4), %ecx
decq %rax
movq %rax, -0x40(%rbp)
addl $0x69, %ecx
movq %rcx, -0x38(%rbp)
xorl %r14d, %r14d
movq -0x38(%rbp), %rax
leaq (%rax,%r14), %rsi
cmpl $0x0, (%r15,%r14,4)
je 0x1bc8e
movq %rbx, %rdi
movl $0x1, %edx
callq 0x3efb
movq -0x30(%rbp), %rax
leaq 0x11abc(%rip), %rcx     # 0x2d730
movl (%rcx,%rax,4), %eax
leal (%r14,%rax), %esi
addl $0xa6, %esi
xorl %edx, %edx
cmpq %r14, %r12
sete %dl
movq %rbx, %rdi
jmp 0x1bc93
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3efb
cmpq %r14, %r12
je 0x1bcaa
leaq 0x1(%r14), %rax
cmpq %r14, -0x40(%rbp)
movq %rax, %r14
jne 0x1bc4d
testl %r13d, %r13d
jle 0x1be67
leaq 0x11a96(%rip), %rax     # 0x2d750
movq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %r14d
movl %r13d, %r13d
xorl %r15d, %r15d
xorl %ecx, %ecx
movq %r14, -0x48(%rbp)
movl -0xd4(%rbp,%r13,4), %r12d
cmpl $0x3, %ecx
movl $0x3, %eax
movl %ecx, -0x38(%rbp)
cmovll %ecx, %eax
incl %eax
testl %r15d, %r15d
movl $0x0, %ecx
cmovnel %ecx, %eax
leal (%r14,%rax), %esi
addl $0xe3, %esi
testl %r12d, %r12d
movl %r15d, -0x40(%rbp)
movq %r12, -0x30(%rbp)
movq %rbx, %rdi
je 0x1bd73
movl $0x1, %edx
callq 0x3efb
cmpl $0x4, %r15d
movl $0x4, %eax
cmovll %r15d, %eax
leal (%rax,%r14), %esi
addl $0x5, %esi
cmpl $0x2, %r12d
jl 0x1bd6a
cmpl $0xe, %r12d
movl $0xe, %r14d
cmovll %r12d, %r14d
movq %rsi, -0x50(%rbp)
leal 0xe3(%rsi), %r15d
decl %r14d
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x3efb
decl %r14d
jne 0x1bd4b
cmpl $0xd, %r12d
movq -0x50(%rbp), %rsi
jg 0x1bdc8
addl $0xe3, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3efb
leaq -0x1(%r13), %r14
xorl %r15d, %r15d
movq %r13, %r12
xorl %r13d, %r13d
cmpl $0x0, -0x30(%rbp)
setne %r15b
sete %r13b
movl -0x90(%rbp,%r14,4), %esi
movq %rbx, %rdi
callq 0x414e
movl -0x38(%rbp), %ecx
addl %r13d, %ecx
movl -0x40(%rbp), %eax
addl %r15d, %eax
movl %eax, %r15d
cmpq $0x1, %r12
movq %r14, %r13
movq -0x48(%rbp), %r14
jg 0x1bcce
jmp 0x1be67
cmpl $0xe, %r12d
jne 0x1bdda
movq %rbx, %rdi
xorl %esi, %esi
callq 0x414e
jmp 0x1bd7a
leal -0xe(%r12), %r15d
xorl %edx, %edx
movl $0x1, %eax
movl %edx, %r14d
subl %eax, %r15d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x414e
leal 0x1(%r14), %edx
movl $0x2, %eax
movl %r14d, %ecx
shll %cl, %eax
cmpl %eax, %r15d
jge 0x1bde6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x414e
xorl %esi, %esi
btl %r14d, %r15d
setb %sil
movq %rbx, %rdi
callq 0x414e
addl $-0x1, %r14d
jb 0x1be16
jmp 0x1bd7a
addq $0x10db60, %rbx         # imm = 0x10DB60
movq -0x48(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x30(%rbp), %edx
callq 0x1bff2
leal 0x55(%rax), %esi
movq %rbx, %rdi
xorl %edx, %edx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3efb
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq