// File: /workspace/github_c_makefile_star2_O0/dapogon[P]cellcoal/src/cellcoal.c
// Function: SimulateISM
void SimulateISM (TreeNode *p, int genome, int doISMhaploid, long int *seed)
    {
    int		i, trials, numMutations, mutationsSoFar;
    double	totalBranchSum;
    int		*modelSites, numModelSites;

	if (doISMhaploid == NO)
		{
		modelSites = DefaultModelSites;
		numModelSites = numDefaultModelSites;
		}
	else
 		{
		modelSites = AltModelSites;
		numModelSites = numAltModelSites;
		}


	totalBranchSum = totalTreeLength * numModelSites;

    if (doSimulateFixedNumMutations == YES) /* conditioned on a specific number of mutations */
        {
        if (genome == MATERNAL)
			 numMutations = numSNVmaternal =  RandomBinomial (0.5, numFixedMutations, seed);
        else
            numMutations = numFixedMutations - numSNVmaternal;
        }
    else
        numMutations = RandomPoisson (totalBranchSum, seed); /* the number of mutations will be distributed as a Poisson with parameter totalBranchSum */
		
     /* if the number of ISM mutations is bigger than the number of ISM sites quit the program and warn the user about violation of ISM */
     if (doISMhaploid == NO && numISMmutations + numMutations > (numModelSites))
        {
        fprintf (stderr, "\n\nERROR: The diploid infinite sites model (ISM) has been violated. There will be");
        fprintf (stderr, "\nmore mutations (%d existing + %d proposed]) than available sites under this model (%d).", numISMmutations, numMutations, numDefaultModelSites);
        fprintf (stderr, "\nTry using a smaller mutation rate or effective population size");
        fprintf (stderr, "\n(i.e. smaller theta) or check the proportion of JC sites.");
        fprintf (stderr, "\nIf using an user tree, try with smaller branch lengths (including the transforming and healthy branches).\n");
        exit (-1);
        }
    else if (doISMhaploid == YES && numISMmutations + numMutations > (2*numModelSites))
        {
        fprintf (stderr, "\n\nERROR: The haploid infinite sites model (ISM) has been violated. There will be");
        fprintf (stderr, "\nmore mutations (%d existing + %d proposed]) than available sites under this model (%d).", numISMmutations, numMutations, 2*numModelSites);
        fprintf (stderr, "\nTry using a smaller mutation rate or effective population size");
        fprintf (stderr, "\n(i.e. smaller theta) or check the proportion of JC sites.");
        fprintf (stderr, "\nIf using an user tree, try with smaller branch lengths (including the transforming and healthy branches).\n");
        exit (-1);
        }
		
    numISMmutations += numMutations;
    trials = 0;
    mutationsSoFar = 0;
    while (mutationsSoFar < numMutations)
        {
        i = RandomUniformTo(numModelSites, seed);  /* choose a site at random */
        while (((doISMhaploid == NO)  && (allSites[modelSites[i]].numMutations != 0))  ||
               ((doISMhaploid == YES) && (genome == MATERNAL) && (allSites[modelSites[i]].numMutationsMaternal != 0)) ||
               ((doISMhaploid == YES) && (genome == PATERNAL) && (allSites[modelSites[i]].numMutationsPaternal != 0)))
            {
            i = RandomUniformTo(numModelSites, seed);
            if (trials++ > 100*numModelSites)
                {
                fprintf (stderr, "\n\n ERROR: after %d trials cannot find an unmuted site",100*numModelSites);
                fprintf (stderr, "\nmutations = %d   mutations so far = %d\n\n",numMutations, mutationsSoFar);
                for (i=0; i<numModelSites; i++)
                    fprintf (stderr, "\nsite %d has %d mutations",modelSites[i]+1, allSites[modelSites[i]].numMutations);
                exit(-1);
                }
            }
        if (alphabet == DNA)
            SimulateISMDNAforSite (p, genome, modelSites[i], doISMhaploid, seed);
        else
            SimulateISMforSite (p, genome, modelSites[i], doISMhaploid, seed);
			
        mutationsSoFar++;
			
        #ifdef MYDEBUG
            fprintf (stderr, "\nmutations = %d   mutations so far = %d\n",numMutations, mutationsSoFar);
            if (allSites[modelSites[i]].numMutations > 1)
                {
                fprintf (stderr, "\n\n ERROR: %d mutations in site %d",allSites[modelSites[i]].numMutations, modelSites[i]+1);
                exit(-1);
                }
            for (i=0; i<numDefaultModelSites; i++)
                fprintf (stderr, "%2d[%d] ",modelSites[i]+1, allSites[modelSites[i]].numMutations);
        #endif
			
        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x1aac2
movq 0x34d0d(%rip), %rax     # 0x4f7c0
movq %rax, -0x38(%rbp)
movl 0x34f0b(%rip), %eax     # 0x4f9c8
movl %eax, -0x3c(%rbp)
jmp 0x1aad6
movq 0x34cff(%rip), %rax     # 0x4f7c8
movq %rax, -0x38(%rbp)
movl 0x34ef9(%rip), %eax     # 0x4f9cc
movl %eax, -0x3c(%rbp)
movsd 0x34caa(%rip), %xmm0    # 0x4f788
cvtsi2sdl -0x3c(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
cmpl $0x1, 0x338cd(%rip)     # 0x4e3c0
jne 0x1ab2e
cmpl $0x0, -0xc(%rbp)
jne 0x1ab1d
movl 0x34ecf(%rip), %edi     # 0x4f9d0
movq -0x18(%rbp), %rsi
movsd 0x24503(%rip), %xmm0    # 0x3f010
callq 0x1b310
movl %eax, 0x34c30(%rip)     # 0x4f748
movl %eax, -0x24(%rbp)
jmp 0x1ab2c
movl 0x34ead(%rip), %eax     # 0x4f9d0
subl 0x34c1f(%rip), %eax     # 0x4f748
movl %eax, -0x24(%rbp)
jmp 0x1ab3f
movsd -0x30(%rbp), %xmm0
movq -0x18(%rbp), %rdi
callq 0x1b370
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x1abed
movl 0x34c81(%rip), %eax     # 0x4f7d0
addl -0x24(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jle 0x1abed
movq 0x33496(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x26590(%rip), %rsi     # 0x410fc
movb $0x0, %al
callq 0x21b0
movq 0x3347e(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
movl 0x34c4d(%rip), %edx     # 0x4f7d0
movl -0x24(%rbp), %ecx
movl 0x34e3b(%rip), %r8d     # 0x4f9c8
leaq 0x265b9(%rip), %rsi     # 0x4114d
movb $0x0, %al
callq 0x21b0
movq 0x33456(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x265fa(%rip), %rsi     # 0x411a6
movb $0x0, %al
callq 0x21b0
movq 0x3343e(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x26622(%rip), %rsi     # 0x411e6
movb $0x0, %al
callq 0x21b0
movq 0x33426(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x26645(%rip), %rsi     # 0x41221
movb $0x0, %al
callq 0x21b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2290
cmpl $0x1, -0x10(%rbp)
jne 0x1ac9f
movl 0x34bd3(%rip), %eax     # 0x4f7d0
addl -0x24(%rbp), %eax
movl -0x3c(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jle 0x1ac9f
movq 0x333e4(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x2678b(%rip), %rsi     # 0x413a9
movb $0x0, %al
callq 0x21b0
movq 0x333cc(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
movl 0x34b9b(%rip), %edx     # 0x4f7d0
movl -0x24(%rbp), %ecx
movl -0x3c(%rbp), %r8d
shll %r8d
leaq 0x26507(%rip), %rsi     # 0x4114d
movb $0x0, %al
callq 0x21b0
movq 0x333a4(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x26548(%rip), %rsi     # 0x411a6
movb $0x0, %al
callq 0x21b0
movq 0x3338c(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x26570(%rip), %rsi     # 0x411e6
movb $0x0, %al
callq 0x21b0
movq 0x33374(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
leaq 0x26593(%rip), %rsi     # 0x41221
movb $0x0, %al
callq 0x21b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2290
jmp 0x1aca1
movl -0x24(%rbp), %eax
addl 0x34b26(%rip), %eax     # 0x4f7d0
movl %eax, 0x34b20(%rip)     # 0x4f7d0
movl $0x0, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1aeae
movl -0x3c(%rbp), %edi
movq -0x18(%rbp), %rsi
callq 0x1ba30
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x1ad07
movq 0x34ac2(%rip), %rcx     # 0x4f7a8
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rdx
movslq (%rax,%rdx,4), %rax
imulq $0x88, %rax, %rax
addq %rax, %rcx
movb $0x1, %al
cmpl $0x0, 0xc(%rcx)
movb %al, -0x3d(%rbp)
jne 0x1ad7e
cmpl $0x1, -0x10(%rbp)
jne 0x1ad3b
cmpl $0x0, -0xc(%rbp)
jne 0x1ad3b
movq 0x34a8e(%rip), %rcx     # 0x4f7a8
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rdx
movslq (%rax,%rdx,4), %rax
imulq $0x88, %rax, %rax
addq %rax, %rcx
movb $0x1, %al
cmpl $0x0, 0x10(%rcx)
movb %al, -0x3d(%rbp)
jne 0x1ad7e
xorl %eax, %eax
cmpl $0x1, -0x10(%rbp)
movb %al, -0x3e(%rbp)
jne 0x1ad78
xorl %eax, %eax
cmpl $0x1, -0xc(%rbp)
movb %al, -0x3e(%rbp)
jne 0x1ad78
movq 0x34a50(%rip), %rax     # 0x4f7a8
movq -0x38(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x88, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x14(%rax)
setne %al
movb %al, -0x3e(%rbp)
movb -0x3e(%rbp), %al
movb %al, -0x3d(%rbp)
movb -0x3d(%rbp), %al
testb $0x1, %al
jne 0x1ad8a
jmp 0x1ae59
movl -0x3c(%rbp), %edi
movq -0x18(%rbp), %rsi
callq 0x1ba30
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x20(%rbp)
imull $0x64, -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jle 0x1ae54
movq 0x33241(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
imull $0x64, -0x3c(%rbp), %edx
leaq 0x26635(%rip), %rsi     # 0x413fa
movb $0x0, %al
callq 0x21b0
movq 0x33225(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
movl -0x28(%rbp), %ecx
leaq 0x2664d(%rip), %rsi     # 0x41430
movb $0x0, %al
callq 0x21b0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x1ae4a
movq 0x331f8(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movq 0x34990(%rip), %rax     # 0x4f7a8
movq -0x38(%rbp), %rcx
movslq -0x1c(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
imulq $0x88, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
leaq 0x26622(%rip), %rsi     # 0x4145a
movb $0x0, %al
callq 0x21b0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1adf1
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2290
jmp 0x1acd9
cmpl $0x1, 0x333c4(%rip)     # 0x4e224
jne 0x1ae82
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x10(%rbp), %ecx
movq -0x18(%rbp), %r8
callq 0x1bb50
jmp 0x1aea0
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x38(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x10(%rbp), %ecx
movq -0x18(%rbp), %r8
callq 0x1bdf0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1acbe
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)