// File: /workspace/github_cpp_makefile_star2_O2/lukasz-wiecaszek[P]h264iframedecoder/picture_cabac.cpp
// Function: ymn::h264::picture_cabac::decode_residual(unsigned char const*, unsigned char const*, ymn::colour_component_e)
void picture_cabac::decode_residual(const uint8_t* scan4x4,
                                    const uint8_t* scan8x8,
                                    colour_component_e cc)
{
    mb* curr_mb = m_context_variables.curr_mb;
    uint32_t mb_type = curr_mb->type;
    int cbp_luma = curr_mb->cbp_luma;

    static const enum ctx_block_cat_e ctx_cat[CC_MAX][4] =
    {
        {CAT_16x16_DC_Y,  CAT_16x16_AC_Y,  CAT_4x4_Y,  CAT_8x8_Y},  // Y
        {CAT_16x16_DC_Cb, CAT_16x16_AC_Cb, CAT_4x4_Cb, CAT_8x8_Cb}, // Cb
        {CAT_16x16_DC_Cr, CAT_16x16_AC_Cr, CAT_4x4_Cr, CAT_8x8_Cr}  // Cr
    };

    if (MB_IS_INTRA_16x16(mb_type)) {
        memset(&m_context_variables.coeffs_dc[to_int(cc)], 0, 16 * sizeof(dctcoeff));
        decode_residual_dc(m_context_variables.coeffs_dc[to_int(cc)],
            ctx_cat[to_int(cc)][0], MB_NZC_DC_BLOCK_IDX(to_int(cc)), scan4x4, 16);

        if (cbp_luma & 0x0F)
            for (int i4x4 = 0; i4x4 < 16; ++i4x4)
                decode_residual_ac(&m_context_variables.coeffs_ac[to_int(cc)][16 * i4x4],
                    ctx_cat[to_int(cc)][1], MB_NZC_AC_BLOCK_IDX(to_int(cc), i4x4), scan4x4 + 1, 15);
        else
            mb_cache_fill_rectangle_4x4(m_context_variables.non_zero_count_cache[to_int(cc)], mb_cache_idx[0], 0);
    }
    else {
        for (int i8x8 = 0; i8x8 < 4; ++i8x8) {
            if (cbp_luma & (1U << i8x8)) {
                if (!MB_IS_8x8DCT(mb_type)) {
                    for (int i4x4 = 0; i4x4 < 4; ++i4x4) {
                        const int index = i8x8 * 4 + i4x4;
                        decode_residual_ac(&m_context_variables.coeffs_ac[to_int(cc)][16 * index],
                            ctx_cat[to_int(cc)][2], MB_NZC_AC_BLOCK_IDX(to_int(cc), index), scan4x4, 16);
                    }
                }
                else {
                    const int index = i8x8 * 4;
                    decode_residual_ac(&m_context_variables.coeffs_ac[to_int(cc)][16 * index],
                        ctx_cat[to_int(cc)][3], MB_NZC_AC_BLOCK_IDX(to_int(cc), index), scan8x8, 64);
                }
            }
            else
                mb_cache_fill_rectangle_2x2(m_context_variables.non_zero_count_cache[to_int(cc)], mb_cache_idx[4 * i8x8], 0);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %ebp
movq %rdx, 0x28(%rsp)
movq %rsi, %rbx
movq 0x28(%rdi), %rax
movl 0x10(%rax), %ecx
movl 0x6c(%rax), %r8d
movl %ecx, 0x10(%rsp)
testb $0x4, %cl
movq %rdi, 0x8(%rsp)
jne 0x12f0c
leaq 0x1b8(%rdi), %rax
movslq %ebp, %rsi
movq %rsi, %rcx
shlq $0x4, %rcx
leaq 0x653f(%rip), %rdx      # 0x19340
addq %rdx, %rcx
addq $0xc, %rcx
movq %rcx, 0x18(%rsp)
shll $0x4, %ebp
imulq $0x28, %rsi, %rcx
addq %rdi, %rcx
addq $0x80, %rcx
movq %rcx, 0x30(%rsp)
shlq $0xa, %rsi
leaq (%rdi,%rsi), %r15
addq $0x1b8, %r15            # imm = 0x1B8
xorl %r12d, %r12d
addq %rax, %rsi
movq %rsi, 0x20(%rsp)
xorl %ecx, %ecx
movl %r8d, 0x14(%rsp)
cmpq $0x4, %rcx
je 0x12fd7
btl %ecx, %r8d
movq %r15, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
jae 0x12e9e
btl $0x13, 0x10(%rsp)
jb 0x12ebb
movq 0x18(%rsp), %rax
movl -0x4(%rax), %r13d
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x12eea
leal (%r12,%r14), %ecx
orl %ebp, %ecx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movl %r13d, %edx
movq %rbx, %r8
pushq $0x10
popq %r9
callq 0x12cf8
incq %r14
addq $0x40, %r15
jmp 0x12e72
leaq 0x5fab(%rip), %rax      # 0x18e50
movzbl (%rax,%rcx,4), %eax
movq 0x30(%rsp), %rcx
andw $0x0, (%rcx,%rax)
andw $0x0, 0x8(%rcx,%rax)
jmp 0x12eea
movq %rcx, %rsi
shlq $0x8, %rsi
addq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rax
movl (%rax), %edx
leal (,%rcx,4), %ecx
orl %ebp, %ecx
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %r8
pushq $0x40
popq %r9
callq 0x12cf8
movq 0x38(%rsp), %rcx
incq %rcx
addq $0x4, %r12
movq 0x40(%rsp), %r15
addq $0x100, %r15            # imm = 0x100
movl 0x14(%rsp), %r8d
jmp 0x12e44
movslq %ebp, %r15
movq %r15, %rax
shlq $0x6, %rax
leaq (%rdi,%rax), %rsi
addq $0xf8, %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
movl %r8d, %ebp
movq %r15, %r14
shlq $0x4, %r14
leaq 0x63fc(%rip), %r13      # 0x19340
movl (%r14,%r13), %edx
leal 0x30(%r15), %ecx
pushq $0x10
popq %r9
movq %rdi, %r12
movq %rbx, %r8
callq 0x12c88
testb $0xf, %bpl
je 0x12faf
movl 0x4(%r14,%r13), %ebp
movl %r15d, %r13d
shll $0x4, %r13d
incq %rbx
shlq $0xa, %r15
addq %r12, %r15
addq $0x1b8, %r15            # imm = 0x1B8
xorl %r12d, %r12d
pushq $0xf
popq %r14
cmpq $0x10, %r12
je 0x12fd7
movl %r13d, %ecx
orl %r12d, %ecx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %rbx, %r8
movl %r14d, %r9d
callq 0x12cf8
incq %r12
addq $0x40, %r15
jmp 0x12f85
imulq $0x28, %r15, %rax
andl $0x0, 0x8c(%r12,%rax)
andl $0x0, 0x94(%r12,%rax)
andl $0x0, 0x9c(%r12,%rax)
andl $0x0, 0xa4(%r12,%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq