// File: /workspace/github_c_makefile_star2_O1/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), %ebx
cmpl %esi, %ebx
jl 0x73d8
movl %esi, %r9d
movq %rdi, %r13
movl 0x4(%rdi), %esi
cmpl %edx, %esi
jl 0x73d8
movq %rdx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
movslq %ebx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, 0xc31a(%rip)      # 0x13448
movq %rsi, -0x30(%rbp)
jne 0x7174
movslq %esi, %rax
imulq -0x40(%rbp), %rax
leaq 0x8(,%rax,4), %r12
movq %r12, %rdi
movq %r9, %r14
callq 0x3330
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x31b0
movq %r14, %r9
movq -0x30(%rbp), %rsi
movl %ebx, (%r15)
movl %esi, 0x4(%r15)
movq %r15, 0xc2d4(%rip)      # 0x13448
movq %rbx, -0x38(%rbp)
testl %esi, %esi
jle 0x725a
leal -0x1(%r9), %eax
movq 0xc2bd(%rip), %r14      # 0x13448
addq $0x8, %r14
movq -0x38(%rbp), %rbx
subl %eax, %ebx
cltq
movl %eax, %r15d
movq -0x40(%rbp), %rcx
leaq (,%rcx,4), %rdi
leaq 0x8(,%rax,4), %r12
addq %r13, %r12
addq $0x8, %r13
xorl %r8d, %r8d
movq %r14, -0x70(%rbp)
movq %r9, -0x78(%rbp)
movq %rdi, -0x68(%rbp)
movl $0x0, %ecx
cmpl $0x2, %r9d
jl 0x71e0
xorl %eax, %eax
xorl %ecx, %ecx
addl (%r13,%rax,4), %ecx
incq %rax
cmpq %rax, %r15
jne 0x71d3
xorl %eax, %eax
testl %ebx, %ebx
jle 0x71ff
addl (%r12,%rax,4), %ecx
movl %ecx, %edx
subl (%r13,%rax,4), %edx
movl %ecx, (%r14,%rax,4)
incq %rax
movl %edx, %ecx
cmpq %rax, %rbx
jne 0x71e6
cmpl -0x38(%rbp), %eax
jge 0x7245
movq -0x38(%rbp), %rsi
movl %esi, %ecx
imull %r8d, %ecx
movslq %ecx, %rcx
movl %eax, %edx
addq %rcx, %rdx
movq -0x70(%rbp), %rcx
leaq (%rcx,%rdx,4), %rdi
notl %eax
addl %esi, %eax
leaq 0x4(,%rax,4), %rdx
xorl %esi, %esi
movq %r8, -0x48(%rbp)
callq 0x31b0
movq -0x48(%rbp), %r8
movq -0x68(%rbp), %rdi
movq -0x78(%rbp), %r9
movq -0x30(%rbp), %rsi
incq %r8
addq %rdi, %r13
addq %rdi, %r12
addq %rdi, %r14
cmpq %rsi, %r8
jne 0x71c4
movq %rsp, -0x48(%rbp)
movq -0x50(%rbp), %rbx
leal -0x1(%rbx), %r14d
movq %rsp, %r12
movq -0x38(%rbp), %rcx
leaq 0xf(,%rcx,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leaq (,%rcx,4), %rdx
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
callq 0x31b0
cmpl $0x2, %ebx
jl 0x72db
movq 0xc1a8(%rip), %rax      # 0x13448
addq $0x8, %rax
movl %r14d, %ecx
movq -0x40(%rbp), %rdx
leaq (,%rdx,4), %rdx
xorl %esi, %esi
movq -0x38(%rbp), %r9
testl %r9d, %r9d
jle 0x72d0
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
addl %r8d, (%r12,%rdi,4)
incq %rdi
cmpq %rdi, %r9
jne 0x72c0
incq %rsi
addq %rdx, %rax
cmpq %rcx, %rsi
jne 0x72b9
movslq %r14d, %rdx
movl $0x0, %ebx
movl $0x0, %r15d
movq -0x30(%rbp), %rax
subl %r14d, %eax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %r14
jle 0x7381
movq 0xc143(%rip), %rax      # 0x13448
movl -0x30(%rbp), %ecx
movq -0x40(%rbp), %rdi
movq %rdi, %rsi
imulq %rdx, %rsi
leaq (%rax,%rsi,4), %rsi
addq $0x8, %rsi
addq $0x8, %rax
leaq (,%rdi,4), %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %ebx, %ebx
xorl %r13d, %r13d
testl %r14d, %r14d
jle 0x736d
xorl %r10d, %r10d
movl (%rsi,%r10,4), %r15d
addl (%r12,%r10,4), %r15d
movl %r15d, %r11d
subl (%rax,%r10,4), %r11d
movl %r15d, (%rax,%r10,4)
cmpl %r9d, %r15d
cmovgl %r8d, %r13d
cmovgl %r10d, %ebx
cmovlel %r9d, %r15d
movl %r11d, (%r12,%r10,4)
incq %r10
movl %r15d, %r9d
cmpq %r10, %r14
jne 0x733a
jmp 0x7370
movl %r9d, %r15d
incq %r8
addq %rdi, %rsi
addq %rdi, %rax
movl %r15d, %r9d
cmpq %rcx, %r8
jne 0x7332
movq 0xc0c0(%rip), %rax      # 0x13448
movq -0x30(%rbp), %rcx
imull %r14d, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rdi
addq $0x8, %rdi
imulq -0x40(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x31b0
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x73b7
movl %r13d, (%rax)
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x73c2
movl %ebx, (%rax)
movq -0x48(%rbp), %rsp
movl %r15d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x37af
xorl %r15d, %r15d
jmp 0x73c6
nop