// File: /workspace/github_cpp_makefile_star2_O2/markondej[P]phase_correl/phase_correl.c
// Function: computeShift
int computeShift(const unsigned char *image1, const unsigned char *image2, unsigned width, unsigned height, int *deltax, int *deltay) {
    if (!width || (width & (width - 1)) || !height || (height & (height - 1))) {
        return -1;
    }

    unsigned i, j;

    double* fft_input1 = (double *)malloc((sizeof(double) * width * height) << 1);
    double* fft_input2 = (double *)malloc((sizeof(double) * width * height) << 1);
    double* fft_output = (double *)malloc((sizeof(double) * width * height) << 1);

    // Convert image pixels to complex number format, use only real part
    for (i = 0; i < width * height; i++) {
        fft_input1[i << 1] = (double)image1[i];
        fft_input2[i << 1] = (double)image2[i];

        fft_input1[(i << 1) + 1] = 0.0;
        fft_input2[(i << 1) + 1] = 0.0;
    }

    // Perform 2D FFT on each image
    fft2D(fft_input1, width, height, 0);
    fft2D(fft_input2, width, height, 0);

    // Compute normalized cross power spectrum
    for (i = 0; i < width * height; i++) {
        computeNormalized(&fft_input1[i << 1], &fft_input2[i << 1], &fft_output[i << 1]);
    }

    // Perform inverse 2D FFT on obtained matrix
    fft2D(fft_output, width, height, 1);

    // Search for peak
    unsigned offset = 0;
    double max = 0.0; *deltax = 0; *deltay = 0;
    for (j = 0; j < height; j++)
        for (i = 0; i < width; i++) {
            double d = sqrt(pow(fft_output[offset << 1], 2) + pow(fft_output[(offset << 1) + 1], 2));
            if (d > max) {
                max = d; *deltax = i; *deltay = j;
            }
            offset++;
        }

    if (*deltax > width >> 1)
        *deltax -= width;
    if (*deltay > height >> 1)
        *deltay -= height;

    free(fft_input1);
    free(fft_input2);
    free(fft_output);

    return 0;
}

// Assembly:
pushq $-0x1
popq %rax
testl %edx, %edx
je 0x19df
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r13d
movl %edx, %r15d
leal -0x1(%r15), %ecx
testl %ecx, %edx
jne 0x19d1
testl %r13d, %r13d
je 0x19d1
movq %r8, %r14
movq %rdi, %rbp
movl %r13d, %edi
shrl %edi
movl %edi, %ecx
andl $0x55555555, %ecx       # imm = 0x55555555
movl %r13d, %edx
subl %ecx, %edx
movl $0x33333333, %ecx       # imm = 0x33333333
movl %edx, %r8d
andl %ecx, %r8d
shrl $0x2, %edx
andl %ecx, %edx
addl %r8d, %edx
movl %edx, %ecx
shrl $0x4, %ecx
addl %edx, %ecx
andl $0xf0f0f0f, %ecx        # imm = 0xF0F0F0F
imull $0x1010101, %ecx, %ecx  # imm = 0x1010101
shrl $0x18, %ecx
cmpl $0x1, %ecx
ja 0x19d1
movq %r9, %rbx
movl %edi, 0x1c(%rsp)
movl %r15d, %eax
movl %r13d, %r12d
imulq %rax, %r12
shlq $0x4, %r12
movq %r12, %rdi
movq %rsi, 0x20(%rsp)
callq 0x1080
movq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x1080
movq %rax, 0x8(%rsp)
movq %r12, %rdi
callq 0x1080
movq %rbp, %r9
movq 0x20(%rsp), %r8
movq %rax, %rbp
movl %r13d, %r12d
imull %r15d, %r12d
pushq $0x1
popq %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x18b5
movzbl (%r9,%rcx), %edx
cvtsi2sd %edx, %xmm0
leal -0x1(%rax), %edx
movq 0x10(%rsp), %rdi
movsd %xmm0, (%rdi,%rdx,8)
movzbl (%r8,%rcx), %esi
cvtsi2sd %esi, %xmm0
movl %eax, %esi
andq $0x0, (%rdi,%rsi,8)
movq 0x8(%rsp), %rdi
movsd %xmm0, (%rdi,%rdx,8)
andq $0x0, (%rdi,%rsi,8)
incq %rcx
addl $0x2, %eax
jmp 0x1873
movl %r13d, %edx
movl %r13d, 0x4(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
movl %r15d, %esi
xorl %ecx, %ecx
callq 0x148b
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl 0x4(%rsp), %edx
xorl %ecx, %ecx
callq 0x148b
addq %r12, %r12
cmpq %r13, %r12
je 0x1914
movl %r13d, %edx
andl $-0x2, %edx
shlq $0x3, %rdx
movq 0x10(%rsp), %rax
leaq (%rax,%rdx), %rdi
movq 0x8(%rsp), %rax
leaq (%rax,%rdx), %rsi
addq %rbp, %rdx
callq 0x166a
addq $0x2, %r13
jmp 0x18e5
pushq $0x1
popq %r12
movq %rbp, %rdi
movl %r15d, %esi
movl 0x4(%rsp), %r13d
movl %r13d, %edx
movl %r12d, %ecx
callq 0x148b
andl $0x0, (%r14)
andl $0x0, (%rbx)
leal (%r15,%r15), %ecx
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorl %edx, %edx
cmpl %r13d, %edx
je 0x1994
xorl %esi, %esi
movl %r12d, %edi
cmpl %esi, %r15d
je 0x198d
leal -0x1(%rdi), %r8d
movsd (%rbp,%r8,8), %xmm1
mulsd %xmm1, %xmm1
movl %edi, %r8d
movsd (%rbp,%r8,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm2
sqrtsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1986
movl %esi, (%r14)
movl %edx, (%rbx)
movl %edx, %eax
movapd %xmm1, %xmm0
incl %esi
addl $0x2, %edi
jmp 0x194b
incl %edx
addl %ecx, %r12d
jmp 0x1941
movl (%r14), %ecx
movl %r15d, %edx
shrl %edx
cmpl %edx, %ecx
jbe 0x19a8
subl %r15d, %ecx
movl %ecx, (%r14)
movl (%rbx), %eax
cmpl 0x1c(%rsp), %eax
jbe 0x19b3
subl %r13d, %eax
movl %eax, (%rbx)
movq 0x10(%rsp), %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq