// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0xbdf0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x381d
movl $0x0, -0x4(%rbp)
jmp 0x3d16
cmpl $0x0, -0x24(%rbp)
jne 0x382f
movl $0x0, -0x4(%rbp)
jmp 0x3d16
movl $0x0, 0x10b2b(%rip)     # 0x14364
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x3ceb
xorl %eax, %eax
movl %eax, %edi
callq 0x3300
movq %rax, -0x260(%rbp)
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
addq $0x18, %rax
movq %rax, -0x250(%rbp)
movq -0x250(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x388a
jmp 0x3cdd
movq -0x250(%rbp), %rdi
leaq 0xaa76(%rip), %rsi      # 0xe30e
callq 0x3250
cmpl $0x0, %eax
jne 0x38ac
movl $0x1, 0x10ac4(%rip)     # 0x14370
movq -0x250(%rbp), %rdi
callq 0x3130
movl %eax, -0x254(%rbp)
cmpl $0x5, -0x254(%rbp)
jge 0x38cc
jmp 0x3cdd
movq -0x20(%rbp), %rdi
movslq -0x28(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x18, %rdi
movslq -0x254(%rbp), %rax
addq %rax, %rdi
addq $-0x4, %rdi
leaq 0xa841(%rip), %rsi      # 0xe135
callq 0x3250
cmpl $0x0, %eax
je 0x39dc
movq -0x250(%rbp), %rdi
movslq -0x254(%rbp), %rax
addq %rax, %rdi
addq $-0x5, %rdi
leaq 0xa9f6(%rip), %rsi      # 0xe314
callq 0x3250
cmpl $0x0, %eax
je 0x39dc
movq -0x250(%rbp), %rdi
movslq -0x254(%rbp), %rax
addq %rax, %rdi
addq $-0x5, %rdi
leaq 0xa9d2(%rip), %rsi      # 0xe31a
callq 0x3250
cmpl $0x0, %eax
jne 0x39d7
movq -0x10(%rbp), %rdi
movq -0x250(%rbp), %rsi
callq 0xbaa0
movq %rax, -0x2f8(%rbp)
movq -0x2f8(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x32b0
cmpl $-0x1, %eax
jne 0x3996
movq -0x250(%rbp), %rdi
callq 0x3430
jmp 0x3cdd
movq -0x260(%rbp), %rax
subq -0x298(%rbp), %rax
cmpq $0x5, %rax
jle 0x39d5
leaq 0x11667(%rip), %rax     # 0x15018
movq (%rax), %rdi
movq -0x2f8(%rbp), %rdx
leaq 0xa95e(%rip), %rsi      # 0xe320
movb $0x0, %al
callq 0x3290
movq -0x2f8(%rbp), %rdi
callq 0x3080
jmp 0x39d7
jmp 0x3cdd
leaq -0x248(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x470(%rbp)
movq -0x10(%rbp), %rdi
movq -0x250(%rbp), %rsi
callq 0xbaa0
movq -0x470(%rbp), %rdi
movq %rax, %rsi
callq 0x30b0
movq -0x10(%rbp), %rdi
callq 0x3130
addq $0x1, %rax
movl %eax, -0x4c(%rbp)
movslq 0x10b58(%rip), %rax     # 0x1457c
leaq 0x10955(%rip), %rdi     # 0x14380
addq $0x8, %rdi
addq %rax, %rdi
movq -0x250(%rbp), %rsi
callq 0x3250
cmpl $0x0, %eax
je 0x3a70
movslq 0x10d4a(%rip), %rax     # 0x14794
leaq 0x1092f(%rip), %rdi     # 0x14380
addq $0x218, %rdi            # imm = 0x218
addq $0x8, %rdi
addq %rax, %rdi
movq -0x250(%rbp), %rsi
callq 0x3250
cmpl $0x0, %eax
jne 0x3a75
jmp 0x3cdd
cmpl $0x0, 0x108f4(%rip)     # 0x14370
je 0x3b47
movq -0x10(%rbp), %rdi
leaq 0xa881(%rip), %rsi      # 0xe30e
callq 0xbaa0
movq %rax, -0x3d0(%rbp)
movq -0x3d0(%rbp), %rdi
leaq 0xd047(%rip), %rsi      # 0x10aee
callq 0x3420
movq %rax, -0x3c8(%rbp)
leaq -0x3c0(%rbp), %rdi
movq -0x3c8(%rbp), %rcx
movl $0x1, %esi
movl $0xc7, %edx
callq 0x30e0
movb $0x0, -0x3c0(%rbp,%rax)
leaq 0x11539(%rip), %rax     # 0x15018
movq (%rax), %rdi
leaq -0x3c0(%rbp), %rdx
leaq 0xa842(%rip), %rsi      # 0xe332
movb $0x0, %al
callq 0x3290
movq -0x3c8(%rbp), %rdi
callq 0x3110
movq -0x3d0(%rbp), %rdi
callq 0x3080
cmpl $0x0, -0x14(%rbp)
je 0x3b42
leaq -0x248(%rbp), %rdi
addq $0x8, %rdi
callq 0x3080
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x18(%rax)
movl $0x0, 0x1082e(%rip)     # 0x14370
jmp 0x3cdd
leaq -0x248(%rbp), %rdi
addq $0x8, %rdi
leaq 0x111ef(%rip), %rax     # 0x14d48
movl (%rax), %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x8920
movq %rax, -0x248(%rbp)
cmpq $0x0, -0x248(%rbp)
jne 0x3bb6
leaq 0x11499(%rip), %rax     # 0x15018
movq (%rax), %rdi
leaq -0x248(%rbp), %rdx
addq $0x8, %rdx
leaq 0xa7a6(%rip), %rsi      # 0xe33a
movb $0x0, %al
callq 0x3290
cmpl $0x0, -0x14(%rbp)
je 0x3bb1
leaq -0x248(%rbp), %rdi
addq $0x8, %rdi
callq 0x3080
jmp 0x3cdd
movq -0x250(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x73, %eax
jne 0x3c13
movq -0x250(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x66, %eax
jne 0x3c13
movq -0x250(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x30, %eax
jl 0x3c13
movq -0x250(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x39, %eax
jg 0x3c13
movq -0x250(%rbp), %rdi
addq $0x2, %rdi
callq 0x3450
cltq
addq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movq %rax, -0x48(%rbp)
jmp 0x3c56
leaq -0x248(%rbp), %rdi
addq $0x8, %rdi
leaq -0x460(%rbp), %rsi
callq 0x32b0
cmpl $-0x1, %eax
jne 0x3c49
leaq -0x248(%rbp), %rdi
addq $0x8, %rdi
callq 0x3430
movl $0x1, %edi
callq 0x3480
movq -0x408(%rbp), %rax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, 0x10707(%rip)     # 0x14368
leaq 0x10d88(%rip), %rax     # 0x149f0
cmpl $0x0, (%rax)
je 0x3cb9
leaq 0x10d6c(%rip), %rax     # 0x149e0
cmpl $0x0, (%rax)
je 0x3cb9
movl -0x28(%rbp), %eax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3cb9
movq -0x260(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x1, %rax
jg 0x3cb9
movq -0x248(%rbp), %rdi
callq 0xd1a0
movl %eax, -0x464(%rbp)
cmpl $0x0, -0x464(%rbp)
je 0x3cb7
callq 0xb0a0
jmp 0x3cb9
movl -0x14(%rbp), %esi
leaq -0x248(%rbp), %rdi
callq 0x48e0
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl 0x10690(%rip), %eax     # 0x14364
addl $0x1, %eax
movl %eax, 0x10687(%rip)     # 0x14364
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x3840
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0xc000
movq $0x0, -0x20(%rbp)
movl 0x10cc3(%rip), %eax     # 0x149c8
addl $0x3e8, %eax            # imm = 0x3E8
movl %eax, 0x10cb8(%rip)     # 0x149c8
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nopw %cs:(%rax,%rax)