// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
movq %rdi, -0x8(%rsp)
movq 0x1fc621(%rip), %rax    # 0x6025a8
movq (%rax), %rdi
movq $0x0, (%rdi)
testl %edx, %edx
js 0x405fbd
movq 0x1fc604(%rip), %r9     # 0x6025a0
leal 0x1(%rdx), %r10d
xorl %r11d, %r11d
movq (%rax,%r11,8), %rbx
movq $0x0, (%rbx)
movq (%r9,%r11,8), %rbx
movb $0x0, (%rbx)
incq %r11
cmpq %r11, %r10
jne 0x405fa3
testl %ecx, %ecx
js 0x405fef
movq 0x1fc5d8(%rip), %r9     # 0x6025a0
movq (%r9), %r9
leal 0x1(%rcx), %r10d
xorl %r11d, %r11d
movq $0x0, (%rdi,%r11,8)
movb $0x0, (%r9,%r11)
movl $0xffffffff, (%r8,%r11,4) # imm = 0xFFFFFFFF
incq %r11
cmpq %r11, %r10
jne 0x405fd2
testl %edx, %edx
jle 0x406137
movq 0x1fc5a2(%rip), %r9     # 0x6025a0
leal 0x1(%rcx), %r10d
leal 0x1(%rdx), %edi
decq %r10
movl $0x1, %ebx
movsd 0x17dff3(%rip), %xmm1   # 0x584008
testl %ecx, %ecx
jle 0x4060b8
movq -0x8(%rsp), %r11
movl -0x4(%r11,%rbx,4), %ebp
movq -0x8(%rax,%rbx,8), %r14
movq (%rax,%rbx,8), %r15
movq -0x8(%r9,%rbx,8), %r12
movq (%r9,%rbx,8), %r13
xorl %r11d, %r11d
movsd (%r14,%r11,8), %xmm3
movapd %xmm3, %xmm2
addsd %xmm1, %xmm2
cmpl (%rsi,%r11,4), %ebp
je 0x406054
movapd %xmm3, %xmm2
movsd 0x8(%r14,%r11,8), %xmm3
cmpb $0x0, 0x1(%r12,%r11)
je 0x406067
addsd %xmm0, %xmm3
movsd (%r15,%r11,8), %xmm4
cmpb $0x0, (%r13,%r11)
je 0x406079
addsd %xmm0, %xmm4
ucomisd %xmm3, %xmm2
jb 0x406094
ucomisd %xmm4, %xmm2
jb 0x406094
movb $0x1, 0x1(%r13,%r11)
movsd %xmm2, 0x8(%r15,%r11,8)
jmp 0x4060b0
movb $0x0, 0x1(%r13,%r11)
ucomisd %xmm3, %xmm4
jae 0x4060a9
movsd %xmm3, 0x8(%r15,%r11,8)
jmp 0x4060b0
movsd %xmm4, 0x8(%r15,%r11,8)
incq %r11
cmpq %r11, %r10
jne 0x40603c
incq %rbx
cmpq %rdi, %rbx
jne 0x406015
testl %ecx, %ecx
jle 0x406137
movq 0x1fc4d1(%rip), %rsi    # 0x6025a0
movl %edx, %r9d
movq (%rsi,%r9,8), %r10
movl %ecx, %r11d
leal -0x1(%rdx), %edi
cmpb $0x1, (%r10,%r11)
jne 0x4060ec
movl %edi, -0x4(%r8,%r11,4)
decl %ecx
jmp 0x406126
movl %edi, %ebx
movq (%rax,%rbx,8), %r14
movsd (%r14,%r11,8), %xmm1
movq (%rsi,%rbx,8), %rbx
cmpb $0x0, (%rbx,%r11)
je 0x406107
addsd %xmm0, %xmm1
movq (%rax,%r9,8), %r11
leal -0x1(%rcx), %r9d
movsd (%r11,%r9,8), %xmm2
cmpb $0x0, (%r10,%r9)
je 0x406120
addsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x40612b
movl %edi, %edx
movl %ecx, %r9d
testl %edx, %edx
jle 0x406137
movl %r9d, %ecx
testl %r9d, %r9d
jg 0x4060cf
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq