// File: /workspace/github_cpp_makefile_star2_O3/iibrahimli[P]adsa_project/src/../include/util.hpp
// Function: calc_psf(BMP_image const&, BMP_image const&, int*, unsigned int)
std::vector<float> calc_psf(const BMP_image& img1, const BMP_image& img2, int* maxshift, unsigned int th /* = 100 */){
    int n_strips = (int) ceil((float) img1.info_h.height / th);
    std::vector<int> sh;
    std::vector<float> psf;
    sh.reserve(n_strips);
    psf.reserve(n_strips);
    unsigned int w = img1.info_h.width;
    unsigned int h = img1.info_h.height;
    int max_shift = 0;

    for(int i=0; i<n_strips; ++i){
        auto corr = x_correlate(img1, {0, i*th}, {w-1, std::min((i+1)*th-1, h-1)}, img2, {0, i*th}, {w-1, std::min((i+1)*th-1, h-1)});
        sh.push_back(corr.first);
        if(corr.first > max_shift) max_shift = corr.first;
        // std::cout << std::setw(4) << i*th << " - " << std::setw(4) << min((i+1)*th-1, h-1) << " : " << corr.first << ", " << corr.second << endl;
    }

    *maxshift = max_shift;

    for(const int& s : sh) psf.push_back((float) max_shift/s);

    // handle +- inf or nan values
    for(int i=0; i<psf.size(); ++i) if(std::isinf(psf[i]) || std::isnan(psf[i])) psf[i] = (i == 0) ? 0 : psf[i-1];

    return psf;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r15
movq %rdx, 0x40(%rsp)
movq %rdi, %rbx
movq %rsi, (%rsp)
cvtsi2ssl 0x16(%rsi), %xmm0
movq %r8, 0x28(%rsp)
movl %r8d, %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
callq 0x2140
cvttss2si %xmm0, %r14d
xorl %eax, %eax
leaq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq %rax, 0x10(%rbx)
movups %xmm0, (%rbx)
movslq %r14d, %rbp
movq %rbp, %rsi
callq 0x565a
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x56f6
movq %r15, 0x30(%rsp)
testl %r14d, %r14d
jle 0x3b51
movq (%rsp), %rax
movl 0x12(%rax), %ecx
movl 0x16(%rax), %r12d
decl %ecx
movq %rcx, 0x38(%rsp)
decl %r12d
movq 0x28(%rsp), %rax
leal -0x1(%rax), %r13d
xorl %r14d, %r14d
xorl %r15d, %r15d
leal (%r14,%r13), %edx
cmpl %edx, %r12d
cmovbl %r12d, %edx
movq %r14, %rsi
shlq $0x20, %rsi
shlq $0x20, %rdx
addq 0x38(%rsp), %rdx
movq (%rsp), %rdi
movq 0x40(%rsp), %rcx
movq %rsi, %r8
movq %rdx, %r9
callq 0x32a8
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3b2b
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3b3a
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x63ca
movl 0x8(%rsp), %eax
cmpl %r15d, %eax
cmovgl %eax, %r15d
addl 0x28(%rsp), %r14d
decq %rbp
jne 0x3ade
jmp 0x3b54
xorl %r15d, %r15d
movq 0x30(%rsp), %rax
movl %r15d, (%rax)
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
cmpq %r13, %r12
je 0x3bc1
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
movss %xmm0, (%rsp)
leaq 0x8(%rsp), %r14
xorps %xmm1, %xmm1
cvtsi2ssl (%r12), %xmm1
movss (%rsp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3bad
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3bb8
movq %rbx, %rdi
movq %r14, %rdx
callq 0x64ea
addq $0x4, %r12
cmpq %r13, %r12
jne 0x3b7d
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
je 0x3c0a
sarq $0x2, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%rax,%rdx,4), %edi
andl %esi, %edi
cmpl $0x7f800000, %edi       # imm = 0x7F800000
jl 0x3c02
testq %rdx, %rdx
je 0x3bfa
movss -0x4(%rax,%rdx,4), %xmm0
jmp 0x3bfd
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x3be0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3c21
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c39
jmp 0x3c39
jmp 0x3c39
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3c50
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3c67
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq %r14, %rdi
callq 0x23a0