// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/compare.c
// Function: LocateMotion
static TriggerInfo_t LocateMotion(ImgMap_t * DiffScaled)
{
    TriggerInfo_t retval;
    int widthSc = DiffScaled->w;

    int maxc, maxr, maxval;

    maxval = BlockFilterImgMap(DiffScaled, wind_w, wind_h, &maxc, &maxr);

    // Now search for the maximum inside a rectangular window.
    retval.DiffLevel = maxval/100;
    retval.x = retval.y = 0;

    // Work out where most of the motion was found inside the search window.
    if (Verbosity) printf("Window contents.  Cols %d-%d Rows %d-%d\n",maxc,maxc+wind_w-1,maxr, maxr+wind_h-1);
    if (maxval > 0){
        double xsum, ysum;
        int sum;
        int row = maxr;
        if (row < 0) row = 0;

        xsum = ysum = sum = 0;
        for (;row<maxr+wind_h;row++){
            int col = maxc;
            if (col < 0) col = 0;
            for(;col<maxc+wind_w;col++){
                int v = DiffScaled->values[row*widthSc+col];
                xsum += col*v;
                ysum += row*v;
                sum += v;
                //printf("%5d",DiffScaled->values[row*widthSc+col]);
            }
            //printf("\n");
        }
        if (Verbosity) printf("Exact col=%5.1f, row=%5.1f\n",xsum*1.0/sum, ysum*1.0/sum);
        retval.x = (int)(xsum*scalef*ScaleDenom/sum)+scalef*ScaleDenom/2;
        retval.y = (int)(ysum*scalef*ScaleDenom/sum)+scalef*ScaleDenom/2;
        if (Verbosity) printf("Picture coordinates: x,y = %d,%d\n",retval.x, retval.y);
    }

    return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %r14
movslq (%rdi), %r12
leaq 0x4(%rsp), %rcx
movq %rsp, %r8
movl $0x4, %esi
movl $0x7, %edx
callq 0x7243
movl %eax, %ebx
leaq 0xc1d4(%rip), %r15      # 0x12998
cmpl $0x0, (%r15)
je 0x67e6
movl 0x4(%rsp), %esi
leal 0x3(%rsi), %edx
movl (%rsp), %ecx
leal 0x6(%rcx), %r8d
leaq 0x617f(%rip), %rdi      # 0xc95e
xorl %eax, %eax
callq 0x3170
testl %ebx, %ebx
jle 0x68e5
movslq (%rsp), %rax
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
xorpd %xmm4, %xmm4
cmpq $-0x6, %rax
jl 0x6894
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
movslq 0x4(%rsp), %rdx
testl %edx, %edx
movl $0x0, %edi
cmovgl %edx, %edi
movq %rdx, %r8
addq $0x3, %r8
addq $0x6, %rax
movq %r12, %r9
imulq %rsi, %r9
leaq (%r14,%r9,4), %r9
addq $0x8, %r9
shlq $0x2, %r12
cmpl $-0x3, %edx
jl 0x687e
movq %rdi, %r10
movl (%r9,%r10,4), %r11d
movl %r10d, %ebp
imull %r11d, %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
addsd %xmm0, %xmm3
addl %r11d, %ecx
imull %esi, %r11d
xorps %xmm0, %xmm0
cvtsi2sd %r11d, %xmm0
addsd %xmm0, %xmm2
leaq 0x1(%r10), %r11
cmpq %r8, %r10
movq %r11, %r10
jl 0x6849
leaq 0x1(%rsi), %r10
addq %r12, %r9
cmpq %rax, %rsi
movq %r10, %rsi
jl 0x6841
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
cmpl $0x0, (%r15)
je 0x68ec
movapd %xmm3, %xmm0
divsd %xmm4, %xmm0
movapd %xmm2, %xmm1
divsd %xmm4, %xmm1
leaq 0x60d6(%rip), %rdi      # 0xc987
movb $0x2, %al
movsd %xmm2, 0x18(%rsp)
movsd %xmm3, 0x10(%rsp)
movsd %xmm4, 0x8(%rsp)
callq 0x3170
movsd 0x8(%rsp), %xmm4
movsd 0x10(%rsp), %xmm3
movsd 0x18(%rsp), %xmm2
cmpl $0x0, (%r15)
sete %al
jmp 0x68ee
xorl %r14d, %r14d
xorl %edx, %edx
jmp 0x6955
movb $0x1, %al
movsd 0x5e1a(%rip), %xmm0     # 0xc710
mulsd %xmm0, %xmm3
leaq 0xc3e7(%rip), %rcx      # 0x12ce8
movl (%rcx), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm1, %xmm3
divsd %xmm4, %xmm3
cvttsd2si %xmm3, %r14d
leal (%rcx,%rcx,4), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
addl %edx, %r14d
mulsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
divsd %xmm4, %xmm2
cvttsd2si %xmm2, %ebp
addl %edx, %ebp
testb %al, %al
jne 0x694f
leaq 0x6060(%rip), %rdi      # 0xc9a3
movl %r14d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3170
shlq $0x20, %r14
movl %ebp, %edx
movslq %ebx, %rax
imulq $0x51eb851f, %rax, %rax # imm = 0x51EB851F
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rax
addl %ecx, %eax
orq %r14, %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq