// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0xfce3(%rip), %rax      # 0x13fd0
movq (%rax), %rcx
leaq 0x10d21(%rip), %rax     # 0x15018
movq %rcx, (%rax)
leaq 0x9e87(%rip), %rdi      # 0xe188
movb $0x0, %al
callq 0x3170
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq 0x106ea(%rip), %rax     # 0x14a00
movq %rcx, (%rax)
leaq 0x10a28(%rip), %rax     # 0x14d48
movl $0x4, (%rax)
leaq 0x106e3(%rip), %rax     # 0x14a10
movb $0x0, (%rax)
leaq 0x10ce9(%rip), %rax     # 0x15020
movl $0xa, (%rax)
leaq 0x10ad4(%rip), %rax     # 0x14e18
movl $0x0, (%rax)
leaq 0x10ac7(%rip), %rax     # 0x14e18
movl $0xf4240, 0x4(%rax)     # imm = 0xF4240
leaq 0x10ab9(%rip), %rax     # 0x14e18
movl $0x0, 0x8(%rax)
leaq 0x10aab(%rip), %rax     # 0x14e18
movl $0xf4240, 0xc(%rax)     # imm = 0xF4240
leaq 0x10a9d(%rip), %rax     # 0x14e18
movl $0x0, 0x60(%rax)
leaq 0x10c9f(%rip), %rax     # 0x15028
movl $0x0, (%rax)
leaq 0x1074a(%rip), %rax     # 0x14ae0
movb $0x0, (%rax)
leaq 0x10810(%rip), %rdi     # 0x14bb0
leaq 0x9e16(%rip), %rsi      # 0xe1bd
callq 0x30b0
xorl %eax, %eax
movl %eax, %edi
callq 0x3300
movq %rax, 0xffac(%rip)      # 0x14368
leaq 0x9e10(%rip), %rax      # 0xe1d3
movq %rax, -0x28(%rbp)
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x4460
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x9deb(%rip), %rsi      # 0xe1e0
callq 0x3250
cmpl $0x0, %eax
jne 0x440e
callq 0x4f30
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movq -0x30(%rbp), %rdi
leaq 0x9dca(%rip), %rsi      # 0xe1e3
callq 0x3250
cmpl $0x0, %eax
jne 0x444e
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x444c
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x444e
jmp 0x4450
jmp 0x4452
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x43ce
movq -0x28(%rbp), %rdi
callq 0x5dd0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x4f90
movl %eax, -0x14(%rbp)
leaq 0x109ff(%rip), %rax     # 0x14e80
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x449e
leaq 0x10b88(%rip), %rax     # 0x15018
movq $0x0, (%rax)
xorl %edi, %edi
callq 0xc6d0
leaq 0x10dfb(%rip), %rax     # 0x152a0
cmpb $0x0, (%rax)
je 0x44b6
leaq 0x10def(%rip), %rdi     # 0x152a0
callq 0xcb10
leaq 0x1088b(%rip), %rax     # 0x14d48
movl (%rax), %esi
leaq 0x10952(%rip), %rdi     # 0x14e18
callq 0x4860
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
leaq 0x1093c(%rip), %rcx     # 0x14e18
cmpl 0x60(%rcx), %eax
jge 0x4510
movslq -0x18(%rbp), %rax
leaq 0x1092c(%rip), %rdi     # 0x14e18
addq $0x10, %rdi
shlq $0x4, %rax
addq %rax, %rdi
leaq 0x1084a(%rip), %rax     # 0x14d48
movl (%rax), %esi
callq 0x4860
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x44d2
leaq 0x104f9(%rip), %rax     # 0x14a10
cmpb $0x0, (%rax)
je 0x453a
leaq 0x104bd(%rip), %rax     # 0x149e0
movl (%rax), %edx
leaq 0x9cc3(%rip), %rdi      # 0xe1ef
leaq 0x104dd(%rip), %rsi     # 0x14a10
movb $0x0, %al
callq 0x3170
leaq 0x1059f(%rip), %rax     # 0x14ae0
cmpb $0x0, (%rax)
je 0x455b
leaq 0x9cc8(%rip), %rdi      # 0xe215
leaq 0x1058c(%rip), %rsi     # 0x14ae0
movb $0x0, %al
callq 0x3170
leaq 0x10ac6(%rip), %rax     # 0x15028
cmpl $0x0, (%rax)
je 0x457e
leaq 0x10aba(%rip), %rax     # 0x15028
movl (%rax), %esi
leaq 0x9cb2(%rip), %rdi      # 0xe229
movb $0x0, %al
callq 0x3170
leaq 0x107cb(%rip), %rax     # 0x14d50
cmpb $0x0, (%rax)
je 0x466d
leaq 0x9cb7(%rip), %rdi      # 0xe24c
leaq 0x107b4(%rip), %rsi     # 0x14d50
movb $0x0, %al
callq 0x3170
leaq 0x1086e(%rip), %rax     # 0x14e18
cmpl $0x0, (%rax)
jne 0x45dc
leaq 0x10862(%rip), %rax     # 0x14e18
cmpl $0x0, 0x8(%rax)
jne 0x45dc
leaq 0x10855(%rip), %rax     # 0x14e18
cmpl $0x186a0, 0x4(%rax)     # imm = 0x186A0
jl 0x45dc
leaq 0x10845(%rip), %rax     # 0x14e18
cmpl $0x186a0, 0xc(%rax)     # imm = 0x186A0
jge 0x45fe
movq 0xfa0d(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x9c75(%rip), %rsi      # 0xe262
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
leaq 0x10813(%rip), %rax     # 0x14e18
cmpl $0x0, 0x60(%rax)
je 0x462d
movq 0xf9de(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x9c78(%rip), %rsi      # 0xe294
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
leaq 0x10714(%rip), %rax     # 0x14d48
movl (%rax), %esi
leaq 0x10713(%rip), %rdi     # 0x14d50
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x8920
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x465b
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
movq -0x38(%rbp), %rdi
callq 0x7cc0
movq -0x38(%rbp), %rdi
callq 0x3040
leaq 0x1036c(%rip), %rax     # 0x149e0
cmpl $0x0, (%rax)
je 0x4687
leaq 0x10390(%rip), %rdi     # 0x14a10
xorl %esi, %esi
callq 0xbb80
leaq 0x10d12(%rip), %rax     # 0x153a0
cmpb $0x0, (%rax)
je 0x46a1
leaq 0x10d06(%rip), %rdi     # 0x153a0
xorl %esi, %esi
callq 0xbb80
leaq 0x10368(%rip), %rax     # 0x14a10
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x4711
movl -0x14(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x4711
leaq 0x10c01(%rip), %rax     # 0x152c0
cmpl $0x0, (%rax)
jne 0x46d2
leaq 0x10345(%rip), %rdi     # 0x14a10
callq 0x3620
jmp 0x470f
leaq 0x10cc7(%rip), %rax     # 0x153a0
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x4703
movq 0xf908(%rip), %rax      # 0x13ff0
movq (%rax), %rdi
leaq 0x9bd5(%rip), %rsi      # 0xe2c7
movb $0x0, %al
callq 0x3290
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3480
leaq 0x10306(%rip), %rdi     # 0x14a10
callq 0x3d30
jmp 0x4711
movl -0x8(%rbp), %eax
subl -0x14(%rbp), %eax
cmpl $0x2, %eax
jne 0x47f6
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x9bba(%rip), %rdi      # 0xe2ed
movb $0x0, %al
callq 0x3170
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x105fb(%rip), %rax     # 0x14d48
movl (%rax), %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x8920
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x9b78(%rip), %rdi      # 0xe2ec
movb $0x0, %al
callq 0x3170
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x105b5(%rip), %rax     # 0x14d48
movl (%rax), %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x8920
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x47e2
cmpq $0x0, -0x48(%rbp)
je 0x47e2
leaq 0x10241(%rip), %rax     # 0x149f8
movl $0x2, (%rax)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
xorl %ecx, %ecx
leaq 0x9b28(%rip), %r8       # 0xe2f6
xorl %eax, %eax
movl %eax, %r9d
movl %ecx, %edx
callq 0x63a0
movq %rax, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3040
movq -0x48(%rbp), %rdi
callq 0x3040
jmp 0x4855
movl -0x14(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x4853
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x9ae8(%rip), %rdi      # 0xe2ff
movb $0x0, %al
callq 0x3170
movq -0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x4, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x8920
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0xd1a0
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x47fc
jmp 0x4855
xorl %eax, %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)