// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdx, 0x30(%rsp)
movq %rsi, %r12
movq 0x28(%rdi), %rax
movl 0x10(%rax), %edx
movl 0x6c(%rax), %ebp
movl %edx, 0x8(%rsp)
testb $0x4, %dl
movq %rdi, (%rsp)
jne 0x17b79
movq %r12, 0x28(%rsp)
leaq 0x1b8(%rdi), %rax
movslq %ecx, %r8
movq %r8, %rsi
shlq $0x4, %rsi
leaq 0x5850(%rip), %rdx      # 0x1d2c0
addq %rsi, %rdx
addq $0xc, %rdx
movq %rdx, 0x10(%rsp)
shll $0x4, %ecx
leaq (%r8,%r8,4), %rdx
leaq (%rdi,%rdx,8), %rdx
addq $0x80, %rdx
movq %rdx, 0x38(%rsp)
shlq $0xa, %r8
leaq (%rdi,%r8), %r13
addq $0x1b8, %r13            # imm = 0x1B8
addq %rax, %r8
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rcx, %r12
xorl %r14d, %r14d
movl %ebp, 0xc(%rsp)
movq 0x28(%rsp), %r15
btl %r14d, %ebp
movq %r13, 0x40(%rsp)
jae 0x17b05
btl $0x13, 0x8(%rsp)
jb 0x17b23
movq 0x10(%rsp), %rax
movl -0x4(%rax), %ebp
xorl %ebx, %ebx
leal (%r12,%rbx), %ecx
movq (%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r15, %r8
movl $0x10, %r9d
callq 0x17954
incq %rbx
addq $0x40, %r13
cmpq $0x4, %rbx
jne 0x17adb
jmp 0x17b53
leaq 0x52c4(%rip), %rax      # 0x1cdd0
movzbl (%rax,%r14,4), %eax
movq 0x38(%rsp), %rcx
xorl %edx, %edx
movw %dx, (%rcx,%rax)
movw %dx, 0x8(%rcx,%rax)
jmp 0x17b53
movq %r14, %rsi
shlq $0x8, %rsi
addq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rax
movl (%rax), %edx
movq 0x20(%rsp), %rax
leal (%rax,%r14,4), %ecx
movq (%rsp), %rdi
movq 0x30(%rsp), %r8
movl $0x40, %r9d
callq 0x17954
incq %r14
addq $0x4, %r12
movq 0x40(%rsp), %r13
addq $0x100, %r13            # imm = 0x100
cmpq $0x4, %r14
movl 0xc(%rsp), %ebp
jne 0x17abe
jmp 0x17c41
movslq %ecx, %r14
movq %r14, %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)
movq %r14, %rbx
shlq $0x4, %rbx
leaq 0x5712(%rip), %r15      # 0x1d2c0
movl (%rbx,%r15), %edx
leal 0x30(%r14), %ecx
movq %rdi, %r13
movq %r12, %r8
movl $0x10, %r9d
callq 0x178e4
testb $0xf, %bpl
je 0x17c1b
movl 0x4(%rbx,%r15), %ebp
movq %r12, %rbx
movl %r14d, %r12d
shll $0x4, %r12d
incq %rbx
shlq $0xa, %r14
leaq (%r14,%r13), %r15
addq $0x1b8, %r15            # imm = 0x1B8
xorl %r14d, %r14d
leal (%r12,%r14), %ecx
movq (%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %rbx, %r8
movl $0xf, %r9d
callq 0x17954
incq %r14
addq $0x40, %r15
cmpq $0x10, %r14
jne 0x17bf1
jmp 0x17c41
leaq (%r14,%r14,4), %rax
xorl %ecx, %ecx
movl %ecx, 0x8c(%r13,%rax,8)
movl %ecx, 0x94(%r13,%rax,8)
movl %ecx, 0x9c(%r13,%rax,8)
movl %ecx, 0xa4(%r13,%rax,8)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq