// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movapd %xmm0, %xmm1
movq %rdi, %r14
cmpl $0x4, 0x1614(%rdi)
jb 0x9baa0
movq 0xda580(%rip), %r15     # 0x175fa8
leaq 0x64b9f(%rip), %rsi     # 0x1005ce
movl $0xe, %edx
movq %r15, %rdi
movsd %xmm1, (%rsp)
callq 0x22ab0
xorpd %xmm0, %xmm0
movq %r15, %rdi
callq 0x23060
movq %rax, %r15
leaq 0x64b86(%rip), %rsi     # 0x1005dd
movl $0x10, %edx
movq %rax, %rdi
callq 0x22ab0
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x23060
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r15, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movsd (%rsp), %xmm1
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x9bbc2
cmpl $0x4, 0x1614(%r14)
jb 0x9bb3a
movq 0xda4e6(%rip), %r12     # 0x175fa8
leaq 0x64b05(%rip), %rsi     # 0x1005ce
movl $0xe, %edx
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x22ab0
xorpd %xmm0, %xmm0
movq %r12, %rdi
callq 0x23060
movq %rax, %r12
leaq 0x64aec(%rip), %rsi     # 0x1005dd
movl $0x10, %edx
movq %rax, %rdi
callq 0x22ab0
movq %r12, %rdi
movsd (%rsp), %xmm0
callq 0x23060
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r12, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movsd (%rsp), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x9bbd2
addq $0x58, %r14
movsd %xmm0, (%rsp)
movq %r14, %rdi
callq 0xb00ba
movq 0x18(%rbx), %r14
addq $0x8, %rbx
cmpq %r14, %rbx
je 0x9bbc2
movl %ebp, %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rax, %rcx
shrq $0x23, %rcx
movl $0x1, %eax
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
xorl %r15d, %r15d
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rdi
callq 0x22a10
movsd 0x8(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd 0x20(%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x9bbc2
movq %rax, %rbx
movsd %xmm1, (%rsp)
incl %r15d
cmpq %r14, %rax
jne 0x9bb91
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x664a6(%rip), %rdi     # 0x10207f
leaq 0x664b1(%rip), %rsi     # 0x102091
leaq 0x664e2(%rip), %rcx     # 0x1020c9
movl $0x9f, %edx
callq 0x22610
nop