// File: /workspace/github_c_makefile_star2_O2/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 0x9138
testq %rax, %rax
sete %dl
movl (%rbx), %esi
testl %esi, %esi
sete %cl
xorl %edi, %edi
orb %dl, %cl
jne 0x4042
andl $0x0, 0xeb02(%rip)      # 0x12314
leaq 0x108(%rsp), %r14
movq %rax, 0x18(%rsp)
movq %rax, %r13
addq $0x1a, %r13
xorl %ebp, %ebp
movl $0x0, 0x8(%rsp)
movslq %esi, %rax
cmpq %rax, %rbp
jge 0x402a
xorl %edi, %edi
callq 0x3310
cmpb $0x0, -0x2(%r13)
je 0x401a
movq %rax, %r15
leaq -0x2(%r13), %r12
movq %r12, %rdi
leaq 0x7a9f(%rip), %rsi      # 0xb2fe
callq 0x3260
testl %eax, %eax
jne 0x386f
movb $0x1, 0xeab1(%rip)      # 0x12320
movq %r12, %rdi
callq 0x3130
movq %rax, %rbx
cmpl $0x5, %ebx
jl 0x401a
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
leaq (%rbx,%r13), %rdi
addq $-0x6, %rdi
leaq 0x78c5(%rip), %rsi      # 0xb15d
callq 0x3260
testl %eax, %eax
je 0x3931
addq %r13, %rbx
addq $-0x7, %rbx
movq %rbx, %rdi
leaq 0x7a4e(%rip), %rsi      # 0xb304
callq 0x3260
testl %eax, %eax
je 0x3931
movq %rbx, %rdi
leaq 0x7a41(%rip), %rsi      # 0xb30a
callq 0x3260
testl %eax, %eax
jne 0x401a
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8eb0
movq %rax, %rbx
movq %rax, %rdi
leaq 0x30(%rsp), %rsi
callq 0x32c0
cmpl $-0x1, %eax
je 0x3ded
subq 0x88(%rsp), %r15
cmpq $0x6, %r15
jl 0x401a
leaq 0xf6b3(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
leaq 0x79f1(%rip), %rsi      # 0xb310
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32a0
movq %rbx, %rdi
jmp 0x3c38
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8eb0
movq %r14, %rdi
movq %rax, %rsi
callq 0x30a0
movq %rbx, %rdi
callq 0x3130
incl %eax
movl %eax, 0x2fc(%rsp)
movslq 0xebc8(%rip), %rax      # 0x1252c
leaq 0xe9c5(%rip), %rbx      # 0x12330
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movq %r12, %rsi
callq 0x3260
testl %eax, %eax
je 0x3a43
movslq 0xedba(%rip), %rax      # 0x12744
leaq (%rax,%rbx), %rdi
addq $0x220, %rdi            # imm = 0x220
movq %r12, %rsi
callq 0x3260
testl %eax, %eax
je 0x3a43
cmpb $0x1, 0xe974(%rip)      # 0x12320
jne 0x3a50
movq 0x10(%rsp), %rdi
leaq 0x7940(%rip), %rsi      # 0xb2fe
callq 0x8eb0
movq %rax, %r15
movq %rax, %rdi
leaq 0x9de1(%rip), %rsi      # 0xd7b1
callq 0x3440
movq %rax, %rbx
movl $0xc7, %edx
leaq 0x30(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rax, %rcx
callq 0x30e0
movb $0x0, 0x30(%rsp,%rax)
leaq 0xf5cf(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
leaq 0x791f(%rip), %rsi      # 0xb322
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x32a0
movq %rbx, %rdi
callq 0x3110
movq %r15, %rdi
callq 0x3080
cmpl $0x0, 0x4(%rsp)
je 0x401a
movq %r14, %rdi
callq 0x3080
xorl %eax, %eax
movb %al, (%r12)
movb %al, 0xe8e2(%rip)       # 0x12320
jmp 0x401a
leaq 0x108(%rsp), %r14
jmp 0x401a
leaq 0xf2a1(%rip), %rax      # 0x12cf8
movl (%rax), %esi
leaq 0x108(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x6d01
movq %rax, 0x100(%rsp)
testq %rax, %rax
je 0x3c0f
cmpb $0x73, (%r12)
jne 0x3aa9
cmpb $0x66, -0x1(%r13)
jne 0x3aa9
movb (%r13), %al
addb $-0x30, %al
cmpb $0x9, %al
ja 0x3aa9
movq %r13, %rdi
callq 0x3470
cltq
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
jmp 0x3acb
leaq 0x108(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x32c0
cmpl $-0x1, %eax
je 0x4056
movl 0x88(%rsp), %eax
movq %rax, 0x300(%rsp)
movq %rax, 0xe83e(%rip)      # 0x12318
leaq 0xeebf(%rip), %rcx      # 0x129a0
cmpl $0x0, (%rcx)
je 0x3b1c
leaq 0xeea3(%rip), %rcx      # 0x12990
cmpl $0x0, (%rcx)
je 0x3b1c
movl 0xc(%rsp), %ecx
decl %ecx
cmpq %rcx, %rbp
jne 0x3b1c
subq %rax, %r15
cmpq $0x1, %r15
jg 0x3b1c
movq 0x100(%rsp), %rdi
callq 0x9e9e
testl %eax, %eax
je 0x3b1c
callq 0x87c0
movl $0x218, %edx            # imm = 0x218
leaq 0xec38(%rip), %rdi      # 0x12760
leaq 0xea19(%rip), %r15      # 0x12548
movq %r15, %rsi
callq 0x32d0
movl $0x218, %edx            # imm = 0x218
movq %r15, %rdi
movq %rbx, %rsi
callq 0x32d0
movl $0x218, %edx            # imm = 0x218
movq %rbx, %rdi
leaq 0x100(%rsp), %rsi
callq 0x32d0
andq $0x0, 0xe9d8(%rip)      # 0x1253c
andl $0x0, 0x30(%rsp)
leaq 0xf540(%rip), %rax      # 0x130b0
movb (%rax), %al
xorl %r15d, %r15d
leaq 0xf6d4(%rip), %rcx      # 0x13250
orb (%rcx), %al
movl $0x0, %r9d
leaq 0x30(%rsp), %rax
cmovneq %rax, %r9
movq 0xe9b4(%rip), %rdi      # 0x12548
testq %rdi, %rdi
leaq 0x108(%rsp), %r14
je 0x3fd6
leaq 0xf42c(%rip), %rax      # 0x12fd8
movslq (%rax), %rsi
testq %rsi, %rsi
jle 0x3be3
movq 0xe975(%rip), %rcx      # 0x12530
cmpq 0xedbe(%rip), %rcx      # 0x12980
jl 0x3bce
movl $0x1, 0xe96e(%rip)      # 0x1253c
addq %rsi, %rcx
movq %rcx, %rax
cqto
idivq %rsi
subq %rdx, %rcx
movq %rcx, 0xed9d(%rip)      # 0x12980
leaq 0xedba(%rip), %rax      # 0x129a4
movl (%rax), %eax
testl %eax, %eax
je 0x3c42
movl 0xe71a(%rip), %ecx      # 0x12310
incl %ecx
xorl %ebx, %ebx
cmpl %eax, %ecx
setge %bl
movl $0x0, %eax
cmovgel %eax, %ecx
movl %ecx, 0xe703(%rip)      # 0x12310
jmp 0x3c44
leaq 0xf3b2(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
leaq 0x770a(%rip), %rsi      # 0xb32a
movq %r14, %rdx
xorl %eax, %eax
callq 0x32a0
cmpl $0x0, 0x4(%rsp)
je 0x401a
movq %r14, %rdi
callq 0x3080
jmp 0x401a
xorl %ebx, %ebx
movq 0xe6e5(%rip), %rsi      # 0x12330
pushq $0x1
popq %rdx
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x5674
movq %rax, %r15
movq %rdx, %r12
movl %r15d, 0xe8d3(%rip)     # 0x12538
movl %ebx, 0xe8d9(%rip)      # 0x12544
leaq 0xed1e(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x3cb1
leaq 0xe69a(%rip), %rdi      # 0x12318
callq 0x3060
leaq 0x26(%rsp), %rbx
movq %rbx, %rdi
pushq $0xa
popq %rsi
leaq 0x76a8(%rip), %rdx      # 0xb33d
movq %rax, %rcx
callq 0x3410
leaq 0xf324(%rip), %rax      # 0x12fc8
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x31a0
jmp 0x3cdf
leaq 0xf310(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
movslq 0xe86a(%rip), %rax      # 0x1252c
leaq 0xe667(%rip), %rcx      # 0x12330
leaq (%rax,%rcx), %rdx
addq $0x8, %rdx
leaq 0x766d(%rip), %rsi      # 0xb345
xorl %eax, %eax
callq 0x32a0
pushq $0x1
popq %rbx
testl %r15d, %r15d
leaq 0xf2dc(%rip), %rsi      # 0x12fc8
je 0x3d41
movq (%rsi), %rdi
leaq 0x7652(%rip), %rsi      # 0xb34a
movl %r15d, %edx
xorl %eax, %eax
callq 0x32a0
leal (%r15,%r15,4), %eax
leaq 0xf2c3(%rip), %rcx      # 0x12fd0
cmpl (%rcx), %eax
jl 0x3d33
leaq 0xf2b0(%rip), %rax      # 0x12fc8
movq (%rax), %rdi
movq %r15, %rdx
shrq $0x20, %rdx
leaq 0x7625(%rip), %rsi      # 0xb34e
movl %r12d, %ecx
xorl %eax, %eax
callq 0x32a0
andl $0x0, 0xec3e(%rip)      # 0x12978
leaq 0xf287(%rip), %rsi      # 0x12fc8
leaq 0xf288(%rip), %rax      # 0x12fd0
movl (%rax), %eax
cmpl %eax, 0xe7e8(%rip)      # 0x12538
jge 0x3d5a
movl 0xe7e8(%rip), %ecx      # 0x12540
jmp 0x3d67
movl $0x1, 0xe7dc(%rip)      # 0x12540
pushq $0x1
popq %rcx
leaq 0xec26(%rip), %rdx      # 0x12994
cmpl $0x0, (%rdx)
je 0x3e07
movq 0xe9e2(%rip), %rdi      # 0x12760
testq %rdi, %rdi
je 0x3e07
testl %ecx, %ecx
je 0x3e07
cmpl $0x0, 0xe9c6(%rip)      # 0x12758
je 0x3e07
movl 0xebce(%rip), %ecx      # 0x12968
cltd
pushq $0x2
popq %r8
idivl %r8d
cmpl %eax, %ecx
jge 0x3e0b
movq 0xe583(%rip), %rsi      # 0x12330
xorl %edx, %edx
pushq $0x1
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x5674
movq %rax, %r15
leaq 0xf209(%rip), %rax      # 0x12fd0
cmpl %r15d, (%rax)
jle 0x3dfa
leaq 0x7586(%rip), %rdi      # 0xb359
movl %r15d, %esi
xorl %eax, %eax
callq 0x3170
andl $0x0, 0xe75c(%rip)      # 0x12540
andl $0x0, 0xe96d(%rip)      # 0x12758
jmp 0x3e41
movq %r12, %rdi
callq 0x3450
jmp 0x401a
movl 0xe740(%rip), %ecx      # 0x12540
leaq 0xf1c1(%rip), %rsi      # 0x12fc8
testl %ecx, %ecx
je 0x3e41
movq (%rsi), %rcx
leaq 0x755b(%rip), %rdi      # 0xb370
movq %rsi, %r14
pushq $0x9
popq %rsi
movq %rbx, %rdx
callq 0x34b0
cmpl $0x0, 0xe71a(%rip)      # 0x12544
je 0x3e41
movq (%r14), %rcx
leaq 0x7544(%rip), %rdi      # 0xb37a
pushq $0x5
popq %rsi
movq %rbx, %rdx
callq 0x34b0
cmpl $0x0, 0xe6f4(%rip)      # 0x1253c
je 0x3e66
leaq 0xf177(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x7525(%rip), %rdi      # 0xb380
pushq $0x7
popq %rsi
movq %rbx, %rdx
callq 0x34b0
leaq 0xec23(%rip), %rax      # 0x12a90
cmpb $0x0, (%rax)
movl 0xeafa(%rip), %eax      # 0x12970
je 0x3edc
cmpl $0x0, 0xe8d9(%rip)      # 0x12758
sete %cl
leaq 0xeb13(%rip), %rdx      # 0x1299c
cmpl $0x0, (%rdx)
sete %dl
movl 0xe41b(%rip), %esi      # 0x122b0
leaq 0xeafc(%rip), %rdi      # 0x12998
cmpl (%rdi), %esi
leaq 0xf123(%rip), %rsi      # 0x12fc8
leaq 0x108(%rsp), %r14
jle 0x3ec0
testl %eax, %eax
jne 0x3ec0
orb %dl, %cl
je 0x3ec0
cmpl $0x0, 0xeaae(%rip)      # 0x1296c
je 0x3ef6
movl 0xeaa2(%rip), %esi      # 0x12968
leaq 0xe89b(%rip), %rdi      # 0x12768
xorl %edx, %edx
callq 0x92ba
movl 0xea96(%rip), %eax      # 0x12970
jmp 0x3ee4
leaq 0x108(%rsp), %r14
testl %eax, %eax
leaq 0xf0db(%rip), %rsi      # 0x12fc8
je 0x3ef6
andl $0x0, 0xe3ba(%rip)      # 0x122b0
movq (%rsi), %rsi
pushq $0xa
popq %rdi
callq 0x3200
incl 0xe3a9(%rip)            # 0x122b0
leaq 0xf342(%rip), %rax      # 0x13250
cmpb $0x0, (%rax)
je 0x3fcc
movl 0x30(%rsp), %eax
leaq 0xf0ae(%rip), %rcx      # 0x12fd0
cmpl (%rcx), %eax
jl 0x3fcc
movq 0xe3ff(%rip), %rax      # 0x12330
movsd 0x34(%rsp), %xmm0
cvtdq2ps %xmm0, %xmm0
movq (%rax), %xmm1
leaq 0xedb3(%rip), %rax      # 0x12cf8
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 0x709b(%rip), %xmm1     # 0xb010
subpd %xmm0, %xmm1
movsd 0x7087(%rip), %xmm2     # 0xb008
addpd %xmm2, %xmm0
shufpd $0x2, %xmm1, %xmm0      # xmm0 = xmm0[0],xmm1[1]
mulpd 0x708e(%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 0x73d8(%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 0x9948
leaq 0xf001(%rip), %rax      # 0x12fd4
andl $0x0, (%rax)
movq 0xe783(%rip), %rdi      # 0x12760
callq 0x3040
cmpl $0x0, 0x4(%rsp)
je 0x3ff5
leaq 0xe778(%rip), %rdi      # 0x12768
callq 0x3080
leaq 0xefd4(%rip), %rax      # 0x12fd0
movl (%rax), %eax
cmpl %eax, 0x30(%rsp)
setge %cl
cmpl %eax, %r15d
setge %al
orb %cl, %al
movzbl %al, %eax
addl %eax, 0x8(%rsp)
incl 0xe2fa(%rip)            # 0x12314
incq %rbp
movl 0xc(%rsp), %esi
addq $0x50, %r13
jmp 0x3830
movq 0x18(%rsp), %rdi
callq 0x92b5
addl $0x3e8, 0xe93a(%rip)    # imm = 0x3E8
movl 0x8(%rsp), %edi
movl %edi, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x108(%rsp), %rdi
callq 0x3450
pushq $0x1
popq %rdi
callq 0x34a0