// File: /workspace/github_cpp_makefile_star2_O1/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, -0x98(%rbp)
movq %rsi, -0x80(%rbp)
movq %rdi, %r12
leaq 0x3f8c(%rip), %r15      # 0x7198
movsd (%r15), %xmm0
mulsd 0x1f4f(%rip), %xmm0     # 0x5168
addsd 0x1f4f(%rip), %xmm0     # 0x5170
cvttsd2si %xmm0, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movq %rax, -0x88(%rbp)
movl %eax, %r8d
negl %r8d
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, -0x50(%rbp)
cmpl $-0x1, %ecx
jl 0x33c2
movslq %r8d, %rsi
movq -0x88(%rbp), %rcx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
incl %eax
movl %eax, -0x74(%rbp)
movq -0x50(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0xb8(%rbp)
movl %esi, %edi
cmpl %esi, %ecx
movl %r8d, %eax
cmovgl %ecx, %eax
negl %eax
cltq
leaq (%rax,%rdi), %rcx
decq %rcx
movapd 0x1eed(%rip), %xmm0     # 0x51a0
movq %r14, %r9
movq %rbx, %rax
movq %r8, -0x60(%rbp)
movq %rdi, -0x38(%rbp)
movq %rcx, -0x58(%rbp)
movq %rax, -0x48(%rbp)
movq %rsi, %rax
imulq %rsi, %rax
movq %rax, -0x40(%rbp)
movq %rsi, -0xc0(%rbp)
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movsd %xmm1, -0x68(%rbp)
xorl %r13d, %r13d
movq %r9, -0x70(%rbp)
leal (%r8,%r13), %eax
leal (%rdi,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd %xmm1, -0x30(%rbp)
imull %eax, %eax
addl -0x40(%rbp), %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
xorpd %xmm0, %xmm2
movapd %xmm2, -0xb0(%rbp)
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
callq 0x2290
mulsd -0x30(%rbp), %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, (%rax,%r13,8)
movsd (%r15), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd -0xb0(%rbp), %xmm0
divsd %xmm1, %xmm0
callq 0x2290
movq -0x70(%rbp), %r9
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rdi
movq -0x60(%rbp), %r8
mulsd -0x68(%rbp), %xmm0
movsd %xmm0, (%r9,%r13,8)
movapd 0x1e13(%rip), %xmm0     # 0x51a0
incq %r13
movl %ecx, %eax
addl %r13d, %eax
jne 0x32ee
movq -0xc0(%rbp), %rsi
incq %rsi
movq -0xb8(%rbp), %rax
movq -0x48(%rbp), %r10
addq %rax, %r10
addq %rax, %r9
cmpl %esi, -0x74(%rbp)
movq %r10, %rax
jne 0x32c5
leaq 0x3dbb(%rip), %rax      # 0x7184
movslq (%rax), %rax
movq %rax, -0x58(%rbp)
testq %rax, %rax
jle 0x350b
leaq 0x3da8(%rip), %rax      # 0x7188
movl (%rax), %eax
movslq %r8d, %rdx
movq -0x88(%rbp), %rcx
movl %ecx, %esi
negl %esi
cmovsl %ecx, %esi
movq %rax, -0x40(%rbp)
movslq %eax, %rdi
incl %esi
movl %esi, -0x30(%rbp)
movq -0x50(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0xb0(%rbp)
movq %rdx, -0x38(%rbp)
cmpl %edx, %ecx
cmovgl %ecx, %r8d
leal (%rcx,%r8), %eax
incl %eax
movl %eax, -0x48(%rbp)
xorl %r10d, %r10d
cmpl $0x0, -0x40(%rbp)
jle 0x34fe
movq -0x98(%rbp), %rax
movq (%rax,%r10,8), %rax
movq %rax, -0x68(%rbp)
movq -0x90(%rbp), %rax
movq (%rax,%r10,8), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rsi
xorl %ecx, %ecx
movq %rcx, -0x70(%rbp)
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpl $-0x1, -0x50(%rbp)
jl 0x34d8
leaq 0x3d15(%rip), %rax      # 0x7184
movslq (%rax), %rdx
xorl %r8d, %r8d
movq -0x38(%rbp), %r11
leaq (%r11,%r10), %r15
movl -0x48(%rbp), %r9d
movq %rsi, %r13
movq %r8, %rcx
testq %r15, %r15
js 0x34bc
cmpq %rdi, %r13
jge 0x34bc
testq %r13, %r13
js 0x34bc
cmpq %rdx, %r15
jge 0x34bc
movq (%r12,%r15,8), %rax
movsd (%rax,%r13,8), %xmm2
movsd (%rbx,%rcx), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
mulsd (%r14,%rcx), %xmm2
addsd %xmm2, %xmm0
addq $0x8, %rcx
incq %r13
decl %r9d
jne 0x3487
incq %r11
addq -0xb0(%rbp), %r8
cmpl %r11d, -0x30(%rbp)
jne 0x3479
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movsd %xmm1, (%rax,%rcx,8)
movq -0x60(%rbp), %rax
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
incq %rsi
cmpq -0x40(%rbp), %rcx
jne 0x3456
incq %r10
cmpq -0x58(%rbp), %r10
jne 0x3428
leaq 0x3c72(%rip), %rax      # 0x7184
movl (%rax), %ecx
testl %ecx, %ecx
jle 0x35c1
xorpd %xmm2, %xmm2
xorl %ebx, %ebx
leaq 0x3c5f(%rip), %r14      # 0x7188
xorpd %xmm0, %xmm0
movq -0x80(%rbp), %r12
movq -0x90(%rbp), %r13
cmpl $0x0, (%r14)
jle 0x35a9
xorl %r15d, %r15d
movapd %xmm0, %xmm3
movq -0x98(%rbp), %rax
movq (%rax,%rbx,8), %rax
movsd (%rax,%r15,8), %xmm0
movq (%r13,%rbx,8), %rax
movsd (%rax,%r15,8), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x3579
sqrtsd %xmm0, %xmm0
jmp 0x358c
movsd %xmm3, -0x30(%rbp)
callq 0x2130
movsd -0x30(%rbp), %xmm3
xorpd %xmm2, %xmm2
movq (%r12,%rbx,8), %rax
movsd %xmm0, (%rax,%r15,8)
maxsd %xmm3, %xmm0
incq %r15
movslq (%r14), %rax
movapd %xmm0, %xmm3
cmpq %rax, %r15
jl 0x3545
incq %rbx
leaq 0x3bd1(%rip), %rax      # 0x7184
movslq (%rax), %rcx
cmpq %rcx, %rbx
jl 0x3538
jmp 0x35c9
xorpd %xmm0, %xmm0
movq -0x80(%rbp), %r12
testl %ecx, %ecx
jle 0x360e
leaq 0x3bb4(%rip), %rax      # 0x7188
movl (%rax), %eax
movl %ecx, %ecx
xorl %edx, %edx
movsd 0x1a2e(%rip), %xmm1     # 0x5010
testl %eax, %eax
jle 0x3606
movq (%r12,%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, %rax
jne 0x35ec
incq %rdx
cmpq %rcx, %rdx
jne 0x35e2
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq