// File: /workspace/github_cpp_makefile_star2_O1/lukasz-wiecaszek[P]h264iframedecoder/picture_cabac.cpp
// Function: ymn::h264::picture_cabac::non_zero_count_cache_init(unsigned int)
void picture_cabac::non_zero_count_cache_init(uint32_t mb_type)
{
    const mb* curr_mb = m_context_variables.curr_mb;
    const int* left_blocks = m_context_variables.left_blocks_nzc;

    mb_cache& nzc_cache_y  = m_context_variables.non_zero_count_cache[CC_Y];
    mb_cache& nzc_cache_cb = m_context_variables.non_zero_count_cache[CC_Cb];
    mb_cache& nzc_cache_cr = m_context_variables.non_zero_count_cache[CC_Cr];

    if (curr_mb->top) {
        const uint8_t* nzc = curr_mb->top->non_zero_count;

        std::memcpy(&nzc_cache_y[0 * 8 + 4], &nzc[3 * 4], 4);

        if (m_context_variables.chroma_array_type == 1 ||
            m_context_variables.chroma_array_type == 2) {
            std::memcpy(&nzc_cache_cb[0 * 8 + 4], &nzc[5 * 4], 4);
            std::memcpy(&nzc_cache_cr[0 * 8 + 4], &nzc[9 * 4], 4);
        }
        else
        if (m_context_variables.chroma_array_type == 3) {
            std::memcpy(&nzc_cache_cb[0 * 8 + 4], &nzc[ 7 * 4], 4);
            std::memcpy(&nzc_cache_cr[0 * 8 + 4], &nzc[11 * 4], 4);
        }
        else {
            /* do nothing */
        }
    }
    else {
        // 0x40 (64) means "value not available"
        uint32_t top_mb_not_available = m_decoder.m_active_pps->entropy_coding_mode_flag && !MB_IS_INTRA(mb_type) ? 0 : 0x40404040;

        DEREFERENCE(uint32_t, &nzc_cache_y [0 * 8 + 4]) = top_mb_not_available;
        DEREFERENCE(uint32_t, &nzc_cache_cb[0 * 8 + 4]) = top_mb_not_available;
        DEREFERENCE(uint32_t, &nzc_cache_cr[0 * 8 + 4]) = top_mb_not_available;
    }

    for (int i = 0; i < 2; ++i) {
        if (curr_mb->left_pair[i]) {
            const uint8_t* nzc = curr_mb->left_pair[i]->non_zero_count;

            nzc_cache_y[1 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[0 + 2 * i]];
            nzc_cache_y[2 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[1 + 2 * i]];

            if (m_context_variables.chroma_array_type == 1) {
                nzc_cache_cb[1 * 8 + 3 + 8 * i] = nzc[left_blocks[4 + 2 * i]];
                nzc_cache_cr[1 * 8 + 3 + 8 * i] = nzc[left_blocks[5 + 2 * i]];
            }
            else
            if (m_context_variables.chroma_array_type == 2) {
                nzc_cache_cb[1 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[0 + 2 * i] - 2 + 4 * 4];
                nzc_cache_cb[2 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[1 + 2 * i] - 2 + 4 * 4];
                nzc_cache_cr[1 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[0 + 2 * i] - 2 + 8 * 4];
                nzc_cache_cr[2 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[1 + 2 * i] - 2 + 8 * 4];
            }
            else
            if (m_context_variables.chroma_array_type == 3) {
                nzc_cache_cb[1 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[0 + 2 * i] + 4 * 4];
                nzc_cache_cb[2 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[1 + 2 * i] + 4 * 4];
                nzc_cache_cr[1 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[0 + 2 * i] + 8 * 4];
                nzc_cache_cr[2 * 8 + 3 + 2 * 8 * i] = nzc[left_blocks[1 + 2 * i] + 8 * 4];
            }
            else {
                /* do nothing */
            }
        }
        else {
            nzc_cache_y [1 * 8 + 3 + 2 * 8 * i] =
            nzc_cache_y [2 * 8 + 3 + 2 * 8 * i] =
            nzc_cache_cb[1 * 8 + 3 + 2 * 8 * i] =
            nzc_cache_cb[2 * 8 + 3 + 2 * 8 * i] =
            nzc_cache_cr[1 * 8 + 3 + 2 * 8 * i] =
            nzc_cache_cr[2 * 8 + 3 + 2 * 8 * i] =
                m_decoder.m_active_pps->entropy_coding_mode_flag && !MB_IS_INTRA(mb_type) ? 0 : 0x40;
        }
    }

    if ((m_context_variables.chroma_array_type == 3) && (MB_IS_INTRA_8x8(mb_type))) {
        //TODO: implement this part
    }
}

