// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq 0xd624(%rip), %rax      # 0x11fd0
movq (%rax), %rax
leaq 0xe602(%rip), %rcx      # 0x12fb8
movq %rax, (%rcx)
leaq 0x69d2(%rip), %rdi      # 0xb392
callq 0x30c0
movq %rbx, 0x8(%rsp)
movq (%rbx), %rax
movl %ebp, %ebx
leaq 0xdfca(%rip), %rcx      # 0x129a0
movq %rax, (%rcx)
leaq 0xe308(%rip), %rax      # 0x12ce8
movl $0x4, (%rax)
leaq 0xdfc3(%rip), %rcx      # 0x129b0
xorl %eax, %eax
movb %al, (%rcx)
leaq 0xe5c8(%rip), %rcx      # 0x12fc0
movl $0xa, (%rcx)
movaps 0x661b(%rip), %xmm0     # 0xb020
leaq 0xe3ac(%rip), %rbp      # 0x12db8
movups %xmm0, (%rbp)
movl %eax, 0x60(%rbp)
leaq 0xe5ae(%rip), %rcx      # 0x12fc8
movl %eax, (%rcx)
leaq 0xe05d(%rip), %rcx      # 0x12a80
movb %al, (%rcx)
leaq 0xe124(%rip), %rax      # 0x12b50
movabsq $0x53254d2548252d, %rcx # imm = 0x53254D2548252D
movq %rcx, 0xe(%rax)
movups 0x675f(%rip), %xmm0     # 0xb1a0
movaps %xmm0, (%rax)
xorl %edi, %edi
callq 0x3300
movq %rax, 0xd8b6(%rip)      # 0x12308
cmpl $0x2, %ebx
movl %ebx, 0x14(%rsp)
jl 0x4abb
movl %ebx, %ebx
leaq 0x6752(%rip), %r14      # 0xb1b6
movl $0x1, %r15d
leaq 0x6755(%rip), %r12      # 0xb1c6
movq 0x8(%rsp), %rax
movq (%rax,%r15,8), %r13
movq %r13, %rdi
leaq 0x673f(%rip), %rsi      # 0xb1c3
callq 0x3250
testl %eax, %eax
je 0x4e11
movq %r13, %rdi
movq %r12, %rsi
callq 0x3250
testl %eax, %eax
jne 0x4ab1
movq 0x8(%rsp), %rax
movq 0x8(%rax,%r15,8), %rax
testq %rax, %rax
cmovneq %rax, %r14
incq %r15
cmpq %r15, %rbx
jne 0x4a71
jmp 0x4ac2
leaq 0x66f4(%rip), %r14      # 0xb1b6
movq %r14, %rdi
callq 0x57d1
movl 0x14(%rsp), %ebx
movl %ebx, %edi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x4e8c
movl %eax, %r14d
leaq 0xe33a(%rip), %rax      # 0x12e20
cmpb $0x0, (%rax)
je 0x4b00
leaq 0xe4c6(%rip), %rax      # 0x12fb8
movq $0x0, (%rax)
xorl %edi, %edi
callq 0xa043
leaq 0xe739(%rip), %rax      # 0x13240
cmpb $0x0, (%rax)
leaq 0xe1d7(%rip), %r12      # 0x12ce8
leaq 0xde98(%rip), %r13      # 0x129b0
je 0x4b26
leaq 0xe71f(%rip), %rdi      # 0x13240
callq 0xa3af
movl (%r12), %esi
movl (%rbp), %eax
movl 0x4(%rbp), %ecx
cltd
idivl %esi
movl %eax, (%rbp)
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x4(%rbp)
movl 0x8(%rbp), %eax
cltd
idivl %esi
movl %eax, 0x8(%rbp)
movl 0xc(%rbp), %eax
cltd
idivl %esi
movl %eax, 0xc(%rbp)
movslq 0x60(%rbp), %rdi
testq %rdi, %rdi
jle 0x4b9f
shlq $0x4, %rdi
xorl %r8d, %r8d
movl 0x10(%rbp,%r8), %eax
movl 0x14(%rbp,%r8), %ecx
cltd
idivl %esi
movl %eax, 0x10(%rbp,%r8)
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x14(%rbp,%r8)
movl 0x18(%rbp,%r8), %eax
cltd
idivl %esi
movl %eax, 0x18(%rbp,%r8)
movl 0x1c(%rbp,%r8), %eax
cltd
idivl %esi
movl %eax, 0x1c(%rbp,%r8)
addq $0x10, %r8
cmpq %r8, %rdi
jne 0x4b60
cmpb $0x0, (%r13)
je 0x4bc4
leaq 0xddd3(%rip), %rax      # 0x12980
movl (%rax), %edx
leaq 0x661c(%rip), %rdi      # 0xb1d2
leaq 0xddf3(%rip), %rsi      # 0x129b0
xorl %eax, %eax
callq 0x3170
leaq 0xdeb5(%rip), %rax      # 0x12a80
cmpb $0x0, (%rax)
je 0x4be5
leaq 0x6621(%rip), %rdi      # 0xb1f8
leaq 0xdea2(%rip), %rsi      # 0x12a80
xorl %eax, %eax
callq 0x3170
leaq 0xe3dc(%rip), %rax      # 0x12fc8
movl (%rax), %esi
testl %esi, %esi
je 0x4c00
leaq 0x6613(%rip), %rdi      # 0xb20c
xorl %eax, %eax
callq 0x3170
leaq 0xe0e9(%rip), %rax      # 0x12cf0
cmpb $0x0, (%rax)
je 0x4c89
leaq 0x661c(%rip), %rdi      # 0xb22f
leaq 0xe0d6(%rip), %rsi      # 0x12cf0
xorl %eax, %eax
callq 0x3170
cmpl $0x0, (%rbp)
jne 0x4e20
cmpl $0x0, 0x8(%rbp)
jne 0x4e20
cmpl $0x186a0, 0x4(%rbp)     # imm = 0x186A0
jl 0x4e20
cmpl $0x1869f, 0xc(%rbp)     # imm = 0x1869F
jle 0x4e20
cmpl $0x0, 0x60(%rbp)
jne 0x4e25
movl (%r12), %esi
leaq 0xe08c(%rip), %rdi      # 0x12cf0
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x73ff
testq %rax, %rax
je 0x4e16
movq %rax, %r15
movq %rax, %rdi
callq 0x6bda
movq %r15, %rdi
callq 0x3040
leaq 0xdcf0(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x4ca3
leaq 0xdd14(%rip), %rdi      # 0x129b0
xorl %esi, %esi
callq 0x98f1
leaq 0xe696(%rip), %r15      # 0x13340
cmpb $0x0, (%r15)
je 0x4cbe
leaq 0xe689(%rip), %rdi      # 0x13340
xorl %esi, %esi
callq 0x98f1
cmpb $0x0, (%r13)
setne %al
subl %r14d, %ebx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4d03
leaq 0xe586(%rip), %rax      # 0x13260
cmpl $0x0, (%rax)
je 0x4cf7
cmpb $0x0, (%r15)
je 0x4e2a
leaq 0xdcc0(%rip), %rdi      # 0x129b0
callq 0x44b6
jmp 0x4d03
leaq 0xdcb2(%rip), %rdi      # 0x129b0
callq 0x3a5c
cmpl $0x2, %ebx
jne 0x4dad
movslq %r14d, %rbx
movq 0x8(%rsp), %r15
movq (%r15,%rbx,8), %rsi
leaq 0x65b1(%rip), %rdi      # 0xb2d0
xorl %eax, %eax
callq 0x3170
movq (%r15,%rbx,8), %rdi
movl (%r12), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x73ff
movq %rax, %r14
movq 0x8(%r15,%rbx,8), %rsi
leaq 0x6589(%rip), %rdi      # 0xb2cf
xorl %eax, %eax
callq 0x3170
movq 0x8(%r15,%rbx,8), %rdi
movl (%r12), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x73ff
movq %rax, %rbx
testq %r14, %r14
setne %al
testq %rbx, %rbx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4d9b
leaq 0xdc1c(%rip), %rax      # 0x12998
movl $0x2, (%rax)
leaq 0x6550(%rip), %r8       # 0xb2d9
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x5b14
movq %r14, %rdi
callq 0x3040
movq %rbx, %rdi
callq 0x3040
jmp 0x4e00
cmpl %r14d, 0x14(%rsp)
jle 0x4e00
movslq %r14d, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
movl %ebx, %r15d
leaq 0x6518(%rip), %rbx      # 0xb2e2
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 0x73ff
movq %rax, %rdi
callq 0xa80a
incq %r12
cmpl %r12d, %r15d
jne 0x4dcd
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4e30
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
callq 0x3588
callq 0x3563
callq 0x35ad
nop