// File: /workspace/github_c_makefile_star2_O0/akumrao[P]videoAnalyzer/encoder/macroblock.c
// Function: x264_macroblock_write_cabac(x264_t*, bs_s*, x264_macroblock_t*)
void x264_macroblock_write_cabac(x264_t *h, bs_t *s, x264_macroblock_t *mb) {LOG_CALL;
    int i;

    /* Write the MB type */
#if 0
    fprintf(stderr, "[%d,%d] type=%d cbp=%d predc=%d\n",
            mb->i_mb_x, mb->i_mb_y,
            1 + mb->i_intra16x16_pred_mode + mb->i_cbp_chroma * 4 + (mb->i_cbp_luma == 0 ? 0 : 12),
            (mb->i_cbp_chroma << 4) | mb->i_cbp_luma,
            mb->i_chroma_pred_mode);
#endif
    x264_cabac_mb_type(h, mb);

    /* PCM special block type UNTESTED */
    if (mb->i_type == I_PCM)
    {
        bs_align_0(s); /* not sure */
        /* Luma */
        for (i = 0; i < 16 * 16; i++)
        {
            bs_write(s, 8, h->picture->plane[0][mb->i_mb_y * 16 * h->picture->i_stride[0] + mb->i_mb_x * 16 + i],"h->picture->plane[0]");
        }
        /* Cb */
        for (i = 0; i < 8 * 8; i++)
        {
            bs_write(s, 8, h->picture->plane[1][mb->i_mb_y * 8 * h->picture->i_stride[1] + mb->i_mb_x * 8 + i],"h->picture->plane[1]");
        }
        /* Cr */
        for (i = 0; i < 8 * 8; i++)
        {
            bs_write(s, 8, h->picture->plane[2][mb->i_mb_y * 8 * h->picture->i_stride[2] + mb->i_mb_x * 8 + i],"h->picture->plane[2]");
        }

        for (i = 0; i < 16 + 8; i++)
        {
            /* special case */
            mb->block[i].i_non_zero_count = 16;
        }

        x264_cabac_encode_init(&h->cabac, s);
        return;
    }

    if (IS_INTRA(mb->i_type))
    {
        /* Prediction */
        if (mb->i_type == I_4x4)
        {
            for (i = 0; i < 16; i++)
            {
                x264_cabac_mb_intra4x4_pred_mode(h, mb,
                        x264_mb_predict_intra4x4_mode(h, mb, i),
                        mb->block[i].i_intra4x4_pred_mode);
            }
        }
        x264_cabac_mb_intra8x8_pred_mode(h, mb);
    } else if (mb->i_type == P_8x8)
    {
        /* sub mb type */
        for (i = 0; i < 4; i++)
        {
            x264_cabac_mb_sub_partition(h, mb->i_sub_partition[i]);
        }
        /* ref 0 */
        if (h->sh.i_num_ref_idx_l0_active > 1)
        {
            for (i = 0; i < 4; i++)
            {
                x264_cabac_mb_ref(h, mb, 0, i);
            }
        }

        for (i = 0; i < 4; i++)
        {
            int i_sub;
            for (i_sub = 0; i_sub < x264_mb_partition_count_table[mb->i_sub_partition[i]]; i_sub++)
            {
                x264_cabac_mb_mv(h, mb, 0, i, i_sub);
            }
        }
    } else if (mb->i_type == B_8x8)
    {
        /* TODO */
        fprintf(stderr, "Arggg B_8x8\n");
    } else if (mb->i_type != B_DIRECT)
    {
        /* FIXME -> invalid for B frame */

        /* Motion Vector */
        int i_part = x264_mb_partition_count_table[mb->i_partition];

        if (h->sh.i_num_ref_idx_l0_active > 1)
        {
            for (i = 0; i < i_part; i++)
            {
                if (mb->i_type == P_L0)
                {
                    x264_cabac_mb_ref(h, mb, 0, i);
                }
            }
        }

        for (i = 0; i < i_part; i++)
        {
            if (mb->i_type == P_L0)
            {
                x264_cabac_mb_mv(h, mb, 0, i, 0);
            }
        }
    }

    if (mb->i_type != I_16x16)
    {
        x264_cabac_mb_cbp_luma(h, mb);
        x264_cabac_mb_cbp_chroma(h, mb);
    }

    if (mb->i_cbp_luma > 0 || mb->i_cbp_chroma > 0 || mb->i_type == I_16x16)
    {
        x264_cabac_mb_qp_delta(h, mb);

        /* write residual */
        if (mb->i_type == I_16x16)
        {
            /* DC Luma */
            block_residual_write_cabac(h, mb, 0, 0, mb->luma16x16_dc, 16);

            if (mb->i_cbp_luma != 0)
            {
                /* AC Luma */
                for (i = 0; i < 16; i++)
                {
                    block_residual_write_cabac(h, mb, 1, i, mb->block[i].residual_ac, 15);
                }
            }
        } else
        {
            for (i = 0; i < 16; i++)
            {
                if (mb->i_cbp_luma & (1 << (i / 4)))
                {
                    block_residual_write_cabac(h, mb, 2, i, mb->block[i].luma4x4, 16);
                }
            }
        }

        if (mb->i_cbp_chroma & 0x03) /* Chroma DC residual present */
        {
            block_residual_write_cabac(h, mb, 3, 0, mb->chroma_dc[0], 4);
            block_residual_write_cabac(h, mb, 3, 1, mb->chroma_dc[1], 4);
        }
        if (mb->i_cbp_chroma & 0x02) /* Chroma AC residual present */
        {
            for (i = 0; i < 8; i++)
            {
                block_residual_write_cabac(h, mb, 4, i, mb->block[16 + i].residual_ac, 15);
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq 0xaaa5(%rip), %rdi      # 0x28eb0
leaq 0x116c2(%rip), %rsi     # 0x2fad4
leaq 0x11a10(%rip), %rdx     # 0x2fe29
movb $0x0, %al
callq 0x4d20
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1ea60
movq -0x18(%rbp), %rax
cmpl $0x2, 0x18(%rax)
jne 0x1e5d5
movq -0x10(%rbp), %rdi
callq 0x1d840
movl $0x0, -0x1c(%rbp)
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
jge 0x1e4b1
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10ee48(%rax), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
shll $0x4, %ecx
movq -0x8(%rbp), %rdx
movq 0x10ee48(%rdx), %rdx
imull 0xc(%rdx), %ecx
movq -0x18(%rbp), %rdx
movl (%rdx), %edx
shll $0x4, %edx
addl %edx, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
movl $0x8, %esi
leaq 0x119a4(%rip), %rcx     # 0x2fe45
callq 0x1d890
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e44b
movl $0x0, -0x1c(%rbp)
cmpl $0x40, -0x1c(%rbp)
jge 0x1e51b
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10ee48(%rax), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
shll $0x3, %ecx
movq -0x8(%rbp), %rdx
movq 0x10ee48(%rdx), %rdx
imull 0x10(%rdx), %ecx
movq -0x18(%rbp), %rdx
movl (%rdx), %edx
shll $0x3, %edx
addl %edx, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
movl $0x8, %esi
leaq 0x1194f(%rip), %rcx     # 0x2fe5a
callq 0x1d890
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e4b8
movl $0x0, -0x1c(%rbp)
cmpl $0x40, -0x1c(%rbp)
jge 0x1e585
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10ee48(%rax), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
shll $0x3, %ecx
movq -0x8(%rbp), %rdx
movq 0x10ee48(%rdx), %rdx
imull 0x14(%rdx), %ecx
movq -0x18(%rbp), %rdx
movl (%rdx), %edx
shll $0x3, %edx
addl %edx, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
movl $0x8, %esi
leaq 0x118fa(%rip), %rcx     # 0x2fe6f
callq 0x1d890
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e522
movl $0x0, -0x1c(%rbp)
cmpl $0x18, -0x1c(%rbp)
jge 0x1e5bc
movq -0x18(%rbp), %rax
addq $0x324, %rax            # imm = 0x324
movslq -0x1c(%rbp), %rcx
imulq $0x84, %rcx, %rcx
addq %rcx, %rax
movl $0x10, 0x4(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e58c
movq -0x8(%rbp), %rdi
addq $0x10db60, %rdi         # imm = 0x10DB60
movq -0x10(%rbp), %rsi
callq 0x3900
jmp 0x1ea4f
movq -0x18(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x1e5e9
movq -0x18(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x1e668
movq -0x18(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x1e656
movl $0x0, -0x1c(%rbp)
cmpl $0x10, -0x1c(%rbp)
jge 0x1e654
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x7400
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl %eax, %edx
movq -0x18(%rbp), %rax
addq $0x324, %rax            # imm = 0x324
movslq -0x1c(%rbp), %rcx
imulq $0x84, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
callq 0x1f020
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e5fa
jmp 0x1e656
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1f100
jmp 0x1e814
movq -0x18(%rbp), %rax
cmpl $0x4, 0x18(%rax)
jne 0x1e73f
movl $0x0, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
jge 0x1e6a3
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl 0x20(%rax,%rcx,4), %esi
callq 0x1f270
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e67d
movq -0x8(%rbp), %rax
cmpl $0x1, 0x10db38(%rax)
jle 0x1e6dc
movl $0x0, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
jge 0x1e6da
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
xorl %edx, %edx
callq 0x1f3b0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e6b7
jmp 0x1e6dc
movl $0x0, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
jge 0x1e73a
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
movslq 0x20(%rcx,%rdx,4), %rdx
leaq 0xf0b9(%rip), %rcx      # 0x2d7c0
cmpl (%rcx,%rdx,4), %eax
jge 0x1e72d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
xorl %edx, %edx
callq 0x1f5d0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1e6f0
jmp 0x1e72f
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e6e3
jmp 0x1e812
movq -0x18(%rbp), %rax
cmpl $0x10, 0x18(%rax)
jne 0x1e766
movq 0x17880(%rip), %rax     # 0x35fd0
movq (%rax), %rdi
leaq 0x1172a(%rip), %rsi     # 0x2fe84
movb $0x0, %al
callq 0x2160
jmp 0x1e810
movq -0x18(%rbp), %rax
cmpl $0x6, 0x18(%rax)
je 0x1e80e
movq -0x18(%rbp), %rax
movslq 0x1c(%rax), %rcx
leaq 0xf03d(%rip), %rax      # 0x2d7c0
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x1, 0x10db38(%rax)
jle 0x1e7d0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1e7ce
movq -0x18(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x1e7c1
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
xorl %edx, %edx
callq 0x1f3b0
jmp 0x1e7c3
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e79d
jmp 0x1e7d0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1e80c
movq -0x18(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x1e7ff
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
xorl %r8d, %r8d
movl %r8d, %edx
callq 0x1f5d0
jmp 0x1e801
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e7d7
jmp 0x1e80e
jmp 0x1e810
jmp 0x1e812
jmp 0x1e814
movq -0x18(%rbp), %rax
cmpl $0x1, 0x18(%rax)
je 0x1e838
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1f870
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1fa50
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2b8(%rax)
jg 0x1e860
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2b4(%rax)
jg 0x1e860
movq -0x18(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x1ea4f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1fc10
movq -0x18(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x1e901
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %r8
addq $0x2c4, %r8             # imm = 0x2C4
xorl %ecx, %ecx
movl $0x10, %r9d
movl %ecx, %edx
callq 0x1fe30
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2b8(%rax)
je 0x1e8fc
movl $0x0, -0x1c(%rbp)
cmpl $0x10, -0x1c(%rbp)
jge 0x1e8fa
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %r8
addq $0x324, %r8             # imm = 0x324
movslq -0x1c(%rbp), %rax
imulq $0x84, %rax, %rax
addq %rax, %r8
addq $0x8, %r8
movl $0x1, %edx
movl $0xf, %r9d
callq 0x1fe30
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e8b1
jmp 0x1e8fc
jmp 0x1e982
movl $0x0, -0x1c(%rbp)
cmpl $0x10, -0x1c(%rbp)
jge 0x1e980
movq -0x18(%rbp), %rax
movl 0x2b8(%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x3c(%rbp), %eax
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1e973
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %r8
addq $0x324, %r8             # imm = 0x324
movslq -0x1c(%rbp), %rax
imulq $0x84, %rax, %rax
addq %rax, %r8
addq $0x44, %r8
movl $0x2, %edx
movl $0x10, %r9d
callq 0x1fe30
jmp 0x1e975
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e908
jmp 0x1e982
movq -0x18(%rbp), %rax
movl 0x2b4(%rax), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1e9e5
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %r8
addq $0x304, %r8             # imm = 0x304
movl $0x3, %edx
xorl %ecx, %ecx
movl $0x4, %r9d
callq 0x1fe30
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %r8
addq $0x304, %r8             # imm = 0x304
addq $0x10, %r8
movl $0x3, %edx
movl $0x1, %ecx
movl $0x4, %r9d
callq 0x1fe30
movq -0x18(%rbp), %rax
movl 0x2b4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1ea4d
movl $0x0, -0x1c(%rbp)
cmpl $0x8, -0x1c(%rbp)
jge 0x1ea4b
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %r8
addq $0x324, %r8             # imm = 0x324
movl -0x1c(%rbp), %eax
addl $0x10, %eax
cltq
imulq $0x84, %rax, %rax
addq %rax, %r8
addq $0x8, %r8
movl $0x4, %edx
movl $0xf, %r9d
callq 0x1fe30
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1e9fe
jmp 0x1ea4d
jmp 0x1ea4f
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)