// Assembly:
movq 0x28(%rdi), %rax
movq 0x50(%rax), %rcx
testq %rcx, %rcx
je 0x1728e
movl 0x84(%rcx), %edx
movl %edx, 0x84(%rdi)
movl 0x40(%rdi), %edx
leal -0x1(%rdx), %r8d
cmpl $0x2, %r8d
jae 0x172c2
movl 0x8c(%rcx), %edx
movl %edx, 0xac(%rdi)
movl 0x9c(%rcx), %ecx
jmp 0x172d9
movq 0x8(%rdi), %rcx
movq 0x88360(%rcx), %rcx
xorl %edx, %edx
cmpl $0x0, 0x14(%rcx)
movl $0x40404040, %ecx       # imm = 0x40404040
cmovel %ecx, %edx
testb $0xf, %sil
cmovnel %ecx, %edx
movl %edx, 0x84(%rdi)
movl %edx, 0xac(%rdi)
movl %edx, 0xd4(%rdi)
jmp 0x172df
cmpl $0x3, %edx
jne 0x172df
movl 0x94(%rcx), %edx
movl %edx, 0xac(%rdi)
movl 0xa4(%rcx), %ecx
movl %ecx, 0xd4(%rdi)
pushq %rbx
movq 0x50(%rdi), %rcx
movl 0x40(%rdi), %edx
movq 0x8(%rdi), %r8
xorl %r9d, %r9d
movl $0x40, %r10d
movq 0x40(%rax,%r9), %r11
testq %r11, %r11
je 0x17365
movslq (%rcx,%r9), %rbx
movb 0x78(%r11,%rbx), %bl
movb %bl, 0x8b(%rdi,%r9,2)
movslq 0x4(%rcx,%r9), %rbx
movb 0x78(%r11,%rbx), %bl
movb %bl, 0x93(%rdi,%r9,2)
cmpl $0x3, %edx
je 0x17406
cmpl $0x2, %edx
je 0x173ba
cmpl $0x1, %edx
jne 0x17458
movslq 0x10(%rcx,%r9), %rbx
movb 0x78(%r11,%rbx), %bl
movb %bl, 0xb3(%rdi,%r9)
movslq 0x14(%rcx,%r9), %rbx
movb 0x78(%r11,%rbx), %r11b
movb %r11b, 0xdb(%rdi,%r9)
jmp 0x17458
movq 0x88360(%r8), %r11
cmpl $0x0, 0x14(%r11)
sete %r11b
shlb $0x6, %r11b
testb $0xf, %sil
movzbl %r11b, %r11d
cmovnel %r10d, %r11d
movb %r11b, 0xe3(%rdi,%r9,2)
movb %r11b, 0xdb(%rdi,%r9,2)
movb %r11b, 0xbb(%rdi,%r9,2)
movb %r11b, 0xb3(%rdi,%r9,2)
movb %r11b, 0x93(%rdi,%r9,2)
movb %r11b, 0x8b(%rdi,%r9,2)
jmp 0x17458
movslq (%rcx,%r9), %rbx
movb 0x86(%r11,%rbx), %bl
movb %bl, 0xb3(%rdi,%r9,2)
movslq 0x4(%rcx,%r9), %rbx
movb 0x86(%r11,%rbx), %bl
movb %bl, 0xbb(%rdi,%r9,2)
movslq (%rcx,%r9), %rbx
movb 0x96(%r11,%rbx), %bl
movb %bl, 0xdb(%rdi,%r9,2)
movslq 0x4(%rcx,%r9), %rbx
movb 0x96(%r11,%rbx), %r11b
jmp 0x17450
movslq (%rcx,%r9), %rbx
movb 0x88(%r11,%rbx), %bl
movb %bl, 0xb3(%rdi,%r9,2)
movslq 0x4(%rcx,%r9), %rbx
movb 0x88(%r11,%rbx), %bl
movb %bl, 0xbb(%rdi,%r9,2)
movslq (%rcx,%r9), %rbx
movb 0x98(%r11,%rbx), %bl
movb %bl, 0xdb(%rdi,%r9,2)
movslq 0x4(%rcx,%r9), %rbx
movb 0x98(%r11,%rbx), %r11b
movb %r11b, 0xe3(%rdi,%r9,2)
addq $0x8, %r9
cmpq $0x8, %r9
je 0x172f4
popq %rbx
retq