// File: /workspace/github_c_makefile_star2_O2/asford[P]tmalign/./TMalign.h
// Function: get_initial_fgt(double**, double**, int, int, int*, int*, int*)
double get_initial_fgt( double **x, 
						double **y, 
						int x_len,
						int y_len, 
						int *xresno,
						int *yresno,
						int *y2x
						)
{
	int fra_min=4;           //minimum fragment for search
	int fra_min1=fra_min-1;  //cutoff for shift, save time

	int xstart=0, ystart=0, xend=0, yend=0;

	find_max_frag(x, xresno, x_len,  &xstart, &xend);
	find_max_frag(y, yresno, y_len, &ystart, &yend);


	int Lx = xend-xstart+1;
	int Ly = yend-ystart+1;
	int *ifr, *y2x_;
	int L_fr=getmin(Lx, Ly);
	ifr= new int[L_fr];
	y2x_= new int[y_len+1];

	//select what piece will be used (this may araise ansysmetry, but
	//only when L1=L2 and Lfr1=Lfr2 and L1 ne Lfr1
	//if L1=Lfr1 and L2=Lfr2 (normal proteins), it will be the same as initial1

	if(Lx<Ly || (Lx==Ly && x_len<=y_len))
	{		
		for(int i=0; i<L_fr; i++)
		{
			ifr[i]=xstart+i;
		}
	}
	else if(Lx>Ly || (Lx==Ly && x_len>y_len))
	{		
		for(int i=0; i<L_fr; i++)
		{
			ifr[i]=ystart+i;
		}	
	}

	
	int L0=getmin(x_len, y_len); //non-redundant to get_initial1
	if(L_fr==L0)
	{
		int n1= (int)(L0*0.1); //my index starts from 0
		int n2= (int)(L0*0.89);

		int j=0;
		for(int i=n1; i<= n2; i++)
		{
			ifr[j]=ifr[i];
			j++;
		}
		L_fr=j;
	}


	//gapless threading for the extracted fragment
	double tmscore, tmscore_max=-1;

	if(Lx<Ly || (Lx==Ly && x_len<=y_len))
	{
		int L1=L_fr;
	    int min_len=getmin(L1, y_len);    
		int min_ali= (int) (min_len/2.5);              //minimum size of considered fragment 
		if(min_ali<=fra_min1)  min_ali=fra_min1;    
		int n1, n2;
		n1 = -y_len+min_ali; 
		n2 = L1-min_ali;

		int i, j, k;
		for(k=n1; k<=n2; k++)
		{
			//get the map
			for(j=0; j<y_len; j++)
			{
				i=j+k;
				if(i>=0 && i<L1)
				{				
					y2x_[j]=ifr[i];
				}
				else
				{
					y2x_[j]=-1;
				}
			}

			//evaluate the map quickly in three iterations
			tmscore=get_score_fast(x, y, x_len, y_len, y2x_);

			if(tmscore>=tmscore_max)
			{
				tmscore_max=tmscore;
				for(j=0; j<y_len; j++)
				{
					y2x[j]=y2x_[j];
				}
			}
		}
	}
	else
	{
		int L2=L_fr;
	    int min_len=getmin(x_len, L2);    
		int min_ali= (int) (min_len/2.5);              //minimum size of considered fragment 
		if(min_ali<=fra_min1)  min_ali=fra_min1;    
		int n1, n2;
		n1 = -L2+min_ali; 
		n2 = x_len-min_ali;

		int i, j, k;	

		for(k=n1; k<=n2; k++)
		{
			//get the map
			for(j=0; j<y_len; j++)
			{
				y2x_[j]=-1;
			}

			for(j=0; j<L2; j++)
			{
				i=j+k;
				if(i>=0 && i<x_len)
				{
					y2x_[ifr[j]]=i;
				}
			}
        
			//evaluate the map quickly in three iterations
			tmscore=get_score_fast(x, y, x_len, y_len, y2x_);
			if(tmscore>=tmscore_max)
			{
				tmscore_max=tmscore;
				for(j=0; j<y_len; j++)
				{
					y2x[j]=y2x_[j];
				}
			}
		}
	}    


	delete [] ifr;
	delete [] y2x_;
    return tmscore_max;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movl %ecx, %r12d
movl %edx, 0x8(%rsp)
movq %rsi, %r14
leaq 0x24(%rsp), %rax
andl $0x0, (%rax)
movq %rax, %rcx
leaq 0x44(%rsp), %r15
andl $0x0, (%r15)
leaq 0x40(%rsp), %rbp
andl $0x0, (%rbp)
leaq 0x3c(%rsp), %r13
andl $0x0, (%r13)
movq %rdi, 0x28(%rsp)
movq %r8, %rsi
movq %rbp, %r8
callq 0x408557
movq %r14, 0x30(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movq %r15, %rcx
movq %r15, %rbx
movq %r13, %r8
movq %r13, %r14
callq 0x408557
movl (%rbp), %r13d
leaq 0x24(%rsp), %rax
movl (%rax), %r15d
subl %r15d, %r13d
movl (%r14), %r14d
movl (%rbx), %eax
movq %rax, 0x10(%rsp)
subl %eax, %r14d
cmpl %r14d, %r13d
movl %r14d, %ebp
cmovll %r13d, %ebp
movslq %ebp, %rbx
incl %ebp
cmpl $-0x1, %ebx
leaq 0x4(,%rbx,4), %rdi
pushq $-0x1
popq %rax
cmovlq %rax, %rdi
callq 0x40be00
movq %rax, 0x18(%rsp)
leal 0x1(%r12), %edi
shlq $0x2, %rdi
movq %r12, (%rsp)
cmpl $-0x1, %r12d
pushq $-0x1
popq %rax
cmovlq %rax, %rdi
callq 0x40be00
movl %r13d, %r9d
movl 0x8(%rsp), %r8d
movq %rax, %r13
cmpl %r14d, %r9d
jl 0x4087ad
cmpl (%rsp), %r8d
jg 0x4087c5
cmpl %r14d, %r9d
jne 0x4087c5
xorl %eax, %eax
movq 0x18(%rsp), %rdi
cmpq %rbx, %rax
jg 0x4087ea
leal (%r15,%rax), %ecx
movl %ecx, (%rdi,%rax,4)
incq %rax
jmp 0x4087b4
cmpl (%rsp), %r8d
setg %al
cmpl %r14d, %r9d
sete %cl
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rdx
jg 0x408a41
andb %cl, %al
jne 0x408a41
movq (%rsp), %rax
cmpl %eax, %r8d
cmovll %r8d, %eax
cmpl %eax, %ebp
jne 0x408838
cvtsi2sd %ebp, %xmm0
movsd 0x17a88b(%rip), %xmm1   # 0x583090
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
mulsd 0x17a883(%rip), %xmm0   # 0x583098
cvttsd2si %xmm0, %ecx
cltq
movslq %ecx, %rcx
leaq (%rdi,%rax,4), %rdx
xorl %ebp, %ebp
leaq (%rax,%rbp), %rsi
cmpq %rcx, %rsi
jg 0x408838
movl (%rdx,%rbp,4), %esi
movl %esi, (%rdi,%rbp,4)
incq %rbp
jmp 0x408824
cmpl %r14d, %r9d
jl 0x408850
cmpl (%rsp), %r8d
jg 0x408929
cmpl %r14d, %r9d
jne 0x408929
movq (%rsp), %rdx
cmpl %edx, %ebp
movl %edx, %eax
cmovll %ebp, %eax
cvtsi2sd %eax, %xmm0
mulsd 0x17a839(%rip), %xmm0   # 0x5830a0
cvttsd2si %xmm0, %eax
cmpl $0x4, %eax
pushq $0x3
popq %rcx
cmovgel %eax, %ecx
movslq %ebp, %rbx
subl %ecx, %ebp
subl %edx, %ecx
xorl %r14d, %r14d
testl %edx, %edx
cmovgl %edx, %r14d
movslq %ecx, %r15
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
leaq (%rdi,%r15,4), %rbp
movsd 0x17a795(%rip), %xmm0   # 0x583030
movsd %xmm0, 0x8(%rsp)
pushq $-0x1
popq %r12
cmpq 0x10(%rsp), %r15
jg 0x408a1f
xorl %eax, %eax
cmpq %rax, %r14
je 0x4088d8
movq %r15, %rdx
addq %rax, %rdx
movl %r12d, %ecx
js 0x4088ce
movl %r12d, %ecx
cmpq %rbx, %rdx
jge 0x4088ce
movl (%rbp,%rax,4), %ecx
movl %ecx, (%r13,%rax,4)
incq %rax
jmp 0x4088b2
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq (%rsp), %rcx
movq %r13, %r8
callq 0x40747b
movq 0x80(%rsp), %rdx
movq 0x18(%rsp), %rdi
ucomisd 0x8(%rsp), %xmm0
jb 0x40891d
xorl %eax, %eax
cmpq %rax, %r14
je 0x408917
movl (%r13,%rax,4), %ecx
movl %ecx, (%rdx,%rax,4)
incq %rax
jmp 0x408905
movsd %xmm0, 0x8(%rsp)
incq %r15
addq $0x4, %rbp
jmp 0x4088a5
cmpl %r8d, %ebp
movl %r8d, %eax
cmovll %ebp, %eax
cvtsi2sd %eax, %xmm0
mulsd 0x17a762(%rip), %xmm0   # 0x5830a0
cvttsd2si %xmm0, %eax
cmpl $0x4, %eax
pushq $0x3
popq %r15
cmovgel %eax, %r15d
movslq %r8d, %rbx
subl %r15d, %r8d
subl %ebp, %r15d
xorl %eax, %eax
movq (%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %r12d
cmovgl %ecx, %r12d
testl %ebp, %ebp
cmovlel %eax, %ebp
movslq %r15d, %r14
movslq %r8d, %rax
movq %rax, 0x10(%rsp)
movsd 0x17a6b0(%rip), %xmm0   # 0x583030
movsd %xmm0, 0x8(%rsp)
cmpq 0x10(%rsp), %r14
jg 0x408a1f
xorl %eax, %eax
cmpq %rax, %r12
je 0x4089a3
orl $-0x1, (%r13,%rax,4)
incq %rax
jmp 0x408993
xorl %eax, %eax
cmpq %rax, %rbp
je 0x4089cf
movq %r14, %rcx
addq %rax, %rcx
sets %dl
cmpq %rbx, %rcx
setge %cl
orb %dl, %cl
jne 0x4089ca
movslq (%rdi,%rax,4), %rcx
leal (%r15,%rax), %edx
movl %edx, (%r13,%rcx,4)
incq %rax
jmp 0x4089a5
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq (%rsp), %rcx
movq %r13, %r8
callq 0x40747b
movq 0x80(%rsp), %rdx
ucomisd 0x8(%rsp), %xmm0
jb 0x408a0f
xorl %eax, %eax
cmpq %rax, %r12
je 0x408a09
movl (%r13,%rax,4), %ecx
movl %ecx, (%rdx,%rax,4)
incq %rax
jmp 0x4089f7
movsd %xmm0, 0x8(%rsp)
incq %r14
incq %r15
movq 0x18(%rsp), %rdi
jmp 0x408986
callq 0x40b100
movq %r13, %rdi
callq 0x40b100
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
cmpq %rbx, %rax
jg 0x4087ea
leal (%rdx,%rax), %ecx
movl %ecx, (%rdi,%rax,4)
incq %rax
jmp 0x408a43