// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, %r12
movq 0x28(%rdi), %rax
movl 0x10(%rax), %edx
movl 0x6c(%rax), %ebp
movl %edx, 0x10(%rsp)
testb $0x4, %dl
movq %rdi, 0x8(%rsp)
jne 0x17f80
movq %r12, 0x38(%rsp)
leaq 0x1b8(%rdi), %rax
movq %rax, 0x28(%rsp)
movslq %ecx, %rsi
movq %rsi, %rax
shlq $0x4, %rax
leaq 0x6462(%rip), %rdx      # 0x1e2d0
addq %rdx, %rax
addq $0xc, %rax
movq %rax, 0x18(%rsp)
shll $0x4, %ecx
leaq (%rsi,%rsi,4), %rax
leaq (%rdi,%rax,8), %rax
addq $0x80, %rax
movq %rax, 0x48(%rsp)
shlq $0xa, %rsi
movq %rsi, 0x20(%rsp)
leaq (%rdi,%rsi), %r13
addq $0x1b8, %r13            # imm = 0x1B8
movq %rcx, 0x30(%rsp)
movq %rcx, %r12
xorl %r14d, %r14d
movl %ebp, 0x14(%rsp)
movq 0x38(%rsp), %r15
btl %r14d, %ebp
movq %r13, 0x50(%rsp)
jae 0x17f00
btl $0x13, 0x10(%rsp)
jb 0x17f1e
movq 0x18(%rsp), %rax
movl -0x4(%rax), %ebp
xorl %ebx, %ebx
leal (%r12,%rbx), %ecx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r15, %r8
movl $0x10, %r9d
callq 0x17d4c
incq %rbx
addq $0x40, %r13
cmpq $0x4, %rbx
jne 0x17ed5
jmp 0x17f5a
leaq 0x5ed9(%rip), %rax      # 0x1dde0
movzbl (%rax,%r14,4), %eax
movq 0x48(%rsp), %rcx
xorl %edx, %edx
movw %dx, (%rcx,%rax)
movw %dx, 0x8(%rcx,%rax)
jmp 0x17f5a
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
addq %rcx, %rax
movq %r14, %rsi
shlq $0x8, %rsi
addq %rax, %rsi
movq 0x18(%rsp), %rax
movl (%rax), %edx
movq 0x30(%rsp), %rax
leal (%rax,%r14,4), %ecx
movq 0x8(%rsp), %rdi
movq 0x40(%rsp), %r8
movl $0x40, %r9d
callq 0x17d4c
incq %r14
addq $0x4, %r12
movq 0x50(%rsp), %r13
addq $0x100, %r13            # imm = 0x100
cmpq $0x4, %r14
movl 0x14(%rsp), %ebp
jne 0x17eb8
jmp 0x18049
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 0x631b(%rip), %r15      # 0x1e2d0
movl (%rbx,%r15), %edx
leal 0x30(%r14), %ecx
movq %rdi, %r13
movq %r12, %r8
movl $0x10, %r9d
callq 0x17cdc
testb $0xf, %bpl
je 0x18023
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 0x8(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %rbx, %r8
movl $0xf, %r9d
callq 0x17d4c
incq %r14
addq $0x40, %r15
cmpq $0x10, %r14
jne 0x17ff8
jmp 0x18049
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 $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq