// File: /workspace/github_cpp_makefile_star2_O3/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 %rdi, %r11
movq %rsi, %r10
shrq $0x20, %r10
movq %rdx, %r14
shrq $0x20, %r14
movq %r8, %rax
shrq $0x20, %rax
movq %r9, %rdi
shrq $0x20, %rdi
movq %r10, 0x10(%rsp)
movq %r14, 0x20(%rsp)
subl %r14d, %r10d
movl %r10d, %r15d
negl %r15d
cmovsl %r10d, %r15d
subl %edi, %eax
movl %eax, %ebp
negl %ebp
cmovsl %eax, %ebp
cmpl %ebp, %r15d
jne 0x300e
movq %rcx, 0x48(%rsp)
subl %esi, %edx
subl %r8d, %r9d
movq %r9, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
cmpl %r9d, %edx
jl 0x307c
movl 0x160(%rsp), %ebx
cmpl 0x168(%rsp), %ebx
jge 0x2ffc
movq 0x178(%rsp), %rcx
movq 0x38(%rsp), %rax
incl %eax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
leal 0x1(%rax), %ebp
movl %ebp, 0x54(%rsp)
movslq %ebx, %r15
movslq 0x170(%rsp), %rdx
movq %r15, %rax
movq %rdx, 0x30(%rsp)
subq %rdx, %rax
leaq (%rcx,%rax,4), %rdi
movl %ebx, %eax
notl %eax
addl 0x168(%rsp), %eax
leaq 0x4(,%rax,4), %rdx
xorl %esi, %esi
movq %r11, 0x28(%rsp)
callq 0x2130
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rax
incl %eax
movl %eax, 0xc(%rsp)
movslq %ebp, %rax
movq %rax, 0x60(%rsp)
negl %ebx
movl $0xff, %r14d
movq %rbx, 0x58(%rsp)
testq %r15, %r15
movq %r15, 0x18(%rsp)
jle 0x2e22
movq 0x38(%rsp), %rax
movl %eax, %r12d
subl %r15d, %r12d
cmpl 0x40(%rsp), %r12d
jle 0x2f01
movl 0xc(%rsp), %eax
cmpl 0x10(%rsp), %eax
jle 0x2fde
movq 0x18(%rsp), %rcx
subq 0x30(%rsp), %rcx
movq 0x10(%rsp), %r13
cmpl $0x0, 0x40(%rsp)
movl 0x54(%rsp), %r12d
movq 0x18(%rsp), %rbx
movq %r13, %r14
js 0x2e05
xorl %ebp, %ebp
leal (%rbx,%rbp), %esi
movl %r14d, %edx
movq %rcx, %r13
callq 0x4f24
movq %rax, %r15
movq 0x48(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x4f24
movl $0xff, %r9d
movq 0x28(%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 0x4216(%rip), %xmm0     # 0x7004
addss (%r8,%r13,4), %xmm0
movss %xmm0, (%r8,%r13,4)
incl %ebp
cmpl %ebp, %r12d
jne 0x2d68
leal 0x1(%r14), %eax
cmpl 0x20(%rsp), %r14d
movl %eax, %r13d
movl $0xff, %r14d
jne 0x2d4e
jmp 0x2fde
movl 0xc(%rsp), %eax
cmpl 0x10(%rsp), %eax
jle 0x2fde
movq 0x18(%rsp), %rbx
movq 0x60(%rsp), %rax
leaq (%rbx,%rax), %r12
subq 0x30(%rsp), %rbx
movq 0x10(%rsp), %r13
testq %r12, %r12
jle 0x2eea
xorl %ebp, %ebp
movl %ebp, %esi
movl %r13d, %edx
callq 0x4f24
movq %rax, %r15
movq 0x58(%rsp), %rax
leal (%rax,%rbp), %esi
movq 0x48(%rsp), %rdi
movl %r13d, %edx
callq 0x4f24
movq 0x28(%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 0x4132(%rip), %xmm0     # 0x7004
addss (%r8,%rbx,4), %xmm0
movss %xmm0, (%r8,%rbx,4)
incq %rbp
cmpq %rbp, %r12
ja 0x2e53
leal 0x1(%r13), %eax
cmpl 0x20(%rsp), %r13d
movl %eax, %r13d
jne 0x2e48
jmp 0x2fde
movl 0xc(%rsp), %eax
cmpl 0x10(%rsp), %eax
jle 0x2fde
movq 0x18(%rsp), %rbx
subq 0x30(%rsp), %rbx
movq 0x10(%rsp), %r13
testl %r12d, %r12d
movq 0x18(%rsp), %r14
jle 0x2fc6
xorl %ebp, %ebp
leal (%r14,%rbp), %esi
movl %r13d, %edx
callq 0x4f24
movq %rax, %r15
movq 0x48(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
callq 0x4f24
movl $0xff, %r9d
movq 0x28(%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 0x4055(%rip), %xmm0     # 0x7004
addss (%r8,%rbx,4), %xmm0
movss %xmm0, (%r8,%rbx,4)
incl %ebp
cmpl %r12d, %ebp
jl 0x2f2e
leal 0x1(%r13), %eax
cmpl 0x20(%rsp), %r13d
movl %eax, %r13d
movl $0xff, %r14d
jne 0x2f1e
movq 0x18(%rsp), %r15
incq %r15
movq 0x58(%rsp), %rbx
decq %rbx
cmpl 0x168(%rsp), %r15d
jne 0x2d08
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 0x88(%rsp), %rdi
movl %r15d, %esi
callq 0x4d6a
leaq 0x43b5(%rip), %rsi      # 0x73e7
leaq 0xe8(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x4eb4
leaq 0x43cc(%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 0x30ae
movups (%rcx), %xmm0
movups %xmm0, (%r15)
jmp 0x30bb
movl $0x10, %edi
callq 0x2110
movq %rax, %rbx
leaq 0x4390(%rip), %rsi      # 0x7420
movq %rax, %rdi
callq 0x20d0
movq 0x6f51(%rip), %rsi      # 0x9ff0
movq 0x6f1a(%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 0xc8(%rsp), %rdi
movl %ebp, %esi
callq 0x4d6a
leaq 0x108(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x4e0c
leaq 0x3f28(%rip), %rsi      # 0x702b
leaq 0x108(%rsp), %rdi
callq 0x23d0
leaq 0xb8(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x3136
movups (%rcx), %xmm0
movups %xmm0, (%r12)
jmp 0x3149
movq %rdx, 0xa8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xb8(%rsp)
movq 0x8(%rax), %rdx
leaq 0xa8(%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 0x6e74(%rip), %rsi      # 0x9ff0
movq 0x6e3d(%rip), %rdx      # 0x9fc0
movq %rbx, %rdi
callq 0x2370
movq %rax, %r14
movq 0xa8(%rsp), %rdi
cmpq %r12, %rdi
je 0x31b3
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x31b3
movq %rax, %r14
movb $0x1, %bpl
leaq 0x118(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x31dc
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x31dc
movq %rax, %r14
movb $0x1, %bpl
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3205
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3205
movq %rax, %r14
movb $0x1, %bpl
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x3224
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3224
movq %rax, %r14
movb $0x1, %bpl
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3245
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2220
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3266
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2220
testb %bpl, %bpl
jne 0x3298
jmp 0x32a0
movq %rax, %r14
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3298
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3298
jmp 0x3295
movq %rax, %r14
movq %rbx, %rdi
callq 0x2190
movq %r14, %rdi
callq 0x23a0