// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/compare.c
// Function: ComparePix
TriggerInfo_t ComparePix(MemImage_t * pic1, MemImage_t * pic2,
    int UpdateFatigue, int SkipFatigue, char * DebugImgName, TriggerInfo_t * no_fatigue_motion)
{
    int width, height, bPerRow;
    int row, col;
    MemImage_t * DiffOut = NULL;
    int DiffHist[256];
    int a;
    int DetectionPixels;
    int m1i, m2i;
    double BrightnessRatio;
    Region_t MainReg;
    TriggerInfo_t RetVal;
    RetVal.x = RetVal.y = 0;
    RetVal.DiffLevel = -1;
    RetVal.Motion = 0;

    if (Verbosity){
        printf("\ncompare pictures %dx%d %d\n", pic1->width, pic1->height, pic1->components);
    }

    if (pic1->width != pic2->width || pic1->height != pic2->height
        || pic1->components != pic2->components){
        fprintf(stderr, "pic size mismatch (maybe clear ramdisk?)\n  %dx%d vs %dx%d\n",pic1->width, pic1->height, pic2->width, pic2->height);
        exit(-1);
        //return RetVal;
    }
    width = pic1->width;
    height = pic1->height;
    bPerRow = width * 3;

    // Make sure we have an allocated working array and weight map.

    if (DiffVal != NULL && (width != DiffVal->w || height != DiffVal->h)){
        // DiffVal allocated size is wrong.
        free(DiffVal);
        free(WeightMap);
        DiffVal = NULL;
    }
    if (DiffVal == NULL){
        DiffVal = MakeImgMap(width,height);

        if (!WeightMap){
            FillWeightMap(width,height);
        }else{
            if (WeightMap->w != width || WeightMap->h != height){
                fprintf(stderr,"diff map image size mismatch\n");
                exit(-1);
            }
        }
    }
    memset(DiffVal->values, 0,  sizeof(DiffVal->values)*width*height);

    if (DebugImgName){
        // Create image for writing difference to.
        int data_size;
        data_size = height * bPerRow;
        DiffOut = malloc(data_size+offsetof(MemImage_t, pixels));
        memcpy(DiffOut, pic1, offsetof(MemImage_t, pixels));
        memset(DiffOut->pixels, 0, data_size);
    }

    MainReg = Regions.DetectReg;
    if (MainReg.y2 > height) MainReg.y2 = height;
    if (MainReg.x2 > width) MainReg.x2 = width;
    if (MainReg.x2 < MainReg.x1 || MainReg.y2 < MainReg.y1){
        fprintf(stderr, "Negative region, or region outside of image\n");
        return RetVal;
    }

    DetectionPixels = (MainReg.x2-MainReg.x1) * (MainReg.y2-MainReg.y1);
    if (DetectionPixels < 1000){
        fprintf(stderr, "Too few pixels in region\n");
        return RetVal;
    }

    if (Verbosity > 0){
        printf("Detection region is %d-%d, %d-%d\n",MainReg.x1, MainReg.x2, MainReg.y1, MainReg.y2);
    }


    // Compute brightness multipliers for the two images
    {
        double b1average, b2average;
        double m1, m2;
        b1average = AverageBright(pic1, MainReg, WeightMap);
        b2average = AverageBright(pic2, MainReg, WeightMap);

        NewestAverageBright = (int)(b2average+0.5);

        if (Verbosity > 0){
            printf("average bright: %f %f\n",b1average, b2average);
        }
        if (b1average < 0.5) b1average = 0.5; // Avoid possible division by zero.
        if (b2average < 0.5) b2average = 0.5;

        m1 = 80/b1average;
        m2 = 80/b2average;

        {
            double maxm, minm;
            if (m1 > m2){
                maxm = m1; minm = m2;
            }else{
                maxm = m2; minm = m1;
            }
            BrightnessRatio = maxm/minm;

            if (maxm > 2.5){
                // Don't allow multiplier to get bigger than 2.5.  Otherwise, for dark images
                // we just end up multiplying pixel noise!
                m1 = m1 * 2.5 / maxm;
                m2 = m2 * 2.5 / maxm;
            }else if (maxm < 1.0){
                // And there's no point in scaling down both images either.
                m1 = m1 * 1 / maxm;
                m2 = m2 * 1 / maxm;
            }
        }

        if (Verbosity) printf("Brightness adjust multipliers:  m1 = %5.2f  m2=%5.2f\n",m1,m2);

        m1i = (int)(m1*256+0.5);
        m2i = (int)(m2*256+0.5);
    }

    // Compute differences
    memset(DiffHist, 0, sizeof(DiffHist));
    for (row=MainReg.y1;row<MainReg.y2;){
        unsigned char * p1, *p2, *pd = NULL;
        int * ExRow;
        int * diffrow;
        p1 = pic1->pixels+row*bPerRow + MainReg.x1*3;
        p2 = pic2->pixels+row*bPerRow + MainReg.x1*3;
        diffrow = &DiffVal->values[width*row];
        ExRow = &WeightMap->values[width*row];
        if (DebugImgName) pd = DiffOut->pixels+row*bPerRow;

        for (col=MainReg.x1;col<MainReg.x2;col++){
            if (ExRow[col]){
                // Data is in order red, green, blue.
                int dr,dg,db;
                int dcomp;
                int max;
                {
                    // Find maximum of red green or blue for either picture after adjustment.
                    int max1, max2;
                    max1 = p1[0];
                    if (p1[1] > max1) max1 = p1[1];
                    if (p1[2] > max1) max1 = p1[2];
                    max1 *= m1i;
                    max2 = p2[0];
                    if (p2[1] > max1) max1 = p2[1];
                    if (p2[2] > max1) max1 = p2[2];
                    max2 *= m2i;
                    max = max2 > max1 ? max2 : max1;
                    max = max >> 8;
                    if (max < 40) max = 40; // Don't allow under 40 to avoid amlifying dark noise.
                }

                // Compute red green and blue differences
                dr = (p1[0]*m1i - p2[0]*m2i);
                dg = (p1[1]*m1i - p2[1]*m2i);
                db = (p1[2]*m1i - p2[2]*m2i);

                // Make differences absolute values.
                if (dr < 0) dr = -dr;
                if (dg < 0) dg = -dg;
                if (db < 0) db = -db;

                // compute composite difference.
                dcomp = (dr + dg*2 + db) >> 8; // Put more emphasis on green
                dcomp = dcomp * 120/max;       // Normalize difference to local brightness.


                // Add computed difference value to histogram bins.
                if (dcomp >= 256) dcomp = 255;
                if (dcomp < 0){
                    fprintf(stderr,"Internal error dcomp\n");
                    dcomp = 0;
                }
                DiffHist[dcomp] += 1;
                diffrow[col] = dcomp;

                if (DebugImgName){
                    // Save the difference image, scale brightness up 4x
                    dr=(dr*60/max)>> 6; if (dr > 255) dr = 255;
                    dg=(dg*60/max)>> 6; if (dg > 255) dg = 255;
                    db=(db*60/max)>> 6; if (db > 255) db = 255;
                    pd[0] = dr;
                    pd[1] = dg;
                    pd[2] = db;
                }
            }

            // Advance pointers to next pixel.
            pd += 3;
            p1 += 3;
            p2 += 3;
        }
        row++;
    }


    if (DebugImgName){
        WritePpmFile(DebugImgName,DiffOut);
        free(DiffOut);
    }


    if (Verbosity > 1){
        printf("Difference histogram\n");
        for (a=0;a<60;a+=4){
            printf("%3d:%6d,%6d,%6d,%6d\n",a,DiffHist[a], DiffHist[a+1], DiffHist[a+2], DiffHist[a+3]);
        }
    }

    TriggerInfo_t Trigger;
    int threshold;
    {
        // Gauge the difference noise level of the difference maps using the built histogram.
        // assuming two thirds of the image has not changed
        int cumsum = 0;
        int twothirds = DetectionPixels*2/3;

        for (a=0;a<256;a++){
            if (cumsum >= twothirds) break;
            cumsum += DiffHist[a];
        }

        threshold = a*3+12;
        if (threshold < 30) threshold = 30;

        int maxth = 180 + BrightnessRatio*5;
        if (threshold > maxth){
            threshold = maxth;
            if (Verbosity) printf("Using limit threshold of %d\n", threshold);
        }else{
            if (Verbosity) printf("2/3 of image is below %d diff.  Using %d threshold\n",a, threshold);
        }
    }

    // Apply motion fatigure and search for a window with the largest difference in it
    Trigger = AnalyzeDifferences(MainReg, threshold, UpdateFatigue, SkipFatigue, no_fatigue_motion);
    return Trigger;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %r9, 0x68(%rsp)
movq %r8, %r15
movl %ecx, 0x1c(%rsp)
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %rbp
leaq 0xd308(%rip), %rax      # 0x129a8
cmpl $0x0, (%rax)
je 0x56bc
movl (%rbp), %esi
movl 0x4(%rbp), %edx
movl 0x8(%rbp), %ecx
leaq 0x70ab(%rip), %rdi      # 0xc760
xorl %eax, %eax
callq 0x3170
movslq (%rbp), %r12
movl (%r13), %r8d
movl 0x4(%r13), %r9d
movl 0x4(%rbp), %r14d
cmpl %r8d, %r12d
jne 0x61a3
cmpl %r9d, %r14d
jne 0x61a3
movl 0x8(%rbp), %eax
movl %r14d, %r9d
cmpl 0x8(%r13), %eax
jne 0x61a3
movq 0xdd2b(%rip), %rdi      # 0x13420
testq %rdi, %rdi
je 0x571e
cmpl (%rdi), %r12d
jne 0x5705
cmpl 0x4(%rdi), %r14d
je 0x576a
callq 0x3040
movq 0xdd07(%rip), %rdi      # 0x13418
callq 0x3040
andq $0x0, 0xdd02(%rip)      # 0x13420
movl %r12d, %edi
movl %r14d, %esi
callq 0x6807
movq %rax, 0xdcf0(%rip)      # 0x13420
movq 0xdce1(%rip), %rax      # 0x13418
testq %rax, %rax
je 0x575f
cmpl %r12d, (%rax)
jne 0x5747
cmpl %r14d, 0x4(%rax)
je 0x576a
movq 0xc8aa(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x705f(%rip), %rdi      # 0xc7b7
pushq $0x1d
jmp 0x5dbe
movl %r12d, %edi
movl %r14d, %esi
callq 0x6388
leal (%r12,%r12,2), %ecx
testq %r15, %r15
movq %r15, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
movl %ecx, 0x30(%rsp)
je 0x57b5
movl %r14d, %eax
imull %ecx, %eax
movslq %eax, %r15
leaq 0xc(%r15), %rdi
callq 0x3340
movq (%rbp), %rcx
movq %rcx, (%rax)
movl 0x8(%rbp), %ecx
movl %ecx, 0x8(%rax)
movq %rax, 0x20(%rsp)
leaq 0xc(%rax), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x31b0
jmp 0x57be
movq $0x0, 0x20(%rsp)
leaq 0xd603(%rip), %rax      # 0x12dc8
movq (%rax), %rbp
movq 0x8(%rax), %rax
movq %rbp, %r15
shrq $0x20, %r15
movq %rax, %rcx
shrq $0x20, %rcx
movq %r14, %rdx
shlq $0x20, %rdx
movl %eax, %esi
orq %rdx, %rsi
cmpl %ecx, %r14d
movq %rsi, %r14
cmovgeq %rax, %r14
cmpl %r15d, %r12d
movq %r12, 0x28(%rsp)
cmovll %r12d, %r15d
movl %r15d, %eax
subl %ebp, %eax
jl 0x615f
movq %r14, %r8
shrq $0x20, %r8
movl %r8d, %ecx
subl %r14d, %ecx
jl 0x615f
imull %eax, %ecx
cmpl $0x3e7, %ecx            # imm = 0x3E7
jle 0x6179
movl %ecx, 0x3c(%rsp)
movl %ebx, 0x38(%rsp)
leaq 0xd170(%rip), %rbx      # 0x129a8
cmpl $0x0, (%rbx)
jle 0x5853
leaq 0x6fd8(%rip), %rdi      # 0xc81c
movl %ebp, %esi
movl %r15d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3170
movq 0xdbbe(%rip), %rcx      # 0x13418
movq %r15, %rax
shlq $0x20, %rax
movl %ebp, %r12d
orq %rax, %r12
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6724
movapd %xmm0, 0x50(%rsp)
movq 0xdb94(%rip), %rcx      # 0x13418
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6724
movapd %xmm0, %xmm1
addsd 0x6e5a(%rip), %xmm0     # 0xc6f8
cvttsd2si %xmm0, %eax
movl %eax, 0xdb80(%rip)      # 0x13428
cmpl $0x0, (%rbx)
jle 0x58d2
leaq 0x6f8a(%rip), %rdi      # 0xc83e
movapd 0x50(%rsp), %xmm0
movapd %xmm1, 0x40(%rsp)
movaps 0x40(%rsp), %xmm1
movb $0x2, %al
callq 0x3170
movapd 0x40(%rsp), %xmm1
movapd 0x50(%rsp), %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd 0x6e3c(%rip), %xmm1     # 0xc720
maxpd %xmm0, %xmm1
movapd 0x6e40(%rip), %xmm4     # 0xc730
divpd %xmm1, %xmm4
movapd %xmm4, %xmm3
unpckhpd %xmm4, %xmm3            # xmm3 = xmm3[1],xmm4[1]
movapd %xmm4, %xmm2
maxsd %xmm3, %xmm2
ucomisd 0x6df4(%rip), %xmm2     # 0xc700
jbe 0x5924
movapd 0x6e2a(%rip), %xmm0     # 0xc740
mulpd %xmm4, %xmm0
movapd %xmm2, %xmm1
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
jmp 0x5942
movsd 0x6ddc(%rip), %xmm1     # 0xc708
ucomisd %xmm2, %xmm1
movapd %xmm4, %xmm0
jbe 0x5946
movapd %xmm2, %xmm1
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
movapd %xmm4, %xmm0
divpd %xmm1, %xmm0
movq %r14, 0x70(%rsp)
minsd %xmm4, %xmm3
cmpl $0x0, (%rbx)
je 0x5994
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
leaq 0x6ef2(%rip), %rdi      # 0xc855
movb $0x2, %al
movapd %xmm2, 0x80(%rsp)
movapd %xmm3, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
callq 0x3170
movapd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm3
movapd 0x80(%rsp), %xmm2
mulpd 0x6db4(%rip), %xmm0     # 0xc750
addpd 0x6d7c(%rip), %xmm0     # 0xc720
cvttpd2dq %xmm0, %xmm1
divsd %xmm3, %xmm2
movapd %xmm2, 0x80(%rsp)
movapd %xmm1, 0xc0(%rsp)
movd %xmm1, %r14d
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
movd %xmm0, 0x40(%rsp)
leaq 0xd0(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x31b0
movdqa 0xc0(%rsp), %xmm4
movabsq $0x300000000, %rax      # imm = 0x300000000
imulq %rbp, %rax
sarq $0x20, %rax
movq 0x20(%rsp), %rcx
addq $0xc, %rcx
movq %rcx, 0xb0(%rsp)
movq %rbp, 0x98(%rsp)
movslq %ebp, %rcx
movq %rcx, 0xa8(%rsp)
movslq %r15d, %rdx
movq 0x70(%rsp), %rcx
movslq %ecx, %r9
sarq $0x20, %rcx
movq %rcx, 0x78(%rsp)
movslq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rsi, %rcx
imulq %r9, %rcx
leaq 0x8(,%rcx,4), %r8
shlq $0x2, %rsi
movq %rsi, 0x28(%rsp)
movq %rdi, %rcx
imulq %r9, %rcx
addq %rax, %rcx
movq 0x10(%rsp), %rax
leaq (%rax,%rcx), %r15
addq $0xe, %r15
leaq 0xe(%r13,%rcx), %rbp
movq %r8, %rcx
pshufd $0xf5, %xmm4, %xmm0     # xmm0 = xmm4[1,1,3,3]
movl $0xff, %r8d
movq %rdx, 0x10(%rsp)
movq %rdi, 0xa0(%rsp)
cmpq 0x78(%rsp), %r9
jge 0x5c5e
movq %r9, 0x30(%rsp)
imulq %rdi, %r9
addq 0xb0(%rsp), %r9
movq 0x8(%rsp), %rax
testq %rax, %rax
cmoveq %rax, %r9
movq 0xd95b(%rip), %r11      # 0x13418
addq %rcx, %r11
movq 0xd959(%rip), %rax      # 0x13420
movq %rcx, 0xb8(%rsp)
addq %rcx, %rax
movq %rax, 0x50(%rsp)
xorl %r13d, %r13d
movq 0xa8(%rsp), %rdi
cmpq %rdx, %rdi
jge 0x5c36
cmpl $0x0, (%r11,%rdi,4)
je 0x5c25
movzbl -0x2(%r15,%r13), %ecx
movzbl -0x1(%r15,%r13), %eax
cmpb %cl, %al
movl %ecx, %r12d
cmoval %eax, %r12d
movzbl (%r15,%r13), %esi
cmpb %r12b, %sil
cmoval %esi, %r12d
imull %r14d, %r12d
movzbl -0x2(%rbp,%r13), %edx
movzbl -0x1(%rbp,%r13), %r10d
cmpl %r10d, %r12d
cmovlel %r10d, %r12d
imull %r14d, %eax
movl 0x40(%rsp), %ebx
imull %ebx, %r10d
subl %r10d, %eax
movzbl (%rbp,%r13), %r10d
imull %ebx, %edx
imull %r14d, %ecx
subl %edx, %ecx
movd %esi, %xmm1
movdqa %xmm1, %xmm2
pinsrw $0x2, %r10d, %xmm2
pshufd $0x55, %xmm2, %xmm3     # xmm3 = xmm2[1,1,1,1]
movd %xmm3, %esi
cmpl %esi, %r12d
cmovbel %esi, %r12d
cmpl %r12d, %edx
cmovgl %edx, %r12d
shrl $0x8, %r12d
cmpl $0x29, %r12d
pushq $0x28
popq %rdx
cmovbl %edx, %r12d
pshufd $0xf5, %xmm2, %xmm2     # xmm2 = xmm2[1,1,3,3]
pmuludq %xmm0, %xmm2
pmuludq %xmm4, %xmm1
psubd %xmm2, %xmm1
movd %xmm1, %edx
movl %ecx, %ebx
negl %ebx
cmovsl %ecx, %ebx
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
movl %edx, %r10d
negl %r10d
cmovsl %edx, %r10d
leal (%rbx,%rsi,2), %eax
addl %r10d, %eax
shrl $0x8, %eax
imull $0x78, %eax, %eax
xorl %edx, %edx
divl %r12d
cmpl %r8d, %eax
cmovael %r8d, %eax
incl 0xd0(%rsp,%rax,4)
movq 0x50(%rsp), %rcx
movl %eax, (%rcx,%rdi,4)
cmpq $0x0, 0x8(%rsp)
je 0x5c25
imull $0x3c, %ebx, %eax
xorl %edx, %edx
divl %r12d
movl %eax, %ecx
shrl $0x6, %ecx
cmpl %r8d, %ecx
cmovael %r8d, %ecx
imull $0x3c, %esi, %eax
xorl %edx, %edx
divl %r12d
movl %eax, %esi
shrl $0x6, %esi
cmpl %r8d, %esi
cmovael %r8d, %esi
imull $0x3c, %r10d, %eax
xorl %edx, %edx
divl %r12d
shrl $0x6, %eax
cmpl %r8d, %eax
cmovael %r8d, %eax
movb %cl, (%r9,%r13)
movb %sil, 0x1(%r9,%r13)
movb %al, 0x2(%r9,%r13)
incq %rdi
addq $0x3, %r13
movq 0x10(%rsp), %rdx
jmp 0x5ae2
movq 0x30(%rsp), %r9
incq %r9
movq 0xb8(%rsp), %rcx
addq 0x28(%rsp), %rcx
movq 0xa0(%rsp), %rdi
addq %rdi, %r15
addq %rdi, %rbp
jmp 0x5a8e
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x5c7d
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x6f26
movq %rbx, %rdi
callq 0x3040
leaq 0xcd24(%rip), %r14      # 0x129a8
cmpl $0x2, (%r14)
jl 0x5cd9
leaq 0x6d34(%rip), %rdi      # 0xc9c5
callq 0x30c0
leaq 0x6bee(%rip), %rbx      # 0xc88b
xorl %r15d, %r15d
cmpq $0x3b, %r15
ja 0x5cd9
movl 0xd0(%rsp,%r15,4), %edx
movl 0xd4(%rsp,%r15,4), %ecx
movl 0xd8(%rsp,%r15,4), %r8d
movl 0xdc(%rsp,%r15,4), %r9d
movq %rbx, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x3170
addq $0x4, %r15
jmp 0x5ca0
movl 0x3c(%rsp), %eax
addl %eax, %eax
xorl %ecx, %ecx
pushq $0x3
popq %rsi
xorl %edx, %edx
divl %esi
xorl %esi, %esi
xorl %edx, %edx
movapd 0x80(%rsp), %xmm0
cmpq $0xff, %rsi
ja 0x5d11
cmpl %eax, %edx
jge 0x5d11
addl 0xd0(%rsp,%rsi,4), %edx
incq %rsi
addl $0x3, %ecx
jmp 0x5cf5
cmpl $0x13, %ecx
pushq $0x12
popq %rbp
cmovael %ecx, %ebp
addl $0xc, %ebp
mulsd 0x69eb(%rip), %xmm0     # 0xc710
addsd 0x69eb(%rip), %xmm0     # 0xc718
cvttsd2si %xmm0, %ebx
movl (%r14), %eax
cmpl %ebx, %ebp
jle 0x5d4e
testl %eax, %eax
je 0x5d64
leaq 0x6b5d(%rip), %rdi      # 0xc8a0
movl %ebx, %esi
xorl %eax, %eax
callq 0x3170
jmp 0x5d64
testl %eax, %eax
je 0x5d62
leaq 0x6b64(%rip), %rdi      # 0xc8bd
movl %ebp, %edx
xorl %eax, %eax
callq 0x3170
movl %ebp, %ebx
movq 0xd6cd(%rip), %r14      # 0x13438
pushq $0x5
popq %r13
testq %r14, %r14
je 0x5dcc
movl 0xd6b2(%rip), %ebp      # 0x1342c
movq 0xd69f(%rip), %r12      # 0x13420
movl (%r12), %r15d
leal 0x4(%r15), %eax
cltd
idivl %r13d
cmpl %eax, %ebp
jne 0x5dab
movl 0xd699(%rip), %ecx      # 0x13430
movl 0x4(%r12), %eax
addl $0x4, %eax
cltd
idivl %r13d
cmpl %eax, %ecx
je 0x5e50
movq 0xc246(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6b35(%rip), %rdi      # 0xc8f1
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
jmp 0x61c1
movq 0xd64d(%rip), %rcx      # 0x13420
movl (%rcx), %eax
addl $0x4, %eax
cltd
idivl %r13d
movl %eax, %edi
movl %eax, 0xd648(%rip)      # 0x1342c
movl 0x4(%rcx), %eax
addl $0x4, %eax
cltd
idivl %r13d
movl %eax, 0xd63c(%rip)      # 0x13430
movl %eax, %esi
callq 0x6807
movq %rax, 0xd636(%rip)      # 0x13438
movl 0xd624(%rip), %edi      # 0x1342c
movl 0xd622(%rip), %esi      # 0x13430
callq 0x6807
movq %rax, 0xd626(%rip)      # 0x13440
movl 0xd60c(%rip), %edi      # 0x1342c
movl 0xd60a(%rip), %esi      # 0x13430
callq 0x6807
movq %rax, 0xd616(%rip)      # 0x13448
movq 0xd5e7(%rip), %r12      # 0x13420
movl (%r12), %r15d
movq 0xd5f4(%rip), %r14      # 0x13438
movl 0xd5e2(%rip), %ebp      # 0x1342c
movl 0xd5e0(%rip), %ecx      # 0x13430
addq $0x8, %r14
movslq %ebp, %rax
movl %ecx, 0x8(%rsp)
movslq %ecx, %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
movslq 0x98(%rsp), %rdi
movslq 0x70(%rsp), %rcx
movslq %r15d, %r8
movq %r8, %rax
imulq %rcx, %rax
leaq 0x8(,%rax,4), %r9
addq 0xd582(%rip), %r9       # 0x13418
shlq $0x2, %r8
leaq (%r12,%rax,4), %r10
addq $0x8, %r10
movq 0x10(%rsp), %r12
cmpq 0x78(%rsp), %rcx
jge 0x5eff
movl %ecx, %eax
cltd
idivl %r13d
imull %ebp, %eax
cltq
leaq (%r14,%rax,4), %r11
movq %rdi, %rsi
cmpq %r12, %rsi
jge 0x5ef4
movl (%r10,%rsi,4), %r15d
subl %ebx, %r15d
jle 0x5eef
movl %esi, %eax
cltd
idivl %r13d
cltq
movl (%r11,%rax,4), %edx
addl %r15d, %edx
movl %edx, (%r11,%rax,4)
cmpl $0x2, (%r9,%rsi,4)
jl 0x5eef
addl %r15d, %edx
movl %edx, (%r11,%rax,4)
incq %rsi
jmp 0x5ec0
incq %rcx
addq %r8, %r9
addq %r8, %r10
jmp 0x5ea7
leaq 0xcaa2(%rip), %rax      # 0x129a8
cmpl $0x1, (%rax)
jle 0x5f2e
leaq 0x69fa(%rip), %rdi      # 0xc90c
movl %ebp, %esi
movl 0x8(%rsp), %edx
xorl %eax, %eax
callq 0x3170
movq 0xd512(%rip), %rdi      # 0x13438
pushq $0x64
popq %rsi
callq 0x6836
leaq 0xc37f(%rip), %rbx      # 0x122b4
cmpl $0x0, (%rbx)
je 0x6092
movq 0x68(%rsp), %r14
testq %r14, %r14
je 0x5f5b
movq 0xd4e9(%rip), %rdi      # 0x13438
callq 0x61c9
movq %rax, (%r14)
movq %rdx, 0x8(%r14)
cmpl $0x0, 0x1c(%rsp)
jne 0x5f75
movq 0xd4d7(%rip), %rdi      # 0x13440
movq 0xd4d8(%rip), %rsi      # 0x13448
callq 0x6973
movq 0xd4bc(%rip), %rdi      # 0x13438
cmpl $0x0, 0x38(%rsp)
je 0x6084
movl 0xd4a3(%rip), %ecx      # 0x13430
movslq 0xd498(%rip), %r8       # 0x1342c
movq 0xd4a5(%rip), %r9       # 0x13440
addq $0x8, %r9
xorl %r10d, %r10d
testl %r8d, %r8d
movl $0x0, %r11d
cmovgl %r8d, %r11d
testl %ecx, %ecx
movl $0x0, %r14d
cmovgl %ecx, %r14d
leaq 0x8(%rdi), %r15
movq %r8, %r12
shlq $0x2, %r12
xorl %esi, %esi
cmpq %r14, %r10
je 0x5ffc
xorl %r13d, %r13d
cmpq %r13, %r11
je 0x5ff1
movl (%rbx), %ebp
leal -0x1(%rbp), %eax
imull (%r9,%r13,4), %eax
addl (%r15,%r13,4), %eax
cltd
idivl %ebp
movl %eax, (%r9,%r13,4)
addl %eax, %esi
incq %r13
jmp 0x5fd0
incq %r10
addq %r12, %r9
addq %r12, %r15
jmp 0x5fc8
movl %r8d, %r9d
imull %ecx, %r9d
movl %esi, %eax
cltd
idivl %r9d
leaq 0xc998(%rip), %rdx      # 0x129a8
cmpl $0x1, (%rdx)
jg 0x6031
cmpl $0x33, %eax
setge %dl
movl 0xd42f(%rip), %ebx      # 0x13450
cmpl $0x3d, %ebx
setge %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x607c
leaq 0xcf90(%rip), %r14      # 0x12fc8
movq (%r14), %rdi
leaq 0x68ed(%rip), %rsi      # 0xc92f
xorl %ebx, %ebx
movl %r8d, %edx
movl %eax, %r8d
xorl %eax, %eax
callq 0x32a0
movq 0xd3e8(%rip), %rdi      # 0x13440
pushq $0x32
popq %rsi
callq 0x6836
movq (%r14), %rcx
leaq 0x68ea(%rip), %rdi      # 0xc954
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
movq 0xd3bc(%rip), %rdi      # 0x13438
incl %ebx
movl %ebx, 0xd3cc(%rip)      # 0x13450
cmpl $0x0, 0x1c(%rsp)
je 0x60d8
callq 0x61c9
jmp 0x60af
movq 0xd39f(%rip), %rdi      # 0x13438
callq 0x61c9
movq 0x68(%rsp), %rcx
testq %rcx, %rcx
je 0x60af
movq %rax, (%rcx)
movq %rdx, 0x8(%rcx)
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
andq %rax, %rcx
movl %eax, %ebx
movl %edx, %edx
orq %rcx, %rbx
movq %rbx, %rax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc1d9(%rip), %rax      # 0x122b8
imull $0x300, (%rax), %eax    # imm = 0x300
pushq $0x64
popq %rcx
cltd
idivl %ecx
movslq 0xd33a(%rip), %rcx      # 0x1342c
movq 0xd34f(%rip), %rdx      # 0x13448
addq $0x8, %rdx
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
movl 0xd31e(%rip), %r9d      # 0x13430
testl %r9d, %r9d
cmovlel %esi, %r9d
leaq 0x8(%rdi), %r10
shlq $0x2, %rcx
xorl %r11d, %r11d
cmpq %r9, %r11
je 0x608b
xorl %ebx, %ebx
cmpq %rbx, %r8
je 0x6154
movl (%rdx,%rbx,4), %ebp
imull %eax, %ebp
sarl $0x8, %ebp
movl (%r10,%rbx,4), %r14d
subl %ebp, %r14d
testl %r14d, %r14d
cmovlel %esi, %r14d
movl %r14d, (%r10,%rbx,4)
incq %rbx
jmp 0x612f
incq %r11
addq %rcx, %r10
addq %rcx, %rdx
jmp 0x6124
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0xbe8d(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6660(%rip), %rdi      # 0xc7d5
pushq $0x2c
jmp 0x6191
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0xbe73(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x6673(%rip), %rdi      # 0xc802
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
xorl %ecx, %ecx
xorl %edx, %edx
jmp 0x60c0
movq 0xbe4e(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x65c8(%rip), %rsi      # 0xc77c
movl %r12d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x32a0
pushq $-0x1
popq %rdi
callq 0x34a0