// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/main.c
// Function: DoDirectoryFunc
static int DoDirectoryFunc(char * Directory, int DeleteProcessed)
{
    DirEntry_t * FileNames;
    int NumEntries;
    int a;
    int SawMotion;

    SawMotion = 0;

    FileNames = GetSortedDir(Directory, &NumEntries);
    if (FileNames == NULL) return 0;
    if (NumEntries == 0) return 0;

    NumProcessed = 0;

    for (a=0;a<NumEntries;a++){
        LastPic_t NewPic;
        char * ThisName;
        int l;
        time_t now = time(NULL);

        // Check that name ends in ".jpg", ".jpeg"
        ThisName = FileNames[a].FileName;
        if (ThisName[0] == 0) continue; // We already did this one.

        if (strcmp(ThisName, "angle") == 0){
            // For my hack of panning the camera with a stepper motor.
            AngleAdjusted = 1;
        }

        l = strlen(ThisName);
        if (l < 5) continue;

        if (strcmp(FileNames[a].FileName+l-4, ".jpg") != 0 &&
                strcmp(ThisName+l-5, ".jpeg") != 0){


            if (strcmp(ThisName+l-5, ".jpg~") == 0){
                // Raspistill may leave files ending with '~' around if it was killed
                // at just the right time.  Remove these files.
                struct stat statbuf;
                char * cpn = CatPath(Directory, ThisName);
                if (stat(cpn, &statbuf) == -1) {
                    perror(ThisName);
                    continue;
                }
                if (now-statbuf.st_mtime > 5){
                    fprintf(Log, "rm temp file: %s\n",cpn);
                    unlink(cpn);
                }
            }
            continue;
        }

        strcpy(NewPic.Name, CatPath(Directory, ThisName));
        NewPic.nind = strlen(Directory)+1;


        if (strcmp(LastPics[0].Name+LastPics[0].nind, ThisName) == 0
             || strcmp(LastPics[1].Name+LastPics[1].nind, ThisName) == 0){
            // Already did this one.
            continue;
        }

        if (AngleAdjusted){
            char PanMessage[200];
            FILE * PanFile;
            char * FileName = CatPath(Directory, "angle");
            PanFile = fopen(FileName,"r");
            PanMessage[fread(PanMessage, 1, 199, PanFile)] = 0;
            fprintf(Log,"Pan: %s",PanMessage);
            fclose(PanFile);
            unlink (FileName);
            if (DeleteProcessed){
                unlink(NewPic.Name); //Not using this image, cause it's panned.
                FileNames[a].FileName[0] = 0;
                AngleAdjusted = 0;
            }
            // Should also reset the motion fatigue map if it's in use, cause everything has now moved.
            continue;
        }

        //printf("use: %s\n",ThisName);

        NewPic.Image = LoadJPEG(NewPic.Name, ScaleDenom, 0, 1);
        if (NewPic.Image == NULL){
            fprintf(Log, "Failed to load %s\n",NewPic.Name);
            if (DeleteProcessed){
                // Raspberry pi timelapse mode may at times dump a corrupt
                // picture at the end of timelapse mode.  Just delete and go on.
                unlink(NewPic.Name);
            }
            continue;
        }

        if (ThisName[0] == 's' && ThisName[1] == 'f' && ThisName[2] >= '0' && ThisName[2] <= '9'){
            // Video decomposed files have no meaningful timestamp,
            // but filename starts with 'sf' and contains unix time minus 1 billion.
            NewPic.mtime = atoi(ThisName+2) + (time_t)1000000000;
        }else{
            struct stat statbuf;
            if (stat(NewPic.Name, &statbuf) == -1) {
                perror(NewPic.Name);
                exit(1);
            }
            NewPic.mtime = (unsigned)statbuf.st_mtime;
        }
        LastPic_mtime = NewPic.mtime;

        if (ExposureManagementOn && FollowDir && a == NumEntries-1 && now-NewPic.mtime <= 1){
            // Latest image of batch.
            // Check exposure before comparison, because we may want to restart raspistill ASAP.
            int d = CalcExposureAdjust(NewPic.Image);
            if (d){
                //fprintf(Log,"Restart raspistill for exposure adjust\n");
                relaunch_camera_prog();
            }
        }

        SawMotion += ProcessImage(&NewPic, DeleteProcessed);
        NumProcessed += 1;
    }

    FreeDir(FileNames, NumEntries); // Free up the whole directory structure.
    FileNames = NULL;

    SinceMotionMs += 1000;

    return SawMotion;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movl %esi, 0x4(%rsp)
leaq 0xc(%rsp), %rbx
movq %rdi, 0x10(%rsp)
movq %rbx, %rsi
callq 0x9ab1
movq %rax, %rdi
testq %rax, %rax
sete %al
movl (%rbx), %esi
testl %esi, %esi
sete %cl
xorl %ebx, %ebx
orb %al, %cl
jne 0x44b8
movl $0x0, 0xe6af(%rip)      # 0x12304
testl %esi, %esi
jle 0x44a7
leaq 0x108(%rsp), %r15
movq %rdi, 0x18(%rsp)
leaq 0x1a(%rdi), %r12
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
xorl %edi, %edi
callq 0x3310
cmpb $0x0, -0x2(%r12)
je 0x3e90
movq %rax, %rbp
movq %r15, %r14
leaq -0x2(%r12), %rbx
movq %rbx, %rdi
leaq 0x765d(%rip), %rsi      # 0xb2fe
callq 0x3260
testl %eax, %eax
jne 0x3cb1
movb $0x1, 0xe65f(%rip)      # 0x12310
movq %rbx, %rdi
callq 0x3130
movq %rax, %r15
cmpl $0x5, %r15d
jge 0x3cca
movq %r14, %r15
jmp 0x3e90
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%r12,%r15), %rdi
addq $-0x6, %rdi
leaq 0x747d(%rip), %rsi      # 0xb15d
callq 0x3260
testl %eax, %eax
je 0x3d7a
addq %r12, %r15
addq $-0x7, %r15
movq %r15, %rdi
leaq 0x7606(%rip), %rsi      # 0xb304
callq 0x3260
testl %eax, %eax
je 0x3d7a
movq %r15, %rdi
leaq 0x75f9(%rip), %rsi      # 0xb30a
callq 0x3260
testl %eax, %eax
jne 0x3cc2
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x9828
movq %rax, %r15
movq %rax, %rdi
leaq 0x30(%rsp), %rsi
callq 0x32c0
cmpl $-0x1, %eax
je 0x4264
subq 0x88(%rsp), %rbp
cmpq $0x6, %rbp
jl 0x3cc2
leaq 0xf25f(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x75ad(%rip), %rsi      # 0xb310
movq %r15, %rdx
xorl %eax, %eax
callq 0x32a0
movq %r15, %rdi
callq 0x3080
jmp 0x3cc2
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x9828
movq %r14, %rdi
movq %rax, %rsi
callq 0x30a0
movq %r15, %rdi
callq 0x3130
incl %eax
movl %eax, 0x2fc(%rsp)
movslq 0xe76f(%rip), %rax      # 0x1251c
leaq 0xe56c(%rip), %r15      # 0x12320
leaq (%rax,%r15), %rdi
addq $0x8, %rdi
movq %rbx, %rsi
callq 0x3260
testl %eax, %eax
je 0x3e88
movslq 0xe961(%rip), %rax      # 0x12734
leaq (%rax,%r15), %rdi
addq $0x220, %rdi            # imm = 0x220
movq %rbx, %rsi
callq 0x3260
testl %eax, %eax
je 0x3e88
cmpb $0x1, 0xe51b(%rip)      # 0x12310
jne 0x3eaa
movq 0x10(%rsp), %rdi
leaq 0x74f7(%rip), %rsi      # 0xb2fe
callq 0x9828
movq %rax, %rbp
movq %rax, %rdi
leaq 0x99c8(%rip), %rsi      # 0xd7e1
callq 0x3430
movq %rax, %r15
movl $0x1, %esi
movl $0xc7, %edx
leaq 0x30(%rsp), %rdi
movq %rax, %rcx
callq 0x30e0
movb $0x0, 0x30(%rsp,%rax)
leaq 0xf174(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x74d4(%rip), %rsi      # 0xb322
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x32a0
movq %r15, %rdi
movq %r14, %r15
callq 0x3110
movq %rbp, %rdi
callq 0x3080
cmpl $0x0, 0x4(%rsp)
je 0x3e90
movq %r15, %rdi
callq 0x3080
xorl %eax, %eax
movb %al, (%rbx)
movb %al, 0xe48a(%rip)       # 0x12310
jmp 0x3e90
leaq 0x108(%rsp), %r15
incq %r13
movslq 0xc(%rsp), %rsi
addq $0x50, %r12
cmpq %rsi, %r13
jl 0x3c79
jmp 0x449c
leaq 0xee37(%rip), %rax      # 0x12ce8
movl (%rax), %esi
leaq 0x108(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x753f
movq %rax, 0x100(%rsp)
testq %rax, %rax
je 0x4083
cmpb $0x73, (%rbx)
jne 0x3f13
cmpb $0x66, -0x1(%r12)
jne 0x3f13
movb (%r12), %al
addb $-0x30, %al
cmpb $0x9, %al
ja 0x3f13
movq %r12, %rdi
callq 0x3460
cltq
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movq %rax, 0x300(%rsp)
leaq 0xe40f(%rip), %r15      # 0x12320
jmp 0x3f44
leaq 0x108(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x32c0
cmpl $-0x1, %eax
leaq 0xe3f1(%rip), %r15      # 0x12320
je 0x44cc
movl 0x88(%rsp), %eax
movq %rax, 0x300(%rsp)
movq %rax, 0xe3bd(%rip)      # 0x12308
leaq 0xea3e(%rip), %rcx      # 0x12990
cmpl $0x0, (%rcx)
je 0x3f8d
leaq 0xea22(%rip), %rcx      # 0x12980
cmpl $0x0, (%rcx)
je 0x3f8d
movl 0xc(%rsp), %ecx
decl %ecx
cmpq %rcx, %r13
jne 0x3f8d
subq %rax, %rbp
cmpq $0x1, %rbp
jg 0x3f8d
movq 0x100(%rsp), %rdi
callq 0xa812
testl %eax, %eax
je 0x3f8d
callq 0x90cc
movl $0x218, %edx            # imm = 0x218
leaq 0xe7b7(%rip), %rdi      # 0x12750
leaq 0xe598(%rip), %rbx      # 0x12538
movq %rbx, %rsi
callq 0x32d0
movl $0x218, %edx            # imm = 0x218
movq %rbx, %rdi
movq %r15, %rsi
callq 0x32d0
movl $0x218, %edx            # imm = 0x218
movq %r15, %rdi
leaq 0x100(%rsp), %rsi
callq 0x32d0
xorl %ebp, %ebp
movq %rbp, 0xe556(%rip)      # 0x1252c
movl $0x0, 0x30(%rsp)
leaq 0xf0bb(%rip), %rax      # 0x130a0
movb (%rax), %al
leaq 0xf252(%rip), %rcx      # 0x13240
orb (%rcx), %al
movl $0x0, %r9d
leaq 0x30(%rsp), %rax
cmovneq %rax, %r9
movq 0xe532(%rip), %rdi      # 0x12538
testq %rdi, %rdi
leaq 0x108(%rsp), %r15
je 0x444f
leaq 0xefaa(%rip), %rax      # 0x12fc8
movslq (%rax), %rsi
testq %rsi, %rsi
jle 0x4055
movq 0xe4f3(%rip), %rcx      # 0x12520
cmpq 0xe93c(%rip), %rcx      # 0x12970
jl 0x4040
movl $0x1, 0xe4ec(%rip)      # 0x1252c
addq %rsi, %rcx
movq %rcx, %rax
cqto
idivq %rsi
subq %rdx, %rcx
movq %rcx, 0xe91b(%rip)      # 0x12970
leaq 0xe938(%rip), %rax      # 0x12994
movl (%rax), %eax
testl %eax, %eax
je 0x40b6
movl 0xe298(%rip), %ecx      # 0x12300
incl %ecx
xorl %r15d, %r15d
cmpl %eax, %ecx
setge %r15b
movl $0x0, %eax
cmovgel %eax, %ecx
movl %ecx, 0xe27f(%rip)      # 0x12300
jmp 0x40b9
leaq 0xef2e(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x7296(%rip), %rsi      # 0xb32a
movq %r15, %rdx
xorl %eax, %eax
callq 0x32a0
cmpl $0x0, 0x4(%rsp)
je 0x3e90
movq %r15, %rdi
callq 0x3080
jmp 0x3e90
xorl %r15d, %r15d
movq 0xe260(%rip), %rsi      # 0x12320
movl $0x1, %edx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x5b88
movq %rax, %rbp
movq %rdx, %rbx
movl %ebp, 0xe44c(%rip)      # 0x12528
movl %r15d, 0xe451(%rip)     # 0x12534
leaq 0xe896(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x412b
leaq 0xe212(%rip), %rdi      # 0x12308
callq 0x3060
movl $0xa, %esi
leaq 0x26(%rsp), %r15
movq %r15, %rdi
leaq 0x722e(%rip), %rdx      # 0xb33d
movq %rax, %rcx
callq 0x3400
leaq 0xee9a(%rip), %rax      # 0x12fb8
movq (%rax), %rsi
movq %r15, %rdi
callq 0x31a0
jmp 0x4159
leaq 0xee86(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movslq 0xe3e0(%rip), %rax      # 0x1251c
leaq 0xe1dd(%rip), %rcx      # 0x12320
leaq (%rax,%rcx), %rdx
addq $0x8, %rdx
leaq 0x71f3(%rip), %rsi      # 0xb345
xorl %eax, %eax
callq 0x32a0
movq %r14, %r15
testl %ebp, %ebp
leaq 0xee53(%rip), %rdx      # 0x12fb8
je 0x41bb
movq (%rdx), %rdi
leaq 0x71d9(%rip), %rsi      # 0xb34a
movl %ebp, %edx
xorl %eax, %eax
callq 0x32a0
leal (%rbp,%rbp,4), %eax
leaq 0xee3b(%rip), %rcx      # 0x12fc0
cmpl (%rcx), %eax
jl 0x41aa
leaq 0xee28(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movq %rbp, %rdx
shrq $0x20, %rdx
leaq 0x71ad(%rip), %rsi      # 0xb34e
movl %ebx, %ecx
xorl %eax, %eax
callq 0x32a0
movl $0x0, 0xe7b4(%rip)      # 0x12968
leaq 0xedfd(%rip), %rdx      # 0x12fb8
leaq 0xedfe(%rip), %rax      # 0x12fc0
movl (%rax), %ecx
cmpl %ecx, 0xe35e(%rip)      # 0x12528
jge 0x41d4
movl 0xe35e(%rip), %eax      # 0x12530
jmp 0x41e3
movl $0x1, 0xe352(%rip)      # 0x12530
movl $0x1, %eax
leaq 0xe79a(%rip), %rsi      # 0x12984
cmpl $0x0, (%rsi)
je 0x427e
movq 0xe556(%rip), %rdi      # 0x12750
testq %rdi, %rdi
je 0x427e
testl %eax, %eax
je 0x427e
cmpl $0x0, 0xe53e(%rip)      # 0x12748
je 0x427e
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
cmpl %eax, 0xe73d(%rip)      # 0x12958
jge 0x4282
movq 0xe0fc(%rip), %rsi      # 0x12320
xorl %edx, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x5b88
movq %rax, %rbp
leaq 0xed80(%rip), %rax      # 0x12fc0
cmpl %ebp, (%rax)
jle 0x4271
leaq 0x710e(%rip), %rdi      # 0xb359
movl %ebp, %esi
xorl %eax, %eax
callq 0x3170
xorl %eax, %eax
movl %eax, 0xe2d4(%rip)      # 0x12530
movl %eax, 0xe4e6(%rip)      # 0x12748
jmp 0x42c0
movq %rbx, %rdi
callq 0x3440
jmp 0x3cc2
movl 0xe2b9(%rip), %eax      # 0x12530
leaq 0xed3a(%rip), %rdx      # 0x12fb8
testl %eax, %eax
je 0x42c0
movq (%rdx), %rcx
movq %rdx, %rbx
movl $0x9, %esi
movl $0x1, %edx
leaq 0x70d7(%rip), %rdi      # 0xb370
callq 0x34a0
cmpl $0x0, 0xe28f(%rip)      # 0x12534
je 0x42c0
movq (%rbx), %rcx
movl $0x5, %esi
movl $0x1, %edx
leaq 0x70bf(%rip), %rdi      # 0xb37a
callq 0x34a0
cmpl $0x0, 0xe265(%rip)      # 0x1252c
je 0x42e9
leaq 0xece8(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
movl $0x7, %esi
movl $0x1, %edx
leaq 0x709c(%rip), %rdi      # 0xb380
callq 0x34a0
leaq 0xe790(%rip), %rax      # 0x12a80
cmpb $0x0, (%rax)
movl 0xe667(%rip), %eax      # 0x12960
je 0x4355
cmpl $0x0, 0xe446(%rip)      # 0x12748
sete %cl
leaq 0xe680(%rip), %rdx      # 0x1298c
cmpl $0x0, (%rdx)
sete %dl
movl 0xdf90(%rip), %esi      # 0x122a8
leaq 0xe669(%rip), %rdi      # 0x12988
cmpl (%rdi), %esi
leaq 0xec90(%rip), %rsi      # 0x12fb8
jle 0x433b
testl %eax, %eax
jne 0x433b
orb %dl, %cl
je 0x433b
cmpl $0x0, 0xe623(%rip)      # 0x1295c
je 0x436a
movl 0xe617(%rip), %esi      # 0x12958
leaq 0xe410(%rip), %rdi      # 0x12758
xorl %edx, %edx
callq 0x9c67
movl 0xe60b(%rip), %eax      # 0x12960
testl %eax, %eax
leaq 0xec5a(%rip), %rsi      # 0x12fb8
je 0x436a
movl $0x0, 0xdf3e(%rip)      # 0x122a8
movq (%rsi), %rsi
movl $0xa, %edi
callq 0x3200
incl 0xdf2b(%rip)            # 0x122a8
leaq 0xeebc(%rip), %rax      # 0x13240
cmpb $0x0, (%rax)
je 0x4442
movl 0x30(%rsp), %eax
leaq 0xec28(%rip), %rcx      # 0x12fc0
cmpl (%rcx), %eax
jl 0x4442
movq 0xdf79(%rip), %rax      # 0x12320
movsd 0x34(%rsp), %xmm0
cvtdq2ps %xmm0, %xmm0
movq (%rax), %xmm1
leaq 0xe92d(%rip), %rax      # 0x12ce8
movd (%rax), %xmm2
pshufd $0xf5, %xmm1, %xmm3     # xmm3 = xmm1[1,1,3,3]
pmuludq %xmm2, %xmm1
pshufd $0xe8, %xmm1, %xmm1     # xmm1 = xmm1[0,2,2,3]
pmuludq %xmm2, %xmm3
pshufd $0xe8, %xmm3, %xmm2     # xmm2 = xmm3[0,2,2,3]
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
cvtdq2ps %xmm1, %xmm1
divps %xmm1, %xmm0
cvtps2pd %xmm0, %xmm0
movapd 0x6c25(%rip), %xmm1     # 0xb010
subpd %xmm0, %xmm1
movsd 0x6c11(%rip), %xmm2     # 0xb008
addpd %xmm2, %xmm0
shufpd $0x2, %xmm1, %xmm0      # xmm0 = xmm0[0],xmm1[1]
mulpd 0x6c18(%rip), %xmm0     # 0xb020
cvttpd2dq %xmm0, %xmm0
movlpd %xmm0, 0x34(%rsp)
movd %xmm0, %esi
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %edx
leaq 0x6f62(%rip), %rdi      # 0xb388
xorl %eax, %eax
callq 0x3170
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %edi
movl 0x38(%rsp), %esi
movl 0x3c(%rsp), %ecx
callq 0xa2cc
leaq 0xeb7b(%rip), %rax      # 0x12fc4
movl $0x0, (%rax)
movq 0xe2fa(%rip), %rdi      # 0x12750
testq %rdi, %rdi
je 0x4460
callq 0x3040
cmpl $0x0, 0x4(%rsp)
je 0x4473
leaq 0xe2ea(%rip), %rdi      # 0x12758
callq 0x3080
leaq 0xeb46(%rip), %rax      # 0x12fc0
movl (%rax), %eax
cmpl %eax, 0x30(%rsp)
setge %cl
cmpl %eax, %ebp
setge %al
orb %cl, %al
movzbl %al, %eax
addl %eax, 0x8(%rsp)
incl 0xde6d(%rip)            # 0x12304
jmp 0x3e90
movq 0x18(%rsp), %rdi
movl 0x8(%rsp), %ebx
jmp 0x44a9
xorl %ebx, %ebx
callq 0x9c62
addl $0x3e8, 0xe4b0(%rip)    # imm = 0x3E8
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x108(%rsp), %rdi
callq 0x3540