// File: /workspace/github_cpp_makefile_star2_O2/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), %rdx
testq %rdx, %rdx
je 0x12314
movl 0x84(%rdx), %ecx
movl %ecx, 0x84(%rdi)
movl 0x40(%rdi), %ecx
leal -0x1(%rcx), %r8d
cmpl $0x2, %r8d
jae 0x1234b
movl 0x8c(%rdx), %r8d
movl %r8d, 0xac(%rdi)
movl 0x9c(%rdx), %edx
movl %edx, 0xd4(%rdi)
jmp 0x1236d
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)
movl 0x40(%rdi), %ecx
jmp 0x1236d
cmpl $0x3, %ecx
jne 0x1236d
movl 0x94(%rdx), %ecx
movl %ecx, 0xac(%rdi)
movl 0xa4(%rdx), %ecx
movl %ecx, 0xd4(%rdi)
movl $0x3, %ecx
pushq %rbx
movq 0x50(%rdi), %rdx
movq 0x8(%rdi), %r8
xorl %r9d, %r9d
movl $0x40, %r10d
cmpq $0x10, %r9
je 0x124f6
movq 0x40(%rax,%r9), %r11
testq %r11, %r11
je 0x123fa
movslq (%rdx,%r9), %rbx
movb 0x78(%r11,%rbx), %bl
movb %bl, 0x8b(%rdi,%r9,2)
movslq 0x4(%rdx,%r9), %rbx
movb 0x78(%r11,%rbx), %bl
movb %bl, 0x93(%rdi,%r9,2)
cmpl $0x3, %ecx
je 0x1249b
cmpl $0x2, %ecx
je 0x1244f
cmpl $0x1, %ecx
jne 0x124ed
movslq 0x10(%rdx,%r9), %rbx
movb 0x78(%r11,%rbx), %bl
movb %bl, 0xb3(%rdi,%r9)
movslq 0x14(%rdx,%r9), %rbx
movb 0x78(%r11,%rbx), %r11b
movb %r11b, 0xdb(%rdi,%r9)
jmp 0x124ed
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 0x124ed
movslq (%rdx,%r9), %rbx
movb 0x86(%r11,%rbx), %bl
movb %bl, 0xb3(%rdi,%r9,2)
movslq 0x4(%rdx,%r9), %rbx
movb 0x86(%r11,%rbx), %bl
movb %bl, 0xbb(%rdi,%r9,2)
movslq (%rdx,%r9), %rbx
movb 0x96(%r11,%rbx), %bl
movb %bl, 0xdb(%rdi,%r9,2)
movslq 0x4(%rdx,%r9), %rbx
movb 0x96(%r11,%rbx), %r11b
jmp 0x124e5
movslq (%rdx,%r9), %rbx
movb 0x88(%r11,%rbx), %bl
movb %bl, 0xb3(%rdi,%r9,2)
movslq 0x4(%rdx,%r9), %rbx
movb 0x88(%r11,%rbx), %bl
movb %bl, 0xbb(%rdi,%r9,2)
movslq (%rdx,%r9), %rbx
movb 0x98(%r11,%rbx), %bl
movb %bl, 0xdb(%rdi,%r9,2)
movslq 0x4(%rdx,%r9), %rbx
movb 0x98(%r11,%rbx), %r11b
movb %r11b, 0xe3(%rdi,%r9,2)
addq $0x8, %r9
jmp 0x1237f
popq %rbx
retq