// File: /workspace/github_c_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %r9, -0xc0(%rbp)
movq %r8, %r14
movq %rcx, -0x80(%rbp)
movq %rsi, %rbx
movq %rdi, %r10
movq %rsp, -0xa8(%rbp)
movl %edx, %eax
movq %rsp, %r15
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0x58(%rbp)
subq %rax, %r15
movq %r15, %rsp
cmpl $0x4, %edx
pushq $0x4
popq %rax
cmovll %edx, %eax
movl %eax, -0x50(%rbp)
movl %edx, -0x2c(%rbp)
cvtsi2sd %edx, %xmm0
movsd %xmm0, -0x38(%rbp)
xorl %r13d, %r13d
movq %rdi, -0x48(%rbp)
cmpq $0x5, %r13
je 0x406f89
leaq 0x1(%r13), %r12
cvtsi2sd %r13d, %xmm0
xorpd 0x17c1b9(%rip), %xmm0   # 0x583110
callq 0x4aa8a0
movq -0x48(%rbp), %r10
mulsd -0x38(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xf0(%rbp,%r13,4)
movq %r12, %r13
cmpl %eax, -0x50(%rbp)
jl 0x406f40
movl -0x50(%rbp), %eax
movl %eax, -0xf4(%rbp,%r12,4)
movq %r12, %rax
jmp 0x406f95
movl -0x50(%rbp), %eax
movl %eax, -0xdc(%rbp)
pushq $0x6
popq %rax
movq %rsp, %r12
subq -0x58(%rbp), %r12
movq %r12, %rsp
movsd 0x17c061(%rip), %xmm0   # 0x583008
divsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd 0x17c074(%rip), %xmm0   # 0x583030
xorl %r13d, %r13d
leaq -0xa0(%rbp), %r9
movsd %xmm0, -0x40(%rbp)
movq %rbx, -0xd0(%rbp)
movq %rax, -0xb0(%rbp)
cmpq %rax, %r13
je 0x40738d
movl -0xf0(%rbp,%r13,4), %eax
movl -0x2c(%rbp), %ecx
subl %eax, %ecx
movl %ecx, -0x6c(%rbp)
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %ecx
movl %eax, -0x70(%rbp)
cmovgl %eax, %ecx
movq %rcx, -0x50(%rbp)
movq %r13, -0xc8(%rbp)
movl %edx, %eax
movq %rax, -0x38(%rbp)
movq 0x1fb5ff(%rip), %rax    # 0x602618
movq 0x1fb600(%rip), %rcx    # 0x602620
movslq %edx, %rsi
leaq (%r10,%rsi,8), %rdx
leaq (%rbx,%rsi,8), %rsi
xorl %edi, %edi
leaq -0x140(%rbp), %r10
cmpq %rdi, -0x50(%rbp)
je 0x40709e
movq (%rdx,%rdi,8), %r8
movsd (%r8), %xmm0
movq (%rax,%rdi,8), %r11
movsd %xmm0, (%r11)
movsd 0x8(%r8), %xmm0
movsd %xmm0, 0x8(%r11)
movsd 0x10(%r8), %xmm0
movsd %xmm0, 0x10(%r11)
movq (%rsi,%rdi,8), %r8
movsd (%r8), %xmm0
movq (%rcx,%rdi,8), %r11
movsd %xmm0, (%r11)
movsd 0x8(%r8), %xmm0
movsd %xmm0, 0x8(%r11)
movsd 0x10(%r8), %xmm0
movsd %xmm0, 0x10(%r11)
movq -0x38(%rbp), %r8
addl %edi, %r8d
movl %r8d, (%r15,%rdi,4)
incq %rdi
jmp 0x407034
movq 0x1fb573(%rip), %rdi    # 0x602618
movq 0x1fb574(%rip), %rsi    # 0x602620
subq $0x8, %rsp
movl -0x70(%rbp), %edx
pushq $0x1
popq %rcx
leaq -0x88(%rbp), %r8
pushq %r10
callq 0x405c40
popq %rcx
popq %rdx
testq %r13, %r13
jne 0x4070e7
movsd -0x88(%rbp), %xmm0
mulsd -0xb8(%rbp), %xmm0
sqrtsd %xmm0, %xmm0
movq 0x18(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x1fb502(%rip), %rsi    # 0x6025f0
movq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %r13d
movl %r13d, %edx
leaq -0xa0(%rbp), %rcx
leaq -0x140(%rbp), %r8
callq 0x405577
movsd 0x1fb474(%rip), %xmm0   # 0x602588
movsd %xmm0, -0x60(%rbp)
addsd 0x17bf0f(%rip), %xmm0   # 0x583030
movq 0x1fb4c8(%rip), %rdi    # 0x6025f0
movq %rdi, -0x58(%rbp)
movq %rbx, %rsi
movl %r13d, %edx
movq %r12, %rcx
leaq -0x68(%rbp), %r8
movl 0x10(%rbp), %r9d
callq 0x406d99
movsd -0x68(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movsd -0x60(%rbp), %xmm1
movq -0x80(%rbp), %rsi
jae 0x4071a6
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x407199
movsd -0xa0(%rbp,%rdx,8), %xmm1
movsd %xmm1, (%rsi,%rdx,8)
movups -0x140(%rbp,%rcx), %xmm1
movups %xmm1, (%r14,%rcx)
movsd -0x130(%rbp,%rcx), %xmm1
movsd %xmm1, 0x10(%r14,%rcx)
incq %rdx
addq $0x18, %rcx
jmp 0x40715f
movsd 0x1fb3e7(%rip), %xmm1   # 0x602588
movsd %xmm0, -0x40(%rbp)
addsd 0x17be5a(%rip), %xmm1   # 0x583008
xorl %ecx, %ecx
movq -0x48(%rbp), %r10
leaq -0xa0(%rbp), %r9
movq -0x38(%rbp), %rsi
movsd %xmm1, -0x60(%rbp)
cmpl $0x14, %ecx
je 0x40735a
movl %ecx, -0x74(%rbp)
movq 0x1fb441(%rip), %rcx    # 0x602618
movq 0x1fb442(%rip), %rdx    # 0x602620
testl %eax, %eax
movl $0x0, %r13d
cmovgl %eax, %r13d
xorl %esi, %esi
cmpq %rsi, %r13
je 0x407252
movslq (%r12,%rsi,4), %rdi
movq (%r10,%rdi,8), %r8
movsd (%r8), %xmm0
movq (%rcx,%rsi,8), %r11
movsd %xmm0, (%r11)
movsd 0x8(%r8), %xmm0
movsd %xmm0, 0x8(%r11)
movsd 0x10(%r8), %xmm0
movsd %xmm0, 0x10(%r11)
movq (%rbx,%rdi,8), %r8
movsd (%r8), %xmm0
movq (%rdx,%rsi,8), %r11
movsd %xmm0, (%r11)
movsd 0x8(%r8), %xmm0
movsd %xmm0, 0x8(%r11)
movsd 0x10(%r8), %xmm0
movsd %xmm0, 0x10(%r11)
movl %edi, (%r15,%rsi,4)
incq %rsi
jmp 0x4071ec
movq 0x1fb3bf(%rip), %rdi    # 0x602618
movq 0x1fb3c0(%rip), %rsi    # 0x602620
subq $0x8, %rsp
movl %eax, %edx
pushq $0x1
popq %rcx
leaq -0x88(%rbp), %r8
leaq -0x140(%rbp), %rax
pushq %rax
callq 0x405c40
popq %rcx
popq %rdx
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0xa0(%rbp), %rcx
leaq -0x140(%rbp), %r8
callq 0x405577
movq 0x1fb34c(%rip), %rdi    # 0x6025f0
movq %rdi, -0x58(%rbp)
movq %rbx, %rsi
movl -0x2c(%rbp), %edx
movsd -0x60(%rbp), %xmm0
movq %r12, %rcx
leaq -0x68(%rbp), %r8
movl 0x10(%rbp), %r9d
callq 0x406d99
movsd -0x68(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm1
movq -0x80(%rbp), %rsi
jae 0x40731a
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x407315
movsd -0xa0(%rbp,%rdx,8), %xmm1
movsd %xmm1, (%rsi,%rdx,8)
movups -0x140(%rbp,%rcx), %xmm1
movups %xmm1, (%r14,%rcx)
movsd -0x130(%rbp,%rcx), %xmm1
movsd %xmm1, 0x10(%r14,%rcx)
incq %rdx
addq $0x18, %rcx
jmp 0x4072db
movsd %xmm0, -0x40(%rbp)
cmpl %r13d, %eax
movq -0xd0(%rbp), %rbx
movq -0x48(%rbp), %r10
leaq -0xa0(%rbp), %r9
movq -0x38(%rbp), %rsi
jne 0x407350
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x40735a
movl (%r12,%rcx,4), %edx
cmpl (%r15,%rcx,4), %edx
jne 0x40734b
incq %rcx
jmp 0x407337
cmpl %ecx, %r13d
je 0x40735a
movl -0x74(%rbp), %ecx
incl %ecx
jmp 0x4071c4
movl -0x6c(%rbp), %eax
cmpl %eax, %esi
movq -0xc8(%rbp), %r13
jge 0x40737e
movl %eax, %ecx
movq -0xc0(%rbp), %rax
leal (%rsi,%rax), %edx
cmpl %ecx, %edx
cmovgel %ecx, %edx
jmp 0x40700c
incq %r13
movq -0xb0(%rbp), %rax
jmp 0x406fd9
movq -0xa8(%rbp), %rsp
movsd -0x40(%rbp), %xmm0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq