// File: 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
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq 0x5346(%rip), %rax      # 0x9228
movsd (%rax), %xmm0
movsd 0x227a(%rip), %xmm1     # 0x6168
mulsd %xmm1, %xmm0
movsd 0x2276(%rip), %xmm1     # 0x6170
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movq %rax, -0xb8(%rbp)
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
movq %rax, %rcx
imulq %rcx, %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb0(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movq %rax, -0x40(%rbp)
movl -0x24(%rbp), %eax
movl %eax, %ecx
movq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq %rcx, %rdx
imulq %rdx, %rdx
leaq 0xf(,%rdx,8), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xa0(%rbp)
movq %rdx, %rsp
movq %rcx, -0x48(%rbp)
movq %rax, -0x50(%rbp)
xorl %eax, %eax
subl -0x28(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x40f6
xorl %eax, %eax
subl -0x28(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x40e6
cvtsi2sdl -0x58(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movl -0x54(%rbp), %eax
imull -0x54(%rbp), %eax
movl -0x58(%rbp), %ecx
imull -0x58(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
movsd 0x21a5(%rip), %xmm0     # 0x6180
leaq 0x5246(%rip), %rax      # 0x9228
mulsd (%rax), %xmm0
leaq 0x523b(%rip), %rax      # 0x9228
mulsd (%rax), %xmm0
divsd %xmm0, %xmm1
movsd 0x217b(%rip), %xmm0     # 0x6178
mulsd %xmm1, %xmm0
callq 0x2390
movq -0xb8(%rbp), %rdx
movq -0xb0(%rbp), %rax
movaps %xmm0, %xmm1
movsd -0xc8(%rbp), %xmm0
mulsd %xmm1, %xmm0
movl -0x54(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movl -0x58(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cvtsi2sdl -0x54(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movl -0x54(%rbp), %eax
imull -0x54(%rbp), %eax
movl -0x58(%rbp), %ecx
imull -0x58(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
movsd 0x2112(%rip), %xmm0     # 0x6180
leaq 0x51b3(%rip), %rax      # 0x9228
mulsd (%rax), %xmm0
leaq 0x51a8(%rip), %rax      # 0x9228
mulsd (%rax), %xmm0
divsd %xmm0, %xmm1
movsd 0x20e8(%rip), %xmm0     # 0x6178
mulsd %xmm1, %xmm0
callq 0x2390
movq -0xa8(%rbp), %rdx
movq -0xa0(%rbp), %rax
movaps %xmm0, %xmm1
movsd -0xc0(%rbp), %xmm0
mulsd %xmm1, %xmm0
movl -0x54(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movl -0x58(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x3fa6
jmp 0x40e8
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x3f92
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
leaq 0x510d(%rip), %rcx      # 0x9214
cmpl (%rcx), %eax
jge 0x42d4
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
leaq 0x50f8(%rip), %rcx      # 0x9218
cmpl (%rcx), %eax
jge 0x42c4
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorl %eax, %eax
subl -0x28(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x4282
xorl %eax, %eax
subl -0x28(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x4272
movl -0x6c(%rbp), %eax
addl -0x74(%rbp), %eax
cmpl $0x0, %eax
jl 0x4198
movl -0x70(%rbp), %eax
addl -0x78(%rbp), %eax
cmpl $0x0, %eax
jl 0x4198
movl -0x6c(%rbp), %eax
addl -0x74(%rbp), %eax
leaq 0x5091(%rip), %rcx      # 0x9214
cmpl (%rcx), %eax
jge 0x4198
movl -0x70(%rbp), %eax
addl -0x78(%rbp), %eax
leaq 0x5084(%rip), %rcx      # 0x9218
cmpl (%rcx), %eax
jl 0x419d
jmp 0x4264
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %rcx
movq -0xb8(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0x6c(%rbp), %r8d
addl -0x74(%rbp), %r8d
movslq %r8d, %r8
movq (%rsi,%r8,8), %rsi
movl -0x70(%rbp), %r8d
addl -0x78(%rbp), %r8d
movslq %r8d, %r8
movsd (%rsi,%r8,8), %xmm0
movl -0x74(%rbp), %esi
addl -0x28(%rbp), %esi
movslq %esi, %rsi
imulq %rdi, %rsi
shlq $0x3, %rsi
addq %rsi, %rcx
movl -0x78(%rbp), %esi
addl -0x28(%rbp), %esi
movslq %esi, %rsi
movsd (%rcx,%rsi,8), %xmm2
movsd -0x60(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x8(%rbp), %rcx
movl -0x6c(%rbp), %esi
addl -0x74(%rbp), %esi
movslq %esi, %rsi
movq (%rcx,%rsi,8), %rcx
movl -0x70(%rbp), %esi
addl -0x78(%rbp), %esi
movslq %esi, %rsi
movsd (%rcx,%rsi,8), %xmm0
movl -0x74(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movl -0x78(%rbp), %ecx
addl -0x28(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x68(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x4154
jmp 0x4274
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4140
movsd -0x60(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x70(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x68(%rbp), %xmm0
movq -0x20(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x70(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x4116
jmp 0x42c6
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x40fd
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
leaq 0x4f1e(%rip), %rcx      # 0x9214
cmpl (%rcx), %eax
jge 0x4401
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
leaq 0x4f03(%rip), %rcx      # 0x9218
cmpl (%rcx), %eax
jge 0x43eb
movq -0x18(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x90(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x90(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x20(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x90(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x20(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x90(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x2180
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
ucomisd -0x88(%rbp), %xmm0
jbe 0x43b7
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x80(%rbp), %xmm0
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x90(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x4308
jmp 0x43ed
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x42e9
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
leaq 0x4dfc(%rip), %rcx      # 0x9214
cmpl (%rcx), %eax
jge 0x44ac
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
leaq 0x4de1(%rip), %rcx      # 0x9218
cmpl (%rcx), %eax
jge 0x4496
movq -0x10(%rbp), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x98(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd -0x88(%rbp), %xmm0
movsd 0x1baa(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x98(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x442a
jmp 0x4498
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x440b
movq -0x30(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)