// File: sorazy[P]canny/canny.cpp
// Function: recursiveDT(double**, double**, HashMap*, HashMap*, int, int, int)
void recursiveDT(double **mag, double **final, HashMap *h, HashMap *peaks, int a, int b, int flag)
{
	// If the pixel value is < lo, out-of-bounds, or at a point we've visited before,
	// then exit the funciton.
	if (mag[a][b] < lo || a < 0 || b < 0 || a >= height || b >= width)
		return;
	if (h->contains(a, b))
		return;

	// Insert the current pixel so we know we've been here before.
	h->insert(a, b);

	// If flag = 0, that means that this is the first pixel of the "series" that
	// we're looking at. We're going to look for a pixel in "final" that's set to
	// ON. If we found one, assert the flag and break out of the loops.
	if (!flag)
	{
		for (int p = -1; p <= 1; p++)
		{
			for (int q = -1; q <= 1; q++)
			{
				if (final[a+p][b+q] == 255)
				{
					final[a][b] = 255;
					flag = 1;
					break;
				}
			}

			if (flag)
				break;
		}
	}
	
	// If flag is asserted, that means we found a pixel that's in final, all what
	// we have to do now is just search for pixels that are in the mid range.
	// Also, make sure that it's in the peaks to begin with.
	if (flag)
	{
		for (int p = -1; p <= 1; p++)
		{
			for (int q = -1; q <= 1; q++)
			{
				if (mag[a+p][b+q] >= lo && peaks->contains(a+p, b+q))
				{
					recursiveDT(mag, final, h, peaks, a+p, b+q, 1);
					final[a][b] = 255;
				}
			}
		}
	}

	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
leaq 0x4725(%rip), %rax      # 0x9224
cvtsi2sdl (%rax), %xmm0
ucomisd %xmm1, %xmm0
ja 0x4b31
cmpl $0x0, -0x24(%rbp)
jl 0x4b31
cmpl $0x0, -0x28(%rbp)
jl 0x4b31
movl -0x24(%rbp), %eax
leaq 0x46f5(%rip), %rcx      # 0x9214
cmpl (%rcx), %eax
jge 0x4b31
movl -0x28(%rbp), %eax
leaq 0x46eb(%rip), %rcx      # 0x9218
cmpl (%rcx), %eax
jl 0x4b36
jmp 0x4ce5
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
callq 0x5740
cmpl $0x0, %eax
je 0x4b4f
jmp 0x4ce5
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
callq 0x5850
cmpl $0x0, 0x10(%rbp)
jne 0x4c02
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x2c(%rbp)
jg 0x4c00
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x30(%rbp)
jg 0x4be8
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x28(%rbp), %ecx
addl -0x30(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x1463(%rip), %xmm1     # 0x6010
ucomisd %xmm1, %xmm0
jne 0x4bdb
jp 0x4bdb
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
movsd 0x1443(%rip), %xmm0     # 0x6010
movsd %xmm0, (%rax,%rcx,8)
movl $0x1, 0x10(%rbp)
jmp 0x4be8
jmp 0x4bdd
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x4b80
cmpl $0x0, 0x10(%rbp)
je 0x4bf0
jmp 0x4c00
jmp 0x4bf2
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4b6f
jmp 0x4c02
cmpl $0x0, 0x10(%rbp)
je 0x4ce3
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x34(%rbp)
jg 0x4ce1
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, -0x38(%rbp)
jg 0x4cd1
movq -0x8(%rbp), %rax
movl -0x24(%rbp), %ecx
addl -0x34(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x28(%rbp), %ecx
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x45d0(%rip), %rax      # 0x9224
cvtsi2sdl (%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x4cc1
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
addl -0x34(%rbp), %esi
movl -0x28(%rbp), %edx
addl -0x38(%rbp), %edx
callq 0x5740
cmpl $0x0, %eax
je 0x4cc1
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x24(%rbp), %r8d
addl -0x34(%rbp), %r8d
movl -0x28(%rbp), %r9d
addl -0x38(%rbp), %r9d
movl $0x1, (%rsp)
callq 0x4ac0
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
movsd 0x1354(%rip), %xmm0     # 0x6010
movsd %xmm0, (%rax,%rcx,8)
jmp 0x4cc3
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x4c24
jmp 0x4cd3
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4c13
jmp 0x4ce3
jmp 0x4ce5
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)