// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%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 0x564e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x56e4
movq %r15, 0x30(%rsp)
testl %r14d, %r14d
jle 0x3b4b
movq 0x8(%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 0x8(%rsp), %rdi
movq 0x40(%rsp), %rcx
movq %rsi, %r8
movq %rdx, %r9
callq 0x3299
movq %rax, (%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3b28
movl (%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3b35
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
callq 0x63a0
movl (%rsp), %eax
cmpl %r15d, %eax
cmovgl %eax, %r15d
addl 0x28(%rsp), %r14d
decq %rbp
jne 0x3ad8
jmp 0x3b4e
xorl %r15d, %r15d
movq 0x30(%rsp), %rax
movl %r15d, (%rax)
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
cmpq %r13, %r12
je 0x3bba
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
movss %xmm0, 0x8(%rsp)
movq %rsp, %r14
xorps %xmm1, %xmm1
cvtsi2ssl (%r12), %xmm1
movss 0x8(%rsp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3ba6
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3bb1
movq %rbx, %rdi
movq %r14, %rdx
callq 0x64c0
addq $0x4, %r12
cmpq %r13, %r12
jne 0x3b76
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
je 0x3c03
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 0x3bfb
testq %rdx, %rdx
je 0x3bf3
movss -0x4(%rax,%rdx,4), %xmm0
jmp 0x3bf6
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x3bd9
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1a
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 0x3c32
jmp 0x3c32
jmp 0x3c32
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3c49
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3c60
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2220
movq %r14, %rdi
callq 0x23a0