// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq %rsi, %rdi
shrq $0x20, %rdi
movq %rdx, %r10
shrq $0x20, %r10
movq %r8, %rax
shrq $0x20, %rax
movq %r9, %rcx
shrq $0x20, %rcx
movq %rdi, 0x18(%rsp)
movq %r10, 0x8(%rsp)
subl %r10d, %edi
movl %edi, %r13d
negl %r13d
cmovsl %edi, %r13d
subl %ecx, %eax
movl %eax, %ebp
negl %ebp
cmovsl %eax, %ebp
cmpl %ebp, %r13d
jne 0x2b35
movl %edx, %edi
subl %esi, %edi
movl %r9d, %r10d
subl %r8d, %r10d
cmpl %r10d, %edi
jl 0x2bf5
movq 0x178(%rsp), %rbx
movl 0x160(%rsp), %eax
incl %edi
movq %rax, %r11
movslq %eax, %r15
movslq 0x168(%rsp), %r13
movslq 0x170(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq (,%rax,4), %rcx
movq %rbx, %rax
subq %rcx, %rax
movq %r15, %rcx
cmpq %r13, %rcx
jge 0x294c
andl $0x0, (%rax,%rcx,4)
incq %rcx
jmp 0x293e
movl %r10d, 0x14(%rsp)
movl %edi, 0x44(%rsp)
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
addl %r11d, %esi
subl %esi, %edx
incl %edx
addl %r11d, %r9d
subl %r8d, %r9d
incl %r9d
negl %r11d
movq %r11, 0x20(%rsp)
movq %r13, 0x50(%rsp)
testl %r9d, %r9d
movl $0x0, %r14d
movq %r9, 0x60(%rsp)
cmovgl %r9d, %r14d
testl %edx, %edx
movl $0x0, %r12d
cmovgl %edx, %r12d
cmpq %r13, %r15
jge 0x2b23
movq %rdx, 0x58(%rsp)
testq %r15, %r15
movq %r15, 0x28(%rsp)
jle 0x2a92
movl 0x44(%rsp), %eax
subl %r15d, %eax
movq %r15, %r14
subq 0x48(%rsp), %r14
movq 0x18(%rsp), %rcx
movl %ecx, %ebp
cmpl 0x14(%rsp), %eax
jle 0x2a39
movq 0x18(%rsp), %rax
movl %eax, %ebp
cmpl 0x8(%rsp), %ebp
jge 0x2af6
xorl %r13d, %r13d
movl 0x14(%rsp), %r12d
cmpl %r12d, %r13d
jg 0x2a35
movq 0x28(%rsp), %rax
leal (%rax,%r13), %esi
movq 0x30(%rsp), %rdi
movl %ebp, %edx
callq 0x42be
movq %rax, %r15
movq 0x38(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x42be
movq %r15, %rdi
movq %rax, %rsi
callq 0x4269
addss (%rbx,%r14,4), %xmm0
movss %xmm0, (%rbx,%r14,4)
incl %r13d
jmp 0x29ed
incl %ebp
jmp 0x29db
cmpl 0x8(%rsp), %ebp
jge 0x2af6
xorl %r13d, %r13d
cmpl %r13d, %r12d
je 0x2a8e
movq 0x28(%rsp), %rax
leal (%rax,%r13), %esi
movq 0x30(%rsp), %rdi
movl %ebp, %edx
callq 0x42be
movq %rax, %r15
movq 0x38(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x42be
movq %r15, %rdi
movq %rax, %rsi
callq 0x4269
addss (%rbx,%r14,4), %xmm0
movss %xmm0, (%rbx,%r14,4)
incl %r13d
jmp 0x2a46
incl %ebp
jmp 0x2a39
movq %r15, %r12
subq 0x48(%rsp), %r12
movq 0x18(%rsp), %rax
movl %eax, %ebp
cmpl 0x8(%rsp), %ebp
jge 0x2af6
xorl %r13d, %r13d
cmpl %r13d, %r14d
je 0x2af2
movq 0x30(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x42be
movq %rax, %r15
movq 0x20(%rsp), %rax
leal (%rax,%r13), %esi
movq 0x38(%rsp), %rdi
movl %ebp, %edx
callq 0x42be
movq %r15, %rdi
movq %rax, %rsi
callq 0x4269
addss (%rbx,%r12,4), %xmm0
movss %xmm0, (%rbx,%r12,4)
incl %r13d
jmp 0x2aaa
incl %ebp
jmp 0x2aa1
movq 0x28(%rsp), %r15
incq %r15
movq 0x58(%rsp), %rdx
decl %edx
movq 0x60(%rsp), %r9
incl %r9d
movq 0x20(%rsp), %rax
decl %eax
movq %rax, 0x20(%rsp)
movq 0x50(%rsp), %r13
jmp 0x297e
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
movl %r13d, %esi
callq 0x4166
leaq 0x3890(%rip), %rsi      # 0x63e7
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x4245
leaq 0x38a7(%rip), %rdx      # 0x641a
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x41bf
leaq 0x68(%rsp), %rdi
movl %ebp, %esi
callq 0x4166
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x41de
leaq 0x3476(%rip), %rdx      # 0x602b
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x41bf
movb $0x1, %bpl
leaq 0x108(%rsp), %rsi
movq %rbx, %rdi
callq 0x2300
xorl %ebp, %ebp
movq 0x740a(%rip), %rsi      # 0x9ff0
movq 0x73d3(%rip), %rdx      # 0x9fc0
movq %rbx, %rdi
callq 0x2350
pushq $0x10
popq %rdi
callq 0x2110
movq %rax, %rbx
leaq 0x3819(%rip), %rsi      # 0x6420
movq %rax, %rdi
callq 0x20b0
movq 0x73da(%rip), %rsi      # 0x9ff0
movq 0x73a3(%rip), %rdx      # 0x9fc0
movq %rbx, %rdi
callq 0x2350
movq %rax, %r14
leaq 0x108(%rsp), %rdi
callq 0x20e0
jmp 0x2c3d
movq %rax, %r14
movb $0x1, %bpl
leaq 0xe8(%rsp), %rdi
callq 0x20e0
jmp 0x2c52
movq %rax, %r14
movb $0x1, %bpl
leaq 0x68(%rsp), %rdi
callq 0x20e0
jmp 0x2c64
movq %rax, %r14
movb $0x1, %bpl
leaq 0xc8(%rsp), %rdi
callq 0x20e0
jmp 0x2c79
movq %rax, %r14
movb $0x1, %bpl
leaq 0xa8(%rsp), %rdi
callq 0x20e0
jmp 0x2c8e
movq %rax, %r14
movb $0x1, %bpl
leaq 0x88(%rsp), %rdi
callq 0x20e0
testb %bpl, %bpl
jne 0x2ca7
jmp 0x2caf
jmp 0x2ca4
movq %rax, %r14
movq %rbx, %rdi
callq 0x2170
movq %r14, %rdi
callq 0x2380