// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/main.c
// Function: main
int main(int argc, char **argv)
{
    int file_index, a, argn;

    Log = stdout;

    printf("Imgcomp version 1.01 (Jun 2023) by Matthias Wandel\n\n");

    progname = argv[0];

    // Reset to default parameters.
    ScaleDenom = 4;
    DoDirName[0] = '\0';
    Sensitivity = 10;
    Regions.DetectReg.x1 = 0;
    Regions.DetectReg.x2 = 1000000;
    Regions.DetectReg.y1 = 0;
    Regions.DetectReg.y2 = 1000000;
    Regions.NumExcludeReg = 0;
    TimelapseInterval = 0;
    SaveDir[0] = 0;
    strcpy(SaveNames, "%y%m%d/%H/%m%d-%H%M%S");
    LastPic_mtime = time(NULL); // Log names are based on this time, need before images.

    // Default configuration file to look for
    char *config_file = "imgcomp.conf";

    for (argn = 1; argn < argc; argn++) {
        //printf("argn = %d\n",argn);
        char * arg;
        arg = argv[argn];
        if (strcmp(arg, "-h") == 0){
            usage();
            exit (-1);
        } else if(strcmp(arg, "-configfile") == 0) {
            // Allow multiple config files by specifying them with -c configfilename.
            // This silently fails (does not override the config file name) if the
            // -configfile is the last argument.
            if(argv[argn+1]) config_file = argv[argn+1];
        }
    }

    // First read the configuration file.
    read_config_file(config_file);

    // Get command line arguments (which may override configuration file)
    file_index = parse_switches(argc, argv, 0);

    if (LogToFile[0] != '\0'){
        Log = NULL;
        LogFileMaintain(0);
    }

    if (UdpDest[0]) InitUDP(UdpDest);

    // Adjust region of interest to scale.
    ScaleRegion(&Regions.DetectReg, ScaleDenom);
    for (a=0;a<Regions.NumExcludeReg;a++){
        ScaleRegion(&Regions.ExcludeReg[a], ScaleDenom);
    }

    if (DoDirName[0]) printf("    Source directory = %s, follow=%d\n",DoDirName, FollowDir);
    if (SaveDir[0]) printf("    Save to dir %s\n",SaveDir);
    if (TimelapseInterval) printf("    Timelapse interval %d seconds\n",TimelapseInterval);

    if (DiffMapFileName[0]){
        MemImage_t *MapPic;
        printf("    Diffmap file: %s\n",DiffMapFileName);
        if (Regions.DetectReg.x1 || Regions.DetectReg.y1
                || (Regions.DetectReg.x2 <  100000) || (Regions.DetectReg.y2 < 100000)){
            fprintf(stderr, "Specify diff map or detect regions, but not both\n");
            exit(-1);
        }

        if (Regions.NumExcludeReg){
            fprintf(stderr, "Specify diff map or exclude regions, but not both\n");
            exit(-1);
        }

        MapPic  = LoadJPEG(DiffMapFileName, ScaleDenom, 0, 0);
        if (MapPic == 0) exit(-1); // error is already reported.

        ProcessDiffMap(MapPic);
        free(MapPic);
    }


    // These directories are likely to be on ramdisk, so they may need re-creating.
    if (FollowDir) EnsurePathExists(DoDirName,0);
    if (TempDirName[0]) EnsurePathExists(TempDirName,0);

    if (DoDirName[0] && file_index == argc){
        // if dodir is specified in config file, but files are specified
        // on the command line, do the files instead.
        if (!VidMode){
            DoDirectory(DoDirName);
        }else{
            if (TempDirName[0] == 0){
                fprintf(stderr, "must specify tempdir for video mode\n");
                exit(-1);
            }
            DoDirectoryVideos(DoDirName);
        }
    }

    if (argc-file_index == 2){
        MemImage_t *pic1, *pic2;

        printf("load %s\n",argv[file_index]);
        pic1 = LoadJPEG(argv[file_index], ScaleDenom, 0, 0);

        printf("\nload %s\n",argv[file_index+1]);
        pic2 = LoadJPEG(argv[file_index+1], ScaleDenom, 0, 0);

        if (pic1 && pic2){
            Verbosity = 2;
            ComparePix(pic1, pic2, 0, 0,"diff.ppm", NULL);
        }
        free(pic1);
        free(pic2);
    }else{
        int a;
        MemImage_t * pic;

        for (a=file_index;a<argc;a++){
            printf("input file %s\n",argv[a]);

            // Load file into memory.
            pic = LoadJPEG(argv[a], 4, 0, 1);

            CalcExposureAdjust(pic);
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl %edi, %ebp
movq 0xd5cb(%rip), %rax      # 0x11fd0
movq (%rax), %rax
leaq 0xe5a9(%rip), %rcx      # 0x12fb8
movq %rax, (%rcx)
leaq 0x6986(%rip), %rdi      # 0xb39f
callq 0x30c0
movq (%rbx), %rax
leaq 0xdf78(%rip), %rcx      # 0x129a0
movq %rax, (%rcx)
leaq 0xe2b6(%rip), %rax      # 0x12ce8
movl $0x4, (%rax)
leaq 0xdf71(%rip), %rcx      # 0x129b0
xorl %eax, %eax
movb %al, (%rcx)
leaq 0xe576(%rip), %rcx      # 0x12fc0
movl $0xa, (%rcx)
movaps 0x65d9(%rip), %xmm0     # 0xb030
leaq 0xe35a(%rip), %r12      # 0x12db8
movups %xmm0, (%r12)
movl %eax, 0x60(%r12)
leaq 0xe559(%rip), %rcx      # 0x12fc8
movl %eax, (%rcx)
leaq 0xe008(%rip), %rcx      # 0x12a80
movb %al, (%rcx)
leaq 0xe0cf(%rip), %rax      # 0x12b50
movabsq $0x53254d2548252d, %rcx # imm = 0x53254D2548252D
movq %rcx, 0xe(%rax)
movups 0x671a(%rip), %xmm0     # 0xb1b0
movaps %xmm0, (%rax)
xorl %edi, %edi
callq 0x3310
movq %rax, 0xd861(%rip)      # 0x12308
cmpl $0x2, %ebp
movl %ebp, 0x4(%rsp)
jl 0x4b01
movl %ebp, %r13d
leaq 0x670c(%rip), %r14      # 0xb1c6
movl $0x1, %ebp
leaq 0x670d(%rip), %r15      # 0xb1d3
movq (%rbx,%rbp,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x4adf
cmpb $0x68, 0x1(%rdi)
jne 0x4adf
cmpb $0x0, 0x2(%rdi)
je 0x4e6a
movq %r15, %rsi
callq 0x3260
testl %eax, %eax
jne 0x4af7
movq 0x8(%rbx,%rbp,8), %rax
testq %rax, %rax
cmovneq %rax, %r14
incq %rbp
cmpq %rbp, %r13
jne 0x4ac6
jmp 0x4b08
leaq 0x66be(%rip), %r14      # 0xb1c6
movq %r14, %rdi
callq 0x5826
movl 0x4(%rsp), %ebp
movl %ebp, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4ee4
movl %eax, %r14d
leaq 0xe2f6(%rip), %rax      # 0x12e20
cmpb $0x0, (%rax)
je 0x4b44
leaq 0xe482(%rip), %rax      # 0x12fb8
movq $0x0, (%rax)
xorl %edi, %edi
callq 0xa03e
leaq 0xe6f5(%rip), %rax      # 0x13240
cmpb $0x0, (%rax)
leaq 0xe193(%rip), %r13      # 0x12ce8
je 0x4b63
leaq 0xe6e2(%rip), %rdi      # 0x13240
callq 0xa3a0
movl (%r13), %esi
movl (%r12), %eax
movl 0x4(%r12), %ecx
cltd
idivl %esi
movl %eax, (%r12)
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x4(%r12)
movl 0x8(%r12), %eax
cltd
idivl %esi
movl %eax, 0x8(%r12)
movl 0xc(%r12), %eax
cltd
idivl %esi
movl %eax, 0xc(%r12)
movslq 0x60(%r12), %rdi
testq %rdi, %rdi
jle 0x4beb
shlq $0x4, %rdi
xorl %r8d, %r8d
movl 0x10(%r12,%r8), %eax
movl 0x14(%r12,%r8), %ecx
cltd
idivl %esi
movl %eax, 0x10(%r12,%r8)
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x14(%r12,%r8)
movl 0x18(%r12,%r8), %eax
cltd
idivl %esi
movl %eax, 0x18(%r12,%r8)
movl 0x1c(%r12,%r8), %eax
cltd
idivl %esi
movl %eax, 0x1c(%r12,%r8)
addq $0x10, %r8
cmpq %r8, %rdi
jne 0x4bac
leaq 0xddbe(%rip), %rax      # 0x129b0
cmpb $0x0, (%rax)
je 0x4c15
leaq 0xdd82(%rip), %rax      # 0x12980
movl (%rax), %edx
leaq 0x65d8(%rip), %rdi      # 0xb1df
leaq 0xdda2(%rip), %rsi      # 0x129b0
xorl %eax, %eax
callq 0x3170
leaq 0xde64(%rip), %rax      # 0x12a80
cmpb $0x0, (%rax)
je 0x4c36
leaq 0x65dd(%rip), %rdi      # 0xb205
leaq 0xde51(%rip), %rsi      # 0x12a80
xorl %eax, %eax
callq 0x3170
leaq 0xe38b(%rip), %rax      # 0x12fc8
movl (%rax), %esi
testl %esi, %esi
je 0x4c51
leaq 0x65cf(%rip), %rdi      # 0xb219
xorl %eax, %eax
callq 0x3170
leaq 0xe098(%rip), %rax      # 0x12cf0
cmpb $0x0, (%rax)
je 0x4ce7
leaq 0x65d4(%rip), %rdi      # 0xb23c
leaq 0xe081(%rip), %rsi      # 0x12cf0
xorl %eax, %eax
callq 0x3170
cmpl $0x0, (%r12)
jne 0x4e79
cmpl $0x0, 0x8(%r12)
jne 0x4e79
cmpl $0x186a0, 0x4(%r12)     # imm = 0x186A0
jl 0x4e79
cmpl $0x1869f, 0xc(%r12)     # imm = 0x1869F
jle 0x4e79
cmpl $0x0, 0x60(%r12)
jne 0x4e7e
movl (%r13), %esi
leaq 0xe02e(%rip), %rdi      # 0x12cf0
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x753f
testq %rax, %rax
je 0x4e6f
movq %rax, %r15
movq %rax, %rdi
callq 0x6d4f
movq %r15, %rdi
callq 0x3040
leaq 0xdc92(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x4d01
leaq 0xdcb6(%rip), %rdi      # 0x129b0
xorl %esi, %esi
callq 0x98e5
leaq 0xe638(%rip), %r15      # 0x13340
cmpb $0x0, (%r15)
je 0x4d1c
leaq 0xe62b(%rip), %rdi      # 0x13340
xorl %esi, %esi
callq 0x98e5
leaq 0xdc8d(%rip), %rax      # 0x129b0
cmpb $0x0, (%rax)
setne %al
subl %r14d, %ebp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4d66
leaq 0xe523(%rip), %rax      # 0x13260
cmpl $0x0, (%rax)
je 0x4d5a
cmpb $0x0, (%r15)
je 0x4e83
leaq 0xdc5d(%rip), %rdi      # 0x129b0
callq 0x44d9
jmp 0x4d66
leaq 0xdc4f(%rip), %rdi      # 0x129b0
callq 0x3a6c
cmpl $0x2, %ebp
jne 0x4e0b
movslq %r14d, %r15
movq (%rbx,%r15,8), %rsi
leaq 0x6560(%rip), %rdi      # 0xb2dd
xorl %eax, %eax
callq 0x3170
movq (%rbx,%r15,8), %rdi
movl (%r13), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x753f
movq %rax, %r14
movq 0x8(%rbx,%r15,8), %rsi
leaq 0x6538(%rip), %rdi      # 0xb2dc
xorl %eax, %eax
callq 0x3170
movq 0x8(%rbx,%r15,8), %rdi
movl (%r13), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x753f
movq %rax, %rbx
testq %r14, %r14
setne %al
testq %rbx, %rbx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4df9
leaq 0xdbbe(%rip), %rax      # 0x12998
movl $0x2, (%rax)
leaq 0x64ff(%rip), %r8       # 0xb2e6
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x5b88
movq %r14, %rdi
callq 0x3040
movq %rbx, %rdi
callq 0x3040
jmp 0x4e59
cmpl %r14d, 0x4(%rsp)
jle 0x4e59
movslq %r14d, %rax
leaq (%rbx,%rax,8), %r14
movl %ebp, %r15d
leaq 0x64cc(%rip), %rbx      # 0xb2ef
xorl %r12d, %r12d
movq (%r14,%r12,8), %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3170
movq (%r14,%r12,8), %rdi
movl $0x4, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x753f
movq %rax, %rdi
callq 0xa812
incq %r12
cmpl %r12d, %r15d
jne 0x4e26
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4e88
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3490
callq 0x3598
callq 0x3573
callq 0x35bd