// File: /workspace/github_cpp_makefile_star2_O1/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 0x1aff
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %ebx
movl %edx, %r12d
leal -0x1(%r12), %ecx
testl %ecx, %edx
jne 0x1af1
testl %ebx, %ebx
je 0x1af1
movq %rdi, %r15
movl %ebx, %edi
shrl %edi
movl %edi, %ecx
andl $0x55555555, %ecx       # imm = 0x55555555
movl %ebx, %edx
subl %ecx, %edx
movl $0x33333333, %ecx       # imm = 0x33333333
movl %edx, %r10d
andl %ecx, %r10d
shrl $0x2, %edx
andl %ecx, %edx
addl %r10d, %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 0x1af1
movl %edi, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, %r14
movl %r12d, %eax
movl %ebx, %ebp
imulq %rax, %rbp
shlq $0x4, %rbp
movq %rbp, %rdi
movq %rsi, 0x20(%rsp)
callq 0x1080
movl %ebx, 0xc(%rsp)
movq %rax, %rbx
movq %rbp, %rdi
callq 0x1080
movq %rax, %r13
movq %rbp, %rdi
callq 0x1080
movq 0x20(%rsp), %r8
movq %rax, %rbp
movl 0xc(%rsp), %eax
movq %r12, 0x10(%rsp)
imull %r12d, %eax
movl %eax, %r12d
movl %eax, 0x18(%rsp)
testl %eax, %eax
je 0x1980
movl $0x1, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movzbl (%r15,%rdx), %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
leal -0x1(%rax), %esi
movsd %xmm0, (%rbx,%rsi,8)
movzbl (%r8,%rdx), %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movsd %xmm0, (%r13,%rsi,8)
movl %eax, %esi
movq %rcx, (%rbx,%rsi,8)
movq %rcx, (%r13,%rsi,8)
incq %rdx
addl $0x2, %eax
cmpq %rdx, %r12
jne 0x1943
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movl %r15d, %esi
movl 0xc(%rsp), %edx
xorl %ecx, %ecx
callq 0x1526
movq %r13, %rdi
movl %r15d, %esi
movl 0xc(%rsp), %edx
xorl %ecx, %ecx
callq 0x1526
cmpl $0x0, 0x18(%rsp)
movq %rbx, %r15
je 0x19d8
addq %r12, %r12
xorl %ebx, %ebx
movl %ebx, %edx
andl $-0x2, %edx
shlq $0x3, %rdx
leaq (%r15,%rdx), %rdi
leaq (%rdx,%r13), %rsi
addq %rbp, %rdx
callq 0x172c
addq $0x2, %rbx
cmpq %rbx, %r12
jne 0x19b6
movq %r13, 0x38(%rsp)
movq %r15, 0x40(%rsp)
movq %rbp, %rdi
movq 0x10(%rsp), %rbx
movl %ebx, %esi
movl 0xc(%rsp), %r15d
movl %r15d, %edx
movl $0x1, %ecx
callq 0x1526
movq 0x30(%rsp), %rsi
xorl %eax, %eax
movl %eax, (%rsi)
cmpl $0x1, %ebx
movl %ebx, %r12d
movl $0x0, %eax
adcl %eax, %r12d
cmpl $0x1, %r15d
adcl %eax, %r15d
movl %r15d, 0x2c(%rsp)
movq %r14, %rdx
movl %eax, (%r14)
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
xorl %r13d, %r13d
movq %rax, 0x18(%rsp)
leal (%rax,%rax), %ebx
xorl %r15d, %r15d
movl %ebx, %eax
movsd (%rbp,%rax,8), %xmm1
mulsd %xmm1, %xmm1
leal 0x1(%rbx), %eax
movsd (%rbp,%rax,8), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x1a66
sqrtsd %xmm0, %xmm0
jmp 0x1a83
movsd %xmm3, 0x20(%rsp)
callq 0x10a0
movsd 0x20(%rsp), %xmm3
xorpd %xmm2, %xmm2
movq 0x30(%rsp), %rsi
movq %r14, %rdx
ucomisd %xmm3, %xmm0
jbe 0x1a93
movl %r15d, (%rsi)
movl %r13d, (%rdx)
movapd %xmm0, %xmm3
incl %r15d
addl $0x2, %ebx
cmpl %r15d, %r12d
jne 0x1a3d
incl %r13d
movq 0x18(%rsp), %rax
addl %r15d, %eax
cmpl 0x2c(%rsp), %r13d
jne 0x1a32
movl (%rsi), %eax
movq 0x10(%rsp), %rdi
movl %edi, %ecx
shrl %ecx
cmpl %ecx, %eax
jbe 0x1ac3
subl %edi, %eax
movl %eax, (%rsi)
movl (%rdx), %eax
cmpl 0x28(%rsp), %eax
movl 0xc(%rsp), %ecx
movq 0x40(%rsp), %rdi
jbe 0x1ad8
subl %ecx, %eax
movl %eax, (%rdx)
callq 0x1030
movq 0x38(%rsp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq