// File: /workspace/github_c_makefile_star2_O2/asford[P]tmalign/./NW.h
// Function: NWDP_TM(int*, int*, int, int, double, int*)
void NWDP_TM(int *secx, int *secy, int len1, int len2, double gap_open, int j2i[])
{
	//NW dynamic programming for alignment
	//not a standard implementation of NW algorithm
    //Input: secondary structure secx, secy, 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			
			if(secx[i-1]==secy[j-1])
			{
				d=val[i-1][j-1] + 1.0;
			}
			else
			{
				d=val[i-1][j-1];
			}

			//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 %r12
pushq %rbx
movq 0x1fcb12(%rip), %rax    # 0x6025a8
movq (%rax), %r9
andq $0x0, (%r9)
movq 0x1fcafc(%rip), %r10    # 0x6025a0
movslq %edx, %r11
xorl %ebx, %ebx
cmpq %r11, %rbx
jg 0x405ac3
movq (%rax,%rbx,8), %r14
andq $0x0, (%r14)
movq (%r10,%rbx,8), %r14
movb $0x0, (%r14)
incq %rbx
jmp 0x405aa9
movq 0x1fcad6(%rip), %r10    # 0x6025a0
movslq %ecx, %r11
xorl %ebx, %ebx
cmpq %r11, %rbx
jg 0x405aeb
andq $0x0, (%r9,%rbx,8)
movq (%r10), %r14
movb $0x0, (%r14,%rbx)
orl $-0x1, (%r8,%rbx,4)
incq %rbx
jmp 0x405acf
xorl %r9d, %r9d
testl %ecx, %ecx
movl $0x0, %r10d
cmovgl %ecx, %r10d
testl %edx, %edx
cmovgl %edx, %r9d
incl %r9d
movabsq $0x1, %r11
movsd 0x17d4f3(%rip), %xmm1   # 0x583008
cmpq %r9, %r11
je 0x405bcb
movq 0x1fca7b(%rip), %rbx    # 0x6025a0
xorl %r14d, %r14d
cmpq %r14, %r10
je 0x405bc3
movl -0x4(%rdi,%r11,4), %ebp
movq -0x8(%rax,%r11,8), %r15
movsd (%r15,%r14,8), %xmm3
movapd %xmm3, %xmm2
addsd %xmm1, %xmm2
cmpl (%rsi,%r14,4), %ebp
je 0x405b53
movapd %xmm3, %xmm2
movsd 0x8(%r15,%r14,8), %xmm3
movq -0x8(%rbx,%r11,8), %r15
cmpb $0x0, 0x1(%r15,%r14)
je 0x405b6b
addsd %xmm0, %xmm3
movq (%rax,%r11,8), %r15
movq (%rbx,%r11,8), %r12
movsd (%r15,%r14,8), %xmm4
cmpb $0x0, (%r12,%r14)
je 0x405b84
addsd %xmm0, %xmm4
ucomisd %xmm3, %xmm2
jb 0x405b9f
ucomisd %xmm4, %xmm2
jb 0x405b9f
movb $0x1, 0x1(%r12,%r14)
movsd %xmm2, 0x8(%r15,%r14,8)
jmp 0x405bbb
movb $0x0, 0x1(%r12,%r14)
ucomisd %xmm3, %xmm4
jae 0x405bb4
movsd %xmm3, 0x8(%r15,%r14,8)
jmp 0x405bbb
movsd %xmm4, 0x8(%r15,%r14,8)
incq %r14
jmp 0x405b28
incq %r11
jmp 0x405b15
movq 0x1fc9ce(%rip), %rsi    # 0x6025a0
movl %edx, %r9d
decl %edx
movl %ecx, %edi
incl %ecx
testl %r9d, %r9d
jle 0x405c37
testl %edi, %edi
jle 0x405c37
movq (%rsi,%r9,8), %r10
cmpb $0x1, (%r10,%rdi)
je 0x405c2c
movq (%rax,%rdx,8), %r11
movsd (%r11,%rdi,8), %xmm1
movq (%rsi,%rdx,8), %r11
cmpb $0x0, (%r11,%rdi)
je 0x405c08
addsd %xmm0, %xmm1
movq (%rax,%r9,8), %r11
decq %rdi
movl %edi, %ebx
movsd (%r11,%rbx,8), %xmm2
cmpb $0x0, (%r10,%rbx)
je 0x405c22
addsd %xmm0, %xmm2
decl %ecx
ucomisd %xmm1, %xmm2
jae 0x405bdb
jmp 0x405bd2
movl %edx, -0x4(%r8,%rdi,4)
decl %edi
movl %edi, %ecx
jmp 0x405bd2
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq