// File: /workspace/github_c_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movl %r9d, -0x84(%rbp)
movq %r8, %r13
movq %rcx, -0x68(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %rbx
movq %rsp, -0xd8(%rbp)
movl %edx, %r14d
movq %rsp, %rax
leaq 0xf(,%r14,4), %rcx
andq $-0x10, %rcx
movq %rcx, -0x38(%rbp)
subq %rcx, %rax
movq %rax, -0x78(%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 %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
xorpd 0x17cc6e(%rip), %xmm0   # 0x584100
movq %rcx, %r15
callq 0x4abc30
movq %r15, %rcx
mulsd -0x48(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x110(%rbp,%r15,4)
cmpl %eax, %r12d
jge 0x4074ca
incq %rcx
cmpq $0x5, %rcx
jne 0x407483
movl %r12d, -0xfc(%rbp)
movl $0x6, %ecx
jmp 0x4074d5
movl %r12d, -0x110(%rbp,%rcx,4)
incq %rcx
movq %rsp, %rsi
subq -0x38(%rbp), %rsi
movq %rsi, %rsp
movsd 0x17cb21(%rip), %xmm0   # 0x584008
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd 0x17cb34(%rip), %xmm0   # 0x584030
movsd %xmm0, -0x50(%rbp)
xorl %r15d, %r15d
movq -0x78(%rbp), %r11
movq %r13, -0xa0(%rbp)
movq %r14, -0xa8(%rbp)
movq %rcx, -0xe0(%rbp)
movq %rsi, -0x38(%rbp)
movl -0x110(%rbp,%r15,4), %edx
movl -0x2c(%rbp), %eax
subl %edx, %eax
movl %eax, -0x88(%rbp)
movl $0x0, -0x3c(%rbp)
movq %rdx, -0xf0(%rbp)
movq %r15, -0x98(%rbp)
movq 0x1fb0c8(%rip), %rdi    # 0x602618
movq 0x1fb0c9(%rip), %rsi    # 0x602620
testl %edx, %edx
jle 0x4075d5
movslq -0x3c(%rbp), %r8
leaq (%rbx,%r8,8), %rax
movq -0x60(%rbp), %rcx
leaq (%rcx,%r8,8), %rcx
movl %r8d, %r12d
xorl %r8d, %r8d
movq (%rax,%r8,8), %r9
movsd (%r9), %xmm0
movq (%rdi,%r8,8), %r10
movsd %xmm0, (%r10)
movsd 0x8(%r9), %xmm0
movsd %xmm0, 0x8(%r10)
movsd 0x10(%r9), %xmm0
movsd %xmm0, 0x10(%r10)
movq (%rcx,%r8,8), %r9
movsd (%r9), %xmm0
movq (%rsi,%r8,8), %r10
movsd %xmm0, (%r10)
movsd 0x8(%r9), %xmm0
movsd %xmm0, 0x8(%r10)
movsd 0x10(%r9), %xmm0
movsd %xmm0, 0x10(%r10)
leal (%r12,%r8), %r9d
movl %r9d, (%r11,%r8,4)
incq %r8
cmpq %r8, %rdx
jne 0x407571
subq $0x8, %rsp
movl $0x1, %ecx
leaq -0xb0(%rbp), %r8
leaq -0xd0(%rbp), %r9
leaq -0x160(%rbp), %rax
pushq %rax
callq 0x406142
addq $0x10, %rsp
testq %r15, %r15
jne 0x40761e
movsd -0xb0(%rbp), %xmm0
mulsd -0xe8(%rbp), %xmm0
sqrtsd %xmm0, %xmm0
movq 0x18(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x1fafcb(%rip), %r15    # 0x6025f0
cmpl $0x0, -0x2c(%rbp)
jle 0x40765f
xorl %r14d, %r14d
leaq -0x160(%rbp), %r12
movq (%rbx,%r14,8), %rdx
movq (%r15,%r14,8), %rcx
leaq -0xd0(%rbp), %rdi
movq %r12, %rsi
callq 0x4058ff
incq %r14
cmpq %r14, -0xa8(%rbp)
jne 0x407635
movq 0x1faf91(%rip), %r15    # 0x6025f0
movsd 0x1faf21(%rip), %xmm0   # 0x602588
movsd %xmm0, -0x70(%rbp)
addsd 0x17c9bc(%rip), %xmm0   # 0x584030
movq %r15, -0x48(%rbp)
movq %r15, %rdi
movq -0x60(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x38(%rbp), %rcx
leaq -0x80(%rbp), %r8
movl 0x10(%rbp), %r9d
callq 0x407312
movsd -0x80(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movsd -0x70(%rbp), %xmm1
movq -0x68(%rbp), %rdx
jae 0x4076f9
movl $0x2, %esi
xorl %ecx, %ecx
movsd -0xd0(%rbp,%rcx,8), %xmm1
movsd %xmm1, (%rdx,%rcx,8)
movups -0x170(%rbp,%rsi,8), %xmm1
movups %xmm1, -0x10(%r13,%rsi,8)
movsd -0x160(%rbp,%rsi,8), %xmm1
movsd %xmm1, (%r13,%rsi,8)
incq %rcx
addq $0x3, %rsi
cmpq $0x3, %rcx
jne 0x4076b3
movsd 0x1fae94(%rip), %xmm1   # 0x602588
movsd %xmm0, -0x50(%rbp)
addsd 0x17c907(%rip), %xmm1   # 0x584008
movsd %xmm1, -0x70(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x38(%rbp), %rsi
leaq -0xd0(%rbp), %r15
movq 0x1faef9(%rip), %r13    # 0x602618
movq 0x1faefa(%rip), %r12    # 0x602620
movl $0x0, %ecx
testl %eax, %eax
jle 0x4077b6
movl %eax, %r15d
movl %eax, %r14d
leaq (,%r14,4), %rdx
movq -0x78(%rbp), %rdi
callq 0x401118
movq -0x38(%rbp), %r8
xorl %eax, %eax
movq -0x60(%rbp), %rdi
movslq (%r8,%rax,4), %rcx
movq (%rbx,%rcx,8), %rdx
movsd (%rdx), %xmm0
movq (%r13,%rax,8), %rsi
movsd %xmm0, (%rsi)
movsd 0x8(%rdx), %xmm0
movsd %xmm0, 0x8(%rsi)
movsd 0x10(%rdx), %xmm0
movsd %xmm0, 0x10(%rsi)
movq (%rdi,%rcx,8), %rcx
movsd (%rcx), %xmm0
movq (%r12,%rax,8), %rdx
movsd %xmm0, (%rdx)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rdx)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%rdx)
incq %rax
cmpq %rax, %r14
jne 0x407754
movl %r15d, %eax
movl %r15d, %ecx
leaq -0xd0(%rbp), %r15
movl %ecx, -0x8c(%rbp)
subq $0x8, %rsp
movq %r13, %rdi
movq %r12, %rsi
movl %eax, %edx
movl $0x1, %ecx
leaq -0xb0(%rbp), %r8
movq %r15, %r9
leaq -0x160(%rbp), %rax
pushq %rax
callq 0x406142
addq $0x10, %rsp
cmpl $0x0, -0x2c(%rbp)
jle 0x40783c
xorl %r14d, %r14d
movq %rbx, %r13
movq %r15, %rbx
leaq -0x160(%rbp), %r15
movq -0x48(%rbp), %r12
movq (%r13,%r14,8), %rdx
movq (%r12,%r14,8), %rcx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4058ff
incq %r14
cmpq %r14, -0xa8(%rbp)
jne 0x407802
movq 0x1fadc7(%rip), %rdi    # 0x6025f0
movq %rbx, %r15
movq %r13, %rbx
movq -0xa0(%rbp), %r13
movq -0x68(%rbp), %r12
jmp 0x40784b
movq -0xa0(%rbp), %r13
movq -0x68(%rbp), %r12
movq -0x48(%rbp), %rdi
movq %rdi, -0x48(%rbp)
movq -0x60(%rbp), %rsi
movl -0x2c(%rbp), %edx
movsd -0x70(%rbp), %xmm0
movq -0x38(%rbp), %rcx
leaq -0x80(%rbp), %r8
movl 0x10(%rbp), %r9d
callq 0x407312
movsd -0x80(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movq -0x78(%rbp), %r11
movq -0xf0(%rbp), %rdx
movl -0x54(%rbp), %r8d
movl -0x8c(%rbp), %r10d
jae 0x4078d8
movl $0x2, %esi
xorl %ecx, %ecx
movsd -0xd0(%rbp,%rcx,8), %xmm1
movsd %xmm1, (%r12,%rcx,8)
movups -0x170(%rbp,%rsi,8), %xmm1
movups %xmm1, -0x10(%r13,%rsi,8)
movsd -0x160(%rbp,%rsi,8), %xmm1
movsd %xmm1, (%r13,%rsi,8)
incq %rcx
addq $0x3, %rsi
cmpq $0x3, %rcx
jne 0x407899
movsd %xmm0, -0x50(%rbp)
cmpl %r10d, %eax
jne 0x407900
testl %r10d, %r10d
movq -0x38(%rbp), %rsi
jle 0x40790c
movl %r10d, %ecx
xorl %r9d, %r9d
movl (%rsi,%r9,4), %edi
cmpl (%r11,%r9,4), %edi
jne 0x40790f
incq %r9
cmpq %r9, %rcx
jne 0x4078ec
jmp 0x407926
cmpl $0x12, %r8d
movq -0x38(%rbp), %rsi
jbe 0x40791a
jmp 0x407926
xorl %r9d, %r9d
cmpl %r10d, %r9d
je 0x407926
cmpl $0x13, %r8d
jae 0x407926
incl %r8d
movl %r8d, -0x54(%rbp)
jmp 0x407718
movl -0x88(%rbp), %eax
movl -0x3c(%rbp), %ecx
cmpl %eax, %ecx
jge 0x40794d
addl -0x84(%rbp), %ecx
cmpl %eax, %ecx
cmovgel %eax, %ecx
movl %ecx, -0x3c(%rbp)
movq -0x98(%rbp), %r15
jmp 0x407549
movq -0x98(%rbp), %r15
incq %r15
movq -0xe0(%rbp), %rcx
cmpq %rcx, %r15
jne 0x407521
movq -0xd8(%rbp), %rsp
movsd -0x50(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq