// File: /workspace/github_c_makefile_star2_O2/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 0xdaf0(%rip), %rax      # 0x11fd0
movq (%rax), %rax
leaq 0xeade(%rip), %rcx      # 0x12fc8
movq %rax, (%rcx)
leaq 0x6eab(%rip), %rdi      # 0xb39f
callq 0x30c0
movq (%rbx), %rax
leaq 0xe4ad(%rip), %rcx      # 0x129b0
movq %rax, (%rcx)
leaq 0xe7eb(%rip), %rax      # 0x12cf8
movl $0x4, (%rax)
leaq 0xe4a6(%rip), %rcx      # 0x129c0
xorl %eax, %eax
movb %al, (%rcx)
leaq 0xeaab(%rip), %rcx      # 0x12fd0
movaps 0x6b04(%rip), %xmm0     # 0xb030
leaq 0xe895(%rip), %r15      # 0x12dc8
movups %xmm0, (%r15)
andl $0x0, 0x60(%r15)
leaq 0xea95(%rip), %rdx      # 0x12fd8
andl $0x0, (%rdx)
movl $0xa, (%rcx)
leaq 0xe53d(%rip), %rcx      # 0x12a90
movb %al, (%rcx)
leaq 0xe604(%rip), %rax      # 0x12b60
movabsq $0x53254d2548252d, %rcx # imm = 0x53254D2548252D
movq %rcx, 0xe(%rax)
movups 0x6c3f(%rip), %xmm0     # 0xb1b0
movaps %xmm0, (%rax)
xorl %edi, %edi
callq 0x3310
movq %rax, 0xdd96(%rip)      # 0x12318
movl %ebp, (%rsp)
movslq %ebp, %rbp
leaq 0x6c37(%rip), %r14      # 0xb1c6
pushq $0x1
popq %r13
leaq 0x6c39(%rip), %r12      # 0xb1d3
cmpq %rbp, %r13
jge 0x45d5
movq (%rbx,%r13,8), %rdi
cmpb $0x2d, (%rdi)
jne 0x45b8
cmpb $0x68, 0x1(%rdi)
jne 0x45b8
cmpb $0x0, 0x2(%rdi)
je 0x48dc
movq %r12, %rsi
callq 0x3260
testl %eax, %eax
jne 0x45d0
movq 0x8(%rbx,%r13,8), %rax
testq %rax, %rax
cmovneq %rax, %r14
incq %r13
jmp 0x459a
movq %r14, %rdi
callq 0x5322
movl (%rsp), %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x49b2
movl %eax, %r14d
leaq 0xe83c(%rip), %rax      # 0x12e30
cmpb $0x0, (%rax)
je 0x460b
leaq 0xe9c8(%rip), %rax      # 0x12fc8
andq $0x0, (%rax)
xorl %edi, %edi
callq 0x96a4
movl %r14d, 0x4(%rsp)
leaq 0xec39(%rip), %rax      # 0x13250
cmpb $0x0, (%rax)
je 0x4628
leaq 0xec2d(%rip), %rdi      # 0x13250
callq 0x9a24
leaq 0xe6c9(%rip), %rax      # 0x12cf8
movl (%rax), %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0x4931
leaq 0x10(%r15), %r13
xorl %r14d, %r14d
movslq 0x60(%r15), %rax
cmpq %rax, %r14
jge 0x4661
movq %r13, %rdi
movl %r12d, %esi
callq 0x4931
incq %r14
addq $0x10, %r13
jmp 0x4644
leaq 0xe358(%rip), %r12      # 0x129c0
cmpb $0x0, (%r12)
je 0x468d
leaq 0xe31a(%rip), %rax      # 0x12990
movl (%rax), %edx
leaq 0x6b60(%rip), %rdi      # 0xb1df
leaq 0xe33a(%rip), %rsi      # 0x129c0
xorl %eax, %eax
callq 0x3170
leaq 0xe3fc(%rip), %rax      # 0x12a90
cmpb $0x0, (%rax)
movl (%rsp), %r14d
je 0x46b2
leaq 0x6b61(%rip), %rdi      # 0xb205
leaq 0xe3e5(%rip), %rsi      # 0x12a90
xorl %eax, %eax
callq 0x3170
leaq 0xe91f(%rip), %rax      # 0x12fd8
movl (%rax), %esi
testl %esi, %esi
je 0x46cd
leaq 0x6b53(%rip), %rdi      # 0xb219
xorl %eax, %eax
callq 0x3170
leaq 0xe62c(%rip), %rax      # 0x12d00
cmpb $0x0, (%rax)
je 0x4763
leaq 0x6b58(%rip), %rdi      # 0xb23c
leaq 0xe615(%rip), %rsi      # 0x12d00
xorl %eax, %eax
callq 0x3170
cmpl $0x0, (%r15)
jne 0x48e9
cmpl $0x0, 0x8(%r15)
jne 0x48e9
cmpl $0x186a0, 0x4(%r15)     # imm = 0x186A0
jl 0x48e9
cmpl $0x1869f, 0xc(%r15)     # imm = 0x1869F
jle 0x48e9
cmpl $0x0, 0x60(%r15)
jne 0x48fe
leaq 0xe5c3(%rip), %rax      # 0x12cf8
movl (%rax), %esi
leaq 0xe5c2(%rip), %rdi      # 0x12d00
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6d01
testq %rax, %rax
je 0x48e1
movq %rax, %r15
movq %rax, %rdi
callq 0x6602
movq %r15, %rdi
callq 0x3040
leaq 0xe226(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x477d
leaq 0xe24a(%rip), %rdi      # 0x129c0
xorl %esi, %esi
callq 0x8f67
leaq 0xebcc(%rip), %r15      # 0x13350
cmpb $0x0, (%r15)
je 0x4798
leaq 0xebbf(%rip), %rdi      # 0x13350
xorl %esi, %esi
callq 0x8f67
cmpb $0x0, (%r12)
setne %al
movl 0x4(%rsp), %r12d
subl %r12d, %r14d
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x47e2
leaq 0xeab7(%rip), %rax      # 0x13270
cmpl $0x0, (%rax)
je 0x47d6
cmpb $0x0, (%r15)
je 0x4913
leaq 0xe1f1(%rip), %rdi      # 0x129c0
callq 0x406b
jmp 0x47e2
leaq 0xe1e3(%rip), %rdi      # 0x129c0
callq 0x363c
movslq %r12d, %r13
cmpl $0x2, %r14d
jne 0x489c
movq (%rbx,%r13,8), %rsi
leaq 0x6ae3(%rip), %rdi      # 0xb2dd
xorl %eax, %eax
callq 0x3170
movq (%rbx,%r13,8), %rdi
leaq 0xe4ec(%rip), %r15      # 0x12cf8
movl (%r15), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6d01
movq %rax, %r14
movq 0x8(%rbx,%r13,8), %rsi
leaq 0x6ab5(%rip), %rdi      # 0xb2dc
xorl %eax, %eax
callq 0x3170
movq 0x8(%rbx,%r13,8), %rdi
movl (%r15), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6d01
movq %rax, %rbx
testq %r14, %r14
setne %al
testq %rbx, %rbx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x487b
leaq 0xe14c(%rip), %rax      # 0x129a8
movl $0x2, (%rax)
leaq 0x6a7d(%rip), %r8       # 0xb2e6
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x5674
movq %r14, %rdi
callq 0x3040
movq %rbx, %rdi
callq 0x3040
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a4c(%rip), %r14      # 0xb2ef
pushq $0x4
popq %r12
pushq $0x1
popq %r15
cmpq %rbp, %r13
jge 0x488b
movq (%rbx,%r13,8), %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x3170
movq (%rbx,%r13,8), %rdi
movl %r12d, %esi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x6d01
movq %rax, %rdi
callq 0x9e9e
incq %r13
jmp 0x48ab
callq 0x4958
pushq $-0x1
popq %rdi
callq 0x34a0
movq 0xd708(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6958(%rip), %rdi      # 0xb252
pushq $0x31
jmp 0x4926
movq 0xd6f3(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6975(%rip), %rdi      # 0xb284
pushq $0x32
jmp 0x4926
movq 0xd6de(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6993(%rip), %rdi      # 0xb2b7
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
jmp 0x48e1