// File: /workspace/github_c_makefile_star2_O3/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, 0x40(%rsp)
movl %ecx, %r14d
movl %edx, %ebx
movq %rsi, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
movsd 0x1f9d87(%rip), %xmm0   # 0x602580
addsd 0x17b87f(%rip), %xmm0   # 0x584080
maxsd 0x1f9d5f(%rip), %xmm0   # 0x602568
movsd %xmm0, 0x20(%rsp)
leal 0x1(%r14), %eax
shlq $0x2, %rax
cmpl $-0x1, %ecx
movq $-0x1, %rdi
cmovgeq %rax, %rdi
callq 0x40d300
movq %rax, 0x60(%rsp)
cmpl %r14d, %ebx
movq %r14, 0x28(%rsp)
movl %r14d, %r13d
movl %ebx, 0x14(%rsp)
cmovll %ebx, %r13d
movl $0x32, %ebp
cmpl $0xfa, %r13d
jg 0x408872
movl $0x28, %ebp
cmpl $0xc8, %r13d
jg 0x408872
cmpl $0x97, %r13d
movl $0x1e, %eax
movl $0x14, %ebp
cmovgel %eax, %ebp
movslq %r13d, %rax
imulq $0x55555556, %rax, %r15 # imm = 0x55555556
movq %r15, %rax
shrq $0x3f, %rax
shrq $0x20, %r15
addl %eax, %r15d
cmpl %r15d, %ebp
movl %r15d, %eax
cmovll %ebp, %eax
cmpl $0x3c, %r13d
movl $0x14, %r14d
cmovll %r15d, %r14d
movq %rax, 0x48(%rsp)
leal (%rax,%r14), %ebx
movl 0x14(%rsp), %r12d
subl %ebx, %r12d
movq 0x28(%rsp), %rax
movl %eax, %esi
testl %eax, %eax
jle 0x4088de
leaq (,%rsi,4), %rdx
movq 0x40(%rsp), %rdi
movq %rsi, 0x18(%rsp)
movl $0xff, %esi
callq 0x401060
movq 0x18(%rsp), %rsi
cmpl %r12d, %r14d
movq %r14, %rcx
movq 0x60(%rsp), %r14
jle 0x4088f2
xorl %ebp, %ebp
jmp 0x408b53
movsd 0x20(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x28(%rsp), %rax
movl %eax, %edx
subl %ebx, %edx
movq 0x48(%rsp), %rax
cmpl $0x2, %eax
movl $0x1, %ebx
cmovgel %eax, %ebx
shlq $0x2, %rsi
movq %rsi, 0x18(%rsp)
leal -0x1(%rcx), %eax
movslq %eax, %rsi
movslq %r15d, %rax
movl %ebp, %edi
cmpq %rdi, %rax
cmovlq %rax, %rdi
movl %edx, 0x38(%rsp)
movslq %edx, %rax
movq %rax, 0xa8(%rsp)
movslq %r12d, %rax
movq %rax, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movslq %ecx, %rax
movq 0x58(%rsp), %rcx
leaq -0x8(%rcx,%rax,8), %rcx
movq %rcx, 0x80(%rsp)
movq %rdi, 0x70(%rsp)
leaq (,%rdi,8), %rcx
movq %rcx, 0x68(%rsp)
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
addq $-0x8, %r15
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movq %rsi, 0x88(%rsp)
movl %r13d, 0x3c(%rsp)
movq %rsi, 0xa0(%rsp)
movl 0x38(%rsp), %eax
cmpl %eax, 0x98(%rsp)
jg 0x408b33
movq 0x80(%rsp), %r12
movq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movsd %xmm1, 0x30(%rsp)
movq %rbp, 0xb0(%rsp)
movq 0x1f9c36(%rip), %rdi    # 0x602618
movq 0x1f9c37(%rip), %rsi    # 0x602620
cmpl $0x3, %r13d
jl 0x408a41
xorl %eax, %eax
movq (%r15,%rax,8), %rcx
movsd (%rcx), %xmm0
movq (%rdi,%rax,8), %rdx
movsd %xmm0, (%rdx)
movsd 0x8(%rcx), %xmm0
movsd %xmm0, 0x8(%rdx)
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%rdx)
movq (%r12,%rax,8), %rcx
movsd (%rcx), %xmm0
movq (%rsi,%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, %rbx
jne 0x4089f1
movq 0x28(%rsp), %r13
testl %r13d, %r13d
setle 0x13(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, (%rsp)
movq 0x48(%rsp), %rdx
movl $0x1, %ecx
leaq 0xb8(%rsp), %r8
leaq 0xc0(%rsp), %r9
callq 0x406142
movq %r14, (%rsp)
xorpd %xmm1, %xmm1
movq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq 0x58(%rsp), %r14
movq %r14, %rsi
movl 0x14(%rsp), %edx
movl %r13d, %ecx
leaq 0xc0(%rsp), %r8
leaq 0xe0(%rsp), %r9
movsd 0x20(%rsp), %xmm0
callq 0x405cf7
movq %rbp, %rdi
movq %r14, %rsi
movq 0x60(%rsp), %r14
movl %r13d, %ecx
movq %r14, %r8
callq 0x407a58
movsd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
seta %al
setbe %cl
orb 0x13(%rsp), %cl
je 0x408af0
maxsd %xmm0, %xmm1
movq 0xb0(%rsp), %rbp
orb %al, %bpl
jmp 0x408b11
movq 0x40(%rsp), %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movsd %xmm0, 0x30(%rsp)
callq 0x401118
movb $0x1, %bpl
movsd 0x30(%rsp), %xmm1
movq 0x78(%rsp), %rax
addq 0x70(%rsp), %rax
addq 0x68(%rsp), %r12
cmpq 0xa8(%rsp), %rax
movl 0x3c(%rsp), %r13d
jl 0x4089c8
movq 0xa0(%rsp), %rsi
addq 0x70(%rsp), %rsi
addq 0x68(%rsp), %r15
cmpq 0x90(%rsp), %rsi
jl 0x40899f
movq %r14, %rdi
callq 0x40c600
andb $0x1, %bpl
movl %ebp, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq