// File: /workspace/github_cpp_makefile_star2_O2/sorazy[P]canny/canny.cpp
// Function: magnitude_matrix(double**, double**, double**, double**)
void magnitude_matrix(double **pic, double **mag, double **x, double **y)
{
	int dim = 6 * sig + 1, cent = dim / 2;
	double maskx[dim][dim], masky[dim][dim];

	// Use the Gausian 1st derivative formula to fill in the mask values
	for (int p = -cent; p <= cent; p++)
	{	
		for (int q = -cent; q <= cent; q++)
		{
			maskx[p+cent][q+cent] = q * exp(-1 * ((p * p + q * q) / (2 * sig * sig)));
			masky[p+cent][q+cent] = p * exp(-1 * ((p * p + q * q) / (2 * sig * sig)));
		}
	}

	// Scanning convolution
	double sumx, sumy;
	for (int i = 0; i < height; i++)
	{ 
		for (int j = 0; j < width; j++)
		{
			sumx = 0;
			sumy = 0;

			// This is the convolution
			for (int p = -cent; p <= cent; p++)
			{
				for (int q = -cent; q <= cent; q++)
				{
					if ((i+p) < 0 || (j+q) < 0 || (i+p) >= height || (j+q) >= width)
						continue;
					
					sumx += pic[i+p][j+q] * maskx[p+cent][q+cent];
					sumy += pic[i+p][j+q] * masky[p+cent][q+cent];
				}
			}
			
			// Store convolution result in respective matrix
			x[i][j] = sumx;
			y[i][j] = sumy;
		}
	}

	// Find magnitude and maxVal, then store it in the 'mag' matrix
	double mags;
	double maxVal = 0;
	for (int i = 0; i < height; i++)
	{
		for(int j = 0; j < width; j++)
		{
			mags = sqrt((x[i][j] * x[i][j]) + (y[i][j] * y[i][j]));

			if (mags > maxVal)
				maxVal = mags;

			mag[i][j] = mags;
		}
	}

	// Make sure all the magnitude values are between 0-255
	for (int i = 0; i < height; i++)
		for (int j = 0; j < width; j++)
			mag[i][j] = mag[i][j] / maxVal * 255;

	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movq %rsi, -0x88(%rbp)
leaq 0x32d6(%rip), %rax      # 0x6188
movsd (%rax), %xmm0
mulsd 0x12aa(%rip), %xmm0     # 0x4168
addsd 0x12aa(%rip), %xmm0     # 0x4170
cvttsd2si %xmm0, %r12d
movq %rdi, %r13
pushq $0x2
popq %rcx
movl %r12d, %eax
cltd
idivl %ecx
movq %r12, %rcx
imulq %r12, %rcx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, %rsp
movq %rsp, %rsi
subq %rcx, %rsi
movq %rsi, %rsp
movslq %eax, %r8
negl %eax
movq %rax, -0x98(%rbp)
movslq %eax, %rdi
leal 0x1(%r8), %ecx
cmpl %ecx, %edi
cmovgl %edi, %ecx
movslq %ecx, %rcx
movq %rcx, -0x68(%rbp)
leaq 0x1(,%r8,2), %rax
movq %rax, -0x60(%rbp)
shlq $0x3, %r12
movq %rsi, -0xa0(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0xa8(%rbp)
movq %rdx, -0x38(%rbp)
movq %rdi, -0x48(%rbp)
cmpq -0x68(%rbp), %rdi
je 0x302a
movq %rdi, %rbx
imulq %rdi, %rbx
movq %rdi, -0x90(%rbp)
cvtsi2sd %edi, %xmm0
movsd %xmm0, -0xb0(%rbp)
xorl %r14d, %r14d
cmpq %r14, -0x60(%rbp)
je 0x3013
movq -0x48(%rbp), %rax
addq %r14, %rax
movq -0x98(%rbp), %rcx
addl %r14d, %ecx
cvtsi2sd %ecx, %xmm0
movsd %xmm0, -0x40(%rbp)
imull %eax, %eax
addl %ebx, %eax
cvtsi2sd %eax, %xmm2
xorpd 0x120f(%rip), %xmm2     # 0x41b0
movapd %xmm2, -0x80(%rbp)
leaq 0x31db(%rip), %r15      # 0x6188
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
callq 0x2260
mulsd -0x40(%rbp), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd -0x80(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x2260
mulsd -0xb0(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
jmp 0x2f6c
movq -0x90(%rbp), %rdi
incq %rdi
addq %r12, -0x38(%rbp)
addq %r12, -0x30(%rbp)
jmp 0x2f45
leaq 0x3143(%rip), %rax      # 0x6174
movl (%rax), %eax
leaq 0x313e(%rip), %rcx      # 0x6178
movslq (%rcx), %rsi
xorl %edi, %edi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
movq %rcx, -0x80(%rbp)
testl %eax, %eax
movl $0x0, %ecx
movq %rax, -0x30(%rbp)
cmovgl %eax, %ecx
movq %rcx, -0x38(%rbp)
cmpq -0x38(%rbp), %rdi
je 0x313b
movq -0x48(%rbp), %r11
xorl %ecx, %ecx
cmpq -0x80(%rbp), %rcx
je 0x3133
movq %rcx, -0x40(%rbp)
leaq 0x30f0(%rip), %rax      # 0x6174
movl (%rax), %eax
movq %rax, -0x30(%rbp)
movslq %eax, %rcx
xorpd %xmm0, %xmm0
movq -0xa0(%rbp), %r9
movq -0xa8(%rbp), %rdx
movq -0x48(%rbp), %r14
xorpd %xmm1, %xmm1
cmpq -0x68(%rbp), %r14
je 0x310a
leaq (%r14,%rdi), %r8
xorl %eax, %eax
movq %r11, %r10
movq -0x60(%rbp), %rbx
subq $0x1, %rbx
jb 0x30ff
testq %r8, %r8
js 0x30f6
cmpq %rsi, %r10
jge 0x30f6
testq %r10, %r10
js 0x30f6
cmpq %rcx, %r8
jge 0x30f6
movq (%r13,%r8,8), %r15
movsd (%r15,%r10,8), %xmm2
movsd (%rdx,%rax), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
mulsd (%r9,%rax), %xmm2
addsd %xmm2, %xmm1
incq %r10
addq $0x8, %rax
jmp 0x30ba
incq %r14
addq %r12, %rdx
addq %r12, %r9
jmp 0x30a7
movq -0x50(%rbp), %rax
movq (%rax,%rdi,8), %rax
movq -0x40(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movq (%rax,%rdi,8), %rax
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
incq %r11
jmp 0x306f
incq %rdi
jmp 0x305f
leaq 0x3036(%rip), %r9       # 0x6178
movl (%r9), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq -0x30(%rbp), %r8
testl %r8d, %r8d
cmovlel %ecx, %r8d
xorpd %xmm0, %xmm0
movq -0x88(%rbp), %rdi
cmpq %r8, %rcx
je 0x31b3
xorl %edx, %edx
cmpq %rdx, %rax
je 0x31ae
movq -0x50(%rbp), %rsi
movq (%rsi,%rcx,8), %rsi
movsd (%rsi,%rdx,8), %xmm1
movq -0x58(%rbp), %rsi
movq (%rsi,%rcx,8), %rsi
movsd (%rsi,%rdx,8), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
sqrtsd %xmm1, %xmm1
movq (%rdi,%rcx,8), %rsi
movsd %xmm1, (%rsi,%rdx,8)
maxsd %xmm0, %xmm1
incq %rdx
movapd %xmm1, %xmm0
jmp 0x3169
incq %rcx
jmp 0x3162
movl (%r9), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movsd 0xe4b(%rip), %xmm1      # 0x4010
cmpq %r8, %rcx
je 0x31f1
xorl %edx, %edx
cmpq %rdx, %rax
je 0x31ec
movq (%rdi,%rcx,8), %rsi
movsd (%rsi,%rdx,8), %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd %xmm2, (%rsi,%rdx,8)
incq %rdx
jmp 0x31cc
incq %rcx
jmp 0x31c5
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq