// File: /workspace/github_c_makefile_star2_O2/Matthias-Wandel[P]imgcomp/src/compare_util.c
// Function: BlockFilterImgMap
int BlockFilterImgMap(const ImgMap_t * src, int fw, int fh, int * pmaxc, int * pmaxr)
{
	int w = src->w;
	int h = src->h;
	if (fw > w || fh > h){
		fprintf(stderr, "filter too big\n");
		return 0;
	}
    
    static ImgMap_t * dst_tmp = NULL;
    if (dst_tmp == NULL){
        dst_tmp = MakeImgMap(src->w, src->h); // All subsequent images same size.
    }
    

	// Sum horizontally and copy to dst_tmp
	fw -= 1;
	for (int r=0;r<h;r++){
		int sum=0;
		const int * row = &src->values[r*w];
		int * dstrow = &dst_tmp->values[r*w];
		int c;
		for (c=0;c<fw;c++){
			sum += row[c];
		}
		for (c=0;c<w-fw;c++){
			int ov = row[c];
			sum += row[c+fw];
			dstrow[c] = sum;
			sum -= ov;
		}
		for (;c<w;c++) dstrow[c] = 0; // Clear unused columns.
	}

	int maxr, maxc, maxv; // For maximum search while doing columns.
	maxr = maxc = maxv = 0;
	
	// Sum vertically.
	fh -= 1;
	int sums[w];
	memset(sums, 0, sizeof(sums));
	for (int r=0;r<fh;r++){
		int * row = &dst_tmp->values[r*w];
		for (int c=0;c<w;c++){
			sums[c] += row[c];
		}
	}
	for (int r=0;r<h-fh;r++){
		int * row = &dst_tmp->values[r*w];
		int * rowp = &dst_tmp->values[(r+fh)*w];
		for (int c=0;c<w;c++){
			int ov = row[c];
			int sum = sums[c]+rowp[c];
			row[c] = sum;
			if (sum > maxv){
				maxv = sum;
				maxr = r;
				maxc = c;
			}
			sums[c] = sum-ov;
		}
	}
	// Clear out unused rows.
	memset(&dst_tmp->values[(h-fh)*w],0,sizeof(dst_tmp->values[0])*w*fh);
	
	
	// Return location of peak
	//printf("max of %d found at %d,%d\n",maxv,maxc,maxr);
	if (pmaxr) *pmaxr = maxr;
	if (pmaxc) *pmaxc = maxc;
	return maxv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl (%rdi), %r13d
cmpl %esi, %r13d
jl 0x6cc5
movq %rdi, %r14
movl 0x4(%rdi), %eax
movl %eax, -0x2c(%rbp)
cmpl %edx, %eax
jl 0x6cc5
movl %esi, %r15d
movl %edx, -0x38(%rbp)
movq %r8, -0x60(%rbp)
movq %rcx, -0x68(%rbp)
movslq %r13d, %r12
movq 0xc990(%rip), %rax      # 0x13458
testq %rax, %rax
jne 0x6adf
movl %r13d, %edi
movl -0x2c(%rbp), %esi
callq 0x6807
movq %rax, 0xc979(%rip)      # 0x13458
decl %r15d
leaq 0x8(%r14), %rbx
movl %r13d, %ecx
subl %r15d, %ecx
xorl %edx, %edx
testl %r15d, %r15d
movl $0x0, %esi
cmovgl %r15d, %esi
movq %rax, -0x58(%rbp)
leaq 0x8(%rax), %r9
testl %ecx, %ecx
cmovlel %edx, %ecx
movl -0x2c(%rbp), %r8d
testl %r8d, %r8d
movl $0x0, %edi
cmovgl %r8d, %edi
movslq %r15d, %r8
leaq (,%r12,4), %r10
movq %r10, -0x48(%rbp)
leaq (%r14,%r8,4), %r8
addq $0x8, %r8
movq %r9, -0x40(%rbp)
cmpq %rdi, %rdx
je 0x6b8e
xorl %r11d, %r11d
xorl %r10d, %r10d
cmpq %r11, %rsi
je 0x6b4b
addl (%rbx,%r11,4), %r10d
incq %r11
jmp 0x6b3d
xorl %r11d, %r11d
cmpq %r11, %rcx
je 0x6b6a
addl (%r8,%r11,4), %r10d
movl %r10d, %r14d
subl (%rbx,%r11,4), %r14d
movl %r10d, (%r9,%r11,4)
incq %r11
movl %r14d, %r10d
jmp 0x6b4e
movq %rcx, %r10
cmpq %r12, %r10
jge 0x6b7c
andl $0x0, (%r9,%r10,4)
incq %r10
jmp 0x6b6d
incq %rdx
movq -0x48(%rbp), %r10
addq %r10, %rbx
addq %r10, %r8
addq %r10, %r9
jmp 0x6b32
movq %rsp, -0x50(%rbp)
movl -0x38(%rbp), %ebx
decl %ebx
movq %rsp, %r14
leaq 0xf(,%r13,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
leaq (,%r13,4), %rdx
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
movq -0x40(%rbp), %rdx
cmpq %rcx, %r15
je 0x6bfc
xorl %esi, %esi
cmpq %rsi, %rax
je 0x6bf3
movl (%rdx,%rsi,4), %edi
addl %edi, (%r14,%rsi,4)
incq %rsi
jmp 0x6be2
incq %r15
addq -0x48(%rbp), %rdx
jmp 0x6bdb
movq %r13, -0x38(%rbp)
movl -0x2c(%rbp), %edi
subl %ebx, %edi
movslq %ebx, %rsi
xorl %ecx, %ecx
testl %edi, %edi
movl $0x0, %edx
movl %edi, %r11d
cmovgl %edi, %edx
imulq %rsi, %r12
movq -0x58(%rbp), %rsi
leaq (%rsi,%r12,4), %rsi
addq $0x8, %rsi
movq -0x40(%rbp), %rdi
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %ebx, %ebx
cmpq %rdx, %rcx
je 0x6c7a
xorl %r8d, %r8d
cmpq %r8, %rax
je 0x6c6b
movl (%rsi,%r8,4), %r9d
addl (%r14,%r8,4), %r9d
movl %r9d, %r10d
subl (%rdi,%r8,4), %r10d
movl %r9d, (%rdi,%r8,4)
cmpl %ebx, %r9d
cmovgl %ecx, %r15d
cmovgl %r8d, %r13d
cmovgl %r9d, %ebx
movl %r10d, (%r14,%r8,4)
incq %r8
jmp 0x6c3b
incq %rcx
movq -0x48(%rbp), %r8
addq %r8, %rsi
addq %r8, %rdi
jmp 0x6c33
imull -0x38(%rbp), %r11d
movslq %r11d, %rax
movq -0x40(%rbp), %rcx
leaq (%rcx,%rax,4), %rdi
shlq $0x2, %r12
xorl %esi, %esi
movq %r12, %rdx
callq 0x31b0
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x6ca4
movl %r15d, (%rax)
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x6cb0
movl %r13d, (%rax)
movq -0x50(%rbp), %rsp
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb32c(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x5d5f(%rip), %rdi      # 0xca35
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
xorl %ebx, %ebx
jmp 0x6cb4
nopl (%rax)