// File: /workspace/github_c_makefile_star2_O3/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, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movl %ecx, %r15d
movl %edx, 0x14(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movl 0x98(%rsp), %r14d
leal 0x1(%r15), %eax
shlq $0x2, %rax
cmpl $-0x1, %ecx
movq $-0x1, %rdi
cmovgeq %rax, %rdi
callq 0x40d300
movq %rax, %rbp
cmpl 0xa0(%rsp), %r14d
jge 0x40995e
movsd 0x1f8df9(%rip), %xmm0   # 0x602580
mulsd %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movl %r15d, %ebx
leaq (,%rbx,4), %rax
movq %rax, 0x30(%rsp)
movslq %r14d, %rax
movsd 0x17a884(%rip), %xmm1   # 0x584030
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq %r15, 0x40(%rsp)
cmpl $0x0, 0xa8(%rsp)
movq %rax, 0x28(%rsp)
jle 0x40993d
leaq 0x17a9cc(%rip), %rcx    # 0x5841a0
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %r14d, %r14d
movsd %xmm1, 0x8(%rsp)
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rsi
movl 0x1c(%rsp), %edx
movl %r15d, %ecx
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
movsd 0x40(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
pushq %rbp
callq 0x405cf7
addq $0x10, %rsp
movq 0x1f8dc0(%rip), %rdi    # 0x6025e0
movq 0x1f8dc1(%rip), %rsi    # 0x6025e8
movl $0x0, %edx
testl %r15d, %r15d
jle 0x40989b
xorl %eax, %eax
xorl %edx, %edx
movslq (%rbp,%rax,4), %rcx
testq %rcx, %rcx
js 0x409893
movq (%r13,%rcx,8), %rcx
movsd (%rcx), %xmm0
movslq %edx, %rdx
movq (%rdi,%rdx,8), %r8
movsd %xmm0, (%r8)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%r8)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%r8)
movq (%r12,%rax,8), %rcx
movsd (%rcx), %xmm0
movq (%rsi,%rdx,8), %r8
movsd %xmm0, (%r8)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%r8)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%r8)
incl %edx
incq %rax
cmpq %rax, %rbx
jne 0x409835
testl %r15d, %r15d
setle %r15b
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %r8
movl $0x28, %r9d
leaq 0x50(%rsp), %rax
pushq %rax
pushq $0x8
callq 0x407418
addq $0x10, %rsp
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
setbe %al
orb %r15b, %al
je 0x4098db
maxsd %xmm0, %xmm1
jmp 0x409900
movq 0x90(%rsp), %rdi
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movsd %xmm0, 0x8(%rsp)
callq 0x401118
movsd 0x8(%rsp), %xmm0
movapd %xmm0, %xmm1
testl %r14d, %r14d
movq 0x40(%rsp), %r15
je 0x409925
movsd (%rsp), %xmm2
subsd %xmm0, %xmm2
andpd 0x17a815(%rip), %xmm2   # 0x584130
ucomisd 0x17a775(%rip), %xmm2   # 0x584098
jb 0x40993d
incl %r14d
movsd %xmm0, (%rsp)
cmpl 0xa8(%rsp), %r14d
jne 0x4097e2
jmp 0x409942
movsd (%rsp), %xmm0
movq 0x28(%rsp), %rax
incq %rax
movsd %xmm0, (%rsp)
cmpl 0xa0(%rsp), %eax
jne 0x4097ba
jmp 0x409966
movsd 0x17a6ca(%rip), %xmm1   # 0x584030
movsd %xmm1, 0x8(%rsp)
movq %rbp, %rdi
callq 0x40c600
movsd 0x8(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq