// File: /workspace/github_cpp_makefile_star2_O1/iibrahimli[P]adsa_project/src/../include/util.hpp
// Function: x_correlate_region(BMP_image const&, std::pair<int, int>, std::pair<int, int>, BMP_image const&, std::pair<int, int>, std::pair<int, int>, int, int, int, float*)
void x_correlate_region(const BMP_image& a, std::pair<int, int> a_start, std::pair<int, int> a_end,
                        const BMP_image& b, std::pair<int, int> b_start, std::pair<int, int> b_end,
                        int sh_start, int sh_end, int sh_begin, sim_t* act)
{
    // heights are different
    if(abs(a_start.second - a_end.second)+1 != abs(b_start.second - b_end.second)+1){
        throw std::runtime_error("The heights of correlated regions must match (got "
                                + std::to_string(abs(a_start.second-a_end.second)) + " and "
                                + std::to_string(abs(b_start.second-b_end.second)) + ")");
    }

    int height  = a_end.second - a_start.second + 1;
    int width_a = a_end.first - a_start.first + 1;
    int width_b = b_end.first - b_start.first + 1;

    // width of b exceeds width of a
    if(width_a < width_b){
        throw std::runtime_error("Width of b is greater than width of a. Swap the arguments maybe");
    }

    // activations initialized to 0
    for(int i=sh_start; i<sh_end; ++i) act[i - sh_begin] = 0;

    // outer loop with j for cache efficiency

    for(int shift=sh_start; shift<sh_end; ++shift){
        // b entering a
        if(shift <= 0){
            for(int j=a_start.second; j<a_start.second+height; ++j){
                for(int i=0; i<width_b+shift; ++i){
                    act[shift - sh_begin] += a.ploc(i, j) * b.ploc(i-shift, j);
                }
            }
        }
        // b fully inside a
        else if ((width_a - shift) >= width_b){
            for(int j=a_start.second; j<a_start.second+height; ++j){
                for(int i=0; i<width_b; ++i){
                    act[shift - sh_begin] += a.ploc(i+shift, j) * b.ploc(i, j);
                }
            }
        }
        // b leaving a
        else{
            for(int j=a_start.second; j<a_start.second+height; ++j){
                for(int i=0; i<width_a-shift; ++i){
                    act[shift - sh_begin] += a.ploc(i+shift, j) * b.ploc(i, j);
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %r10
shrq $0x20, %r10
movq %rdx, %r14
shrq $0x20, %r14
movq %r8, %rax
shrq $0x20, %rax
movq %r9, %r11
shrq $0x20, %r11
movq %r10, 0x10(%rsp)
movq %r14, 0x28(%rsp)
subl %r14d, %r10d
movl %r10d, %r15d
negl %r15d
cmovsl %r10d, %r15d
subl %r11d, %eax
movl %eax, %ebp
negl %ebp
cmovsl %eax, %ebp
cmpl %ebp, %r15d
jne 0x3017
subl %esi, %edx
subl %r8d, %r9d
cmpl %r9d, %edx
jl 0x3085
movq %rdx, 0x38(%rsp)
movq %rcx, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movq 0x178(%rsp), %rcx
movl 0x168(%rsp), %ebx
movl 0x160(%rsp), %r15d
movslq 0x170(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpl %ebx, %r15d
movq %rdi, 0x30(%rsp)
jge 0x2cd2
movslq %r15d, %rax
subq 0x20(%rsp), %rax
leaq (%rcx,%rax,4), %rdi
movl %r15d, %eax
notl %eax
addl %ebx, %eax
leaq 0x4(,%rax,4), %rdx
xorl %esi, %esi
callq 0x2130
movq 0x30(%rsp), %rdi
cmpl %ebx, %r15d
jge 0x3005
movq 0x38(%rsp), %rax
incl %eax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x54(%rsp)
movslq %r15d, %rdx
movq 0x28(%rsp), %rax
incl %eax
movl %eax, 0xc(%rsp)
movslq %ecx, %rax
movq %rax, 0x60(%rsp)
negl %r15d
movl $0xff, %r14d
movq %r15, 0x58(%rsp)
testq %rdx, %rdx
movq %rdx, 0x18(%rsp)
jle 0x2e2c
movq 0x38(%rsp), %rax
movl %eax, %r12d
subl %edx, %r12d
cmpl 0x40(%rsp), %r12d
jle 0x2f0b
movl 0xc(%rsp), %eax
cmpl 0x10(%rsp), %eax
jle 0x2fe8
movq 0x18(%rsp), %rcx
subq 0x20(%rsp), %rcx
movq 0x10(%rsp), %r13
cmpl $0x0, 0x40(%rsp)
movl 0x54(%rsp), %r12d
movq 0x18(%rsp), %rbx
movq %r13, %r14
js 0x2e0f
xorl %ebp, %ebp
leal (%rbx,%rbp), %esi
movl %r14d, %edx
movq %rcx, %r13
callq 0x4f20
movq %rax, %r15
movq 0x48(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4f20
movl $0xff, %r9d
movq 0x30(%rsp), %rdi
movzbl 0x2(%r15), %ecx
movzbl 0x2(%rax), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movzbl 0x1(%r15), %ecx
andl %r9d, %edx
movzbl 0x1(%rax), %esi
subl %esi, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movzbl (%r15), %ecx
movq 0x178(%rsp), %r8
andl %r9d, %esi
movzbl (%rax), %eax
subl %eax, %ecx
movl %ecx, %eax
addl %edx, %esi
negl %eax
cmovsl %ecx, %eax
movq %r13, %rcx
andl %r9d, %eax
addl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss 0x420c(%rip), %xmm0     # 0x7004
addss (%r8,%r13,4), %xmm0
movss %xmm0, (%r8,%r13,4)
incl %ebp
cmpl %ebp, %r12d
jne 0x2d72
leal 0x1(%r14), %eax
cmpl 0x28(%rsp), %r14d
movl %eax, %r13d
movl $0xff, %r14d
jne 0x2d58
jmp 0x2fe8
movl 0xc(%rsp), %eax
cmpl 0x10(%rsp), %eax
jle 0x2fe8
movq 0x18(%rsp), %rbx
movq 0x60(%rsp), %rax
leaq (%rbx,%rax), %r12
subq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r13
testq %r12, %r12
jle 0x2ef4
xorl %ebp, %ebp
movl %ebp, %esi
movl %r13d, %edx
callq 0x4f20
movq %rax, %r15
movq 0x58(%rsp), %rax
leal (%rax,%rbp), %esi
movq 0x48(%rsp), %rdi
movl %r13d, %edx
callq 0x4f20
movq 0x30(%rsp), %rdi
movzbl 0x2(%r15), %ecx
movzbl 0x2(%rax), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movzbl 0x1(%r15), %ecx
andl %r14d, %edx
movzbl 0x1(%rax), %esi
subl %esi, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movzbl (%r15), %ecx
movq 0x178(%rsp), %r8
andl %r14d, %esi
movzbl (%rax), %eax
subl %eax, %ecx
movl %ecx, %eax
addl %edx, %esi
negl %eax
cmovsl %ecx, %eax
andl %r14d, %eax
addl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss 0x4128(%rip), %xmm0     # 0x7004
addss (%r8,%rbx,4), %xmm0
movss %xmm0, (%r8,%rbx,4)
incq %rbp
cmpq %rbp, %r12
jg 0x2e5d
leal 0x1(%r13), %eax
cmpl 0x28(%rsp), %r13d
movl %eax, %r13d
jne 0x2e52
jmp 0x2fe8
movl 0xc(%rsp), %eax
cmpl 0x10(%rsp), %eax
jle 0x2fe8
movq 0x18(%rsp), %rbx
subq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r13
testl %r12d, %r12d
movq 0x18(%rsp), %r14
jle 0x2fd0
xorl %ebp, %ebp
leal (%r14,%rbp), %esi
movl %r13d, %edx
callq 0x4f20
movq %rax, %r15
movq 0x48(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0x4f20
movl $0xff, %r9d
movq 0x30(%rsp), %rdi
movzbl 0x2(%r15), %ecx
movzbl 0x2(%rax), %edx
subl %edx, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
movzbl 0x1(%r15), %ecx
andl %r9d, %edx
movzbl 0x1(%rax), %esi
subl %esi, %ecx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movzbl (%r15), %ecx
movq 0x178(%rsp), %r8
andl %r9d, %esi
movzbl (%rax), %eax
subl %eax, %ecx
movl %ecx, %eax
addl %edx, %esi
negl %eax
cmovsl %ecx, %eax
andl %r9d, %eax
addl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss 0x404b(%rip), %xmm0     # 0x7004
addss (%r8,%rbx,4), %xmm0
movss %xmm0, (%r8,%rbx,4)
incl %ebp
cmpl %r12d, %ebp
jl 0x2f38
leal 0x1(%r13), %eax
cmpl 0x28(%rsp), %r13d
movl %eax, %r13d
movl $0xff, %r14d
jne 0x2f28
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x58(%rsp), %r15
decq %r15
cmpl 0x168(%rsp), %edx
jne 0x2d12
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x2110
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
movl %r15d, %esi
callq 0x4d66
leaq 0x43ac(%rip), %rsi      # 0x73e7
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0x4eb0
leaq 0x43c3(%rip), %rsi      # 0x741a
leaq 0xe8(%rsp), %rdi
callq 0x23d0
leaq 0x78(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x30b7
movups (%rcx), %xmm0
movups %xmm0, (%r15)
jmp 0x30c4
movl $0x10, %edi
callq 0x2110
movq %rax, %rbx
leaq 0x4387(%rip), %rsi      # 0x7420
movq %rax, %rdi
callq 0x20d0
movq 0x6f48(%rip), %rsi      # 0x9ff0
movq 0x6f11(%rip), %rdx      # 0x9fc0
movq %rbx, %rdi
callq 0x2370
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x70(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0xa8(%rsp), %rdi
movl %ebp, %esi
callq 0x4d66
leaq 0x108(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0x4e08
leaq 0x3f1f(%rip), %rsi      # 0x702b
leaq 0x108(%rsp), %rdi
callq 0x23d0
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x313f
movups (%rcx), %xmm0
movups %xmm0, (%r12)
jmp 0x3152
movq %rdx, 0x88(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x98(%rsp)
movq 0x8(%rax), %rdx
leaq 0x88(%rsp), %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x2320
xorl %ebp, %ebp
movq 0x6e6b(%rip), %rsi      # 0x9ff0
movq 0x6e34(%rip), %rdx      # 0x9fc0
movq %rbx, %rdi
callq 0x2370
movq %rax, %r14
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x31bc
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x31bc
movq %rax, %r14
movb $0x1, %bpl
leaq 0x118(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x31e5
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x31e5
movq %rax, %r14
movb $0x1, %bpl
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x320e
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x320e
movq %rax, %r14
movb $0x1, %bpl
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x322d
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x322d
movq %rax, %r14
movb $0x1, %bpl
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3256
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3256
movq %rax, %r14
movb $0x1, %bpl
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3284
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3284
movq %rax, %r14
jmp 0x3289
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x3291
movq %rbx, %rdi
callq 0x2190
movq %r14, %rdi
callq 0x23a0