// File: /workspace/github_cpp_makefile_star2_O1/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 0x9c568
movq 0xd9aa0(%rip), %r15     # 0x175f90
leaq 0x640f0(%rip), %rsi     # 0x1005e7
movl $0xe, %edx
movq %r15, %rdi
movsd %xmm1, (%rsp)
callq 0x22ab0
xorpd %xmm0, %xmm0
movq %r15, %rdi
callq 0x23060
movq %rax, %r15
leaq 0x640d7(%rip), %rsi     # 0x1005f6
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 0x9c68a
cmpl $0x4, 0x1614(%r14)
jb 0x9c602
movq 0xd9a06(%rip), %r12     # 0x175f90
leaq 0x64056(%rip), %rsi     # 0x1005e7
movl $0xe, %edx
movq %r12, %rdi
movsd %xmm1, (%rsp)
callq 0x22ab0
xorpd %xmm0, %xmm0
movq %r12, %rdi
callq 0x23060
movq %rax, %r12
leaq 0x6403d(%rip), %rsi     # 0x1005f6
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 0x9c69a
addq $0x58, %r14
movsd %xmm0, (%rsp)
movq %r14, %rdi
callq 0xb093a
movq 0x18(%rbx), %r14
addq $0x8, %rbx
cmpq %r14, %rbx
je 0x9c68a
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 0x9c68a
movq %rax, %rbx
movsd %xmm1, (%rsp)
incl %r15d
cmpq %r14, %rax
jne 0x9c659
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x659f7(%rip), %rdi     # 0x102098
leaq 0x65a02(%rip), %rsi     # 0x1020aa
leaq 0x65a33(%rip), %rcx     # 0x1020e2
movl $0x9f, %edx
callq 0x22610
nop