// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/main.c
// Function: DoDirectoryVideos
int DoDirectoryVideos(char * DirName)
{
    int a,ret;
    int infileindex;
    int alt = 0;

    Raspistill_restarted = 0;
    infileindex = strstr(VidDecomposeCmd, "<infile>")-VidDecomposeCmd;

    if (infileindex <= 0){
        fprintf(stderr, "Must specify '<infile>' as part of videodecomposecmd\n");
        exit(-1);
    }

    for (;;){
        DirEntry_t * FileNames;
        int NumEntries;
        char VidFileName[200];
        char FFCmd[300];
        int Saw_motion;
        time_t now;
        int VideoActive = 0;

        NumProcessed = 0;
        alt += 1;

        FileNames = GetSortedDir(DirName, &NumEntries);
        if (FileNames == NULL){
            fprintf(stderr, "Could not read dir %s\n",DirName);
            return 0;
        }

        now = time(NULL);
        if (NumEntries != 1){
            fprintf(Log,"%d files to process",NumEntries);
            fprintf(Log, "(%02d:%02d)...\n",(int)(now%3600/60), (int)(now%60));
        }
        for (a=0;a<NumEntries;a++){
            int age;

            age = (int)(now-FileNames[a].MTime);
            if (age < 1){
                VideoActive = 1; // Video files getting updated.
                if (alt & 1 || NumEntries > 1){
                    // Print waiting only every other time to cut down on log clutter.
                    fprintf(Log, "Vid '%s' aged %d %dk (Wait)\n",FileNames[a].FileName, age, FileNames[a].FileSize>>10);
                }
                continue;
            }else{
                fprintf(Log,"Vid '%s': %dk Extract keyframes (%02d:%02d)...\n", FileNames[a].FileName, FileNames[a].FileSize>>10,
                    (int)(now%3600)/60, (int)(now%60));
            }

            strcpy(VidFileName, CatPath(DirName, FileNames[a].FileName));
            strncpy(FFCmd, VidDecomposeCmd, infileindex);
            FFCmd[infileindex] = 0;
            strcpy(FFCmd+infileindex, VidFileName);
            strcat(FFCmd, VidDecomposeCmd+infileindex+8);

            // Use timestamp of video file to sequence output file names (so we'll have the respective times for those)
            unsigned seq = (unsigned)FileNames[a].MTime - 1000000000;
            sprintf(FFCmd+strlen(FFCmd), " -start_number %u %s/sf%%d.jpg",seq, TempDirName);

            errno = 0;
            ret = system(FFCmd);
            if (ret || errno){
                // A command can however fail without errno getting set or system returning an error.
                if (errno) perror("system");
                fprintf(Log, "Error on command %s\n",FFCmd);
                continue;
            }

            // Now should have some files in temp dir.
            Saw_motion = DoDirectoryFunc(TempDirName, 1);
            if (Saw_motion){
                char * DstName;
                char * Ext;
                fprintf(Log,"Vid has motion %d\n", Saw_motion);
                // Make filename, but don't copy the file.
                DstName = BackupImageFile(VidFileName, Saw_motion,1);

                Ext = strstr(DstName, ".h264");
                if (Ext) strcpy(Ext, ".mp4"); // Change xtension to .mp4

                if (DstName){
                    sprintf(FFCmd,"MP4Box -add %s \"%s\"",VidFileName, DstName);
                    errno = 0;
                    ret = system(FFCmd);
                    if (ret || errno){
                        if (errno) perror("system");
                        fprintf(Log, "Error on command %s\n",FFCmd);
                        continue;
                    }
                }
            }

            if (FollowDir){
                //fprintf(Log, "Delete video %s\n",VidFileName);
                unlink(VidFileName);
            }
        }
        FreeDir(FileNames, NumEntries); // Free up the whole directory structure.

        if (FollowDir){
            int b = manage_camera_prog(VideoActive);
            if (b) Raspistill_restarted = 1;
            if (LogToFile[0] != '\0') LogFileMaintain(0);
            sleep(1);
        }else{
            break;
        }

    }
    return a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rdi, %r14
leaq 0xead0(%rip), %rax      # 0x12fc4
movl $0x0, (%rax)
leaq 0xed6f(%rip), %r15      # 0x13270
leaq 0x6b84(%rip), %rsi      # 0xb08c
movq %r15, %rdi
callq 0x3500
movq %rax, %rbx
subl %r15d, %ebx
testl %ebx, %ebx
jle 0x49e9
movl $0x0, 0xdddc(%rip)      # 0x12304
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x9ab1
testq %rax, %rax
je 0x49b6
movq %rax, %r15
movl %ebx, %ecx
leaq (%rsp,%rcx), %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, 0x40(%rsp)
leaq 0xed17(%rip), %rax      # 0x13270
movq %rcx, 0x48(%rsp)
leaq 0x8(%rax,%rcx), %rax
movq %rax, 0x38(%rsp)
movl $0x1, %ebx
leaq 0x120(%rsp), %rbp
movl $0x0, 0x1c(%rsp)
movl %ebx, %r12d
xorl %edi, %edi
callq 0x3310
movq %rax, %rbx
movl 0x8(%rsp), %edx
movl $0x1, %esi
cmpl $0x1, %edx
movq %rax, 0x10(%rsp)
je 0x465c
leaq 0xea10(%rip), %rbx      # 0x12fb8
movq (%rbx), %rdi
movl $0x0, 0xc(%rsp)
leaq 0x6b28(%rip), %rsi      # 0xb0e2
xorl %eax, %eax
callq 0x32a0
movq 0x10(%rsp), %rax
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq (%rbx), %rdi
movq 0x10(%rsp), %rbx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0xa, %rdx
addl %eax, %edx
imull $0xe10, %edx, %eax      # imm = 0xE10
movl %ebx, %ecx
subl %eax, %ecx
movswl %cx, %eax
imull $0xffff8889, %eax, %eax # imm = 0xFFFF8889
shrl $0x10, %eax
addl %ecx, %eax
movzwl %ax, %eax
movswl %ax, %ecx
shrl $0xf, %eax
sarl $0x5, %ecx
addl %eax, %ecx
movswl %cx, %r8d
movq %rbx, %rax
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
imulq %rcx
addq %rbx, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
movl %ebx, %ecx
subl %eax, %ecx
leaq 0x6af3(%rip), %rsi      # 0xb133
movl %r8d, %edx
xorl %eax, %eax
callq 0x32a0
movl 0x8(%rsp), %esi
movl $0x0, %r13d
testl %esi, %esi
jle 0x493a
movq %r12, 0x28(%rsp)
movq %rbx, %rax
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0xa, %rdx
addl %eax, %edx
imull $0xe10, %edx, %eax      # imm = 0xE10
movl %ebx, %ecx
subl %eax, %ecx
movswl %cx, %eax
imull $0xffff8889, %eax, %eax # imm = 0xFFFF8889
shrl $0x10, %eax
addl %ecx, %eax
movzwl %ax, %eax
movswl %ax, %ecx
shrl $0xf, %eax
sarl $0x5, %ecx
addl %eax, %ecx
movswl %cx, %eax
movl %eax, 0x24(%rsp)
movq %rbx, %rax
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
imulq %rcx
addq %rbx, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
movl %ebx, %ecx
subl %eax, %ecx
movl %ecx, 0x20(%rsp)
movq %r15, 0x30(%rsp)
leaq 0x18(%r15), %r12
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movl %ebx, %ecx
subl -0x10(%r12), %ecx
testl %ecx, %ecx
jle 0x4800
leaq 0xe8b8(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movl -0x18(%r12), %ecx
shrl $0xa, %ecx
leaq 0x6a01(%rip), %rsi      # 0xb113
movq %r12, %rdx
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r9d
xorl %eax, %eax
callq 0x32a0
movq %r14, %rbx
movq %r14, %rdi
movq %r12, %rsi
callq 0x9828
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x30a0
movq %rbp, %rdi
leaq 0xeb22(%rip), %rsi      # 0x13270
movq 0x48(%rsp), %r14
movq %r14, %rdx
callq 0x3090
movb $0x0, 0x120(%rsp,%r14)
movq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x30a0
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
callq 0x3470
movl -0x10(%r12), %r14d
movl $0xc4653600, %eax       # imm = 0xC4653600
addl %eax, %r14d
movq %rbp, %rdi
callq 0x3130
leaq (%rsp,%rax), %rdi
addq $0x120, %rdi            # imm = 0x120
leaq 0x699e(%rip), %rsi      # 0xb143
movl %r14d, %edx
leaq 0xeb91(%rip), %rcx      # 0x13340
xorl %eax, %eax
callq 0x3480
callq 0x3070
movq %rax, %r15
movl $0x0, (%rax)
movq %rbp, %rdi
callq 0x3150
movl (%r15), %ecx
testl %eax, %eax
je 0x4852
testl %ecx, %ecx
jne 0x491f
leaq 0xe7d6(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x697d(%rip), %rsi      # 0xb169
movq %rbp, %rdx
xorl %eax, %eax
callq 0x32a0
movq %rbx, %r14
movq 0x10(%rsp), %rbx
jmp 0x4838
cmpl $0x2, %esi
setl %al
movl $0x1, 0xc(%rsp)
testb %al, 0x1c(%rsp)
jne 0x4838
leaq 0xe79d(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
movl -0x18(%r12), %r8d
shrl $0xa, %r8d
leaq 0x68c8(%rip), %rsi      # 0xb0f6
movq %r12, %rdx
xorl %eax, %eax
callq 0x32a0
incq %r13
movslq 0x8(%rsp), %rsi
addq $0x50, %r12
cmpq %rsi, %r13
jl 0x46ea
jmp 0x4930
testl %ecx, %ecx
jne 0x491f
leaq 0xeadf(%rip), %rdi      # 0x13340
movl $0x1, %esi
callq 0x3c0a
testl %eax, %eax
je 0x4900
movl %eax, %r14d
leaq 0xe73b(%rip), %rax      # 0x12fb8
movq (%rax), %rdi
leaq 0x68f7(%rip), %rsi      # 0xb17e
movl %r14d, %edx
xorl %eax, %eax
callq 0x32a0
leaq 0x50(%rsp), %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0x9c67
movq %rax, %r14
movq %rax, %rdi
leaq 0x68e1(%rip), %rsi      # 0xb191
callq 0x3500
testq %rax, %rax
je 0x48c4
movb $0x0, 0x4(%rax)
movl $0x34706d2e, (%rax)     # imm = 0x34706D2E
testq %r14, %r14
je 0x4900
movq %rbp, %rdi
leaq 0x68c9(%rip), %rsi      # 0xb19c
leaq 0x50(%rsp), %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x3480
movl $0x0, (%r15)
movq %rbp, %rdi
callq 0x3150
movl (%r15), %ecx
testl %eax, %eax
jne 0x47d3
testl %ecx, %ecx
jne 0x491f
leaq 0xe079(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x47f6
leaq 0x50(%rsp), %rdi
callq 0x3080
jmp 0x47f6
leaq 0x683c(%rip), %rdi      # 0xb162
callq 0x3440
jmp 0x47db
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r12
movq %r15, %rdi
callq 0x9c62
leaq 0xe037(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x49d4
movl 0xc(%rsp), %edi
callq 0x960f
testl %eax, %eax
je 0x496c
leaq 0xe65e(%rip), %rax      # 0x12fc4
movl $0x1, (%rax)
leaq 0xe4ad(%rip), %rax      # 0x12e20
cmpb $0x0, (%rax)
je 0x497f
xorl %edi, %edi
callq 0xa03e
movl $0x1, %edi
callq 0x34d0
movl $0x0, 0xd971(%rip)      # 0x12304
leal 0x1(%r12), %ebx
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x9ab1
movq %rax, %r15
movl %r12d, 0x1c(%rsp)
testq %rax, %rax
jne 0x457d
movq 0xd63b(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x6704(%rip), %rsi      # 0xb0cb
xorl %r13d, %r13d
movq %r14, %rdx
xorl %eax, %eax
callq 0x32a0
movl %r13d, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x354e