// File: 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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movl %esi, 0x6c(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x50(%rax), %rcx
movq %rcx, 0x58(%rsp)
movq %rax, %rcx
addq $0x18, %rcx
addq $0x68, %rcx
movq %rcx, 0x50(%rsp)
movq %rax, %rcx
addq $0x18, %rcx
addq $0x68, %rcx
addq $0x28, %rcx
movq %rcx, 0x48(%rsp)
addq $0x18, %rax
addq $0x68, %rax
addq $0x50, %rax
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rax
cmpq $0x0, 0x50(%rax)
je 0x200de
movq 0x60(%rsp), %rax
movq 0x50(%rax), %rax
addq $0x78, %rax
movq %rax, 0x38(%rsp)
movq 0x50(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq 0x38(%rsp), %rdx
movl 0xc(%rdx), %edx
movl %edx, (%rcx)
cmpl $0x1, 0x40(%rax)
je 0x20062
movq 0x20(%rsp), %rax
cmpl $0x2, 0x40(%rax)
jne 0x20096
movq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movq 0x38(%rsp), %rcx
movl 0x14(%rcx), %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movq 0x38(%rsp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, (%rax)
jmp 0x200d9
movq 0x20(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x200d5
movq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movq 0x38(%rsp), %rcx
movl 0x1c(%rcx), %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movq 0x38(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movl %ecx, (%rax)
jmp 0x200d7
jmp 0x200d7
jmp 0x200d9
jmp 0x20179
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x88360(%rax), %rcx
xorl %eax, %eax
cmpl $0x0, 0x14(%rcx)
movb %al, 0x1f(%rsp)
je 0x2010d
movl 0x6c(%rsp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %dl
movl $0x40404040, %eax       # imm = 0x40404040
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x50(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movl 0x10(%rsp), %ecx
movl %ecx, (%rax)
movl 0x34(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x48(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movl 0x14(%rsp), %ecx
movl %ecx, (%rax)
movl 0x34(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
movl $0x4, %esi
callq 0x22380
movl 0x18(%rsp), %ecx
movl %ecx, (%rax)
movl $0x0, 0x30(%rsp)
cmpl $0x2, 0x30(%rsp)
jge 0x20621
movq 0x60(%rsp), %rax
movslq 0x30(%rsp), %rcx
cmpq $0x0, 0x40(%rax,%rcx,8)
je 0x2051d
movq 0x60(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq 0x40(%rax,%rcx,8), %rax
addq $0x78, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x0, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, 0xe(%rsp)
movq 0x50(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0xe(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, 0xf(%rsp)
movq 0x50(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0xf(%rsp), %dl
movq %rax, %rcx
movq 0x20(%rsp), %rax
movb %dl, (%rcx)
cmpl $0x1, 0x40(%rax)
jne 0x202c9
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x4, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, 0xc(%rsp)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x3, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0xc(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x5, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, 0xd(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x3, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0xd(%rsp), %cl
movb %cl, (%rax)
jmp 0x20518
movq 0x20(%rsp), %rax
cmpl $0x2, 0x40(%rax)
jne 0x203f5
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x0, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x2, %ecx
addl $0x10, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x8(%rsp)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x8(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x2, %ecx
addl $0x10, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x9(%rsp)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x9(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x0, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x2, %ecx
addl $0x20, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0xa(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0xa(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x2, %ecx
addl $0x20, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0xb(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0xb(%rsp), %cl
movb %cl, (%rax)
jmp 0x20516
movq 0x20(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x20512
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x0, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x10, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x4(%rsp)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x4(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x10, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x5(%rsp)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x5(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x0, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x20, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x6(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x6(%rsp), %cl
movb %cl, (%rax)
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %rcx
movl 0x30(%rsp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x20, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, 0x7(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x7(%rsp), %cl
movb %cl, (%rax)
jmp 0x20514
jmp 0x20514
jmp 0x20516
jmp 0x20518
jmp 0x2060f
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x88360(%rax), %rcx
xorl %eax, %eax
cmpl $0x0, 0x14(%rcx)
movb %al, 0x3(%rsp)
je 0x2054c
movl 0x6c(%rsp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x3(%rsp)
movb 0x3(%rsp), %dl
movl $0x40, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movb %al, 0x2(%rsp)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x2(%rsp), %cl
movb %cl, (%rax)
movq 0x40(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x2(%rsp), %cl
movb %cl, (%rax)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x2(%rsp), %cl
movb %cl, (%rax)
movq 0x48(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x2(%rsp), %cl
movb %cl, (%rax)
movq 0x50(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0x13, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x2(%rsp), %cl
movb %cl, (%rax)
movq 0x50(%rsp), %rdi
movl 0x30(%rsp), %eax
shll $0x4, %eax
addl $0xb, %eax
movslq %eax, %rsi
callq 0x22380
movb 0x2(%rsp), %cl
movb %cl, (%rax)
jmp 0x20611
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x20181
movq 0x20(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x2063a
movl 0x6c(%rsp), %eax
andl $0x3, %eax
cmpl $0x2, %eax
jne 0x2063a
jmp 0x2063a
addq $0x78, %rsp
retq
nop