// File: /workspace/github_c_makefile_star2_O2/asford[P]tmalign/./TMalign.h
// Function: get_score_fast(double**, double**, int, int, int*)
double get_score_fast(double **x, double **y, int x_len, int y_len, int invmap[])
{
    double rms, tmscore, tmscore1, tmscore2;
    int i, j, k;

    k=0;
    for(j=0; j<y_len; j++)
    {
        i=invmap[j];
        if(i>=0)
        {
            r1[k][0]=x[i][0];
            r1[k][1]=x[i][1];
            r1[k][2]=x[i][2];

            r2[k][0]=y[j][0];
            r2[k][1]=y[j][1];
            r2[k][2]=y[j][2];
            
            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++;
        }
        else if(i!=-1)
        {
            PrintErrorAndQuit("Wrong map!\n");
        }       
    }
    Kabsch(r1, r2, k, 1, &rms, t, u);
    
    //evaluate score   
    double di;
	const int len=k;
    double dis[len];    
	double d00=d0_search;
	double d002=d00*d00;
	double d02=d0*d0;
	
    int n_ali=k;
	double xrot[3];
	tmscore=0;
	for(k=0; k<n_ali; k++)
	{
        transform(t, u, &xtm[k][0], xrot);        
        di=dist(xrot, &ytm[k][0]);
        dis[k]=di;
        tmscore += 1/(1+di/d02);
    }
	
   
   
   //second iteration 
    double d002t=d002;
    while(1)
    {
		j=0;
        for(k=0; k<n_ali; k++)
        {            
            if(dis[k]<=d002t)
            {
                r1[j][0]=xtm[k][0];
                r1[j][1]=xtm[k][1];
                r1[j][2]=xtm[k][2];
                
                r2[j][0]=ytm[k][0];
                r2[j][1]=ytm[k][1];
                r2[j][2]=ytm[k][2];
                
                j++;
            }
        }
        //there are not enough feasible pairs, relieve the threshold 
        if(j<3 && n_ali>3)
        {
            d002t += 0.5;
        }
        else
        {
            break;
        }
    }
    
    if(n_ali!=j)
    {
        Kabsch(r1, r2, j, 1, &rms, t, u);
    	tmscore1=0;
    	for(k=0; k<n_ali; k++)
    	{
            transform(t, u, &xtm[k][0], xrot);        
            di=dist(xrot, &ytm[k][0]);
            dis[k]=di;
            tmscore1 += 1/(1+di/d02);
        }
        
        //third iteration
        d002t=d002+1;
       
        while(1)
        {
			j=0;
            for(k=0; k<n_ali; k++)
            {            
                if(dis[k]<=d002t)
                {
                    r1[j][0]=xtm[k][0];
                    r1[j][1]=xtm[k][1];
                    r1[j][2]=xtm[k][2];
                    
                    r2[j][0]=ytm[k][0];
                    r2[j][1]=ytm[k][1];
                    r2[j][2]=ytm[k][2];
                                        
                    j++;
                }
            }
            //there are not enough feasible pairs, relieve the threshold 
            if(j<3 && n_ali>3)
            {
                d002t += 0.5;
            }
            else
            {
                break;
            }
        }

        //evaluate the score
        Kabsch(r1, r2, j, 1, &rms, t, u);
        tmscore2=0;
        for(k=0; k<n_ali; k++)
        {
            transform(t, u, &xtm[k][0], xrot);
            di=dist(xrot, &ytm[k][0]);
            tmscore2 += 1/(1+di/d02);
        }    
    }
    else
    {
        tmscore1=tmscore;
        tmscore2=tmscore;
    }
    
      
    if(tmscore1>=tmscore) tmscore=tmscore1;
    if(tmscore2>=tmscore) tmscore=tmscore2;

    
    return tmscore; // no need to normalize this score because it will not be used for latter scoring
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rax
movq %rdi, %rdx
movq 0x1fb17f(%rip), %rdi    # 0x602618
movq 0x1fb180(%rip), %rsi    # 0x602620
movq 0x1fb139(%rip), %r9     # 0x6025e0
movq 0x1fb13a(%rip), %r10    # 0x6025e8
xorl %r11d, %r11d
testl %ecx, %ecx
cmovlel %r11d, %ecx
movq $0x0, -0x30(%rbp)
cmpq %r11, %rcx
je 0x40758d
movslq (%r8,%r11,4), %rbx
testq %rbx, %rbx
js 0x40757c
movq (%rdx,%rbx,8), %rbx
movsd (%rbx), %xmm0
movq -0x30(%rbp), %r12
movslq %r12d, %r12
movq (%rdi,%r12,8), %r14
movsd %xmm0, (%r14)
movsd 0x8(%rbx), %xmm0
movsd %xmm0, 0x8(%r14)
movsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x10(%r14)
movq (%rax,%r11,8), %r14
movsd (%r14), %xmm0
movq (%rsi,%r12,8), %r15
movsd %xmm0, (%r15)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r15)
movsd 0x10(%r14), %xmm0
movsd %xmm0, 0x10(%r15)
movsd (%rbx), %xmm0
movq (%r9,%r12,8), %r15
movsd %xmm0, (%r15)
movsd 0x8(%rbx), %xmm0
movsd %xmm0, 0x8(%r15)
movsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x10(%r15)
movsd (%r14), %xmm0
movq (%r10,%r12,8), %rbx
movsd %xmm0, (%rbx)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%rbx)
movsd 0x10(%r14), %xmm0
movsd %xmm0, 0x10(%rbx)
incl %r12d
movq %r12, -0x30(%rbp)
jmp 0x407585
cmpl $-0x1, %ebx
jne 0x407957
incq %r11
jmp 0x4074bf
subq $0x8, %rsp
leaq 0x1fb0b8(%rip), %rax    # 0x602650
leaq 0x1fb091(%rip), %r9     # 0x602630
pushq $0x1
popq %rcx
leaq -0x60(%rbp), %r8
movq -0x30(%rbp), %rdx
movq %rdx, %r13
pushq %rax
callq 0x405c40
addq $0x10, %rsp
movq %rsp, -0x70(%rbp)
movl %r13d, %eax
movq %rsp, %r14
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movsd 0x1fafad(%rip), %xmm0   # 0x602588
movsd %xmm0, -0x38(%rbp)
movsd 0x1faf98(%rip), %xmm0   # 0x602580
mulsd %xmm0, %xmm0
movq 0x1fafed(%rip), %r15    # 0x6025e0
movq 0x1fafee(%rip), %r12    # 0x6025e8
xorl %ebx, %ebx
movq %r13, %rax
testl %eax, %eax
movl $0x0, %r13d
cmovgl %eax, %r13d
movsd 0x17b9f5(%rip), %xmm1   # 0x583008
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
xorpd %xmm2, %xmm2
cmpq %rbx, %r13
movsd %xmm2, -0x58(%rbp)
je 0x407681
movq (%r15,%rbx,8), %rdx
leaq 0x1faffb(%rip), %rdi    # 0x602630
leaq 0x1fb014(%rip), %rsi    # 0x602650
leaq -0x90(%rbp), %rcx
callq 0x4054f3
movq (%r12,%rbx,8), %rsi
leaq -0x90(%rbp), %rdi
callq 0x4054a5
movsd -0x58(%rbp), %xmm2
movsd %xmm0, (%r14,%rbx,8)
mulsd -0x40(%rbp), %xmm0
movsd 0x17b998(%rip), %xmm1   # 0x583008
addsd %xmm1, %xmm0
divsd %xmm0, %xmm1
addsd %xmm1, %xmm2
incq %rbx
jmp 0x407620
movsd -0x38(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq 0x1faf82(%rip), %rax    # 0x602618
xorl %ecx, %ecx
movq 0x1faf81(%rip), %rsi    # 0x602620
xorl %edx, %edx
cmpq %rcx, %r13
je 0x407706
ucomisd (%r14,%rcx,8), %xmm0
jb 0x407701
movq (%r15,%rcx,8), %rdi
movsd (%rdi), %xmm1
movslq %edx, %rdx
movq (%rax,%rdx,8), %r8
movsd %xmm1, (%r8)
movsd 0x8(%rdi), %xmm1
movsd %xmm1, 0x8(%r8)
movsd 0x10(%rdi), %xmm1
movsd %xmm1, 0x10(%r8)
movq (%r12,%rcx,8), %rdi
movsd (%rdi), %xmm1
movq (%rsi,%rdx,8), %r8
movsd %xmm1, (%r8)
movsd 0x8(%rdi), %xmm1
movsd %xmm1, 0x8(%r8)
movsd 0x10(%rdi), %xmm1
movsd %xmm1, 0x10(%r8)
incl %edx
incq %rcx
jmp 0x4076a1
cmpl $0x2, %edx
jg 0x40772c
cmpl $0x4, -0x30(%rbp)
jl 0x40772c
addsd 0x17b8ff(%rip), %xmm0   # 0x583018
movq 0x1faec0(%rip), %r15    # 0x6025e0
movq 0x1faec1(%rip), %r12    # 0x6025e8
jmp 0x40768f
movapd %xmm2, %xmm1
movapd %xmm2, %xmm0
cmpl %edx, -0x30(%rbp)
je 0x40793c
movq 0x1faed4(%rip), %rdi    # 0x602618
movq 0x1faed5(%rip), %r12    # 0x602620
subq $0x8, %rsp
leaq 0x1faeda(%rip), %r9     # 0x602630
pushq $0x1
popq %rcx
leaq -0x60(%rbp), %r8
movq %rdi, -0x68(%rbp)
movq %r12, %rsi
leaq 0x1faee5(%rip), %rax    # 0x602650
pushq %rax
callq 0x405c40
addq $0x10, %rsp
movq 0x1fae64(%rip), %rbx    # 0x6025e0
movq 0x1fae65(%rip), %r15    # 0x6025e8
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd %xmm0, -0x48(%rbp)
cmpq %rax, %r13
je 0x4077f6
movq (%rbx,%rax,8), %rdx
leaq 0x1fae92(%rip), %rdi    # 0x602630
leaq 0x1faeab(%rip), %rsi    # 0x602650
leaq -0x90(%rbp), %rcx
movq %rax, -0x50(%rbp)
callq 0x4054f3
movq -0x50(%rbp), %rax
movq (%r15,%rax,8), %rsi
leaq -0x90(%rbp), %rdi
callq 0x4054a5
movq -0x50(%rbp), %rax
movsd %xmm0, (%r14,%rax,8)
mulsd -0x40(%rbp), %xmm0
movsd 0x17b828(%rip), %xmm1   # 0x583008
addsd %xmm1, %xmm0
divsd %xmm0, %xmm1
movsd -0x48(%rbp), %xmm0
addsd %xmm1, %xmm0
incq %rax
jmp 0x407789
movsd -0x38(%rbp), %xmm1
addsd 0x17b805(%rip), %xmm1   # 0x583008
movsd 0x17b80d(%rip), %xmm2   # 0x583018
movq -0x68(%rbp), %rdi
xorl %eax, %eax
xorl %edx, %edx
cmpq %rax, %r13
je 0x407872
ucomisd (%r14,%rax,8), %xmm1
jb 0x40786d
movq (%rbx,%rax,8), %rcx
movsd (%rcx), %xmm0
movslq %edx, %rdx
movq (%rdi,%rdx,8), %rsi
movsd %xmm0, (%rsi)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rsi)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%rsi)
movq (%r15,%rax,8), %rcx
movsd (%rcx), %xmm0
movq (%r12,%rdx,8), %rsi
movsd %xmm0, (%rsi)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rsi)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%rsi)
incl %edx
incq %rax
jmp 0x407813
cmpl $0x2, %edx
jg 0x407894
cmpl $0x4, -0x30(%rbp)
jl 0x407894
addsd %xmm2, %xmm1
movq 0x1fad58(%rip), %rbx    # 0x6025e0
movq 0x1fad59(%rip), %r15    # 0x6025e8
jmp 0x40780f
subq $0x8, %rsp
leaq 0x1fad91(%rip), %r9     # 0x602630
pushq $0x1
popq %rcx
leaq -0x60(%rbp), %r8
movq %r12, %rsi
leaq 0x1fada0(%rip), %rax    # 0x602650
pushq %rax
callq 0x405c40
addq $0x10, %rsp
movq 0x1fad1f(%rip), %r15    # 0x6025e0
movq 0x1fad20(%rip), %r12    # 0x6025e8
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
leaq -0x90(%rbp), %r14
cmpq %rbx, %r13
je 0x407932
movq (%r15,%rbx,8), %rdx
leaq 0x1fad4b(%rip), %rdi    # 0x602630
leaq 0x1fad64(%rip), %rsi    # 0x602650
movq %r14, %rcx
movsd %xmm1, -0x30(%rbp)
callq 0x4054f3
movq (%r12,%rbx,8), %rsi
movq %r14, %rdi
callq 0x4054a5
mulsd -0x40(%rbp), %xmm0
movsd 0x17b6f6(%rip), %xmm1   # 0x583008
addsd %xmm1, %xmm0
divsd %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm1
incq %rbx
jmp 0x4078d5
movsd -0x58(%rbp), %xmm2
movsd -0x48(%rbp), %xmm0
maxsd %xmm2, %xmm0
maxsd %xmm1, %xmm0
movq -0x70(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17b8ba(%rip), %rdi    # 0x583218
callq 0x404708