// File: /workspace/github_c_makefile_star2_O1/asford[P]tmalign/./TMalign.h
// Function: get_initial_local(double**, double**, int, int, int*)
bool get_initial_local(  double **x, 
						 double **y, 
						 int x_len,
						 int y_len, 
						 int *y2x
						 )
{
    double GL, rmsd;    
    double t[3];
    double u[3][3];

	double d01=d0+1.5;
	if(d01 < D0_MIN) d01=D0_MIN;
	double d02=d01*d01;
	
	double GLmax=0;
	int n_frag=20; //length of fragment for superposition
	int ns=20; //tail length to discard
	int *invmap=new int[y_len+1];
      
	int aL=getmin(x_len, y_len);
	if(aL>250)
	{
		n_frag=50;
	}
	else if(aL>200)
	{
		n_frag=40;
	}
	else if(aL>150)
	{
		n_frag=30;
	}
	else
	{
		n_frag=20;
	}
	
	int smallest=aL/3; // I change here from aL/2 to aL/3

	if(n_frag>smallest) n_frag=smallest; 
	if(ns>smallest) ns=smallest;
    
	int m1=x_len-n_frag-ns;
	int m2=y_len-n_frag-ns;

	bool flag=false;

	for(int ii=0; ii<y_len; ii++) 
	{                
		y2x[ii]=-1;
	}

	int count=0;
	for(int i=ns-1; i<m1; i=i+n_frag) //index starts from 0, different from FORTRAN
	{
		for(int j=ns-1; j<m2; j=j+n_frag)
		{
			for(int k=0; k<n_frag; k++) //fragment in y
			{ 
				r1[k][0]=x[k+i][0];  
				r1[k][1]=x[k+i][1]; 
				r1[k][2]=x[k+i][2]; 													
                        
                r2[k][0]=y[k+j][0];  
                r2[k][1]=y[k+j][1]; 
                r2[k][2]=y[k+j][2];
			}


			Kabsch(r1, r2, n_frag, 1, &rmsd, t, u);			
			count++;

			double gap_open=0.0;			
			NWDP_TM(x, y, x_len, y_len, t, u, d02, gap_open, invmap);
			GL=get_score_fast(x, y, x_len, y_len, invmap);
			if(GL>GLmax)
			{
				GLmax=GL;
				for(int ii=0; ii<y_len; ii++) 
				{                
					y2x[ii]=invmap[ii];
				}
				flag=true;
			}
		}
	}	


	delete [] invmap;
	return flag;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r8, 0x48(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movq %rsi, 0x60(%rsp)
movq %rdi, 0x58(%rsp)
movsd 0x1f9e55(%rip), %xmm0   # 0x602580
addsd 0x17b94d(%rip), %xmm0   # 0x584080
maxsd 0x1f9e2d(%rip), %xmm0   # 0x602568
movsd %xmm0, 0x28(%rsp)
movslq %ecx, %rbx
leaq 0x4(,%rbx,4), %rax
cmpl $-0x1, %ebx
movq $-0x1, %rdi
cmovgeq %rax, %rdi
callq 0x40d060
movq %rax, %r13
cmpl %ebx, %r14d
movl %ebp, 0x18(%rsp)
movl %ebp, %r12d
movl %r14d, 0x1c(%rsp)
cmovll %r14d, %r12d
movl $0x32, %r14d
cmpl $0xfa, %r12d
jg 0x4087a9
movl $0x28, %r14d
cmpl $0xc8, %r12d
jg 0x4087a9
cmpl $0x97, %r12d
movl $0x1e, %eax
movl $0x14, %r14d
cmovgel %eax, %r14d
movslq %r12d, %rax
imulq $0x55555556, %rax, %r15 # imm = 0x55555556
movq %r15, %rax
shrq $0x3f, %rax
shrq $0x20, %r15
addl %eax, %r15d
cmpl %r15d, %r14d
movl %r15d, %eax
cmovll %r14d, %eax
cmpl $0x3c, %r12d
movl $0x14, %ecx
cmovll %r15d, %ecx
movq %rax, 0x50(%rsp)
movq %rcx, 0x40(%rsp)
leal (%rax,%rcx), %ebx
movl 0x1c(%rsp), %ebp
subl %ebx, %ebp
movl 0x18(%rsp), %eax
movl %eax, %ecx
testl %eax, %eax
jle 0x408816
leaq (,%rcx,4), %rdx
movq 0x48(%rsp), %rdi
movl $0xff, %esi
movq %rcx, 0x20(%rsp)
callq 0x401060
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rdx
cmpl %ebp, %edx
jle 0x408827
xorl %r14d, %r14d
jmp 0x408a99
movl %r12d, 0x3c(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x18(%rsp), %esi
subl %ebx, %esi
movq 0x50(%rsp), %rax
cmpl $0x2, %eax
movl $0x1, %ebx
cmovgel %eax, %ebx
shlq $0x2, %rcx
movq %rcx, 0x20(%rsp)
leal -0x1(%rdx), %eax
movslq %eax, %rdi
movslq %r15d, %rax
movl %r14d, %r8d
cmpq %r8, %rax
cmovlq %rax, %r8
movl %esi, 0x38(%rsp)
movslq %esi, %rax
movq %rax, 0xa0(%rsp)
movslq %ebp, %rax
movq %rax, 0x90(%rsp)
movslq %edx, %rax
movq 0x60(%rsp), %rcx
leaq -0x8(%rcx,%rax,8), %rcx
movq %rcx, 0x80(%rsp)
movq %r8, 0x70(%rsp)
leaq (,%r8,8), %rcx
movq %rcx, 0x68(%rsp)
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
addq $-0x8, %r15
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
movq %rdi, 0x88(%rsp)
movq %r13, 0xa8(%rsp)
movq 0x60(%rsp), %rbp
movq %rdi, 0x98(%rsp)
cmpl 0x38(%rsp), %edx
jg 0x408a74
movq 0x80(%rsp), %r12
movq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movsd %xmm1, 0xb0(%rsp)
movq %r14, 0x30(%rsp)
cmpl $0x3, 0x3c(%rsp)
jl 0x408974
movq 0x1f9cfd(%rip), %rax    # 0x602618
movq 0x1f9cfe(%rip), %rcx    # 0x602620
xorl %edx, %edx
movq (%r15,%rdx,8), %rsi
movsd (%rsi), %xmm0
movq (%rax,%rdx,8), %rdi
movsd %xmm0, (%rdi)
movsd 0x8(%rsi), %xmm0
movsd %xmm0, 0x8(%rdi)
movsd 0x10(%rsi), %xmm0
movsd %xmm0, 0x10(%rdi)
movq (%r12,%rdx,8), %rsi
movsd (%rsi), %xmm0
movq (%rcx,%rdx,8), %rdi
movsd %xmm0, (%rdi)
movsd 0x8(%rsi), %xmm0
movsd %xmm0, 0x8(%rdi)
movsd 0x10(%rsi), %xmm0
movsd %xmm0, 0x10(%rdi)
incq %rdx
cmpq %rdx, %rbx
jne 0x408924
movl 0x18(%rsp), %r14d
testl %r14d, %r14d
setle 0x17(%rsp)
movq 0x1f9c90(%rip), %rdi    # 0x602618
movq 0x1f9c91(%rip), %rsi    # 0x602620
leaq 0xe0(%rsp), %rax
movq %rax, (%rsp)
movq 0x50(%rsp), %rdx
movl $0x1, %ecx
leaq 0xb8(%rsp), %r8
leaq 0xc0(%rsp), %r9
callq 0x406168
movq %r13, (%rsp)
xorpd %xmm1, %xmm1
movq 0x58(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movl 0x1c(%rsp), %edx
movl %r14d, %ecx
leaq 0xc0(%rsp), %r8
leaq 0xe0(%rsp), %r9
movsd 0x28(%rsp), %xmm0
callq 0x405cfb
movq %r13, %rdi
movq 0xa8(%rsp), %r13
movq %rbp, %rsi
movl %r14d, %ecx
movq %r13, %r8
callq 0x407b41
movsd 0xb0(%rsp), %xmm1
ucomisd %xmm1, %xmm0
setbe %al
movq 0x30(%rsp), %r14
movzbl %r14b, %r14d
movl $0x1, %ecx
cmoval %ecx, %r14d
orb 0x17(%rsp), %al
je 0x408a36
maxsd %xmm0, %xmm1
jmp 0x408a57
movq 0x48(%rsp), %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movsd %xmm0, 0x30(%rsp)
callq 0x401118
movb $0x1, %r14b
movsd 0x30(%rsp), %xmm1
movq 0x78(%rsp), %rax
addq 0x70(%rsp), %rax
addq 0x68(%rsp), %r12
cmpq 0xa0(%rsp), %rax
jl 0x4088fa
movq 0x98(%rsp), %rdi
addq 0x70(%rsp), %rdi
addq 0x68(%rsp), %r15
cmpq 0x90(%rsp), %rdi
movq 0x40(%rsp), %rdx
jl 0x4088d8
movq %r13, %rdi
callq 0x40c360
andb $0x1, %r14b
movl %r14d, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq