// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movl %edx, 0x5c(%rsp)
movl %ecx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x48(%rsp)
cmpl $0x0, 0x5c(%rsp)
je 0x1cd2
movl 0x5c(%rsp), %eax
movl 0x5c(%rsp), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x1cd2
cmpl $0x0, 0x58(%rsp)
je 0x1cd2
movl 0x58(%rsp), %eax
movl 0x58(%rsp), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1cdf
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x1ff8
movl 0x5c(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
movl 0x58(%rsp), %eax
imulq %rax, %rdi
shlq %rdi
callq 0x1090
movq %rax, 0x38(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
movl 0x58(%rsp), %eax
imulq %rax, %rdi
shlq %rdi
callq 0x1090
movq %rax, 0x30(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
movl 0x58(%rsp), %eax
imulq %rax, %rdi
shlq %rdi
callq 0x1090
movq %rax, 0x28(%rsp)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl 0x5c(%rsp), %ecx
imull 0x58(%rsp), %ecx
cmpl %ecx, %eax
jae 0x1ddd
movq 0x68(%rsp), %rax
movl 0x44(%rsp), %ecx
movl %ecx, %edx
movl %edx, %ecx
movzbl (%rax,%rdx), %eax
cvtsi2sd %eax, %xmm0
movq 0x38(%rsp), %rax
addl %ecx, %ecx
movsd %xmm0, (%rax,%rcx,8)
movq 0x60(%rsp), %rax
movl 0x44(%rsp), %ecx
movzbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm0
movq 0x30(%rsp), %rax
movl 0x44(%rsp), %ecx
shll %ecx
movl %ecx, %ecx
movsd %xmm0, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movl 0x44(%rsp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x30(%rsp), %rax
movl 0x44(%rsp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x1d44
movq 0x38(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x58(%rsp), %edx
xorl %ecx, %ecx
callq 0x1760
movq 0x30(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x58(%rsp), %edx
xorl %ecx, %ecx
callq 0x1760
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl 0x5c(%rsp), %ecx
imull 0x58(%rsp), %ecx
cmpl %ecx, %eax
jae 0x1e6c
movq 0x38(%rsp), %rdi
movl 0x44(%rsp), %eax
shll %eax
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %rdi
movq 0x30(%rsp), %rsi
movl 0x44(%rsp), %eax
shll %eax
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %rsi
movq 0x28(%rsp), %rdx
movl 0x44(%rsp), %eax
shll %eax
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %rdx
callq 0x1a70
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x1e0d
movq 0x28(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x58(%rsp), %edx
movl $0x1, %ecx
callq 0x1760
movl $0x0, 0x24(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x50(%rsp), %rax
movl $0x0, (%rax)
movq 0x48(%rsp), %rax
movl $0x0, (%rax)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x58(%rsp), %eax
jae 0x1f92
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jae 0x1f80
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
shll %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd 0x1150(%rip), %xmm1     # 0x3040
callq 0x1050
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
shll %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd 0x1128(%rip), %xmm1     # 0x3040
callq 0x1050
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
callq 0x10b0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x1f65
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x44(%rsp), %ecx
movq 0x50(%rsp), %rax
movl %ecx, (%rax)
movl 0x40(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, (%rax)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x1ec8
jmp 0x1f82
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x1eb2
movq 0x50(%rsp), %rax
movl (%rax), %eax
movl 0x5c(%rsp), %ecx
shrl %ecx
cmpl %ecx, %eax
jbe 0x1fb2
movl 0x5c(%rsp), %edx
movq 0x50(%rsp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movq 0x48(%rsp), %rax
movl (%rax), %eax
movl 0x58(%rsp), %ecx
shrl %ecx
cmpl %ecx, %eax
jbe 0x1fd2
movl 0x58(%rsp), %edx
movq 0x48(%rsp), %rax
movl (%rax), %ecx
subl %edx, %ecx
movl %ecx, (%rax)
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)