// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::pickWeighted(double, std::multimap<double, unsigned int, std::less<double>, std::allocator<std::pair<double const, unsigned int>>>&, unsigned int)
unsigned int Swarm::pickWeighted(double weightSum, multimap<double, unsigned int> &weights, unsigned int extraWeight) {
	double lowerBound = 0;
	double upperBound = weightSum;
	if(options.verbosity>=4){
		cout << "lower limit = " << lowerBound << "; upper limit = " << upperBound << endl;
	}
	// TODO: Better error handling here?
	if (upperBound <= 0) {
		return 0;
	}

	if(options.verbosity>=4){

		cout << "lower limit = " << lowerBound << "; upper limit = " << upperBound << endl;
	}
	boost::random::uniform_real_distribution<double> unif(lowerBound, upperBound);

	double random = unif(generalRand);
	double chosen = random * ( 1 - (extraWeight / 10 ));

	//cout << "chosen: " << chosen << endl;

	double currentSum = 0;
	unsigned int indexCounter = 0;
	for (multimap<double, unsigned int>::reverse_iterator w = weights.rbegin(); w != weights.rend(); ++w) {
		currentSum += w->first;
		//cout << "adding " << w->first << endl;

		if (currentSum >= chosen) {
			return indexCounter;
		}
		++indexCounter;
	}
	//cout << "fell off the end" << endl;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
cmpl $0x4, 0x1614(%rax)
jb 0x10eec9
movq 0x1c60cb(%rip), %rdi    # 0x2d4f50
leaq 0xd433a(%rip), %rsi     # 0x1e31c6
callq 0x2ff50
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x30710
movq %rax, %rdi
leaq 0xd432d(%rip), %rsi     # 0x1e31d5
callq 0x2ff50
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x30710
movq %rax, %rdi
movq 0x1c6104(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
xorps %xmm0, %xmm0
ucomisd -0x38(%rbp), %xmm0
jb 0x10eedf
movl $0x0, -0x4(%rbp)
jmp 0x10f018
movq -0x80(%rbp), %rax
cmpl $0x4, 0x1614(%rax)
jb 0x10ef37
movq 0x1c605d(%rip), %rdi    # 0x2d4f50
leaq 0xd42cc(%rip), %rsi     # 0x1e31c6
callq 0x2ff50
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x30710
movq %rax, %rdi
leaq 0xd42bf(%rip), %rsi     # 0x1e31d5
callq 0x2ff50
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x30710
movq %rax, %rdi
movq 0x1c6096(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
leaq -0x48(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0x123940
movq -0x80(%rbp), %rsi
movq -0x88(%rbp), %rdi
addq $0x58, %rsi
callq 0x1239b0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movl -0x24(%rbp), %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x23, %rax
movl %eax, %ecx
movl $0x1, %eax
subl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x1239e0
movq -0x20(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x123a40
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x123a10
testb $0x1, %al
jne 0x10efd2
jmp 0x10f011
leaq -0x70(%rbp), %rdi
callq 0x123a70
movsd (%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
ucomisd -0x58(%rbp), %xmm0
jb 0x10effd
movl -0x64(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x10f018
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x123ab0
jmp 0x10efb2
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)