// File: /workspace/github_c_makefile_star2_O1/asford[P]tmalign/./TMalign.h
// Function: DP_iter(double**, double**, int, int, double*, double (*) [3], int*, int, int, int)
double DP_iter( double **x,
                double **y, 
                int x_len, 
                int y_len, 
                double t[3],
                double u[3][3],
				int invmap0[],
				int g1,
				int g2,
				int iteration_max                                   
				)
{
    double gap_open[2]={-0.6, 0};
    double rmsd; 
    int *invmap=new int[y_len+1];
    
    int iteration, i, j, k;
    double tmscore, tmscore_max, tmscore_old=0;    
    int score_sum_method=8, simplify_step=40;
    tmscore_max=-1;

	//double d01=d0+1.5;
    double d02=d0*d0;
    for(int g=g1; g<g2; g++)
    {
        for(iteration=0; iteration<iteration_max; iteration++)
        {           
			NWDP_TM(x, y, x_len, y_len, t, u, d02, gap_open[g], invmap);
            
            k=0;
            for(j=0; j<y_len; j++) 
            {
                i=invmap[j];

                if(i>=0) //aligned
                {
                    xtm[k][0]=x[i][0];
                    xtm[k][1]=x[i][1];
                    xtm[k][2]=x[i][2];
                    
                    ytm[k][0]=y[j][0];
                    ytm[k][1]=y[j][1];
                    ytm[k][2]=y[j][2];
                    k++;
                }
            }

            tmscore=TMscore8_search(xtm, ytm, k, t, u, simplify_step, score_sum_method, &rmsd);

           
            if(tmscore>tmscore_max)
            {
                tmscore_max=tmscore;
                for(i=0; i<y_len; i++) 
                {                
                    invmap0[i]=invmap[i];                                      
                }				
            }
    
            if(iteration>0)
            {
                if(fabs(tmscore_old-tmscore)<0.000001)
                {     
                    break;       
                }
            }
            tmscore_old=tmscore;
        }// for iteration           
        
    }//for gapopen
    
    
    delete []invmap;
    return tmscore_max;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movl %ecx, %ebp
movl %edx, 0x1c(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movl 0x98(%rsp), %r14d
movslq %ecx, %rax
leaq 0x4(,%rax,4), %rcx
cmpl $-0x1, %eax
movq $-0x1, %rdi
cmovgeq %rcx, %rdi
callq 0x40d060
movq %rax, %r15
cmpl 0xa0(%rsp), %r14d
jge 0x4098c8
movsd 0x1f8e9c(%rip), %xmm0   # 0x602580
mulsd %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movl %ebp, %ebx
leaq (,%rbx,4), %rax
movq %rax, 0x38(%rsp)
movslq %r14d, %rax
movsd 0x17a928(%rip), %xmm1   # 0x584030
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl %ebp, 0x18(%rsp)
cmpl $0x0, 0xa8(%rsp)
movq %rax, 0x30(%rsp)
jle 0x4098a5
leaq 0x17aa80(%rip), %rcx    # 0x5841b0
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %r14d, %r14d
movsd %xmm1, 0x10(%rsp)
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rsi
movl 0x24(%rsp), %edx
movl %ebp, %ecx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
pushq %r15
callq 0x405cfb
addq $0x10, %rsp
movl $0x0, %edx
testl %ebp, %ebp
jle 0x4097f5
movq 0x1f8e5b(%rip), %rax    # 0x6025e0
xorl %ecx, %ecx
movq 0x1f8e5a(%rip), %rsi    # 0x6025e8
xorl %edx, %edx
movslq (%r15,%rcx,4), %rdi
testq %rdi, %rdi
js 0x4097ed
movq (%r13,%rdi,8), %rdi
movsd (%rdi), %xmm0
movslq %edx, %rdx
movq (%rax,%rdx,8), %r8
movsd %xmm0, (%r8)
movsd 0x8(%rdi), %xmm0
movsd %xmm0, 0x8(%r8)
movsd 0x10(%rdi), %xmm0
movsd %xmm0, 0x10(%r8)
movq (%r12,%rcx,8), %rdi
movsd (%rdi), %xmm0
movq (%rsi,%rdx,8), %r8
movsd %xmm0, (%r8)
movsd 0x8(%rdi), %xmm0
movsd %xmm0, 0x8(%r8)
movsd 0x10(%rdi), %xmm0
movsd %xmm0, 0x10(%r8)
incl %edx
incq %rcx
cmpq %rcx, %rbx
jne 0x409790
testl %ebp, %ebp
setle %bpl
movq 0x1f8dde(%rip), %rdi    # 0x6025e0
movq 0x1f8ddf(%rip), %rsi    # 0x6025e8
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movl $0x28, %r9d
leaq 0x50(%rsp), %rax
pushq %rax
pushq $0x8
callq 0x4074d4
addq $0x10, %rsp
movsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
setbe %al
orb %bpl, %al
je 0x409842
maxsd %xmm0, %xmm1
jmp 0x409867
movq 0x90(%rsp), %rdi
movq %r15, %rsi
movq 0x38(%rsp), %rdx
movsd %xmm0, 0x10(%rsp)
callq 0x401118
movsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
testl %r14d, %r14d
movl 0x18(%rsp), %ebp
je 0x40988c
movsd 0x8(%rsp), %xmm2
subsd %xmm0, %xmm2
andpd 0x17a8ee(%rip), %xmm2   # 0x584170
ucomisd 0x17a80e(%rip), %xmm2   # 0x584098
jb 0x4098a5
incl %r14d
movsd %xmm0, 0x8(%rsp)
cmpl 0xa8(%rsp), %r14d
jne 0x40973e
jmp 0x4098ab
movsd 0x8(%rsp), %xmm0
movq 0x30(%rsp), %rax
incq %rax
movsd %xmm0, 0x8(%rsp)
cmpl 0xa0(%rsp), %eax
jne 0x409716
jmp 0x4098d0
movsd 0x17a760(%rip), %xmm1   # 0x584030
movsd %xmm1, 0x10(%rsp)
movq %r15, %rdi
callq 0x40c360
movsd 0x10(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq