// File: /workspace/github_cpp_makefile_star2_O2/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, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, %r15
movq %rsi, (%rsp)
cvtsi2ssl 0x16(%rsi), %xmm0
movq %r8, 0x18(%rsp)
movl %r8d, %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
callq 0x2130
cvttss2si %xmm0, %ebx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movslq %ebx, %r14
movq %r14, %rsi
movq %r15, 0x10(%rsp)
callq 0x4768
movq %r15, %rdi
movq %r14, %rsi
callq 0x47fe
movq (%rsp), %rax
movl 0x12(%rax), %ebp
movl 0x16(%rax), %r14d
decl %ebp
decl %r14d
xorl %r12d, %r12d
testl %ebx, %ebx
cmovlel %r12d, %ebx
movq 0x18(%rsp), %rax
leal -0x1(%rax), %r15d
xorl %r13d, %r13d
subq $0x1, %rbx
jb 0x33e2
leal (%r15,%r12), %edx
cmpl %edx, %r14d
cmovbl %r14d, %edx
movq %r12, %rsi
shlq $0x20, %rsi
shlq $0x20, %rdx
orq %rbp, %rdx
movq (%rsp), %rdi
movq 0x40(%rsp), %rcx
movq %rsi, %r8
movq %rdx, %r9
callq 0x2cb7
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4894
movl 0x8(%rsp), %eax
cmpl %r13d, %eax
cmovgl %eax, %r13d
addl 0x18(%rsp), %r12d
jmp 0x3389
movq 0x38(%rsp), %rax
movl %r13d, (%rax)
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r15
cvtsi2ss %r13d, %xmm0
movss %xmm0, (%rsp)
leaq 0x8(%rsp), %r14
movq 0x10(%rsp), %r12
cmpq %r15, %rbx
je 0x3431
cvtsi2ssl (%rbx), %xmm0
movss (%rsp), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x5832
addq $0x4, %rbx
jmp 0x3408
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
je 0x3474
movl (%rax,%rdx,4), %edi
andl %esi, %edi
cmpl $0x7f800000, %edi       # imm = 0x7F800000
jl 0x346f
testq %rdx, %rdx
je 0x3467
movss -0x4(%rax,%rdx,4), %xmm0
jmp 0x346a
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
jmp 0x3448
leaq 0x20(%rsp), %rdi
callq 0x564c
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3496
jmp 0x3496
jmp 0x3496
movq %rax, %r14
movq 0x10(%rsp), %rdi
callq 0x5680
leaq 0x20(%rsp), %rdi
callq 0x564c
movq %r14, %rdi
callq 0x2380