// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %rcx, -0x90(%rbp)
movq %rdx, -0x88(%rbp)
movq %rsi, -0xa8(%rbp)
movq %rdi, %r12
leaq 0x3f65(%rip), %r15      # 0x7190
movsd (%r15), %xmm0
mulsd 0x1f28(%rip), %xmm0     # 0x5160
addsd 0x1f28(%rip), %xmm0     # 0x5168
cvttsd2si %xmm0, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movq %rax, -0x80(%rbp)
movl %eax, %edi
negl %edi
movq %rcx, %rax
imulq %rcx, %rax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %rcx, -0x48(%rbp)
cmpl $-0x1, %ecx
jl 0x33d9
movslq %edi, %rsi
movq -0x80(%rbp), %rcx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
incl %eax
movl %eax, -0x74(%rbp)
movq -0x48(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0xb0(%rbp)
movl %esi, %r8d
cmpl %esi, %ecx
movl %edi, %eax
cmovgl %ecx, %eax
negl %eax
cltq
leaq (%rax,%r8), %rcx
decq %rcx
movapd 0x1ee6(%rip), %xmm0     # 0x51b0
movq %r14, %r9
movq %rbx, %rax
movq %rdi, -0x58(%rbp)
movq %r8, -0x30(%rbp)
movq %rcx, -0x50(%rbp)
movq %rax, -0x40(%rbp)
movq %rsi, %rax
imulq %rsi, %rax
movq %rax, -0x38(%rbp)
movq %rsi, -0xb8(%rbp)
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movsd %xmm1, -0x60(%rbp)
xorl %r13d, %r13d
movq %r9, -0x68(%rbp)
leal (%rdi,%r13), %eax
leal (%r8,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd %xmm1, -0x70(%rbp)
imull %eax, %eax
addl -0x38(%rbp), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
xorpd %xmm0, %xmm2
movapd %xmm2, -0xa0(%rbp)
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
callq 0x2280
mulsd -0x70(%rbp), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, (%rax,%r13,8)
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd -0xa0(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x2280
movq -0x68(%rbp), %r9
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x58(%rbp), %rdi
mulsd -0x60(%rbp), %xmm0
movsd %xmm0, (%r9,%r13,8)
movapd 0x1e0c(%rip), %xmm0     # 0x51b0
incq %r13
movl %ecx, %eax
addl %r13d, %eax
jne 0x3305
movq -0xb8(%rbp), %rsi
incq %rsi
movq -0xb0(%rbp), %rax
movq -0x40(%rbp), %r10
addq %rax, %r10
addq %rax, %r9
cmpl %esi, -0x74(%rbp)
movq %r10, %rax
jne 0x32dc
leaq 0x3d9c(%rip), %rax      # 0x717c
movslq (%rax), %rax
movq %rax, -0x50(%rbp)
testq %rax, %rax
jle 0x35e2
leaq 0x3d89(%rip), %rax      # 0x7180
movl (%rax), %eax
movslq %edi, %rdx
movq -0x80(%rbp), %rcx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movq %rax, -0x38(%rbp)
movslq %eax, %r9
incl %esi
movl %esi, -0x70(%rbp)
movq -0x48(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0xa0(%rbp)
movq %rdx, -0x30(%rbp)
cmpl %edx, %ecx
cmovgl %ecx, %edi
leal (%rcx,%rdi), %eax
incl %eax
movl %eax, -0x40(%rbp)
xorl %r15d, %r15d
cmpl $0x0, -0x38(%rbp)
jle 0x350f
movq -0x88(%rbp), %rax
movq (%rax,%r15,8), %rax
movq %rax, -0x60(%rbp)
movq -0x90(%rbp), %rax
movq (%rax,%r15,8), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %r8
xorl %ecx, %ecx
movq %rcx, -0x68(%rbp)
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpl $-0x1, -0x48(%rbp)
jl 0x34e9
leaq 0x3cfb(%rip), %rax      # 0x717c
movslq (%rax), %rdx
xorl %r10d, %r10d
movq -0x30(%rbp), %r13
leaq (%r15,%r13), %rax
movl -0x40(%rbp), %r11d
movq %r8, %rdi
movq %r10, %rsi
testq %rax, %rax
js 0x34cd
cmpq %r9, %rdi
jge 0x34cd
testq %rdi, %rdi
js 0x34cd
cmpq %rdx, %rax
jge 0x34cd
movq (%r12,%rax,8), %rcx
movsd (%rcx,%rdi,8), %xmm2
movsd (%rbx,%rsi), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
mulsd (%r14,%rsi), %xmm2
addsd %xmm2, %xmm0
addq $0x8, %rsi
incq %rdi
decl %r11d
jne 0x3499
incq %r13
addq -0xa0(%rbp), %r10
cmpl %r13d, -0x70(%rbp)
jne 0x348b
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
movsd %xmm1, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
incq %r8
cmpq -0x38(%rbp), %rcx
jne 0x3468
incq %r15
cmpq -0x50(%rbp), %r15
jne 0x343a
leaq 0x3c59(%rip), %rax      # 0x717c
movslq (%rax), %rax
testq %rax, %rax
movq -0xa8(%rbp), %r10
leaq 0x3c49(%rip), %r11      # 0x7180
jle 0x35e2
movl (%r11), %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
testl %ecx, %ecx
jle 0x359d
movq -0x88(%rbp), %rsi
movq (%rsi,%rdx,8), %rsi
movq -0x90(%rbp), %rdi
movq (%rdi,%rdx,8), %rdi
movq (%r10,%rdx,8), %r8
xorl %r9d, %r9d
movapd %xmm0, %xmm1
movsd (%rsi,%r9,8), %xmm0
movsd (%rdi,%r9,8), %xmm2
mulsd %xmm2, %xmm2
mulsd %xmm0, %xmm0
addsd %xmm2, %xmm0
sqrtsd %xmm0, %xmm0
movsd %xmm0, (%r8,%r9,8)
maxsd %xmm1, %xmm0
incq %r9
movapd %xmm0, %xmm1
cmpq %r9, %rcx
jne 0x356b
incq %rdx
cmpq %rax, %rdx
jne 0x3546
testl %eax, %eax
jle 0x35e2
movl (%r11), %ecx
xorl %edx, %edx
movsd 0x1bba(%rip), %xmm1     # 0x5170
testl %ecx, %ecx
jle 0x35da
movq (%r10,%rdx,8), %rsi
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd %xmm2, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rcx
jne 0x35c0
incq %rdx
cmpq %rax, %rdx
jne 0x35b6
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq