// File: /workspace/github_c_makefile_star2_O2/asford[P]tmalign/./NW.h
// Function: NWDP_TM(double**, double**, int, int, double*, double (*) [3], double, double, int*)
void NWDP_TM(double **x, double **y, int len1, int len2, double t[3], double u[3][3], double d02, double gap_open, int j2i[])
{
	//NW dynamic programming for alignment
	//not a standard implementation of NW algorithm
    //Input: vectors x, y, rotation matrix t, u, scale factor d02, and gap_open
    //Output: j2i[1:len2] \in {1:len1} U {-1}
	//path[0:len1, 0:len2]=1,2,3, from diagonal, horizontal, vertical

	int i, j;
	double h, v, d;

	//initialization
	val[0][0]=0;
	for(i=0; i<=len1; i++)
	{
		val[i][0]=0;
		path[i][0]=false; //not from diagonal
	}

	for(j=0; j<=len2; j++)
	{
		val[0][j]=0;
		path[0][j]=false; //not from diagonal
		j2i[j]=-1;	//all are not aligned, only use j2i[1:len2]
	}      
	double xx[3], dij;

	
	//decide matrix and path
	for(i=1; i<=len1; i++)	
	{	
		transform(t, u, &x[i-1][0], xx);
		for(j=1; j<=len2; j++)
		{
			//d=val[i-1][j-1]+score[i][j]; //diagonal
			dij=dist(xx, &y[j-1][0]);    					
			d=val[i-1][j-1] +  1.0/(1+dij/d02);

			//symbol insertion in horizontal (= a gap in vertical)
			h=val[i-1][j];
			if(path[i-1][j]) //aligned in last position
				h += gap_open;				

			//symbol insertion in vertical
			v=val[i][j-1];
			if(path[i][j-1]) //aligned in last position
				v += gap_open;


			if(d>=h && d>=v)
			{
				path[i][j]=true; //from diagonal
				val[i][j]=d;
			}
			else 
			{
				path[i][j]=false; //from horizontal
				if(v>=h)
					val[i][j]=v;
				else					
					val[i][j]=h;
			}
		} //for i
	} //for j

	//trace back to extract the alignment
	i=len1;
    j=len2;
	while(i>0 && j>0)
	{
		if(path[i][j]) //from diagonal
		{
			j2i[j-1]=i-1;		
			i--;
			j--;
		}
		else 			
		{
			h=val[i-1][j];
			if(path[i-1][j]) h +=gap_open;

			v=val[i][j-1];
			if(path[i][j-1]) v +=gap_open;

			if(v>=h)
				j--;
			else
				i--;
		}
	}	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm1, (%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, 0x18(%rsp)
movq 0x1fcd31(%rip), %r12    # 0x6025a8
movq (%r12), %rax
andq $0x0, (%rax)
movq 0x90(%rsp), %r10
movq 0x1fcd12(%rip), %r9     # 0x6025a0
movslq %edx, %r8
xorl %esi, %esi
cmpq %r8, %rsi
jg 0x4058ac
movq (%r12,%rsi,8), %rdi
andq $0x0, (%rdi)
movq (%r9,%rsi,8), %rdi
movb $0x0, (%rdi)
incq %rsi
jmp 0x405893
movq 0x1fcced(%rip), %r9     # 0x6025a0
movslq %ecx, %r8
xorl %esi, %esi
cmpq %r8, %rsi
jg 0x4058d3
andq $0x0, (%rax,%rsi,8)
movq (%r9), %rdi
movb $0x0, (%rdi,%rsi)
orl $-0x1, (%r10,%rsi,4)
incq %rsi
jmp 0x4058b8
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %ebp
movq %rcx, 0x8(%rsp)
cmovgl %ecx, %ebp
testl %edx, %edx
movq %rdx, 0x10(%rsp)
cmovgl %edx, %eax
incl %eax
movq %rax, 0x30(%rsp)
movsd 0x17d70b(%rip), %xmm1   # 0x583008
divsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
pushq $0x1
popq %r14
cmpq 0x30(%rsp), %r14
je 0x4059f1
movq 0x18(%rsp), %rax
movq -0x8(%rax,%r14,8), %rdx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rcx
callq 0x4054f3
movq 0x1fcc65(%rip), %rbx    # 0x6025a0
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x4059e9
movq (%r13,%r15,8), %rsi
leaq 0x40(%rsp), %rdi
callq 0x4054a5
movq -0x8(%r12,%r14,8), %rcx
movsd 0x8(%rcx,%r15,8), %xmm1
movq -0x8(%rbx,%r14,8), %rax
cmpb $0x0, 0x1(%rax,%r15)
je 0x405974
addsd (%rsp), %xmm1
mulsd 0x38(%rsp), %xmm0
movsd 0x17d686(%rip), %xmm2   # 0x583008
addsd %xmm2, %xmm0
divsd %xmm0, %xmm2
movq (%r12,%r14,8), %rax
addsd (%rcx,%r15,8), %xmm2
movq (%rbx,%r14,8), %rcx
movsd (%rax,%r15,8), %xmm0
cmpb $0x0, (%rcx,%r15)
je 0x4059aa
addsd (%rsp), %xmm0
ucomisd %xmm1, %xmm2
jb 0x4059c5
ucomisd %xmm0, %xmm2
jb 0x4059c5
movb $0x1, 0x1(%rcx,%r15)
movsd %xmm2, 0x8(%rax,%r15,8)
jmp 0x4059e1
movb $0x0, 0x1(%rcx,%r15)
ucomisd %xmm1, %xmm0
jae 0x4059da
movsd %xmm1, 0x8(%rax,%r15,8)
jmp 0x4059e1
movsd %xmm0, 0x8(%rax,%r15,8)
incq %r15
jmp 0x40593e
incq %r14
jmp 0x40590b
movq 0x1fcba8(%rip), %rax    # 0x6025a0
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r10
movq 0x90(%rsp), %r11
movl %r10d, %ecx
decl %r10d
movl %r9d, %r9d
testl %r9d, %r9d
jle 0x405a78
testl %ecx, %ecx
jle 0x405a78
movq (%rax,%r9,8), %rsi
cmpb $0x1, (%rsi,%rcx)
je 0x405a6a
leaq -0x1(%r9), %rdx
movl %edx, %edi
shlq $0x3, %rdi
movq (%r12,%rdi), %r8
movsd (%r8,%rcx,8), %xmm0
movq (%rax,%rdi), %rdi
cmpb $0x0, (%rdi,%rcx)
je 0x405a49
addsd (%rsp), %xmm0
movq (%r12,%r9,8), %rdi
movsd (%rdi,%r10,8), %xmm1
cmpb $0x0, (%rsi,%r10)
je 0x405a5f
addsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x405a0a
movq %rdx, %r9
jmp 0x405a13
decl %r9d
movl %r9d, -0x4(%r11,%rcx,4)
leal -0x1(%rcx), %r10d
jmp 0x405a0a
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq