// File: /workspace/github_c_makefile_star2_O0/Matthias-Wandel[P]imgcomp/src/compare.c
// Function: AnalyzeDifferences
static TriggerInfo_t AnalyzeDifferences(Region_t Region, int threshold, int UpdateFatigue, int SkipFatigue,
        TriggerInfo_t * no_fatigue_motion // In addition detect motion without fatigue and put it here.
    )
{
    static int widthSc, heightSc;
    static ImgMap_t * DiffScaled = NULL;
    static ImgMap_t * Fatigue = NULL;
    static ImgMap_t * FatigueBl = NULL;

    // Allocate working arrays if necessary.
    if (DiffScaled == NULL){
        #define ROOF_SC(x) ((x+scalef-1)/scalef)
        widthSc = ROOF_SC(DiffVal->w);
        heightSc = ROOF_SC(DiffVal->h);
        DiffScaled = MakeImgMap(widthSc, heightSc);
        Fatigue = MakeImgMap(widthSc, heightSc);
        FatigueBl = MakeImgMap(widthSc, heightSc);
    }else{
        if (widthSc != ROOF_SC(DiffVal->w) || heightSc != ROOF_SC(DiffVal->h)){
            fprintf(stderr, "image size changed, error!");
            // Could reallocate, but probably other code doesn't handle resizing anyway.
            exit(-1);
        }
    }

    // Compute scaled down array of differences.  Destination: DiffScaled[]
    int width = DiffVal->w;
    memset(DiffScaled->values, 0, sizeof(int)*widthSc*heightSc);
    for (int row=Region.y1;row<Region.y2;row++){
        // Compute difference by column using established threshold value
        int * diffrow, *ExRow;
        int * widthScrow;
        diffrow = &DiffVal->values[width*row];
        ExRow = &WeightMap->values[width*row];

        widthScrow = &DiffScaled->values[widthSc*(row/scalef)];
        for (int col=Region.x1;col<Region.x2;col++){
            int d = diffrow[col] - threshold;
            if (d > 0){
                widthScrow[col/scalef] += d;
                if (ExRow[col] > 1){

                    // Double weight region
                    widthScrow[col/scalef] += d;
                }
            }
        }
    }


    if (Verbosity > 1){
        printf("Scaled difference array (%d x %d)\n", widthSc, heightSc);
        ShowImgMap(DiffScaled, 100);
    }

    if (MotionFatigueTc == 0){
        // No motion fatigure applid, both returns are identical.
        TriggerInfo_t retval = LocateMotion(DiffScaled);
        if (no_fatigue_motion) *no_fatigue_motion = retval;
        return retval;
    }else{
        if (no_fatigue_motion){
            // Additional detection prior to motion fatigue.
            *no_fatigue_motion = LocateMotion(DiffScaled);
        }
    }

    if (SkipFatigue == 0){
        // Make a bloomed copy of the fatigue map *before* applying current image fatigue.
        // Blooming the map is using the max of the cell and it's eight neighbours for each cell.
        BloomImgMap(Fatigue, FatigueBl);
        // Possibly bloom the fatigue map a bit more (may or may not want that)
        //BloomImgMap(FatigueBl, FatigueBl2); // Use max of cell and eight neighbours for motion fatigue.
    }

    if (UpdateFatigue){
        static int SinceFatiguePrint = 0;
        int FatigueAverage;
        // Compute motion fatigue
        FatigueAverage = 0;
        for (int row=0;row<heightSc;row++){
            for (int col=0;col<widthSc;col++){
                int ds, nFat;
                ds = DiffScaled->values[row*widthSc+col];
                nFat = (Fatigue->values[row*widthSc+col]*(MotionFatigueTc-1) + ds)/MotionFatigueTc; // Expontential decay on it.
                Fatigue->values[row*widthSc+col] = nFat;
                FatigueAverage += nFat;
            }
        }
        FatigueAverage = FatigueAverage/(heightSc*widthSc); // Divide by array size to get average.

        // Print fatigue map to log from time to time.
        if (Verbosity > 1 || (FatigueAverage > 50 && SinceFatiguePrint > 60)){
            // Print the fatigure array every minuts if there is stuff in it.
            fprintf(Log, "Fatigue map (%d x %d) sum=%d<small>\n", widthSc, heightSc, FatigueAverage);
            ShowImgMap(Fatigue, 50);
            fprintf(Log, "</small>\n");
            SinceFatiguePrint = 0;
        }
        SinceFatiguePrint++;
    }
 
    if (SkipFatigue == 0){
        // Subtract out motion fatigue
        int fatmult = FatigueGainPercent * 3 * 256 / 100;
        for (int row=0;row<heightSc;row++){
            for (int col=0;col<widthSc;col++){
                int FatSub = (FatigueBl->values[row*widthSc+col] * fatmult) >> 8;
                int ds = DiffScaled->values[row*widthSc+col] - FatSub;
                if (ds < 0) ds = 0;
                DiffScaled->values[row*widthSc+col] = ds;
            }
        }
    }


    TriggerInfo_t retval = LocateMotion(DiffScaled);

    return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x20(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, 0xe3bf(%rip)      # 0x15488
jne 0x7157
movq 0xe39a(%rip), %rax      # 0x15470
movl (%rax), %eax
addl $0x5, %eax
subl $0x1, %eax
movl $0x5, %ecx
cltd
idivl %ecx
movl %eax, 0xe390(%rip)      # 0x1547c
movq 0xe37d(%rip), %rax      # 0x15470
movl 0x4(%rax), %eax
addl $0x5, %eax
subl $0x1, %eax
movl $0x5, %ecx
cltd
idivl %ecx
movl %eax, 0xe376(%rip)      # 0x15480
movl 0xe36c(%rip), %edi      # 0x1547c
movl 0xe36a(%rip), %esi      # 0x15480
callq 0x7ff0
movq %rax, 0xe366(%rip)      # 0x15488
movl 0xe354(%rip), %edi      # 0x1547c
movl 0xe352(%rip), %esi      # 0x15480
callq 0x7ff0
movq %rax, 0xe356(%rip)      # 0x15490
movl 0xe33c(%rip), %edi      # 0x1547c
movl 0xe33a(%rip), %esi      # 0x15480
callq 0x7ff0
movq %rax, 0xe346(%rip)      # 0x15498
jmp 0x71da
movl 0xe31f(%rip), %eax      # 0x1547c
movl %eax, -0x9c(%rbp)
movq 0xe306(%rip), %rax      # 0x15470
movl (%rax), %eax
addl $0x5, %eax
subl $0x1, %eax
movl $0x5, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x9c(%rbp), %eax
cmpl %ecx, %eax
jne 0x71b6
movl 0xe2f4(%rip), %eax      # 0x15480
movl %eax, -0xa0(%rbp)
movq 0xe2d7(%rip), %rax      # 0x15470
movl 0x4(%rax), %eax
addl $0x5, %eax
subl $0x1, %eax
movl $0x5, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xa0(%rbp), %eax
cmpl %ecx, %eax
je 0x71d8
movq 0xce33(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x86fe(%rip), %rsi      # 0xf8c5
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
jmp 0x71da
movq 0xe28f(%rip), %rax      # 0x15470
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq 0xe29b(%rip), %rdi      # 0x15488
addq $0x8, %rdi
movslq 0xe284(%rip), %rdx      # 0x1547c
shlq $0x2, %rdx
movslq 0xe27d(%rip), %rax      # 0x15480
imulq %rax, %rdx
xorl %esi, %esi
callq 0x31b0
movl -0x18(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7364
movq 0xe249(%rip), %rax      # 0x15470
addq $0x8, %rax
movl -0x3c(%rbp), %ecx
imull -0x40(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq 0xe221(%rip), %rax      # 0x15468
addq $0x8, %rax
movl -0x3c(%rbp), %ecx
imull -0x40(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq 0xe221(%rip), %rax      # 0x15488
addq $0x8, %rax
movq %rax, -0xa8(%rbp)
movl 0xe204(%rip), %ecx      # 0x1547c
movl -0x40(%rbp), %eax
movl $0x5, %esi
cltd
idivl %esi
movl %eax, %edx
movq -0xa8(%rbp), %rax
imull %edx, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x7354
movq -0x48(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
subl -0x24(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jle 0x7344
movl -0x60(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x5c(%rbp), %eax
movl $0x5, %ecx
cltd
idivl %ecx
movl -0xb4(%rbp), %edx
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
cmpl $0x1, (%rax,%rcx,4)
jle 0x7342
movl -0x60(%rbp), %eax
movl %eax, -0xc4(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0x5c(%rbp), %eax
movl $0x5, %ecx
cltd
idivl %ecx
movl -0xc4(%rbp), %edx
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movslq %ecx, %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x7344
jmp 0x7346
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x72a3
jmp 0x7356
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x7214
leaq 0xd68d(%rip), %rax      # 0x149f8
cmpl $0x1, (%rax)
jle 0x739b
movl 0xe106(%rip), %esi      # 0x1547c
movl 0xe104(%rip), %edx      # 0x15480
leaq 0x855d(%rip), %rdi      # 0xf8e0
movb $0x0, %al
callq 0x3170
movq 0xe0f7(%rip), %rdi      # 0x15488
movl $0x64, %esi
callq 0x8060
leaq 0xcf02(%rip), %rax      # 0x142a4
cmpl $0x0, (%rax)
jne 0x73da
movq 0xe0da(%rip), %rdi      # 0x15488
callq 0x7700
movq %rax, -0x10(%rbp)
movq %rdx, -0x8(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x73d5
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x76e7
cmpq $0x0, -0x38(%rbp)
je 0x7419
movq -0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq 0xe095(%rip), %rdi      # 0x15488
callq 0x7700
movq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
movq -0x70(%rbp), %rcx
movq %rcx, (%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x741b
cmpl $0x0, -0x2c(%rbp)
jne 0x7434
movq 0xe068(%rip), %rdi      # 0x15490
movq 0xe069(%rip), %rsi      # 0x15498
callq 0x8260
cmpl $0x0, -0x28(%rbp)
je 0x75b2
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl 0xe02b(%rip), %eax      # 0x15480
jge 0x7517
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl 0xe011(%rip), %eax      # 0x1547c
jge 0x7507
movq 0xe010(%rip), %rax      # 0x15488
movl -0x78(%rbp), %ecx
imull 0xdffa(%rip), %ecx      # 0x1547c
addl -0x7c(%rbp), %ecx
movslq %ecx, %rcx
movl 0x8(%rax,%rcx,4), %eax
movl %eax, -0x80(%rbp)
movq 0xdffa(%rip), %rax      # 0x15490
movl -0x78(%rbp), %ecx
imull 0xdfdc(%rip), %ecx      # 0x1547c
addl -0x7c(%rbp), %ecx
movslq %ecx, %rcx
movl 0x8(%rax,%rcx,4), %eax
leaq 0xcdf3(%rip), %rcx      # 0x142a4
movl (%rcx), %ecx
subl $0x1, %ecx
imull %ecx, %eax
addl -0x80(%rbp), %eax
leaq 0xcde1(%rip), %rcx      # 0x142a4
cltd
idivl (%rcx)
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %edx
movq 0xdfb7(%rip), %rax      # 0x15490
movl -0x78(%rbp), %ecx
imull 0xdf99(%rip), %ecx      # 0x1547c
addl -0x7c(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, 0x8(%rax,%rcx,4)
movl -0x84(%rbp), %eax
addl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x7462
jmp 0x7509
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x744c
movl -0x74(%rbp), %eax
movl 0xdf60(%rip), %ecx      # 0x15480
imull 0xdf55(%rip), %ecx      # 0x1547c
cltd
idivl %ecx
movl %eax, -0x74(%rbp)
leaq 0xd4c4(%rip), %rax      # 0x149f8
cmpl $0x1, (%rax)
jg 0x7548
cmpl $0x32, -0x74(%rbp)
jle 0x75a3
cmpl $0x3c, 0xdf5a(%rip)     # 0x154a0
jle 0x75a3
leaq 0xdac9(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl 0xdf24(%rip), %edx      # 0x1547c
movl 0xdf22(%rip), %ecx      # 0x15480
movl -0x74(%rbp), %r8d
leaq 0x839a(%rip), %rsi      # 0xf903
movb $0x0, %al
callq 0x3290
movq 0xdf19(%rip), %rdi      # 0x15490
movl $0x32, %esi
callq 0x8060
leaq 0xda90(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x8396(%rip), %rsi      # 0xf928
movb $0x0, %al
callq 0x3290
movl $0x0, 0xdefd(%rip)      # 0x154a0
movl 0xdef7(%rip), %eax      # 0x154a0
addl $0x1, %eax
movl %eax, 0xdeee(%rip)      # 0x154a0
cmpl $0x0, -0x2c(%rbp)
jne 0x76d3
leaq 0xcce5(%rip), %rax      # 0x142a8
imull $0x3, (%rax), %eax
shll $0x8, %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %eax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl 0xde93(%rip), %eax      # 0x15480
jge 0x76d1
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl 0xde73(%rip), %eax      # 0x1547c
jge 0x76bb
movq 0xde82(%rip), %rax      # 0x15498
movl -0x8c(%rbp), %ecx
imull 0xde59(%rip), %ecx      # 0x1547c
addl -0x90(%rbp), %ecx
movslq %ecx, %rcx
movl 0x8(%rax,%rcx,4), %eax
imull -0x88(%rbp), %eax
sarl $0x8, %eax
movl %eax, -0x94(%rbp)
movq 0xde41(%rip), %rax      # 0x15488
movl -0x8c(%rbp), %ecx
imull 0xde28(%rip), %ecx      # 0x1547c
addl -0x90(%rbp), %ecx
movslq %ecx, %rcx
movl 0x8(%rax,%rcx,4), %eax
subl -0x94(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
jge 0x7680
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %edx
movq 0xddfb(%rip), %rax      # 0x15488
movl -0x8c(%rbp), %ecx
imull 0xdde2(%rip), %ecx      # 0x1547c
addl -0x90(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, 0x8(%rax,%rcx,4)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x75fd
jmp 0x76bd
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x75e1
jmp 0x76d3
movq 0xddae(%rip), %rdi      # 0x15488
callq 0x7700
movq %rax, -0x10(%rbp)
movq %rdx, -0x8(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax,%rax)