// File: /workspace/github_c_makefile_star2_O1/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 $0x248, %rsp            # imm = 0x248
movq %rdi, 0x10(%rsp)
leaq 0xeaf1(%rip), %rax      # 0x12fc4
movl $0x0, (%rax)
leaq 0xed90(%rip), %rbx      # 0x13270
leaq 0x6b95(%rip), %rsi      # 0xb07c
movq %rbx, %rdi
callq 0x34f0
subl %ebx, %eax
testl %eax, %eax
jle 0x498d
movl %eax, %eax
leaq (%rsp,%rax), %rcx
addq $0x50, %rcx
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
leaq 0x8(%rbx,%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0xea9a(%rip), %r14      # 0x12fb8
movl $0x0, 0xc(%rsp)
movl $0x0, 0xddd4(%rip)      # 0x12304
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x9ac7
testq %rax, %rax
je 0x494c
movq %rax, %r15
xorl %edi, %edi
callq 0x3300
movq %rax, 0x18(%rsp)
movl 0x8(%rsp), %edx
cmpl $0x1, %edx
je 0x45fb
movq (%r14), %rdi
leaq 0x6b64(%rip), %rsi      # 0xb0d2
xorl %eax, %eax
callq 0x3290
movq (%r14), %rdi
movq 0x18(%rsp), %r9
movq %r9, %rax
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
imulq $0xe10, %rdx, %rax      # imm = 0xE10
movq %r9, %rsi
subq %rax, %rsi
movq %rsi, %rax
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
imulq %rcx
movq %rdx, %r8
addq %rsi, %r8
movq %r8, %rax
shrq $0x3f, %rax
shrq $0x5, %r8
addl %eax, %r8d
movq %r9, %rax
imulq %rcx
addq %r9, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
movl %r9d, %ecx
subl %eax, %ecx
leaq 0x6b32(%rip), %rsi      # 0xb123
movl %r8d, %edx
xorl %eax, %eax
callq 0x3290
movl 0x8(%rsp), %esi
testl %esi, %esi
jle 0x48df
movq 0x18(%rsp), %rdi
movq %rdi, %rax
movabsq $0x48d159e26af37c05, %rcx # imm = 0x48D159E26AF37C05
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xa, %rdx
addq %rax, %rdx
imulq $0xe10, %rdx, %rax      # imm = 0xE10
movq %rdi, %rcx
subq %rax, %rcx
movslq %ecx, %rcx
imulq $-0x77777777, %rcx, %rax # imm = 0x88888889
shrq $0x20, %rax
addl %eax, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
sarl $0x5, %ecx
addl %eax, %ecx
movq %rcx, 0x48(%rsp)
movq %rdi, %rax
movabsq $-0x7777777777777777, %rcx # imm = 0x8888888888888889
imulq %rcx
addq %rdi, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x5, %rdx
addl %eax, %edx
imull $0x3c, %edx, %eax
movl %edi, %ecx
subl %eax, %ecx
movl %ecx, 0x24(%rsp)
movq %r15, 0x28(%rsp)
addq $0x18, %r15
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
movq 0x18(%rsp), %rax
movl %eax, %ecx
subl -0x10(%r15), %ecx
testl %ecx, %ecx
jle 0x47a9
movq %r14, %r12
movq (%r14), %rdi
movl -0x18(%r15), %ecx
shrl $0xa, %ecx
leaq 0x6a47(%rip), %rsi      # 0xb103
movq %r15, %rdx
movq 0x48(%rsp), %r8
movl 0x24(%rsp), %r9d
xorl %eax, %eax
callq 0x3290
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x9834
leaq 0x180(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x30a0
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdx
callq 0x3090
movb $0x0, 0x50(%rsp,%rbx)
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x30a0
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
callq 0x3460
movl -0x10(%r15), %ebx
movl $0xc4653600, %eax       # imm = 0xC4653600
addl %eax, %ebx
movq %rbp, %rdi
callq 0x3130
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
leaq 0x69ea(%rip), %rsi      # 0xb133
movl %ebx, %edx
leaq 0xebee(%rip), %rcx      # 0x13340
xorl %eax, %eax
callq 0x3470
callq 0x3070
movq %rax, %r14
movl $0x0, (%rax)
movq %rbp, %rdi
callq 0x3150
testl %eax, %eax
jne 0x4779
cmpl $0x0, (%r14)
je 0x47e2
cmpl $0x0, (%r14)
jne 0x48c7
movq %r12, %r14
movq (%r12), %rdi
leaq 0x69c8(%rip), %rsi      # 0xb159
leaq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x3290
leaq 0xeacc(%rip), %rbx      # 0x13270
jmp 0x48b0
cmpl $0x2, %esi
setl %al
movl $0x1, 0x4(%rsp)
testb %al, 0xc(%rsp)
jne 0x48b0
movq (%r14), %rdi
movl -0x18(%r15), %r8d
shrl $0xa, %r8d
leaq 0x6913(%rip), %rsi      # 0xb0e6
movq %r15, %rdx
xorl %eax, %eax
callq 0x3290
jmp 0x48b0
leaq 0xeb57(%rip), %rdi      # 0x13340
movl $0x1, %esi
callq 0x3bfa
testl %eax, %eax
je 0x488d
movl %eax, %ebx
movq (%r12), %rdi
leaq 0x6966(%rip), %rsi      # 0xb16e
movl %eax, %edx
xorl %eax, %eax
callq 0x3290
leaq 0x180(%rsp), %rdi
movl %ebx, %esi
movl $0x1, %edx
callq 0x9c76
movq %rax, %rbx
movq %rax, %rdi
leaq 0x694f(%rip), %rsi      # 0xb181
callq 0x34f0
testq %rax, %rax
je 0x4846
movb $0x0, 0x4(%rax)
movl $0x34706d2e, (%rax)     # imm = 0x34706D2E
testq %rbx, %rbx
je 0x488d
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
leaq 0x6932(%rip), %rsi      # 0xb18c
leaq 0x180(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3470
movl $0x0, (%r14)
movq %rbp, %rdi
callq 0x3150
testl %eax, %eax
jne 0x4779
cmpl $0x0, (%r14)
jne 0x4779
leaq 0xe0ec(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x48a6
leaq 0x180(%rsp), %rdi
callq 0x3080
leaq 0xe9c3(%rip), %rbx      # 0x13270
movq %r12, %r14
incq %r13
movslq 0x8(%rsp), %rsi
addq $0x50, %r15
cmpq %rsi, %r13
jl 0x4695
jmp 0x48d8
leaq 0x6884(%rip), %rdi      # 0xb152
callq 0x3430
jmp 0x4783
movq 0x28(%rsp), %r15
jmp 0x48ea
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
movq %r15, %rdi
callq 0x9c71
leaq 0xe087(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x4939
movl 0x4(%rsp), %edi
callq 0x9620
testl %eax, %eax
je 0x4918
leaq 0xe6b2(%rip), %rax      # 0x12fc4
movl $0x1, (%rax)
leaq 0xe501(%rip), %rax      # 0x12e20
cmpb $0x0, (%rax)
je 0x492b
xorl %edi, %edi
callq 0xa043
movl $0x1, %edi
callq 0x34c0
xorl %eax, %eax
jmp 0x493e
movl $0x2, %eax
incl 0xc(%rsp)
testl %eax, %eax
je 0x4526
jmp 0x4970
movq 0xd6a5(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x675e(%rip), %rsi      # 0xb0bb
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x3290
movl $0x1, %eax
jmp 0x493e
cmpl $0x2, %eax
je 0x4978
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x353e