// File: /workspace/github_cpp_makefile_star2_O3/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 0x16e20
movl 0x84(%rdx), %ecx
movl %ecx, 0x84(%rdi)
movl 0x40(%rdi), %ecx
leal -0x1(%rcx), %r8d
cmpl $0x2, %r8d
jae 0x16e57
movl 0x8c(%rdx), %r8d
movl %r8d, 0xac(%rdi)
movl 0x9c(%rdx), %edx
movl %edx, 0xd4(%rdi)
jmp 0x16e79
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 0x16e79
cmpl $0x3, %ecx
jne 0x16e79
movl 0x94(%rdx), %ecx
movl %ecx, 0xac(%rdi)
movl 0xa4(%rdx), %ecx
movl %ecx, 0xd4(%rdi)
movl $0x3, %ecx
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x50(%rdi), %rdx
movq 0x8(%rdi), %r8
movb $0x1, %r9b
xorl %r11d, %r11d
movl $0x40, %r10d
movq 0x40(%rax,%r11,8), %rbx
testq %rbx, %rbx
je 0x16f1f
leaq (%r11,%r11), %r14
movslq (%rdx,%r11,8), %r15
movb 0x78(%rbx,%r15), %bpl
movq %r11, %r15
shlq $0x4, %r15
leaq 0xb(%r15), %r12
movb %bpl, 0x8b(%rdi,%r15)
leaq 0x1(,%r11,2), %r13
movslq 0x4(%rdx,%r14,4), %rbp
movb 0x78(%rbx,%rbp), %bpl
movb %bpl, 0x93(%rdi,%r15)
cmpl $0x3, %ecx
je 0x16fbe
cmpl $0x2, %ecx
je 0x16f74
cmpl $0x1, %ecx
jne 0x1700e
movslq 0x10(%rdx,%r14,4), %r15
movb 0x78(%rbx,%r15), %bpl
movb %bpl, 0xb3(%rdi,%r11,8)
movslq 0x14(%rdx,%r14,4), %r14
movb 0x78(%rbx,%r14), %bl
movb %bl, 0xdb(%rdi,%r11,8)
jmp 0x1700e
movq 0x88360(%r8), %rbx
cmpl $0x0, 0x14(%rbx)
sete %bl
shlb $0x6, %bl
testb $0xf, %sil
movzbl %bl, %ebx
cmovnel %r10d, %ebx
shlq $0x4, %r11
movb %bl, 0xe3(%rdi,%r11)
movb %bl, 0xdb(%rdi,%r11)
movb %bl, 0xbb(%rdi,%r11)
movb %bl, 0xb3(%rdi,%r11)
movb %bl, 0x93(%rdi,%r11)
movb %bl, 0x8b(%rdi,%r11)
jmp 0x1700e
movslq (%rdx,%r14,4), %r11
movb 0x86(%rbx,%r11), %r11b
movb %r11b, 0xa8(%rdi,%r12)
movslq (%rdx,%r13,4), %r11
movb 0x86(%rbx,%r11), %r11b
movb %r11b, 0xbb(%rdi,%r15)
movslq (%rdx,%r14,4), %r11
movb 0x96(%rbx,%r11), %r11b
movb %r11b, 0xd0(%rdi,%r12)
movslq (%rdx,%r13,4), %r11
movb 0x96(%rbx,%r11), %r11b
jmp 0x17006
movslq (%rdx,%r14,4), %r11
movb 0x88(%rbx,%r11), %r11b
movb %r11b, 0xa8(%rdi,%r12)
movslq (%rdx,%r13,4), %r11
movb 0x88(%rbx,%r11), %r11b
movb %r11b, 0xbb(%rdi,%r15)
movslq (%rdx,%r14,4), %r11
movb 0x98(%rbx,%r11), %r11b
movb %r11b, 0xd0(%rdi,%r12)
movslq (%rdx,%r13,4), %r11
movb 0x98(%rbx,%r11), %r11b
movb %r11b, 0xe3(%rdi,%r15)
movl $0x1, %r11d
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x16e97
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop