// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
leaq 0x4(%rsp), %rsi
movq %rdi, 0x10(%rsp)
callq 0x9ac7
xorl %ebx, %ebx
testq %rax, %rax
je 0x4495
cmpl $0x0, 0x4(%rsp)
je 0x4495
movl $0x0, 0xe6c6(%rip)      # 0x12304
movl 0x4(%rsp), %esi
testl %esi, %esi
jle 0x4481
leaq 0x108(%rsp), %r15
movq %rax, 0x18(%rsp)
leaq 0x1a(%rax), %r12
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
xorl %edi, %edi
callq 0x3300
cmpb $0x0, -0x2(%r12)
je 0x3e7d
movq %rax, %rbp
movq %r15, %r14
leaq -0x2(%r12), %rbx
movq %rbx, %rdi
leaq 0x7663(%rip), %rsi      # 0xb2f1
callq 0x3250
testl %eax, %eax
jne 0x3c9e
movb $0x1, 0xe672(%rip)      # 0x12310
movq %rbx, %rdi
callq 0x3130
movq %rax, %r15
cmpl $0x5, %r15d
jge 0x3cb7
movq %r14, %r15
jmp 0x3e7d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%r12,%r15), %rdi
addq $-0x6, %rdi
leaq 0x7480(%rip), %rsi      # 0xb14d
callq 0x3250
testl %eax, %eax
je 0x3d67
addq %r12, %r15
addq $-0x7, %r15
movq %r15, %rdi
leaq 0x760c(%rip), %rsi      # 0xb2f7
callq 0x3250
testl %eax, %eax
je 0x3d67
movq %r15, %rdi
leaq 0x75ff(%rip), %rsi      # 0xb2fd
callq 0x3250
testl %eax, %eax
jne 0x3caf
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x9834
movq %rax, %r15
movq %rax, %rdi
leaq 0x30(%rsp), %rsi
callq 0x32b0
cmpl $-0x1, %eax
je 0x4469
subq 0x88(%rsp), %rbp
cmpq $0x6, %rbp
jl 0x3caf
leaq 0xf272(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x75b3(%rip), %rsi      # 0xb303
movq %r15, %rdx
xorl %eax, %eax
callq 0x3290
movq %r15, %rdi
callq 0x3080
jmp 0x3caf
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x9834
movq %r14, %rdi
movq %rax, %rsi
callq 0x30a0
movq %r15, %rdi
callq 0x3130
incl %eax
movl %eax, 0x2fc(%rsp)
movslq 0xe782(%rip), %rax      # 0x1251c
leaq 0xe57f(%rip), %r15      # 0x12320
leaq (%rax,%r15), %rdi
addq $0x8, %rdi
movq %rbx, %rsi
callq 0x3250
testl %eax, %eax
je 0x3e75
movslq 0xe974(%rip), %rax      # 0x12734
leaq (%rax,%r15), %rdi
addq $0x220, %rdi            # imm = 0x220
movq %rbx, %rsi
callq 0x3250
testl %eax, %eax
je 0x3e75
cmpb $0x1, 0xe52e(%rip)      # 0x12310
jne 0x3e97
movq 0x10(%rsp), %rdi
leaq 0x74fd(%rip), %rsi      # 0xb2f1
callq 0x9834
movq %rax, %rbp
movq %rax, %rdi
leaq 0x996b(%rip), %rsi      # 0xd771
callq 0x3420
movq %rax, %r15
movl $0x1, %esi
movl $0xc7, %edx
leaq 0x30(%rsp), %rdi
movq %rax, %rcx
callq 0x30e0
movb $0x0, 0x30(%rsp,%rax)
leaq 0xf187(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x74da(%rip), %rsi      # 0xb315
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x3290
movq %r15, %rdi
movq %r14, %r15
callq 0x3110
movq %rbp, %rdi
callq 0x3080
cmpl $0x0, 0x8(%rsp)
je 0x3e7d
movq %r15, %rdi
callq 0x3080
xorl %eax, %eax
movb %al, (%rbx)
movb %al, 0xe49d(%rip)       # 0x12310
jmp 0x3e7d
leaq 0x108(%rsp), %r15
incq %r13
movslq 0x4(%rsp), %rsi
addq $0x50, %r12
cmpq %rsi, %r13
jl 0x3c66
jmp 0x4476
leaq 0xee4a(%rip), %rax      # 0x12ce8
movl (%rax), %esi
leaq 0x108(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x73ff
movq %rax, 0x100(%rsp)
testq %rax, %rax
je 0x4080
cmpb $0x73, (%rbx)
jne 0x3f00
cmpb $0x66, -0x1(%r12)
jne 0x3f00
movb (%r12), %al
addb $-0x30, %al
cmpb $0x9, %al
ja 0x3f00
movq %r12, %rdi
callq 0x3450
cltq
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movq %rax, 0x300(%rsp)
leaq 0xe422(%rip), %r15      # 0x12320
jmp 0x3f31
leaq 0x108(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x32b0
cmpl $-0x1, %eax
leaq 0xe404(%rip), %r15      # 0x12320
je 0x44a9
movl 0x88(%rsp), %eax
movq %rax, 0x300(%rsp)
movq 0x300(%rsp), %rax
movq %rax, 0xe3c8(%rip)      # 0x12308
leaq 0xea49(%rip), %rcx      # 0x12990
cmpl $0x0, (%rcx)
je 0x3f8a
leaq 0xea2d(%rip), %rcx      # 0x12980
cmpl $0x0, (%rcx)
je 0x3f8a
movl 0x4(%rsp), %ecx
decl %ecx
cmpq %rcx, %r13
sete %cl
subq %rax, %rbp
cmpq $0x2, %rbp
setl %al
andb %cl, %al
cmpb $0x1, %al
jne 0x3f8a
movq 0x100(%rsp), %rdi
callq 0xa80a
testl %eax, %eax
je 0x3f8a
callq 0x90d8
movl $0x218, %edx            # imm = 0x218
leaq 0xe7ba(%rip), %rdi      # 0x12750
leaq 0xe59b(%rip), %rbx      # 0x12538
movq %rbx, %rsi
callq 0x32c0
movl $0x218, %edx            # imm = 0x218
movq %rbx, %rdi
movq %r15, %rsi
callq 0x32c0
movl $0x218, %edx            # imm = 0x218
movq %r15, %rdi
leaq 0x100(%rsp), %rsi
callq 0x32c0
xorl %ebp, %ebp
movq %rbp, 0xe559(%rip)      # 0x1252c
movl $0x0, 0x30(%rsp)
leaq 0xf0be(%rip), %rax      # 0x130a0
movb (%rax), %al
leaq 0xf255(%rip), %rcx      # 0x13240
orb (%rcx), %al
movl $0x0, %r9d
leaq 0x30(%rsp), %rax
cmovneq %rax, %r9
movq 0xe535(%rip), %rdi      # 0x12538
testq %rdi, %rdi
leaq 0x108(%rsp), %r15
je 0x441c
leaq 0xefad(%rip), %rax      # 0x12fc8
movslq (%rax), %rsi
testq %rsi, %rsi
jle 0x4052
movq 0xe4f6(%rip), %rcx      # 0x12520
cmpq 0xe93f(%rip), %rcx      # 0x12970
jl 0x403d
movl $0x1, 0xe4ef(%rip)      # 0x1252c
addq %rsi, %rcx
movq %rcx, %rax
cqto
idivq %rsi
subq %rdx, %rcx
movq %rcx, 0xe91e(%rip)      # 0x12970
leaq 0xe93b(%rip), %rax      # 0x12994
movl (%rax), %eax
testl %eax, %eax
je 0x40b3
movl 0xe29b(%rip), %ecx      # 0x12300
incl %ecx
xorl %r15d, %r15d
cmpl %eax, %ecx
setge %r15b
movl $0x0, %eax
cmovgel %eax, %ecx
movl %ecx, 0xe282(%rip)      # 0x12300
jmp 0x40b6
leaq 0xef31(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x728c(%rip), %rsi      # 0xb31d
movq %r15, %rdx
xorl %eax, %eax
callq 0x3290
cmpl $0x0, 0x8(%rsp)
je 0x3e7d
movq %r15, %rdi
callq 0x3080
jmp 0x3e7d
xorl %r15d, %r15d
movq 0xe263(%rip), %rsi      # 0x12320
movl $0x1, %edx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x5b14
movq %rax, %rbp
movq %rdx, %rbx
movl %ebp, 0xe44f(%rip)      # 0x12528
movl %r15d, 0xe454(%rip)     # 0x12534
leaq 0xe899(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x4128
leaq 0xe215(%rip), %rdi      # 0x12308
callq 0x3060
movl $0xa, %esi
leaq 0x26(%rsp), %r15
movq %r15, %rdi
leaq 0x7224(%rip), %rdx      # 0xb330
movq %rax, %rcx
callq 0x33f0
leaq 0xee9d(%rip), %rax      # 0x12fb8
movq (%rax), %rsi
movq %r15, %rdi
callq 0x31a0
jmp 0x4156
leaq 0xee89(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movslq 0xe3e3(%rip), %rax      # 0x1251c
leaq 0xe1e0(%rip), %rcx      # 0x12320
leaq (%rax,%rcx), %rdx
addq $0x8, %rdx
leaq 0x71e9(%rip), %rsi      # 0xb338
xorl %eax, %eax
callq 0x3290
movq %r14, %r15
testl %ebp, %ebp
je 0x41b1
leaq 0xee54(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x71cf(%rip), %rsi      # 0xb33d
movl %ebp, %edx
xorl %eax, %eax
callq 0x3290
leal (%rbp,%rbp,4), %eax
leaq 0xee3e(%rip), %rcx      # 0x12fc0
cmpl (%rcx), %eax
jl 0x41a7
leaq 0xee2b(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movq %rbp, %rdx
shrq $0x20, %rdx
leaq 0x71a3(%rip), %rsi      # 0xb341
movl %ebx, %ecx
xorl %eax, %eax
callq 0x3290
movl $0x0, 0xe7b7(%rip)      # 0x12968
leaq 0xee08(%rip), %rax      # 0x12fc0
movl (%rax), %eax
cmpl %eax, 0xe368(%rip)      # 0x12528
jl 0x41cc
movl $0x1, 0xe364(%rip)      # 0x12530
leaq 0xe7b1(%rip), %rcx      # 0x12984
cmpl $0x0, (%rcx)
je 0x424c
movq 0xe571(%rip), %rdi      # 0x12750
testq %rdi, %rdi
je 0x424c
cmpl $0x0, 0xe345(%rip)      # 0x12530
je 0x424c
cmpl $0x0, 0xe554(%rip)      # 0x12748
je 0x424c
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
cmpl %ecx, 0xe753(%rip)      # 0x12958
jge 0x424c
movq 0xe112(%rip), %rsi      # 0x12320
xorl %edx, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x5b14
movq %rax, %rbp
leaq 0xed96(%rip), %rax      # 0x12fc0
cmpl %ebp, (%rax)
jle 0x424c
leaq 0x7117(%rip), %rdi      # 0xb34c
movl %ebp, %esi
xorl %eax, %eax
callq 0x3170
xorl %eax, %eax
movl %eax, 0xe2ea(%rip)      # 0x12530
movl %eax, 0xe4fc(%rip)      # 0x12748
cmpl $0x0, 0xe2dd(%rip)      # 0x12530
je 0x429e
leaq 0xed5c(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
movl $0x9, %esi
movl $0x1, %edx
leaq 0x70f3(%rip), %rdi      # 0xb363
callq 0x3490
cmpl $0x0, 0xe2b8(%rip)      # 0x12534
je 0x429e
leaq 0xed33(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
movl $0x5, %esi
movl $0x1, %edx
leaq 0x70d4(%rip), %rdi      # 0xb36d
callq 0x3490
cmpl $0x0, 0xe287(%rip)      # 0x1252c
je 0x42c7
leaq 0xed0a(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
movl $0x7, %esi
movl $0x1, %edx
leaq 0x70b1(%rip), %rdi      # 0xb373
callq 0x3490
leaq 0xe7b2(%rip), %rax      # 0x12a80
cmpb $0x0, (%rax)
je 0x4325
cmpl $0x0, 0xe46e(%rip)      # 0x12748
sete %al
leaq 0xe6a8(%rip), %rcx      # 0x1298c
cmpl $0x0, (%rcx)
sete %cl
movl 0xdfb0(%rip), %edx      # 0x122a0
leaq 0xe691(%rip), %rsi      # 0x12988
cmpl (%rsi), %edx
jle 0x4311
cmpl $0x0, 0xe65e(%rip)      # 0x12960
jne 0x4311
orb %cl, %al
je 0x4311
cmpl $0x0, 0xe64d(%rip)      # 0x1295c
je 0x4325
movl 0xe641(%rip), %esi      # 0x12958
leaq 0xe43a(%rip), %rdi      # 0x12758
xorl %edx, %edx
callq 0x9c76
cmpl $0x0, 0xe634(%rip)      # 0x12960
je 0x4338
movl $0x0, 0xdf68(%rip)      # 0x122a0
leaq 0xec79(%rip), %rax      # 0x12fb8
movq (%rax), %rsi
movl $0xa, %edi
callq 0x3200
incl 0xdf4e(%rip)            # 0x122a0
leaq 0xeee7(%rip), %rax      # 0x13240
cmpb $0x0, (%rax)
je 0x440f
movl 0x30(%rsp), %eax
leaq 0xec53(%rip), %rcx      # 0x12fc0
cmpl (%rcx), %eax
jl 0x440f
cvtsi2ssl 0x34(%rsp), %xmm0
movq 0xdf9e(%rip), %rax      # 0x12320
leaq 0xe95f(%rip), %rcx      # 0x12ce8
movl (%rcx), %ecx
movl (%rax), %edx
imull %ecx, %edx
cvtsi2ss %edx, %xmm1
cvtsi2ssl 0x38(%rsp), %xmm2
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
imull 0x4(%rax), %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
divss %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
movsd 0x6c58(%rip), %xmm2     # 0xb018
subsd %xmm1, %xmm2
addsd 0x6c3c(%rip), %xmm0     # 0xb008
movsd 0x6c3c(%rip), %xmm1     # 0xb010
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
movl %esi, 0x34(%rsp)
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %edx
movl %edx, 0x38(%rsp)
leaq 0x6f88(%rip), %rdi      # 0xb37b
xorl %eax, %eax
callq 0x3170
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %edi
movl 0x38(%rsp), %esi
movl 0x3c(%rsp), %ecx
callq 0xa2cc
leaq 0xebae(%rip), %rax      # 0x12fc4
movl $0x0, (%rax)
movq 0xe32d(%rip), %rdi      # 0x12750
testq %rdi, %rdi
je 0x442d
callq 0x3040
cmpl $0x0, 0x8(%rsp)
je 0x4440
leaq 0xe31d(%rip), %rdi      # 0x12758
callq 0x3080
leaq 0xeb79(%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, 0xc(%rsp)
incl 0xdea0(%rip)            # 0x12304
jmp 0x3e7d
movq %rbx, %rdi
callq 0x3430
jmp 0x3caf
movq 0x18(%rsp), %rax
movl 0xc(%rsp), %ebx
jmp 0x4483
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x9c71
addl $0x3e8, 0xe4d3(%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 0x3530