// File: /workspace/github_c_makefile_star2_O2/akumrao[P]videoAnalyzer/core/macroblock.c
// Function: x264_mb_predict_mv(x264_macroblock_t*, int, int, int, int*)
void x264_mb_predict_mv(x264_macroblock_t *mb, int i_list, int i_part, int i_subpart, int mv[2]) {LOG_CALL;
    int x, y, xn, yn;
    int w, h;
    int i_ref;

    int i_refa = -1;
    int i_refb = -1;
    int i_refc = -1;

    int mvxa = 0, mvxb = 0, mvxc = 0;
    int mvya = 0, mvyb = 0, mvyc = 0;

    x264_macroblock_t *mbn;


    x264_mb_partition_getxy(mb, i_part, i_subpart, &x, &y);
    x264_mb_partition_size(mb, i_part, i_subpart, &w, &h);
    i_ref = mb->partition[x][y].i_ref[i_list];

    /* Left  pixel (-1,0)*/
    xn = x - 1;
    mbn = mb;
    if (xn < 0)
    {
        xn += 4;
        mbn = mb->context->mba;
    }
    if (mbn)
    {
        i_refa = -2;
        if (!IS_INTRA(mbn->i_type) && mbn->partition[xn][y].i_ref[i_list] != -1)
        {
            i_refa = mbn->partition[xn][y].i_ref[i_list];
            mvxa = mbn->partition[xn][y].mv[i_list][0];
            mvya = mbn->partition[xn][y].mv[i_list][1];
        }
    }

    /* Up ( pixel(0,-1)*/
    yn = y - 1;
    mbn = mb;
    if (yn < 0)
    {
        yn += 4;
        mbn = mb->context->mbb;
    }
    if (mbn)
    {
        i_refb = -2;
        if (!IS_INTRA(mbn->i_type) && mbn->partition[x][yn].i_ref[i_list] != -1)
        {
            i_refb = mbn->partition[x][yn].i_ref[i_list];
            mvxb = mbn->partition[x][yn].mv[i_list][0];
            mvyb = mbn->partition[x][yn].mv[i_list][1];
        }
    }

    /* Up right pixel(width,-1)*/
    xn = x + w;
    yn = y - 1;

    mbn = mb;
    if (yn < 0 && xn >= 4)
    {
        if (mb->context->mbc)
        {
            xn -= 4;
            yn += 4;
            mbn = mb->context->mbc;
        } else
        {
            mbn = NULL;
        }
    } else if (yn < 0)
    {
        yn += 4;
        mbn = mb->context->mbb;
    } else if (xn >= 4 || (xn == 2 && (yn == 0 || yn == 2)))
    {
        mbn = NULL; /* not yet decoded */
    }

    if (mbn == NULL)
    {
        /* load top left pixel(-1,-1) */
        xn = x - 1;
        yn = y - 1;

        mbn = mb;
        if (yn < 0 && xn < 0)
        {
            if (mb->context->mba && mb->context->mbb)
            {
                xn += 4;
                yn += 4;
                mbn = mb->context->mbb - 1;
            } else
            {
                mbn = NULL;
            }
        } else if (yn < 0)
        {
            yn += 4;
            mbn = mb->context->mbb;
        } else if (xn < 0)
        {
            xn += 4;
            mbn = mb->context->mba;
        }
    }

    if (mbn)
    {
        i_refc = -2;
        if (!IS_INTRA(mbn->i_type) && mbn->partition[xn][yn].i_ref[i_list] != -1)
        {
            i_refc = mbn->partition[xn][yn].i_ref[i_list];
            mvxc = mbn->partition[xn][yn].mv[i_list][0];
            mvyc = mbn->partition[xn][yn].mv[i_list][1];
        }
    }

    if (mb->i_partition == D_16x8 && i_part == 0 && i_refb == i_ref)
    {
        mv[0] = mvxb;
        mv[1] = mvyb;
    } else if (mb->i_partition == D_16x8 && i_part == 1 && i_refa == i_ref)
    {
        mv[0] = mvxa;
        mv[1] = mvya;
    } else if (mb->i_partition == D_8x16 && i_part == 0 && i_refa == i_ref)
    {
        mv[0] = mvxa;
        mv[1] = mvya;
    } else if (mb->i_partition == D_8x16 && i_part == 1 && i_refc == i_ref)
    {
        mv[0] = mvxc;
        mv[1] = mvyc;
    } else
    {
        int i_count;

        i_count = 0;
        if (i_refa == i_ref) i_count++;
        if (i_refb == i_ref) i_count++;
        if (i_refc == i_ref) i_count++;

        if (i_count > 1)
        {
            mv[0] = x264_median(mvxa, mvxb, mvxc);
            mv[1] = x264_median(mvya, mvyb, mvyc);
        } else if (i_count == 1)
        {
            if (i_refa == i_ref)
            {
                mv[0] = mvxa;
                mv[1] = mvya;
            } else if (i_refb == i_ref)
            {
                mv[0] = mvxb;
                mv[1] = mvyb;
            } else
            {
                mv[0] = mvxc;
                mv[1] = mvyc;
            }
        } else if (i_refb == -1 && i_refc == -1 && i_refa != -1)
        {
            mv[0] = mvxa;
            mv[1] = mvya;
        } else
        {
            mv[0] = x264_median(mvxa, mvxb, mvxc);
            mv[1] = x264_median(mvya, mvyb, mvyc);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, -0x40(%rbp)
movl %ecx, %r12d
movl %edx, %r14d
movl %esi, -0x2c(%rbp)
movq %rdi, %r13
leaq 0x156c0(%rip), %rdi     # 0x1b1b4
leaq 0x1609d(%rip), %rsi     # 0x1bb98
leaq 0x16167(%rip), %rdx     # 0x1bc69
movl $0x0, -0x30(%rbp)
xorl %eax, %eax
callq 0x39ba
leaq -0x58(%rbp), %rbx
leaq -0x54(%rbp), %r15
movq %r13, %rdi
movl %r14d, %esi
movl %r12d, %edx
movq %rbx, %rcx
movq %r15, %r8
callq 0x58f5
leaq -0x4c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movq %r13, %rdi
movl %r14d, -0x48(%rbp)
movl %r14d, %esi
callq 0x5997
movslq (%rbx), %r10
imulq $0xa0, %r10, %r8
leaq (%r8,%r13), %rdx
movslq (%r15), %r11
imulq $0x28, %r11, %rcx
leal -0x1(%r10), %r9d
movslq -0x2c(%rbp), %rdi
movq %r13, %rsi
movl %r9d, %ebx
testq %r10, %r10
jg 0x5b7b
movq 0x10(%r13), %rax
movq (%rax), %rsi
testq %rsi, %rsi
je 0x5bbc
leal 0x3(%r10), %ebx
pushq $-0x2
popq %rax
cmpl $0x2, 0x18(%rsi)
jb 0x5bbf
movslq %ebx, %rbx
imulq $0xa0, %rbx, %rbx
addq %rbx, %rsi
leaq (%rsi,%rcx), %rbx
movl 0x30(%rbx,%rdi,4), %ebx
movl $0x0, %r14d
cmpl $-0x1, %ebx
je 0x5bc2
leaq (%rcx,%rsi), %rax
addq $0x30, %rax
movl 0x8(%rax,%rdi,8), %esi
movl %esi, -0x30(%rbp)
movl 0xc(%rax,%rdi,8), %r14d
movl %ebx, %eax
jmp 0x5bc2
pushq $-0x1
popq %rax
xorl %r14d, %r14d
movl %r14d, -0x34(%rbp)
addq %rcx, %rdx
leal -0x1(%r11), %ebx
movq %r13, %r15
movl %ebx, %r14d
testl %r11d, %r11d
jg 0x5be9
movq 0x10(%r13), %rcx
movq 0x8(%rcx), %r15
testq %r15, %r15
je 0x5c30
leal 0x3(%r11), %r14d
pushq $-0x2
popq %rsi
movl $0x0, -0x2c(%rbp)
cmpl $0x2, 0x18(%r15)
jb 0x5c3a
addq %r8, %r15
movslq %r14d, %r8
imulq $0x28, %r8, %r12
leaq (%r15,%r12), %r8
movl 0x30(%r8,%rdi,4), %r8d
movl $0x0, %ecx
cmpl $-0x1, %r8d
je 0x5c3c
leaq (%r12,%r15), %rcx
addq $0x30, %rcx
movl 0x8(%rcx,%rdi,8), %esi
movl %esi, -0x2c(%rbp)
movl 0xc(%rcx,%rdi,8), %ecx
movl %r8d, %esi
jmp 0x5c3c
movl $0x0, -0x2c(%rbp)
pushq $-0x1
popq %rsi
xorl %ecx, %ecx
movl 0x30(%rdx,%rdi,4), %r8d
movl -0x4c(%rbp), %edx
addl %r10d, %edx
testl %r11d, %r11d
movl %ecx, -0x44(%rbp)
jg 0x5c7d
cmpl $0x4, %edx
jl 0x5c7d
movl %eax, %ecx
movl -0x30(%rbp), %r14d
movq %r13, %rax
movq 0x10(%r13), %r12
movq 0x10(%r12), %r13
testq %r13, %r13
je 0x5cc0
addl $-0x4, %edx
addl $0x3, %r11d
movl %edx, %r9d
movl %r11d, %ebx
jmp 0x5d52
testl %r11d, %r11d
jle 0x5c9a
cmpl $0x3, %edx
jle 0x5cd2
movl %eax, %ecx
testl %r10d, %r10d
jle 0x5cef
movl -0x30(%rbp), %r14d
movq %r13, %rax
jmp 0x5d52
movl %eax, %ecx
movl -0x30(%rbp), %r14d
leal 0x3(%r11), %ebx
movq %r13, %rax
movq 0x10(%r13), %r12
movq 0x8(%r12), %r13
testq %r13, %r13
jne 0x5d4f
testl %r10d, %r10d
jg 0x5ccc
jmp 0x5d12
testl %r10d, %r10d
jle 0x5d12
addl $0x3, %r11d
movl %r11d, %ebx
addq $0x8, %r12
jmp 0x5d01
cmpl $0x2, %edx
jne 0x5d46
movl %ebx, %edx
andl $-0x3, %edx
pushq $0x2
popq %r11
cmovnel %r11d, %r9d
movl %eax, %ecx
testl %r10d, %r10d
jg 0x5c8e
testl %edx, %edx
jne 0x5c8e
movl -0x30(%rbp), %r14d
addl $0x3, %r10d
movq %r13, %rax
movq 0x10(%r13), %r12
movl %r10d, %r9d
movq (%r12), %r13
testq %r13, %r13
jne 0x5d52
xorl %edx, %edx
pushq $-0x1
popq %r15
jmp 0x5d5f
xorl %edx, %edx
pushq $-0x1
popq %r15
cmpq $0x0, (%r12)
je 0x5d5f
movq 0x8(%r12), %r13
movl $0x0, %r12d
testq %r13, %r13
je 0x5da1
addl $0x3, %r10d
addl $0x3, %r11d
addq $-0xf88, %r13           # imm = 0xF078
movl %r10d, %r9d
jmp 0x5c75
movl %eax, %ecx
movl -0x30(%rbp), %r14d
movq %r13, %rax
movl %edx, %r9d
xorl %edx, %edx
pushq $-0x2
popq %r15
cmpl $0x2, 0x18(%r13)
jae 0x5d64
xorl %r12d, %r12d
jmp 0x5da1
movslq %r9d, %r9
imulq $0xa0, %r9, %r9
addq %r9, %r13
movslq %ebx, %r9
imulq $0x28, %r9, %r10
leaq (%r10,%r13), %r9
movl 0x30(%r9,%rdi,4), %r9d
movl $0x0, %r12d
cmpl $-0x1, %r9d
je 0x5da1
addq %r13, %r10
addq $0x30, %r10
movl 0x8(%r10,%rdi,8), %edx
movl 0xc(%r10,%rdi,8), %r12d
movl %r9d, %r15d
movl 0x1c(%rax), %r9d
movl -0x48(%rbp), %r10d
testl %r10d, %r10d
jne 0x5dc7
cmpl $0xe, %r9d
jne 0x5dc7
cmpl %r8d, %esi
jne 0x5dc7
movq -0x40(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x44(%rbp), %ecx
jmp 0x5df2
cmpl $0x1, %r10d
jne 0x5dd8
cmpl $0xe, %r9d
jne 0x5dd8
cmpl %r8d, %ecx
je 0x5de8
testl %r10d, %r10d
jne 0x5e04
cmpl $0xf, %r9d
jne 0x5e04
cmpl %r8d, %ecx
jne 0x5e04
movq -0x40(%rbp), %rax
movl %r14d, (%rax)
movl -0x34(%rbp), %ecx
movl %ecx, 0x4(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %r10d
jne 0x5e21
cmpl $0xf, %r9d
jne 0x5e21
cmpl %r8d, %r15d
jne 0x5e21
movq -0x40(%rbp), %rax
movl %edx, (%rax)
movl %r12d, 0x4(%rax)
jmp 0x5df5
xorl %r11d, %r11d
cmpl %r8d, %r15d
sete %r11b
xorl %r9d, %r9d
cmpl %r8d, %ecx
sete %r9b
leal 0x1(%r9), %r10d
cmpl %r8d, %esi
cmovnel %r9d, %r10d
addl %r10d, %r11d
cmpl $0x2, %r11d
jb 0x5e6d
movl %r14d, %edi
movl -0x2c(%rbp), %esi
callq 0x5e9b
movq -0x40(%rbp), %rbx
movl %eax, (%rbx)
movl -0x34(%rbp), %edi
movl -0x44(%rbp), %esi
movl %r12d, %edx
callq 0x5e9b
movl %eax, 0x4(%rbx)
jmp 0x5df5
cmpl $0x1, %r11d
jne 0x5e87
cmpl %r8d, %ecx
je 0x5de8
cmpl %r8d, %esi
je 0x5db9
jmp 0x5e15
cmpl $-0x1, %ecx
je 0x5e49
andl %esi, %r15d
cmpl $-0x1, %r15d
je 0x5de8
jmp 0x5e49