// File: /workspace/github_c_makefile_star2_O2/genevalab[P]msmove/ms.c
// Function: gensam
int gensam(char **list, double *pprobss, double *ptmrca, double *pttot, int *migflag) 
{
	int nsegs, i, k, seg, ns, start, end, len, segsit;
	int *ss, segsitesin, nsites, nsam, mfreq;
	double nsinv, tseg, tt, theta, es, *pk;
	struct segl *seglst;

	nsites = pars.cp.nsites;
	nsinv = 1.0/nsites;
	*migflag = 0;
	seglst = segtre_mig(&(pars.cp), &nsegs, migflag);
	nsam = pars.cp.nsam;
	segsitesin = pars.mp.segsitesin;
	theta = pars.mp.theta;
	mfreq = pars.mp.mfreq;

	if (pars.mp.treeflag) 
	{
	  	ns = 0;
	    for (seg=0, k=0; k < nsegs; seg = seglst[seg].next, k++)
		{
			if ( (pars.cp.r > 0) || (pars.cp.f > 0) )
			{
				end = k < nsegs-1 ? seglst[seglst[seg].next].beg-1 : nsites-1;
				start = seglst[seg].beg;
				len = end-start+1;
				fprintf(stdout, "[%d]", len);
			}
			prtree(seglst[seg].ptree, nsam);
			if ((segsitesin == 0) && (theta == 0) && (pars.mp.timeflag == 0)) 
				free(seglst[seg].ptree);
		}
	}

	if (pars.mp.timeflag)
	{
		tt = 0;
		for (seg=0, k=0; k < nsegs; seg = seglst[seg].next, k++)
		{ 
			if (mfreq > 1) 
				ndes_setup(seglst[seg].ptree, nsam);
			end = k < nsegs-1 ? seglst[seglst[seg].next].beg-1 : nsites-1;
			start = seglst[seg].beg;
			if ((nsegs == 1) || ((start <= nsites/2) && (end >= nsites/2)))
				*ptmrca = (seglst[seg].ptree+2*nsam-2)->time;
			len = end-start+1;
			tseg = len / (double)nsites;
			if (mfreq == 1)
				tt += ttime(seglst[seg].ptree, nsam) * tseg;
			else 
				tt += ttimemf(seglst[seg].ptree, nsam, mfreq)*tseg;
			if ((segsitesin == 0) && (theta == 0)) 
				free(seglst[seg].ptree);
		}
		*pttot = tt;
	}	
	
    if ((segsitesin == 0) && (theta > 0))
	{
		ns = 0;
		for (seg=0, k=0; k < nsegs; seg = seglst[seg].next, k++)
		{ 
			if (mfreq > 1)
				ndes_setup(seglst[seg].ptree, nsam);
			end = k < nsegs-1 ? seglst[seglst[seg].next].beg-1 : nsites-1;
			start = seglst[seg].beg;
			len = end-start+1;
			tseg = len*theta/nsites;
			if (mfreq == 1)
				tt = ttime(seglst[seg].ptree, nsam);
			else
				tt = ttimemf(seglst[seg].ptree, nsam, mfreq);
			segsit = poisso(tseg*tt);
			if ((unsigned int)(segsit+ns) >= maxsites)
			{
				maxsites = segsit+ns+SITESINC;
				posit = (double*)realloc(posit, (size_t)maxsites*sizeof(double));
				if (posit == NULL)
					perror("Realloc failure for posit");
				biggerlist(nsam, list); 
			}
			make_gametes(nsam, mfreq, seglst[seg].ptree, tt, segsit, ns, list);
			free(seglst[seg].ptree);
			locate(segsit, start * nsinv, len * nsinv, posit + ns);   
			ns += segsit;
		}
	}
	else
		if (segsitesin > 0)
		{
			pk = (double*)malloc((size_t)nsegs*sizeof(double));
			ss = (int*)malloc((size_t)nsegs*sizeof(int));
			if ((pk == 0) || (ss == 0))
				perror("malloc error. gensam.2");
			tt = 0;
			for (seg=0, k=0; k < nsegs; seg = seglst[seg].next, k++)
			{ 
				if (mfreq > 1)
					ndes_setup(seglst[seg].ptree, nsam);
				end = k < nsegs-1 ? seglst[seglst[seg].next].beg-1 : nsites-1;
				start = seglst[seg].beg;
				len = end-start+1;
				tseg = len/(double)nsites;
				if (mfreq == 1)
					pk[k] = ttime(seglst[seg].ptree, nsam)*tseg;
				else 
					pk[k] = ttimemf(seglst[seg].ptree, nsam, mfreq)*tseg;
				tt += pk[k];
			}
			if (theta > 0)
			{ 
				es = theta*tt;
				*pprobss = exp(-1.0*es)*pow(es, (double)segsitesin)/segfac;
			}
			if (tt > 0)
			{
				for (k=0; k < nsegs; k++)
					pk[k] *= 1.0/tt;
				mnmial(segsitesin, nsegs, pk, ss);
			}
			else
				for (k=0; k < nsegs; k++)
					ss[k] = 0;
			ns = 0;
			for (seg=0, k=0; k < nsegs; seg = seglst[seg].next, k++)
			{ 
				end = k < nsegs-1 ? seglst[seglst[seg].next].beg-1 : nsites-1;
				start = seglst[seg].beg;
				len = end-start+1;
				tseg = len/(double)nsites;
				make_gametes(nsam, mfreq, seglst[seg].ptree, tt*pk[k]/tseg, ss[k], ns, list);
				free(seglst[seg].ptree);
				locate(ss[k], start*nsinv, len*nsinv, posit+ns);
				ns += ss[k];
			}
			free(pk);
			free(ss);
		}
		
		for (i=0; i < nsam; i++)
			list[i][ns] = '\0';
		return ns;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, -0xb0(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, -0xa8(%rbp)
movq %rdi, -0x80(%rbp)
movl 0x6ae7(%rip), %eax      # 0x9130
movl %eax, -0x44(%rbp)
leaq 0x6abd(%rip), %rdi      # 0x9110
andl $0x0, (%r8)
leaq -0x2c(%rbp), %rsi
movq %r8, %rdx
callq 0x3d28
movq %rax, %r12
movslq 0x6aa7(%rip), %rax      # 0x9114
movq %rax, -0x40(%rbp)
movl 0x6af1(%rip), %eax      # 0x9168
movl %eax, -0x48(%rbp)
movsd 0x6ade(%rip), %xmm0     # 0x9160
movl 0x6aeb(%rip), %r8d      # 0x9174
cmpl $0x0, 0x6adc(%rip)      # 0x916c
movq %r12, -0x38(%rbp)
movl %r8d, -0x30(%rbp)
movapd %xmm0, -0xa0(%rbp)
je 0x278a
cmpl $0x0, -0x48(%rbp)
sete %al
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setnp %cl
sete %r14b
andb %cl, %r14b
andb %al, %r14b
leaq 0x3a90(%rip), %rbx      # 0x6159
xorl %eax, %eax
xorl %r13d, %r13d
movl -0x2c(%rbp), %ecx
cmpl %ecx, %r13d
jge 0x278a
movsd 0x6a46(%rip), %xmm0     # 0x9128
ucomisd %xmm1, %xmm0
ja 0x26ff
movsd 0x6a48(%rip), %xmm0     # 0x9138
ucomisd %xmm1, %xmm0
ja 0x26ff
movslq %eax, %r12
movq -0x38(%rbp), %r15
jmp 0x2740
decl %ecx
movslq %eax, %r12
movl -0x44(%rbp), %edx
cmpl %ecx, %r13d
jge 0x2720
imulq $0x18, %r12, %rax
movq -0x38(%rbp), %rcx
movslq 0x10(%rcx,%rax), %rax
imulq $0x18, %rax, %rax
movl (%rcx,%rax), %edx
imulq $0x18, %r12, %rax
movq -0x38(%rbp), %r15
subl (%r15,%rax), %edx
movq 0x6895(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1130
imulq $0x18, %r12, %rax
leaq (%r15,%rax), %r12
movq 0x8(%r15,%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x2dce
cmpl $0x0, 0x6a13(%rip)      # 0x9170
sete %al
andb %r14b, %al
cmpb $0x1, %al
jne 0x2771
movq 0x8(%r12), %rdi
callq 0x10c0
movl 0x10(%r12), %eax
incl %r13d
movq -0x38(%rbp), %r12
movl -0x30(%rbp), %r8d
xorpd %xmm1, %xmm1
jmp 0x26ce
cvtsi2sdl -0x44(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movapd -0xa0(%rbp), %xmm0
movsd 0x3864(%rip), %xmm1     # 0x6008
movsd %xmm1, -0x88(%rbp)
cmpl $0x0, 0x69bd(%rip)      # 0x9170
je 0x28f1
pushq $0x2
popq %rcx
movl -0x44(%rbp), %eax
cltd
idivl %ecx
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x48(%rbp)
sete %al
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setnp %cl
sete %dl
andb %cl, %dl
movq -0x40(%rbp), %rcx
leaq (%rcx,%rcx), %rsi
andb %al, %dl
movb %dl, -0x68(%rbp)
xorl %ecx, %ecx
shlq $0x4, %rsi
movq %rsi, -0xb8(%rbp)
xorl %r14d, %r14d
movl -0x2c(%rbp), %eax
cmpl %eax, %r14d
jge 0x28e6
movsd %xmm1, -0x50(%rbp)
movslq %ecx, %r15
cmpl $0x2, %r8d
jl 0x282c
imulq $0x18, %r15, %rax
movq 0x8(%r12,%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x2e8f
movl -0x30(%rbp), %r8d
movl -0x2c(%rbp), %eax
leal -0x1(%rax), %ecx
movl -0x44(%rbp), %ebx
cmpl %ecx, %r14d
jge 0x2848
imulq $0x18, %r15, %rcx
movslq 0x10(%r12,%rcx), %rcx
imulq $0x18, %rcx, %rcx
movl (%r12,%rcx), %ebx
imulq $0x18, %r15, %rcx
leaq (%r12,%rcx), %r13
movl (%r12,%rcx), %r12d
cmpl $0x1, %eax
je 0x2864
cmpl -0x58(%rbp), %r12d
jg 0x287d
cmpl -0x58(%rbp), %ebx
jle 0x287d
movq 0x8(%r13), %rax
movq -0xb8(%rbp), %rcx
movsd -0x18(%rax,%rcx), %xmm0
movq -0x78(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x8(%r13), %r15
movq %r15, %rdi
cmpl $0x1, %r8d
jne 0x2895
movq -0x40(%rbp), %rsi
callq 0x2f13
jmp 0x28a1
movq -0x40(%rbp), %rsi
movl %r8d, %edx
callq 0x2f55
cmpb $0x0, -0x68(%rbp)
je 0x28b9
movq %r15, %rdi
movsd %xmm0, -0x60(%rbp)
callq 0x10c0
movsd -0x60(%rbp), %xmm0
subl %r12d, %ebx
cvtsi2sd %ebx, %xmm1
divsd -0x70(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x50(%rbp), %xmm1
addsd %xmm0, %xmm1
movl 0x10(%r13), %ecx
incl %r14d
movq -0x38(%rbp), %r12
movl -0x30(%rbp), %r8d
jmp 0x27f9
movq -0xb0(%rbp), %rax
movsd %xmm1, (%rax)
movsd 0x370f(%rip), %xmm0     # 0x6008
divsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x2aa2
xorpd %xmm0, %xmm0
movapd -0xa0(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2aa2
xorl %r13d, %r13d
xorl %eax, %eax
xorl %r15d, %r15d
movl -0x2c(%rbp), %ecx
cmpl %ecx, %r15d
jge 0x2d81
movslq %eax, %r14
cmpl $0x2, %r8d
jl 0x2959
imulq $0x18, %r14, %rax
movq 0x8(%r12,%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x2e8f
movl -0x30(%rbp), %r8d
movl -0x2c(%rbp), %ecx
decl %ecx
movl -0x44(%rbp), %ebx
cmpl %ecx, %r15d
jge 0x2974
imulq $0x18, %r14, %rax
movslq 0x10(%r12,%rax), %rax
imulq $0x18, %rax, %rax
movl (%r12,%rax), %ebx
imulq $0x18, %r14, %r14
movl (%r12,%r14), %eax
movl %eax, -0x38(%rbp)
movq 0x8(%r12,%r14), %rdi
cmpl $0x1, %r8d
movl %r15d, -0x50(%rbp)
jne 0x2999
movq -0x40(%rbp), %rsi
callq 0x2f13
jmp 0x29a5
movq -0x40(%rbp), %rsi
movl %r8d, %edx
callq 0x2f55
movapd %xmm0, %xmm2
subl -0x38(%rbp), %ebx
cvtsi2sd %ebx, %xmm1
movapd -0xa0(%rbp), %xmm0
movsd %xmm1, -0x68(%rbp)
mulsd %xmm1, %xmm0
divsd -0x70(%rbp), %xmm0
movsd %xmm2, -0x60(%rbp)
mulsd %xmm2, %xmm0
callq 0x2fb4
movl %eax, %ebx
movq %r13, %r15
addl %ebx, %r13d
cmpl 0x6705(%rip), %r13d     # 0x90e8
jb 0x2a1c
leal 0xa(%r13), %esi
movl %esi, 0x66f9(%rip)      # 0x90e8
movq 0x6782(%rip), %rdi      # 0x9178
shlq $0x3, %rsi
callq 0x1150
movq %rax, 0x6772(%rip)      # 0x9178
testq %rax, %rax
je 0x2a91
movq -0x40(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x303f
addq %r12, %r14
movq 0x8(%r14), %rdx
movq -0x40(%rbp), %rdi
movq %r13, -0x58(%rbp)
movl -0x30(%rbp), %r13d
movl %r13d, %esi
movsd -0x60(%rbp), %xmm0
movl %ebx, %ecx
movl %r15d, %r8d
movq -0x80(%rbp), %r9
callq 0x3097
movq 0x8(%r14), %rdi
callq 0x10c0
cvtsi2sdl -0x38(%rbp), %xmm0
movsd -0x78(%rbp), %xmm2
mulsd %xmm2, %xmm0
movsd -0x68(%rbp), %xmm1
mulsd %xmm2, %xmm1
movslq %r15d, %rsi
shlq $0x3, %rsi
addq 0x6705(%rip), %rsi      # 0x9178
movl %ebx, %edi
callq 0x3146
movl %r13d, %r8d
movl 0x10(%r14), %eax
movl -0x50(%rbp), %r15d
incl %r15d
movq -0x58(%rbp), %r13
jmp 0x292b
leaq 0x36c6(%rip), %rdi      # 0x615e
callq 0x1070
jmp 0x2a0f
cmpl $0x0, -0x48(%rbp)
jle 0x2c68
movslq -0x2c(%rbp), %rbx
leaq (,%rbx,8), %rdi
callq 0x1100
movq %rax, %r13
leaq (,%rbx,4), %rdi
callq 0x1100
movq %rax, %r14
testq %r13, %r13
je 0x2dba
testq %r14, %r14
je 0x2dba
movq %r14, -0x60(%rbp)
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
xorl %eax, %eax
movl -0x30(%rbp), %edx
movslq %ebx, %rcx
cmpq %rcx, %r14
movapd -0xa0(%rbp), %xmm0
jge 0x2baf
movslq %eax, %r15
cmpl $0x2, %edx
jl 0x2b2f
imulq $0x18, %r15, %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx,%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x2e8f
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ebx
leal -0x1(%rbx), %eax
cltq
movl -0x44(%rbp), %r12d
cmpq %rax, %r14
jge 0x2b52
imulq $0x18, %r15, %rax
movq -0x38(%rbp), %rcx
movslq 0x10(%rcx,%rax), %rax
imulq $0x18, %rax, %rax
movl (%rcx,%rax), %r12d
imulq $0x18, %r15, %r15
movq -0x38(%rbp), %rax
subl (%rax,%r15), %r12d
movq 0x8(%rax,%r15), %rdi
cmpl $0x1, %edx
jne 0x2b73
movq -0x40(%rbp), %rsi
callq 0x2f13
jmp 0x2b7c
movq -0x40(%rbp), %rsi
callq 0x2f55
addq -0x38(%rbp), %r15
cvtsi2sd %r12d, %xmm1
divsd -0x70(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%r14,8)
movsd -0x50(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movl 0x10(%r15), %eax
incq %r14
jmp 0x2af4
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq -0x38(%rbp), %r12
jbe 0x2c12
mulsd -0x50(%rbp), %xmm0
movapd %xmm0, %xmm1
movapd %xmm0, -0xa0(%rbp)
movapd 0x347a(%rip), %xmm0     # 0x6050
xorpd %xmm1, %xmm0
callq 0x1180
movsd %xmm0, -0x68(%rbp)
cvtsi2sdl -0x48(%rbp), %xmm1
movapd -0xa0(%rbp), %xmm0
callq 0x1170
xorpd %xmm1, %xmm1
mulsd -0x68(%rbp), %xmm0
divsd 0x6579(%rip), %xmm0     # 0x9180
movq -0xa8(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x50(%rbp), %xmm0
ucomisd %xmm1, %xmm0
movl -0x2c(%rbp), %esi
movq -0x60(%rbp), %rcx
jbe 0x2c70
movapd %xmm0, %xmm1
movsd -0x88(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
cmpq %rax, %rdx
je 0x2c85
movsd (%r13,%rax,8), %xmm0
mulsd -0x88(%rbp), %xmm0
movsd %xmm0, (%r13,%rax,8)
incq %rax
jmp 0x2c48
xorl %r13d, %r13d
jmp 0x2d81
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
cmpq %rax, %rsi
je 0x2c90
andl $0x0, (%rcx,%rax,4)
incq %rax
jmp 0x2c77
movl -0x48(%rbp), %edi
movq %r13, %rdx
callq 0x31a0
movq %r13, -0x58(%rbp)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %eax, %eax
movslq -0x2c(%rbp), %rdx
cmpq %rdx, %r14
jge 0x2d6f
decq %rdx
cltq
movl -0x44(%rbp), %ecx
cmpq %rdx, %r14
jge 0x2cc7
imulq $0x18, %rax, %rcx
movslq 0x10(%r12,%rcx), %rcx
imulq $0x18, %rcx, %rcx
movl (%r12,%rcx), %ecx
imulq $0x18, %rax, %r15
movq -0x38(%rbp), %rax
movl (%rax,%r15), %r12d
subl %r12d, %ecx
cvtsi2sd %ecx, %xmm1
movsd %xmm1, -0x68(%rbp)
divsd -0x70(%rbp), %xmm1
movq -0x38(%rbp), %rax
movq 0x8(%rax,%r15), %rdx
movq -0x58(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
mulsd -0x50(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x60(%rbp), %rax
movl (%rax,%r14,4), %ebx
movq -0x40(%rbp), %rdi
movl -0x30(%rbp), %esi
movl %ebx, %ecx
movl %r13d, %r8d
movq -0x80(%rbp), %r9
callq 0x3097
movq -0x38(%rbp), %rax
movq 0x8(%rax,%r15), %rdi
callq 0x10c0
cvtsi2sd %r12d, %xmm0
movq -0x38(%rbp), %r12
movsd -0x78(%rbp), %xmm2
mulsd %xmm2, %xmm0
movsd -0x68(%rbp), %xmm1
mulsd %xmm2, %xmm1
movslq %r13d, %r13
leaq (,%r13,8), %rsi
addq 0x6420(%rip), %rsi      # 0x9178
movl %ebx, %edi
callq 0x3146
addl %ebx, %r13d
movl 0x10(%r12,%r15), %eax
incq %r14
jmp 0x2c9c
movq -0x58(%rbp), %rdi
callq 0x10c0
movq -0x60(%rbp), %rdi
callq 0x10c0
movslq %r13d, %rax
xorl %ecx, %ecx
movq -0x40(%rbp), %rsi
testl %esi, %esi
cmovlel %ecx, %esi
cmpq %rcx, %rsi
je 0x2da5
movq -0x80(%rbp), %rdx
movq (%rdx,%rcx,8), %rdx
movb $0x0, (%rdx,%rax)
incq %rcx
jmp 0x2d8f
movl %r13d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x33b7(%rip), %rdi      # 0x6178
callq 0x1070
movl -0x2c(%rbp), %ebx
jmp 0x2ae2