// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movl (%rdi), %r15d
cmpl %esi, %r15d
jl 0x7519
movl %esi, %r9d
movq %rdi, %rbx
movl 0x4(%rdi), %esi
cmpl %edx, %esi
jl 0x7519
movq %rdx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
movslq %r15d, %rax
movq %rax, -0x38(%rbp)
movq 0xc1bd(%rip), %rax      # 0x13448
movq %rax, -0x30(%rbp)
testq %rax, %rax
movq %rsi, -0x40(%rbp)
jne 0x72cd
movslq %esi, %rax
imulq -0x38(%rbp), %rax
leaq 0x8(,%rax,4), %rsi
movl $0x1, %edi
movq %r9, %r14
callq 0x3240
movq %r14, %r9
movq -0x40(%rbp), %rsi
movl %r15d, (%rax)
movl %esi, 0x4(%rax)
movq %rax, -0x30(%rbp)
movq %rax, 0xc17b(%rip)      # 0x13448
testl %esi, %esi
jle 0x73b2
leal -0x1(%r9), %edi
movq -0x30(%rbp), %rax
leaq 0x8(%rax), %r12
movl %r15d, %r14d
subl %edi, %r14d
movslq %edi, %rax
movq -0x38(%rbp), %rcx
leaq (,%rcx,4), %r8
leaq (%rbx,%rax,4), %r13
addq $0x8, %r13
addq $0x8, %rbx
xorl %r10d, %r10d
movq %r12, -0x70(%rbp)
movq %r9, -0x80(%rbp)
movq %rdi, -0x78(%rbp)
movq %r8, -0x68(%rbp)
movl $0x0, %eax
cmpl $0x2, %r9d
jl 0x732f
xorl %ecx, %ecx
xorl %eax, %eax
addl (%rbx,%rcx,4), %eax
incq %rcx
cmpq %rcx, %rdi
jne 0x7324
testl %r14d, %r14d
jle 0x7353
xorl %ecx, %ecx
addl (%r13,%rcx,4), %eax
movl %eax, %edx
subl (%rbx,%rcx,4), %edx
movl %eax, (%r12,%rcx,4)
incq %rcx
movl %edx, %eax
cmpq %rcx, %r14
jne 0x7336
movl %r14d, %eax
jmp 0x7355
xorl %eax, %eax
cmpl %r15d, %eax
jge 0x739d
movl %r15d, %ecx
imull %r10d, %ecx
movslq %ecx, %rcx
movl %eax, %edx
addq %rcx, %rdx
movq -0x70(%rbp), %rcx
leaq (%rcx,%rdx,4), %rdi
notl %eax
addl %r15d, %eax
leaq 0x4(,%rax,4), %rdx
xorl %esi, %esi
movq %r10, -0x48(%rbp)
callq 0x31b0
movq -0x48(%rbp), %r10
movq -0x68(%rbp), %r8
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %r9
movq -0x40(%rbp), %rsi
incq %r10
addq %r8, %rbx
addq %r8, %r13
addq %r8, %r12
cmpq %rsi, %r10
jne 0x7315
movq %rsp, -0x48(%rbp)
movq -0x50(%rbp), %r12
leal -0x1(%r12), %r14d
movq %rsp, %rbx
leaq 0xf(,%r15,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
leaq (,%r15,4), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31b0
cmpl $0x2, %r12d
jl 0x7427
movq -0x30(%rbp), %rax
addq $0x8, %rax
movl %r14d, %ecx
movq -0x38(%rbp), %rdx
leaq (,%rdx,4), %rdx
xorl %esi, %esi
testl %r15d, %r15d
jle 0x741c
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
addl %r8d, (%rbx,%rdi,4)
incq %rdi
cmpq %rdi, %r15
jne 0x740c
incq %rsi
addq %rdx, %rax
cmpq %rcx, %rsi
jne 0x7405
movq -0x40(%rbp), %rcx
subl %r14d, %ecx
jle 0x74bc
movq -0x30(%rbp), %r8
leaq 0x8(%r8), %rax
movslq %r14d, %rdx
movq %rcx, -0x40(%rbp)
movl %ecx, %ecx
movq -0x38(%rbp), %rdi
movq %rdi, %rsi
imulq %rdx, %rsi
leaq (%r8,%rsi,4), %rsi
addq $0x8, %rsi
leaq (,%rdi,4), %rdi
xorl %r8d, %r8d
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
testl %r15d, %r15d
jle 0x74a8
xorl %r9d, %r9d
movl %r12d, %r10d
movl (%rsi,%r9,4), %r12d
addl (%rbx,%r9,4), %r12d
movl %r12d, %r11d
subl (%rax,%r9,4), %r11d
movl %r12d, (%rax,%r9,4)
cmpl %r10d, %r12d
cmovgl %r8d, %r13d
cmovgl %r9d, %r14d
cmovlel %r10d, %r12d
movl %r11d, (%rbx,%r9,4)
incq %r9
movl %r12d, %r10d
cmpq %r9, %r15
jne 0x7477
incq %r8
addq %rdi, %rsi
addq %rdi, %rax
cmpq %rcx, %r8
jne 0x746c
movq -0x40(%rbp), %rcx
jmp 0x74c8
movslq %r14d, %rdx
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r12d, %r12d
imull %r15d, %ecx
movslq %ecx, %rax
movq -0x30(%rbp), %rcx
leaq (%rcx,%rax,4), %rdi
addq $0x8, %rdi
imulq -0x38(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x31b0
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x74f7
movl %r13d, (%rax)
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x7503
movl %r14d, (%rax)
movq -0x48(%rbp), %rsp
movl %r12d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x37bf
xorl %r12d, %r12d
jmp 0x7507
nop