// File: /workspace/github_c_makefile_star2_O0/dapogon[P]cellcoal/src/cellcoal.c
// Function: TreeRateSwitch
void TreeRateSwitch (TreeNode *p, long int *seed)
	{
	double		uniform, inTreeLength, cumBranchLength;
	int			i, j, numTipsSwitched, equal;
	TreeNode	**switchNodes;
	char		*concat;
	
	concat = (char *) calloc(300, sizeof(char));
	
	switchNodes = (TreeNode **) calloc(numTreeRateSwitches, sizeof(TreeNode*));
	if (!switchNodes)
		{
		fprintf (stderr, "Could not allocate switchNode (%ld)\n", numTreeRateSwitches * sizeof(TreeNode*));
		exit (-1);
		}
	
	inTreeLength = SumBranches(p) - p->branchLength;
	//fprintf(stderr,"\ninTreeBL = %6.4f;  pBL = %6.4f ; htBL = %6.4f;  total = %6.4f\n", inTreeLength, p->branchLength, healthyTipBranchLength, inTreeLength + p->branchLength + healthyTipBranchLength);
	
	// choose all switches
	for (i=0; i<numTreeRateSwitches; i++)
		{
		uniform = RandomUniform(seed) * inTreeLength;
		cumBranchLength = 0;

		for (j=0; j<numNodes; j++)
			{
			p = treeNodes + j;
			cumBranchLength += p->branchLength;
			if (cumBranchLength >= uniform)
				break;
			}
		
		// avoid switches in the same branch
		equal = NO;
		if (i>0)
			{
			for (j=i; j>=0; j--)
				if (p == switchNodes[j])
					equal = YES;
			}

		if (equal == NO)
			switchNodes[i] = p;
		else
			i--;
		}

	// do all switches
	for (i=0; i<numTreeRateSwitches; i++)
		{
		p = switchNodes[i];
		sprintf (concat, "\n  node %d  *%4.2f  ( ", p->label+1, treeRateSwitchMultiplier[i]);
		strcat (treeRateInfo, concat);
		numTipsSwitched = 0;
		BranchRateSwitch (p, treeRateSwitchMultiplier[i], &numTipsSwitched, seed);
		strcat (treeRateInfo, ")");
		p->rateSwitch = treeRateSwitchMultiplier[i];
		}
	free (switchNodes);
	free (concat);
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x12c, %edi            # imm = 0x12C
movl $0x1, %esi
callq 0x2190
movq %rax, -0x48(%rbp)
movslq 0x407d6(%rip), %rdi     # 0x4e220
movl $0x8, %esi
callq 0x2190
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xda8c
movq 0x40592(%rip), %rax     # 0x4dff8
movq (%rax), %rdi
movslq 0x407b0(%rip), %rdx     # 0x4e220
shlq $0x3, %rdx
leaq 0x33558(%rip), %rsi     # 0x40fd3
movb $0x0, %al
callq 0x21b0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2290
movq -0x8(%rbp), %rdi
callq 0xdc80
movq -0x8(%rbp), %rax
subsd 0x40(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x4076d(%rip), %eax     # 0x4e220
jge 0xdb9c
movq -0x10(%rbp), %rdi
callq 0xa500
mulsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl 0x409f4(%rip), %eax     # 0x4e4d8
jge 0xdb2a
movq 0x41d73(%rip), %rax     # 0x4f860
movslq -0x30(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movsd 0x40(%rax), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
ucomisd -0x18(%rbp), %xmm0
jb 0xdb1d
jmp 0xdb2a
jmp 0xdb1f
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xdadb
movl $0x0, -0x38(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0xdb6b
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jl 0xdb69
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x30(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0xdb5c
movl $0x1, -0x38(%rbp)
jmp 0xdb5e
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xdb3d
jmp 0xdb6b
cmpl $0x0, -0x38(%rbp)
jne 0xdb83
movq -0x8(%rbp), %rdx
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xdb8c
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xdb8e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xdaaa
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x40674(%rip), %eax     # 0x4e220
jge 0xdc64
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
movq -0x48(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %edx
addl $0x1, %edx
movq 0x41d49(%rip), %rax     # 0x4f920
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x33411(%rip), %rsi     # 0x40ff8
movb $0x1, %al
callq 0x2280
movq 0x41b83(%rip), %rdi     # 0x4f778
movq -0x48(%rbp), %rsi
callq 0x2270
movl $0x0, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movq 0x41d10(%rip), %rax     # 0x4f920
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rdx
leaq -0x34(%rbp), %rsi
callq 0x1a280
movq 0x41b47(%rip), %rdi     # 0x4f778
leaq 0x350bd(%rip), %rsi     # 0x42cf5
callq 0x2270
movq 0x41cdc(%rip), %rax     # 0x4f920
movslq -0x2c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x50(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xdba3
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x48(%rbp), %rdi
callq 0x2030
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)