// File: /workspace/github_cpp_makefile_star2_O3/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:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %edx, %edx
je 0x1b0f
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %r13d
movl %edx, %r15d
leal -0x1(%r15), %ecx
testl %ecx, %edx
jne 0x1b01
testl %r13d, %r13d
je 0x1b01
movq %r9, %rbx
movq %r8, %r14
movl %r13d, %r9d
shrl %r9d
movl %r9d, %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 0x1b01
movq %rdi, 0x30(%rsp)
movq %rsi, 0x10(%rsp)
movl %r9d, 0x20(%rsp)
movl %r15d, %eax
movl %r13d, %ebp
imulq %rax, %rbp
shlq $0x4, %rbp
movq %rbp, %rdi
callq 0x1080
movq %rax, %r12
movq %rbp, %rdi
callq 0x1080
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x1080
movq %rax, %rbp
movl %r13d, %eax
imull %r15d, %eax
testl %eax, %eax
movl %r13d, 0x24(%rsp)
movq %r12, 0x28(%rsp)
movq %r12, %rdi
je 0x1a24
movl %eax, 0x1c(%rsp)
movl %eax, %r12d
movl $0x1, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r10
movq 0x30(%rsp), %r11
movzbl (%r11,%rdx), %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
leal -0x1(%rax), %esi
movsd %xmm0, (%rdi,%rsi,8)
movzbl (%r10,%rdx), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
movsd %xmm0, (%r9,%rsi,8)
movl %eax, %esi
movq %rcx, (%rdi,%rsi,8)
movq %rcx, (%r9,%rsi,8)
incq %rdx
addl $0x2, %eax
cmpq %rdx, %r12
jne 0x197d
movl %r15d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x151c
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x151c
movq 0x28(%rsp), %rax
cmpl $0x0, 0x1c(%rsp)
je 0x1a43
addq %r12, %r12
xorl %r13d, %r13d
movq %r12, 0x10(%rsp)
movl %r13d, %edx
andl $-0x2, %edx
shlq $0x3, %rdx
movq %rax, %r12
leaq (%rax,%rdx), %rdi
movq 0x8(%rsp), %rax
leaq (%rax,%rdx), %rsi
addq %rbp, %rdx
callq 0x1748
movq %r12, %rax
movq 0x10(%rsp), %r12
addq $0x2, %r13
cmpq %r13, %r12
jne 0x19ef
jmp 0x1a43
movl %r15d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x151c
movq 0x8(%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x151c
movl $0x1, %r12d
movq %rbp, %rdi
movl %r15d, %esi
movl 0x24(%rsp), %r13d
movl %r13d, %edx
movl $0x1, %ecx
callq 0x151c
xorl %eax, %eax
movl %eax, (%r14)
movl %eax, (%rbx)
leal (%r15,%r15), %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movl %r12d, %esi
xorl %edi, %edi
leal -0x1(%rsi), %r8d
movsd (%rbp,%r8,8), %xmm1
mulsd %xmm1, %xmm1
movl %esi, %r8d
movsd (%rbp,%r8,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm2
xorps %xmm1, %xmm1
sqrtsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1ab0
movl %edi, (%r14)
movl %edx, (%rbx)
movl %edx, %eax
movapd %xmm1, %xmm0
incl %edi
addl $0x2, %esi
cmpl %edi, %r15d
jne 0x1a77
incl %edx
addl %ecx, %r12d
cmpl %r13d, %edx
jne 0x1a72
movl (%r14), %ecx
movl %r15d, %edx
shrl %edx
cmpl %edx, %ecx
jbe 0x1ad8
subl %r15d, %ecx
movl %ecx, (%r14)
movl (%rbx), %eax
cmpl 0x20(%rsp), %eax
movq 0x28(%rsp), %rdi
jbe 0x1ae8
subl %r13d, %eax
movl %eax, (%rbx)
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq