// File: /workspace/github_c_makefile_star2_O3/asford[P]tmalign/./NW.h
// Function: NWDP_TM(int, int, double, int*)
void NWDP_TM(int len1, int len2, double gap_open, int j2i[])
{
	//NW dynamic programming for alignment
	//not a standard implementation of NW algorithm
    //Input: score[1:len1, 1:len2], 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]
	}      

	
	//decide matrix and path
	for(i=1; i<=len1; i++)	
	{	
		for(j=1; j<=len2; j++)
		{
			d=val[i-1][j-1]+score[i][j]; //diagonal

			//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
movq 0x1fca59(%rip), %rax    # 0x6025a8
movq (%rax), %rcx
movq $0x0, (%rcx)
testl %edi, %edi
js 0x405b86
movq 0x1fca3c(%rip), %r8     # 0x6025a0
leal 0x1(%rdi), %r9d
xorl %r10d, %r10d
movq (%rax,%r10,8), %r11
movq $0x0, (%r11)
movq (%r8,%r10,8), %r11
movb $0x0, (%r11)
incq %r10
cmpq %r10, %r9
jne 0x405b6b
testl %esi, %esi
js 0x405bb8
movq 0x1fca0f(%rip), %r8     # 0x6025a0
movq (%r8), %r8
leal 0x1(%rsi), %r9d
xorl %r10d, %r10d
movq $0x0, (%rcx,%r10,8)
movb $0x0, (%r8,%r10)
movl $0xffffffff, (%rdx,%r10,4) # imm = 0xFFFFFFFF
incq %r10
cmpq %r10, %r9
jne 0x405b9b
testl %edi, %edi
jle 0x405cec
movq 0x1fc9d1(%rip), %rcx    # 0x602598
movq 0x1fc9d2(%rip), %r8     # 0x6025a0
leal 0x1(%rsi), %r9d
leal 0x1(%rdi), %r10d
movl $0x1, %r11d
testl %esi, %esi
jle 0x405c6a
movq -0x8(%rax,%r11,8), %rbx
movq (%rax,%r11,8), %r14
movq (%rcx,%r11,8), %r15
movq -0x8(%r8,%r11,8), %r12
movq (%r8,%r11,8), %r13
movl $0x1, %ebp
movsd (%rbx,%rbp,8), %xmm1
cmpb $0x0, (%r12,%rbp)
je 0x405c0f
addsd %xmm0, %xmm1
movsd -0x8(%r14,%rbp,8), %xmm2
cmpb $0x0, -0x1(%r13,%rbp)
je 0x405c22
addsd %xmm0, %xmm2
movsd (%r15,%rbp,8), %xmm3
addsd -0x8(%rbx,%rbp,8), %xmm3
ucomisd %xmm1, %xmm3
jb 0x405c48
ucomisd %xmm2, %xmm3
jb 0x405c48
movb $0x1, (%r13,%rbp)
movsd %xmm3, (%r14,%rbp,8)
jmp 0x405c62
movb $0x0, (%r13,%rbp)
ucomisd %xmm1, %xmm2
jae 0x405c5c
movsd %xmm1, (%r14,%rbp,8)
jmp 0x405c62
movsd %xmm2, (%r14,%rbp,8)
incq %rbp
cmpq %rbp, %r9
jne 0x405bff
incq %r11
cmpq %r10, %r11
jne 0x405bdc
testl %esi, %esi
jle 0x405cec
movq 0x1fc91f(%rip), %rcx    # 0x6025a0
movl %edi, %r9d
movq (%rcx,%r9,8), %r10
movl %esi, %r11d
leal -0x1(%rdi), %r8d
cmpb $0x1, (%r10,%r11)
jne 0x405c9f
movl %r8d, -0x4(%rdx,%r11,4)
decl %esi
jmp 0x405cda
movl %r8d, %ebx
movq (%rax,%rbx,8), %r14
movsd (%r14,%r11,8), %xmm1
movq (%rcx,%rbx,8), %rbx
cmpb $0x0, (%rbx,%r11)
je 0x405cbb
addsd %xmm0, %xmm1
movq (%rax,%r9,8), %r11
leal -0x1(%rsi), %r9d
movsd (%r11,%r9,8), %xmm2
cmpb $0x0, (%r10,%r9)
je 0x405cd4
addsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x405ce0
movl %esi, %r9d
movl %r8d, %edi
testl %edi, %edi
jle 0x405cec
movl %r9d, %esi
testl %r9d, %r9d
jg 0x405c81
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq