// File: /workspace/github_c_makefile_star2_O1/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 0x1fc61a(%rip), %rax    # 0x6025a8
movq (%rax), %rdi
movq $0x0, (%rdi)
testl %edx, %edx
js 0x405fc4
movq 0x1fc5fd(%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 0x405faa
testl %ecx, %ecx
js 0x405ff6
movq 0x1fc5d1(%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 0x405fd9
movq %r8, -0x10(%rsp)
testl %edx, %edx
jle 0x4060de
movq 0x1fc596(%rip), %r9     # 0x6025a0
leal 0x1(%rcx), %r10d
leal 0x1(%rdx), %r8d
decq %r10
movl $0x1, %ebx
movsd 0x17dfe6(%rip), %xmm1   # 0x584008
testl %ecx, %ecx
jle 0x4060d2
movq -0x8(%rsp), %rdi
movl -0x4(%rdi,%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
cmpl (%rsi,%r11,4), %ebp
jne 0x40605f
movq -0x8(%rax,%rbx,8), %rdi
movsd (%rdi,%r11,8), %xmm2
addsd %xmm1, %xmm2
jmp 0x40606a
movq -0x8(%rax,%rbx,8), %rdi
movsd (%rdi,%r11,8), %xmm2
movsd 0x8(%r14,%r11,8), %xmm3
cmpb $0x0, 0x1(%r12,%r11)
je 0x40607d
addsd %xmm0, %xmm3
movsd (%r15,%r11,8), %xmm4
cmpb $0x0, (%r13,%r11)
je 0x40608f
addsd %xmm0, %xmm4
ucomisd %xmm3, %xmm2
jb 0x4060aa
ucomisd %xmm4, %xmm2
jb 0x4060aa
movb $0x1, 0x1(%r13,%r11)
movsd %xmm2, 0x8(%r15,%r11,8)
jmp 0x4060c6
movb $0x0, 0x1(%r13,%r11)
ucomisd %xmm3, %xmm4
jae 0x4060bf
movsd %xmm3, 0x8(%r15,%r11,8)
jmp 0x4060c6
movsd %xmm4, 0x8(%r15,%r11,8)
incq %r11
cmpq %r11, %r10
jne 0x406048
incq %rbx
cmpq %r8, %rbx
jne 0x406022
testl %edx, %edx
setle %sil
testl %ecx, %ecx
setle %dil
orb %sil, %dil
movq -0x10(%rsp), %r11
jne 0x40615d
movq 0x1fc4a5(%rip), %rsi    # 0x6025a0
movl %edx, %edi
movq (%rsi,%rdi,8), %r9
movl %ecx, %ecx
decl %edx
cmpb $0x1, (%r9,%rcx)
jne 0x406115
movl %edx, -0x4(%r11,%rcx,4)
decl %ecx
jmp 0x406155
movl %edx, %r8d
movq (%rax,%r8,8), %r10
movsd (%r10,%rcx,8), %xmm1
movq (%rsi,%r8,8), %r8
cmpb $0x0, (%r8,%rcx)
je 0x406131
addsd %xmm0, %xmm1
movq (%rax,%rdi,8), %r8
leal -0x1(%rcx), %r10d
movsd (%r8,%r10,8), %xmm2
cmpb $0x0, (%r9,%r10)
je 0x40614a
addsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x406155
movl %edi, %edx
movl %r10d, %ecx
testl %edx, %edx
jle 0x40615d
testl %ecx, %ecx
jg 0x4060fb
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq