// File: /workspace/github_cpp_makefile_star2_O1/kebwi[P]Trifid/src/FourierTransform.cpp
// Function: convolveImages(ImageReal const&, ImageReal const&, ImageReal&)
bool convolveImages(const ImageReal &img1, const ImageReal &img2, ImageReal &imgConv)
{
	int maxWidth = max(img1.width(), img2.width());
	int maxHeight = max(img1.height(), img2.height());
	int maxHalfWidth = maxWidth / 2, maxHalfHeight = maxHeight / 2;
	
	if (!assertValidDims(maxWidth, maxHeight))
		return false;
	
	ImageReal img1Temp;
	int offsetX, offsetY;
	assert(embedImageInPow2Bounds(img1, maxWidth, maxHeight, img1Temp, offsetX, offsetY));
	ImageReal img2Temp;
	assert(embedImageInPow2Bounds(img2, maxWidth, maxHeight, img2Temp, offsetX, offsetY));
	
	//Half shift the second image
	ImageReal img2Shifted(maxWidth, maxHeight);
	for (int y = 0; y < maxHeight; y++)
		for (int x = 0; x < maxWidth; x++)
			img2Shifted(x, y) =
				img2Temp((x + maxHalfWidth) % maxWidth, (y + maxHalfHeight) % maxHeight);
	
	//Get the Fourier Transform of the images
	ImageReal img1Ft(maxWidth, maxHeight);
	assert(doFourierAnalysis(img1Temp, img1Ft));
	ImageReal img2Ft(maxWidth, maxHeight);
	assert(doFourierAnalysis(img2Shifted, img2Ft));
	
	//Multiplty the Fourier Transforms
	imgConv.resize(maxWidth, maxHeight);
	rspect2dprod(img1Ft.img(), img2Ft.img(), imgConv.img(), maxHeight, maxWidth);
	
	int err = fft2dInit(log2(maxHeight), log2(maxWidth));
	if (err != 0)
		return false;
	
	//Invert the Fourier Transform
	rifft2d(imgConv.img(), log2(maxHeight), log2(maxWidth));
	
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movl 0x28(%rdi), %eax
movl 0x2c(%rdi), %ecx
movl 0x28(%rsi), %ebp
cmpl %ebp, %eax
cmovgl %eax, %ebp
movl 0x2c(%rsi), %r14d
cmpl %r14d, %ecx
cmovgl %ecx, %r14d
movl %ebp, %edi
movl %r14d, %esi
callq 0x1929c
testb %al, %al
je 0x1a511
leaq 0x128(%rsp), %r13
movq %r13, %rdi
callq 0xcfd0
leaq 0x1c(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %r12, %rdi
movl %ebp, %esi
movl %r14d, %edx
movq %r13, %rcx
callq 0x19cba
testb %al, %al
je 0x1a527
leaq 0x68(%rsp), %rdi
callq 0xcfd0
leaq 0x68(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %r15, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x19cba
testb %al, %al
je 0x1a546
movq %rbx, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xd07e
testl %r14d, %r14d
jle 0x1a3d9
movl %ebp, %edi
shrl $0x1f, %edi
addl %ebp, %edi
sarl %edi
movl %r14d, %r8d
shrl $0x1f, %r8d
addl %r14d, %r8d
sarl %r8d
movq 0xa0(%rsp), %r9
movl 0x90(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x60(%rsp), %r11
movl 0x50(%rsp), %r15d
movl %ebp, %eax
movq %rax, 0x8(%rsp)
xorl %r10d, %r10d
xorl %ebx, %ebx
testl %ebp, %ebp
jle 0x1a3cf
leal (%rbx,%r8), %eax
xorl %edx, %edx
divl %r14d
movl %edx, %ecx
imull 0x10(%rsp), %ecx
movq 0x8(%rsp), %r12
movl %r10d, %r13d
movl %edi, %esi
movl %esi, %eax
xorl %edx, %edx
divl %ebp
addl %ecx, %edx
movsd (%r9,%rdx,8), %xmm0
movl %r13d, %eax
movsd %xmm0, (%r11,%rax,8)
incl %esi
incl %r13d
decq %r12
jne 0x1a3ae
incl %ebx
addl %r15d, %r10d
cmpl %r14d, %ebx
jne 0x1a390
leaq 0xe8(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xd07e
leaq 0x128(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x19a5c
movq 0x20(%rsp), %rbx
testb %al, %al
je 0x1a565
leaq 0xa8(%rsp), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xd07e
leaq 0x28(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x19a5c
testb %al, %al
je 0x1a584
movq (%rbx), %rax
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq *0x28(%rax)
movq 0x120(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0x38(%rbx), %rdx
movslq %r14d, %rcx
movslq %ebp, %r8
callq 0x246f3
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x3510
cvttsd2si %xmm0, %r14
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x3510
cvttsd2si %xmm0, %rsi
movq %r14, %rdi
callq 0x23658
testl %eax, %eax
sete %bpl
jne 0x1a4d4
movq 0x38(%rbx), %rbx
movsd 0x10(%rsp), %xmm0
callq 0x3510
cvttsd2si %xmm0, %r14
movsd 0x8(%rsp), %xmm0
callq 0x3510
cvttsd2si %xmm0, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x243ac
leaq 0xa8(%rsp), %rdi
callq 0xd0fc
leaq 0xe8(%rsp), %rdi
callq 0xd0fc
leaq 0x28(%rsp), %rdi
callq 0xd0fc
leaq 0x68(%rsp), %rdi
callq 0xd0fc
leaq 0x128(%rsp), %rdi
callq 0xd0fc
jmp 0x1a513
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x138ed(%rip), %rdi     # 0x2de1b
leaq 0x1367e(%rip), %rsi     # 0x2dbb3
leaq 0x1392d(%rip), %rcx     # 0x2de69
movl $0x18a, %edx            # imm = 0x18A
callq 0x31c0
leaq 0x13963(%rip), %rdi     # 0x2deb0
leaq 0x1365f(%rip), %rsi     # 0x2dbb3
leaq 0x1390e(%rip), %rcx     # 0x2de69
movl $0x18c, %edx            # imm = 0x18C
callq 0x31c0
leaq 0x13992(%rip), %rdi     # 0x2defe
leaq 0x13640(%rip), %rsi     # 0x2dbb3
leaq 0x138ef(%rip), %rcx     # 0x2de69
movl $0x197, %edx            # imm = 0x197
callq 0x31c0
leaq 0x13997(%rip), %rdi     # 0x2df22
leaq 0x13621(%rip), %rsi     # 0x2dbb3
leaq 0x138d0(%rip), %rcx     # 0x2de69
movl $0x199, %edx            # imm = 0x199
callq 0x31c0
jmp 0x1a5a5
movq %rax, %rbx
jmp 0x1a5cf
movq %rax, %rbx
jmp 0x1a5dc
jmp 0x1a5b1
movq %rax, %rbx
jmp 0x1a5e6
jmp 0x1a5b8
movq %rax, %rbx
jmp 0x1a5f0
jmp 0x1a5bf
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0xd0fc
leaq 0xe8(%rsp), %rdi
callq 0xd0fc
leaq 0x28(%rsp), %rdi
callq 0xd0fc
leaq 0x68(%rsp), %rdi
callq 0xd0fc
leaq 0x128(%rsp), %rdi
callq 0xd0fc
movq %rbx, %rdi
callq 0x3500