// File: /workspace/github_c_makefile_star2_O0/Matthias-Wandel[P]imgcomp/src/main.c
// Function: ProcessImage
static int ProcessImage(LastPic_t * New, int DeleteProcessed)
{
    LastPics[2] = LastPics[1];
    LastPics[1] = LastPics[0];

    LastPics[0] = *New;
    LastPics[0].IsMotion = LastPics[0].IsTimelapse = 0;

// if lights, or motion report, also do motion detect without fatigue.
// But DoMotionRun is called from parent function to do the lights.

    TriggerInfo_t Trig;
    TriggerInfo_t Trig_nf;
    Trig_nf.DiffLevel = Trig.DiffLevel = 0;
    TriggerInfo_t* Trig_nf_p = NULL;
    if (UdpDest[0] || lighton_run[0]){
        // Also need unfatigued motion detection for triggering stuff.
        Trig_nf_p = &Trig_nf;
    }


    if (LastPics[1].Image != NULL){
        // Handle timelapsing.
        if (TimelapseInterval >= 1){
            if (LastPics[0].mtime >= NextTimelapsePix){
                LastPics[0].IsTimelapse = 1;
            }

            // Figure out when the next timelapse interval should be.
            NextTimelapsePix = LastPics[0].mtime+TimelapseInterval;
            NextTimelapsePix -= (NextTimelapsePix % TimelapseInterval);
        }

        // compare with previous picture.
        Trig.DiffLevel = Trig.x = Trig.y = 0;

        int SkipFatigue = 0;
        if (FatigueSkipCount && ++FatigueSkipCountdown >= FatigueSkipCount){
            FatigueSkipCountdown = 0;
            SkipFatigue = 1;
        }

        if (LastPics[1].Image){
            Trig = ComparePix(LastPics[1].Image, LastPics[0].Image, 1, SkipFatigue, NULL, Trig_nf_p);
        }

        LastPics[0].DiffMag = Trig.DiffLevel;
        LastPics[0].IsSkipFatigue = SkipFatigue;

        if (FollowDir){
            // When real-time following, the timestamp is more useful than the file name
            char TimeString[10];
            strftime(TimeString, 10, "%H%M%S ", localtime(&LastPic_mtime));
            fputs(TimeString,Log);
        }else{
            fprintf(Log,"%s: ",LastPics[0].Name+LastPics[0].nind);
        }
        if (Trig.DiffLevel){
            fprintf(Log,"%4d", Trig.DiffLevel);
            if (Trig.DiffLevel*5 >= Sensitivity){
                fprintf(Log," (%4d,%4d)", Trig.x, Trig.y);
            }
            SinceMotionMs = 0;
        }

        if (LastPics[0].DiffMag >= Sensitivity){
            LastPics[0].IsMotion = 1;
        }

        if (SpuriousReject && LastPics[2].Image &&
            LastPics[0].IsMotion && LastPics[1].IsMotion
            && LastPics[2].DiffMag < Sensitivity/2){
            // Compare to picture before last picture.
            Trig = ComparePix(LastPics[2].Image, LastPics[0].Image, 0, 1, NULL, NULL);

            //printf("Diff with pix before last: %d\n",Trig.DiffLevel);
            if (Trig.DiffLevel < Sensitivity){
                // An event that was just one frame.  We assume this was something
                // spurious, like an insect or a rain drop or a camera glitch.
                printf(" (spurious %d, ignore)", Trig.DiffLevel);
                LastPics[0].IsMotion = 0;
                LastPics[1].IsMotion = 0;
            }
        }
        if (LastPics[0].IsMotion){
            fprintf(Log," (motion)");
            if (LastPics[0].IsSkipFatigue) fprintf(Log, " (sf)");
        }
        if (LastPics[0].IsTimelapse) fprintf(Log," (time)");


        if (SaveDir[0]){
            int KeepImage = 0;
            if (LastPics[2].IsTimelapse) KeepImage = 1;
            if (LastPics[1].IsMotion && PreMotionKeep) KeepImage |= 2;
            if (LastPics[2].IsMotion) KeepImage |= 4;
            if (SinceMotionPix <= PostMotionKeep) KeepImage |= 8;

            if (KeepImage){
                //printf(" (%s %d)",LastPics[2].Name, KeepImage);
                BackupImageFile(LastPics[2].Name, LastPics[2].DiffMag, 0);
            }
        }

        if (LastPics[2].IsMotion) SinceMotionPix = 0;

        fprintf(Log,"\n");
        SinceMotionPix += 1;


        if (UdpDest[0] && Trig_nf.DiffLevel >= Sensitivity){
            // Use un-fatigued diff level for reporting motion via UDP.
            GeometryConvert(&Trig_nf);

            printf("Send UDP motion %d,%d\n", Trig_nf.x, Trig_nf.y);

            SendUDP(Trig_nf.x, Trig_nf.y, Trig_nf.DiffLevel, Trig_nf.Motion);
        }

        Raspistill_restarted = 0;
    }

    if (LastPics[2].Image){
        // Third picture now falls out of the window.  Free it and delete it.
        free(LastPics[2].Image);
    }

    if (DeleteProcessed){
        unlink(LastPics[2].Name);
    }
    if (Trig_nf.DiffLevel >= Sensitivity || Trig.DiffLevel >= Sensitivity){
        // Return un-fatigued motion detection (if we have it) -- used for turning on lights.
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
leaq 0xfa87(%rip), %rsi      # 0x14380
movq %rsi, %rdi
addq $0x430, %rdi            # imm = 0x430
addq $0x218, %rsi            # imm = 0x218
movl $0x218, %edx            # imm = 0x218
callq 0x32c0
leaq 0xfa65(%rip), %rsi      # 0x14380
movq %rsi, %rdi
addq $0x218, %rdi            # imm = 0x218
movl $0x218, %edx            # imm = 0x218
callq 0x32c0
movq -0x10(%rbp), %rsi
leaq 0xfa46(%rip), %rdi      # 0x14380
movl $0x218, %edx            # imm = 0x218
callq 0x32c0
movl $0x0, 0xfc3e(%rip)      # 0x1458c
movl $0x0, 0xfc38(%rip)      # 0x14590
movl $0x0, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0x1092b(%rip), %rax     # 0x152a0
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x498c
leaq 0x1077c(%rip), %rax     # 0x15100
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x4994
leaq -0x34(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, 0xfbfc(%rip)      # 0x14598
je 0x4e01
leaq 0x1067f(%rip), %rax     # 0x15028
cmpl $0x1, (%rax)
jl 0x4a0a
movq 0xfbcb(%rip), %rax      # 0x14580
cmpq 0x10014(%rip), %rax     # 0x149d0
jl 0x49c8
movl $0x1, 0xfbc4(%rip)      # 0x1458c
movq 0xfbb1(%rip), %rax      # 0x14580
leaq 0x10652(%rip), %rcx     # 0x15028
movslq (%rcx), %rcx
addq %rcx, %rax
movq %rax, 0xffed(%rip)      # 0x149d0
movq 0xffe6(%rip), %rax      # 0x149d0
leaq 0x10637(%rip), %rcx     # 0x15028
movslq (%rcx), %rcx
cqto
idivq %rcx
movq 0xffd0(%rip), %rax      # 0x149d0
subq %rdx, %rax
movq %rax, 0xffc6(%rip)      # 0x149d0
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x44(%rbp)
leaq 0xffc7(%rip), %rax      # 0x149f4
cmpl $0x0, (%rax)
je 0x4a5d
movl 0xf928(%rip), %eax      # 0x14360
addl $0x1, %eax
movl %eax, 0xf91f(%rip)      # 0x14360
leaq 0xffac(%rip), %rcx      # 0x149f4
cmpl (%rcx), %eax
jl 0x4a5d
movl $0x0, 0xf90a(%rip)      # 0x14360
movl $0x1, -0x44(%rbp)
cmpq $0x0, 0xfb33(%rip)      # 0x14598
je 0x4aa3
movq 0xfb2a(%rip), %rdi      # 0x14598
movq 0xf90b(%rip), %rsi      # 0x14380
movl -0x44(%rbp), %ecx
movq -0x40(%rbp), %r9
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %r8d
callq 0x63a0
movq %rax, -0x54(%rbp)
movq %rdx, -0x4c(%rbp)
movq -0x54(%rbp), %rax
movq %rax, -0x24(%rbp)
movq -0x4c(%rbp), %rax
movq %rax, -0x1c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, 0xfadc(%rip)      # 0x14588
movl -0x44(%rbp), %eax
movl %eax, 0xfadf(%rip)      # 0x14594
leaq 0xff24(%rip), %rax      # 0x149e0
cmpl $0x0, (%rax)
je 0x4b02
leaq -0x5e(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq 0xf898(%rip), %rdi      # 0x14368
callq 0x3060
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl $0xa, %esi
leaq 0x9865(%rip), %rdx      # 0xe34d
callq 0x33f0
leaq -0x5e(%rbp), %rdi
leaq 0x10520(%rip), %rax     # 0x15018
movq (%rax), %rsi
callq 0x31a0
jmp 0x4b2f
leaq 0x1050f(%rip), %rax     # 0x15018
movq (%rax), %rdi
movslq 0xfa69(%rip), %rax      # 0x1457c
leaq 0xf866(%rip), %rdx      # 0x14380
addq $0x8, %rdx
addq %rax, %rdx
leaq 0x982d(%rip), %rsi      # 0xe355
movb $0x0, %al
callq 0x3290
cmpl $0x0, -0x24(%rbp)
je 0x4b87
leaq 0x104dc(%rip), %rax     # 0x15018
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x9811(%rip), %rsi      # 0xe35a
movb $0x0, %al
callq 0x3290
imull $0x5, -0x24(%rbp), %eax
leaq 0x104c5(%rip), %rcx     # 0x15020
cmpl (%rcx), %eax
jl 0x4b7d
leaq 0x104b2(%rip), %rax     # 0x15018
movq (%rax), %rdi
movl -0x20(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq 0x97e8(%rip), %rsi      # 0xe35e
movb $0x0, %al
callq 0x3290
movl $0x0, 0xfe41(%rip)      # 0x149c8
movl 0xf9fb(%rip), %eax      # 0x14588
leaq 0x1048c(%rip), %rcx     # 0x15020
cmpl (%rcx), %eax
jl 0x4ba2
movl $0x1, 0xf9ee(%rip)      # 0x14590
leaq 0xfe3b(%rip), %rax      # 0x149e4
cmpl $0x0, (%rax)
je 0x4c72
cmpq $0x0, 0xfbf6(%rip)      # 0x147b0
je 0x4c72
cmpl $0x0, 0xf9c9(%rip)      # 0x14590
je 0x4c72
cmpl $0x0, 0xfbd4(%rip)      # 0x147a8
je 0x4c72
movl 0xfdd8(%rip), %eax      # 0x149b8
movl %eax, -0x84(%rbp)
leaq 0x10433(%rip), %rax     # 0x15020
movl (%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x84(%rbp), %eax
cmpl %ecx, %eax
jge 0x4c72
movq 0xfba6(%rip), %rdi      # 0x147b0
movq 0xf76f(%rip), %rsi      # 0x14380
xorl %edx, %edx
movl $0x1, %ecx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x63a0
movq %rax, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x24(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x1c(%rbp)
movl -0x24(%rbp), %eax
leaq 0x103d9(%rip), %rcx     # 0x15020
cmpl (%rcx), %eax
jge 0x4c70
movl -0x24(%rbp), %esi
leaq 0x9714(%rip), %rdi      # 0xe369
movb $0x0, %al
callq 0x3170
movl $0x0, 0xf92a(%rip)      # 0x14590
movl $0x0, 0xfb38(%rip)      # 0x147a8
jmp 0x4c72
cmpl $0x0, 0xf917(%rip)      # 0x14590
je 0x4cb6
leaq 0x10396(%rip), %rax     # 0x15018
movq (%rax), %rdi
leaq 0x96f4(%rip), %rsi      # 0xe380
movb $0x0, %al
callq 0x3290
cmpl $0x0, 0xf8fa(%rip)      # 0x14594
je 0x4cb4
leaq 0x10375(%rip), %rax     # 0x15018
movq (%rax), %rdi
leaq 0x96dd(%rip), %rsi      # 0xe38a
movb $0x0, %al
callq 0x3290
jmp 0x4cb6
cmpl $0x0, 0xf8cf(%rip)      # 0x1458c
je 0x4cd7
leaq 0x10352(%rip), %rax     # 0x15018
movq (%rax), %rdi
leaq 0x96c0(%rip), %rsi      # 0xe390
movb $0x0, %al
callq 0x3290
leaq 0xfe02(%rip), %rax      # 0x14ae0
cmpb $0x0, (%rax)
je 0x4d6f
movl $0x0, -0x74(%rbp)
cmpl $0x0, 0xfcc7(%rip)      # 0x149bc
je 0x4cfe
movl $0x1, -0x74(%rbp)
cmpl $0x0, 0xfaa3(%rip)      # 0x147a8
je 0x4d1c
leaq 0xfcde(%rip), %rax      # 0x149ec
cmpl $0x0, (%rax)
je 0x4d1c
movl -0x74(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x74(%rbp)
cmpl $0x0, 0xfc9d(%rip)      # 0x149c0
je 0x4d2e
movl -0x74(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x74(%rbp)
movl 0xf56c(%rip), %eax      # 0x142a0
leaq 0xfcad(%rip), %rcx      # 0x149e8
cmpl (%rcx), %eax
jg 0x4d48
movl -0x74(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x4d6d
movl 0xfc64(%rip), %esi      # 0x149b8
leaq 0xf625(%rip), %rdi      # 0x14380
addq $0x430, %rdi            # imm = 0x430
addq $0x8, %rdi
xorl %edx, %edx
callq 0xc020
jmp 0x4d6f
cmpl $0x0, 0xfc4a(%rip)      # 0x149c0
je 0x4d82
movl $0x0, 0xf51e(%rip)      # 0x142a0
leaq 0x1028f(%rip), %rax     # 0x15018
movq (%rax), %rdi
leaq 0x9428(%rip), %rsi      # 0xe1bb
movb $0x0, %al
callq 0x3290
movl 0xf500(%rip), %eax      # 0x142a0
addl $0x1, %eax
movl %eax, 0xf4f7(%rip)      # 0x142a0
leaq 0x104f0(%rip), %rax     # 0x152a0
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x4df4
movl -0x34(%rbp), %eax
leaq 0x1025e(%rip), %rcx     # 0x15020
cmpl (%rcx), %eax
jl 0x4df4
leaq -0x34(%rbp), %rdi
callq 0x4e70
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
leaq 0x95bc(%rip), %rdi      # 0xe398
movb $0x0, %al
callq 0x3170
movl -0x30(%rbp), %edi
movl -0x2c(%rbp), %esi
movl -0x34(%rbp), %edx
movl -0x28(%rbp), %ecx
callq 0xca10
leaq 0x10229(%rip), %rax     # 0x15024
movl $0x0, (%rax)
cmpq $0x0, 0xf9a7(%rip)      # 0x147b0
je 0x4e17
movq 0xf99e(%rip), %rdi      # 0x147b0
callq 0x3040
cmpl $0x0, -0x14(%rbp)
je 0x4e34
leaq 0xf55c(%rip), %rdi      # 0x14380
addq $0x430, %rdi            # imm = 0x430
addq $0x8, %rdi
callq 0x3080
movl -0x34(%rbp), %eax
leaq 0x101e2(%rip), %rcx     # 0x15020
cmpl (%rcx), %eax
jge 0x4e50
movl -0x24(%rbp), %eax
leaq 0x101d4(%rip), %rcx     # 0x15020
cmpl (%rcx), %eax
jl 0x4e59
movl $0x1, -0x4(%rbp)
jmp 0x4e60
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)