// File: /workspace/github_c_makefile_star2_O1/asford[P]tmalign/./TMalign.h
// Function: TMscore8_search(double**, double**, int, double*, double (*) [3], int, int, double*)
double TMscore8_search( double **xtm, 
                        double **ytm,
                        int Lali, 
                        double t0[3],
                        double u0[3][3],
                        int simplify_step,
                        int score_sum_method,
                        double *Rcomm
                       )
{   
    int i, m;
    double score_max, score, rmsd;    
    const int kmax=Lali;    
    int k_ali[kmax], ka, k;
    double t[3];
    double u[3][3];
	double d;
	

	//iterative parameters
	int n_it=20;            //maximum number of iterations
    const int n_init_max=6; //maximum number of different fragment length 
    int L_ini[n_init_max];  //fragment lengths, Lali, Lali/2, Lali/4 ... 4   
    int L_ini_min=4;
    if(Lali<4) L_ini_min=Lali;   
    int n_init=0, i_init;      
    for(i=0; i<n_init_max-1; i++)
    {
        n_init++;
        L_ini[i]=(int) (Lali/pow(2.0, (double) i));
        if(L_ini[i]<=L_ini_min)
        {
            L_ini[i]=L_ini_min;
            break;
        }
    }
    if(i==n_init_max-1)
    {
        n_init++;
        L_ini[i]=L_ini_min;
    }
    
    score_max=-1;
    //find the maximum score starting from local structures superposition
    int i_ali[kmax], n_cut;
    int L_frag; //fragment length
    int iL_max; //maximum starting postion for the fragment
    for(i_init=0; i_init<n_init; i_init++)
    {
        L_frag=L_ini[i_init];
        iL_max=Lali-L_frag;
      
        i=0;   
        while(1)
        {
            //extract the fragment starting from position i 
            ka=0;
            for(k=0; k<L_frag; k++)
            {
				int kk=k+i;
                r1[k][0]=xtm[kk][0];  
                r1[k][1]=xtm[kk][1]; 
                r1[k][2]=xtm[kk][2];   
                
                r2[k][0]=ytm[kk][0];  
                r2[k][1]=ytm[kk][1]; 
                r2[k][2]=ytm[kk][2];
                
                k_ali[ka]=kk;
                ka++;
            }
            
            //extract rotation matrix based on the fragment
            Kabsch(r1, r2, L_frag, 1, &rmsd, t, u);
            if(i_init==0)
            {
                *Rcomm=sqrt(rmsd/Lali);
            }
            do_rotation(xtm, xt, Lali, t, u);
            
            //get subsegment of this fragment
            d=d0_search-1;
            n_cut=score_fun8(xt, ytm, Lali, d, i_ali, &score, score_sum_method);
            if(score>score_max)
            {
                score_max=score;
                
                //save the rotation matrix
                for(k=0; k<3; k++)
                {
                    t0[k]=t[k];
                    u0[k][0]=u[k][0];
                    u0[k][1]=u[k][1];
                    u0[k][2]=u[k][2];
                }
            }
            
            //try to extend the alignment iteratively            
            d=d0_search+1;
            for(int it=0; it<n_it; it++)            
            {
                ka=0;
                for(k=0; k<n_cut; k++)
                {
                    m=i_ali[k];
                    r1[k][0]=xtm[m][0];  
                    r1[k][1]=xtm[m][1]; 
                    r1[k][2]=xtm[m][2];
                    
                    r2[k][0]=ytm[m][0];  
                    r2[k][1]=ytm[m][1]; 
                    r2[k][2]=ytm[m][2];
                    
                    k_ali[ka]=m;
                    ka++;
                } 
                //extract rotation matrix based on the fragment                
                Kabsch(r1, r2, n_cut, 1, &rmsd, t, u);
                do_rotation(xtm, xt, Lali, t, u);
                n_cut=score_fun8(xt, ytm, Lali, d, i_ali, &score, score_sum_method);
                if(score>score_max)
                {
                    score_max=score;

                    //save the rotation matrix
                    for(k=0; k<3; k++)
                    {
                        t0[k]=t[k];
                        u0[k][0]=u[k][0];
                        u0[k][1]=u[k][1];
                        u0[k][2]=u[k][2];
                    }                     
                }
                
                //check if it converges                 
			
                if(n_cut==ka)
                {				
                    for(k=0; k<n_cut; k++)
                    {
                        if(i_ali[k]!=k_ali[k])
						{
							break;
						}
                    }
                    if(k==n_cut)
                    {						
                        break; //stop iteration
                    }
                }                                                               
            } //for iteration            

			if(i<iL_max)
			{
				i=i+simplify_step; //shift the fragment		
				if(i>iL_max) i=iL_max;  //do this to use the last missed fragment
			}
			else if(i>=iL_max)
			{
				break;
			}
        }//while(1)
        //end of one fragment
    }//for(i_init
    return score_max;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %r9d, -0x74(%rbp)
movq %r8, %r13
movq %rcx, -0x88(%rbp)
movq %rsi, -0x68(%rbp)
movq %rdi, -0x60(%rbp)
movq %rsp, -0xc8(%rbp)
movl %edx, %r14d
movq %rsp, %rax
leaq 0xf(,%r14,4), %rbx
andq $-0x10, %rbx
subq %rbx, %rax
movq %rax, -0x50(%rbp)
movq %rax, %rsp
cmpl $0x4, %edx
movl $0x4, %r12d
cmovll %edx, %r12d
movl %edx, -0x2c(%rbp)
cvtsi2sd %edx, %xmm0
movsd %xmm0, -0x48(%rbp)
xorl %r15d, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
xorpd 0x17cbf3(%rip), %xmm0   # 0x584140
callq 0x4ab990
mulsd -0x48(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x100(%rbp,%r15,4)
cmpl %eax, %r12d
jge 0x407580
incq %r15
cmpq $0x5, %r15
jne 0x40753d
movb $0x1, %cl
movl $0x5, %r15d
movl $0x6, %eax
jmp 0x407596
movl %r12d, -0x100(%rbp,%r15,4)
cmpq $0x5, %r15
sete %cl
leaq 0x2(%r15), %rax
incq %r15
testb %cl, %cl
je 0x4075a4
movl %r12d, -0xec(%rbp)
movq %rax, %r15
movq %rsp, %rsi
subq %rbx, %rsi
movq %rsi, %rsp
movl %r15d, %eax
movq %rax, -0xd0(%rbp)
movsd 0x17ca49(%rip), %xmm0   # 0x584008
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd 0x17ca5c(%rip), %xmm0   # 0x584030
movsd %xmm0, -0x58(%rbp)
xorl %r15d, %r15d
movq -0x88(%rbp), %r12
movq -0x50(%rbp), %r11
movq %r13, -0x90(%rbp)
movq %r14, -0x98(%rbp)
movq %rsi, -0x40(%rbp)
movl -0x100(%rbp,%r15,4), %ebx
movl -0x2c(%rbp), %eax
subl %ebx, %eax
movl %eax, -0x78(%rbp)
movl $0x0, -0x30(%rbp)
movq %r15, -0x80(%rbp)
movq %rbx, -0xe0(%rbp)
testl %ebx, %ebx
jle 0x4076ae
movq 0x1fafee(%rip), %rax    # 0x602618
movq 0x1fafef(%rip), %rcx    # 0x602620
movslq -0x30(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq (%rdx,%rdi,8), %rdx
movq -0x68(%rbp), %rsi
leaq (%rsi,%rdi,8), %rsi
movl %edi, %edi
xorl %r8d, %r8d
movq (%rdx,%r8,8), %r9
movsd (%r9), %xmm0
movq (%rax,%r8,8), %r10
movsd %xmm0, (%r10)
movsd 0x8(%r9), %xmm0
movsd %xmm0, 0x8(%r10)
movsd 0x10(%r9), %xmm0
movsd %xmm0, 0x10(%r10)
movq (%rsi,%r8,8), %r9
movsd (%r9), %xmm0
movq (%rcx,%r8,8), %r10
movsd %xmm0, (%r10)
movsd 0x8(%r9), %xmm0
movsd %xmm0, 0x8(%r10)
movsd 0x10(%r9), %xmm0
movsd %xmm0, 0x10(%r10)
leal (%rdi,%r8), %r9d
movl %r9d, (%r11,%r8,4)
incq %r8
cmpq %r8, %rbx
jne 0x40764a
movq 0x1faf63(%rip), %rdi    # 0x602618
movq 0x1faf64(%rip), %rsi    # 0x602620
subq $0x8, %rsp
movl %ebx, %edx
movl $0x1, %ecx
leaq -0xa0(%rbp), %r8
leaq -0xc0(%rbp), %r9
leaq -0x150(%rbp), %rax
pushq %rax
callq 0x406168
addq $0x10, %rsp
testq %r15, %r15
jne 0x407707
movsd -0xa0(%rbp), %xmm0
mulsd -0xd8(%rbp), %xmm0
sqrtsd %xmm0, %xmm0
movq 0x18(%rbp), %rax
movsd %xmm0, (%rax)
cmpl $0x0, -0x2c(%rbp)
movq -0x98(%rbp), %r13
movq -0x60(%rbp), %rbx
jle 0x407745
movq 0x1faed1(%rip), %r14    # 0x6025f0
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdx
movq (%r14,%r15,8), %rcx
leaq -0xc0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x4058ff
incq %r15
cmpq %r15, %r13
jne 0x407722
movsd 0x1fae3b(%rip), %xmm0   # 0x602588
addsd 0x17c8db(%rip), %xmm0   # 0x584030
movq 0x1fae94(%rip), %rdi    # 0x6025f0
movq -0x68(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x40(%rbp), %rcx
leaq -0x70(%rbp), %r8
movl 0x10(%rbp), %r9d
callq 0x4073d2
movsd -0x70(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movq -0x90(%rbp), %r13
leaq -0x150(%rbp), %rbx
leaq -0xc0(%rbp), %r14
jae 0x4077df
movl $0x2, %edx
xorl %ecx, %ecx
movsd -0xc0(%rbp,%rcx,8), %xmm1
movsd %xmm1, (%r12,%rcx,8)
movups -0x160(%rbp,%rdx,8), %xmm1
movups %xmm1, -0x10(%r13,%rdx,8)
movsd -0x150(%rbp,%rdx,8), %xmm1
movsd %xmm1, (%r13,%rdx,8)
incq %rcx
addq $0x3, %rdx
cmpq $0x3, %rcx
jne 0x4077a0
movsd %xmm0, -0x58(%rbp)
movsd 0x1fada1(%rip), %xmm0   # 0x602588
addsd 0x17c819(%rip), %xmm0   # 0x584008
movsd %xmm0, -0xe8(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x40(%rbp), %rsi
movl $0x0, %r15d
testl %eax, %eax
jle 0x4078aa
movq 0x1fae01(%rip), %r13    # 0x602618
movq 0x1fae02(%rip), %r12    # 0x602620
movl %eax, -0x48(%rbp)
movl %eax, %r14d
leaq (,%r14,4), %rdx
movq -0x50(%rbp), %rdi
callq 0x401118
movq -0x40(%rbp), %rdi
xorl %r15d, %r15d
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %r8
movslq (%rdi,%r15,4), %rax
movq (%r8,%rax,8), %rcx
movsd (%rcx), %xmm0
movq (%r13,%r15,8), %rdx
movsd %xmm0, (%rdx)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rdx)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%rdx)
movq (%rsi,%rax,8), %rax
movsd (%rax), %xmm0
movq (%r12,%r15,8), %rcx
movsd %xmm0, (%rcx)
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x8(%rcx)
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x10(%rcx)
incq %r15
cmpq %r15, %r14
jne 0x407844
leaq -0x150(%rbp), %rbx
leaq -0xc0(%rbp), %r14
movl -0x48(%rbp), %eax
movq 0x1fad67(%rip), %rdi    # 0x602618
movq 0x1fad68(%rip), %rsi    # 0x602620
subq $0x8, %rsp
movl %eax, %edx
movl $0x1, %ecx
leaq -0xa0(%rbp), %r8
movq %r14, %r9
pushq %rbx
callq 0x406168
addq $0x10, %rsp
cmpl $0x0, -0x2c(%rbp)
movq %rbx, %r13
movq -0x60(%rbp), %rbx
jle 0x407915
movq 0x1fad05(%rip), %rcx    # 0x6025f0
movq %rcx, -0x48(%rbp)
xorl %r12d, %r12d
movq (%rbx,%r12,8), %rdx
movq -0x48(%rbp), %rcx
movq (%rcx,%r12,8), %rcx
movq %r14, %rdi
movq %r13, %rsi
callq 0x4058ff
incq %r12
cmpq %r12, -0x98(%rbp)
jne 0x4078f2
movq 0x1facd4(%rip), %rdi    # 0x6025f0
movq -0x68(%rbp), %rsi
movl -0x2c(%rbp), %edx
movsd -0xe8(%rbp), %xmm0
movq -0x40(%rbp), %rcx
leaq -0x70(%rbp), %r8
movl 0x10(%rbp), %r9d
callq 0x4073d2
movsd -0x70(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movq -0x88(%rbp), %r12
movq -0x90(%rbp), %r13
jae 0x4079a0
movl $0x2, %edx
xorl %ecx, %ecx
movsd -0xc0(%rbp,%rcx,8), %xmm1
movsd %xmm1, (%r12,%rcx,8)
movups -0x160(%rbp,%rdx,8), %xmm1
movups %xmm1, -0x10(%r13,%rdx,8)
movsd -0x150(%rbp,%rdx,8), %xmm1
movsd %xmm1, (%r13,%rdx,8)
incq %rcx
addq $0x3, %rdx
cmpq $0x3, %rcx
jne 0x407961
movsd %xmm0, -0x58(%rbp)
cmpl %r15d, %eax
jne 0x4079cd
testl %eax, %eax
movq -0x40(%rbp), %rsi
jle 0x4079db
movl %eax, %ecx
xorl %r8d, %r8d
movq -0x50(%rbp), %rdi
movl (%rsi,%r8,4), %edx
cmpl (%rdi,%r8,4), %edx
jne 0x4079de
incq %r8
cmpq %r8, %rcx
jne 0x4079b6
movl %ecx, %r8d
jmp 0x4079de
movl -0x34(%rbp), %ecx
cmpl $0x12, %ecx
movq -0x40(%rbp), %rsi
jbe 0x4079eb
jmp 0x407a03
xorl %r8d, %r8d
cmpl %eax, %r8d
movl -0x34(%rbp), %ecx
je 0x407a03
cmpl $0x13, %ecx
jae 0x407a03
incl %ecx
movl %ecx, -0x34(%rbp)
leaq -0x150(%rbp), %rbx
leaq -0xc0(%rbp), %r14
jmp 0x407802
movl -0x78(%rbp), %eax
movl -0x30(%rbp), %ecx
cmpl %eax, %ecx
jge 0x407a2c
addl -0x74(%rbp), %ecx
cmpl %eax, %ecx
cmovgel %eax, %ecx
movl %ecx, -0x30(%rbp)
movq -0x50(%rbp), %r11
movq -0x80(%rbp), %r15
movq -0xe0(%rbp), %rbx
jmp 0x40761b
movq -0x80(%rbp), %r15
incq %r15
cmpq -0xd0(%rbp), %r15
movq -0x50(%rbp), %r11
jne 0x4075f9
movq -0xc8(%rbp), %rsp
movsd -0x58(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq