// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq 0x90(%rsp), %rax
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movl %edx, 0x74(%rsp)
movl %ecx, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movq %r9, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movsd %xmm1, 0x50(%rsp)
movq 0x203db7(%rip), %rax    # 0x60a5a8
movq (%rax), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jg 0x406844
movq 0x203d94(%rip), %rax    # 0x60a5a8
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x203d75(%rip), %rax    # 0x60a5a0
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x0, (%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x406803
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x70(%rsp), %eax
jg 0x4068a1
movq 0x203d4b(%rip), %rax    # 0x60a5a8
movq (%rax), %rax
movslq 0x48(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x203d2c(%rip), %rax    # 0x60a5a0
movq (%rax), %rax
movslq 0x48(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x90(%rsp), %rax
movslq 0x48(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x40684c
movl $0x1, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jg 0x406b00
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x80(%rsp), %rax
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x10(%rsp), %rcx
callq 0x4060e0
movl $0x1, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x70(%rsp), %eax
jg 0x406aee
leaq 0x10(%rsp), %rdi
movq 0x78(%rsp), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
callq 0x406000
movsd %xmm0, 0x8(%rsp)
movq 0x203c87(%rip), %rax    # 0x60a5a8
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x8(%rsp), %xmm1
divsd 0x58(%rsp), %xmm1
movsd 0x1856b6(%rip), %xmm2   # 0x58c008
addsd %xmm1, %xmm2
movsd 0x1856aa(%rip), %xmm1   # 0x58c008
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x203c35(%rip), %rax    # 0x60a5a8
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x203c08(%rip), %rax    # 0x60a5a0
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
testb $0x1, (%rax,%rcx)
je 0x4069c3
movsd 0x50(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x203bde(%rip), %rax    # 0x60a5a8
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x203bb1(%rip), %rax    # 0x60a5a0
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
testb $0x1, (%rax,%rcx)
je 0x406a1a
movsd 0x50(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x30(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
jb 0x406a71
movsd 0x30(%rsp), %xmm0
ucomisd 0x38(%rsp), %xmm0
jb 0x406a71
movq 0x203b63(%rip), %rax    # 0x60a5a0
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movb $0x1, (%rax,%rcx)
movsd 0x30(%rsp), %xmm0
movq 0x203b4c(%rip), %rax    # 0x60a5a8
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x406adc
movq 0x203b28(%rip), %rax    # 0x60a5a0
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movsd 0x38(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
jb 0x406aba
movsd 0x38(%rsp), %xmm0
movq 0x203b03(%rip), %rax    # 0x60a5a8
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x406ada
movsd 0x40(%rsp), %xmm0
movq 0x203ae1(%rip), %rax    # 0x60a5a8
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x406adc
jmp 0x406ade
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x4068e9
jmp 0x406af0
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x4068a9
movl 0x74(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x48(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x4c(%rsp)
movb %al, 0x7(%rsp)
jle 0x406b29
cmpl $0x0, 0x48(%rsp)
setg %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x406b36
jmp 0x406c63
movq 0x203a63(%rip), %rax    # 0x60a5a0
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
testb $0x1, (%rax,%rcx)
je 0x406b88
movl 0x4c(%rsp), %edx
subl $0x1, %edx
movq 0x90(%rsp), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl 0x4c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x48(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x406c5e
movq 0x203a19(%rip), %rax    # 0x60a5a8
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x2039ec(%rip), %rax    # 0x60a5a0
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq 0x48(%rsp), %rcx
testb $0x1, (%rax,%rcx)
je 0x406bdf
movsd 0x50(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x2039c2(%rip), %rax    # 0x60a5a8
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x203995(%rip), %rax    # 0x60a5a0
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x48(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
testb $0x1, (%rax,%rcx)
je 0x406c36
movsd 0x50(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
ucomisd 0x40(%rsp), %xmm0
jb 0x406c51
movl 0x48(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x406c5c
movl 0x4c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x406c5e
jmp 0x406b10
addq $0x88, %rsp
retq
nopl (%rax,%rax)