// File: /workspace/github_cpp_makefile_star2_O2/samuel500[P]NEAT/species.cpp
// Function: Species::evolve(std::vector<Node*, std::allocator<Node*>>*, std::vector<Connection*, std::allocator<Connection*>>*)
vector<Individual*> Species::evolve(vector<Node*> *all_nodes, vector<Connection*> *all_connections){

	vector<Individual*> new_individuals;

	vector<Node*>::iterator nodePtr;
	vector<Connection*>::iterator conPtr;


	// percentage of species members whose descendants go on to the next generation
	// probably should be set somewhere else
	double p_mating = 0.5; 

	sort(members.begin(), members.end(), indPtr_compare);


	Individual *new_individual;

	// cout << "ev1" << endl;

	while(new_individuals.size() < n_offspring){

		int n_parent = randint((int)(p_mating*members.size()), members.size()-1);


		new_individual = new Individual(*members[n_parent]);


		// cout << "ev2" << endl;

		new_individual->mutate(all_nodes, all_connections);
		// cout << "ev3" << endl;

		for(conPtr=new_individual->connections.begin(); conPtr!=new_individual->connections.end(); ++conPtr){
			all_connections->push_back((*conPtr));
		}

		for(nodePtr=new_individual->nodes.begin(); nodePtr!=new_individual->nodes.end(); ++nodePtr){
			all_nodes->push_back((*nodePtr));
		}
		// cout << "ev4" << endl;

		new_individuals.push_back(new_individual);


		if(n_offspring > 5 && new_individuals.size()<n_offspring){
			new_individual = new Individual(*members[members.size()-1]);
			new_individuals.push_back(new_individual);

			for(conPtr=new_individual->connections.begin(); conPtr!=new_individual->connections.end(); ++conPtr){
				all_connections->push_back((*conPtr));
			}
			for(nodePtr=new_individual->nodes.begin(); nodePtr!=new_individual->nodes.end(); ++nodePtr){
				all_nodes->push_back((*nodePtr));
			}
			// cout << "ev5" << endl;

		}
	}


	// rep = members[members.size()-1];

	vector<Individual*>::iterator indPtr;

	// for(indPtr=members.begin(); indPtr!=prev(members.end()); ++indPtr){
	// 	delete *indPtr;
	// }

	// members.clear();

	return new_individuals;


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
andq $0x0, 0x10(%rdi)
xorpd %xmm0, %xmm0
movq %rdi, -0x38(%rbp)
movupd %xmm0, (%rdi)
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
leaq -0x707(%rip), %rdx      # 0x3cf4
callq 0x2ced
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
movslq 0x40(%r12), %rcx
cmpq %rcx, %rax
jae 0x457d
movq 0x8(%r12), %rsi
subq (%r12), %rsi
sarq $0x3, %rsi
movq %rsi, %xmm0
punpckldq 0x2c49(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2c51(%rip), %xmm0     # 0x7090
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x2d85(%rip), %xmm1     # 0x71d8
cvttsd2si %xmm1, %edi
decl %esi
callq 0x46bd
movl %eax, %ebx
movl $0x98, %edi
callq 0x10f0
movq %rax, %r13
movslq %ebx, %rax
movq (%r12), %rcx
movq (%rcx,%rax,8), %rsi
movq %r13, %rdi
callq 0x54dc
movq %r13, -0x30(%rbp)
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x58e8
movq -0x30(%rbp), %rax
movq 0x60(%rax), %r13
cmpq 0x68(%rax), %r13
je 0x44b5
movq %r14, %rdi
movq %r13, %rsi
callq 0x2248
addq $0x8, %r13
movq -0x30(%rbp), %rax
jmp 0x449a
movq 0x18(%rax), %r13
cmpq 0x20(%rax), %r13
je 0x44d4
movq %r15, %rdi
movq %r13, %rsi
callq 0x226c
addq $0x8, %r13
movq -0x30(%rbp), %rax
jmp 0x44b9
movq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x231e
movslq 0x40(%r12), %rax
cmpq $0x6, %rax
jl 0x4400
movq -0x38(%rbp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
jae 0x4400
movl $0x98, %edi
callq 0x10f0
movq %rax, %r13
movq 0x8(%r12), %rax
movq -0x8(%rax), %rsi
movq %r13, %rdi
callq 0x54dc
movq %r13, -0x30(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x231e
movq -0x30(%rbp), %rax
movq 0x60(%rax), %r13
cmpq 0x68(%rax), %r13
je 0x455a
movq %r14, %rdi
movq %r13, %rsi
callq 0x2248
addq $0x8, %r13
movq -0x30(%rbp), %rax
jmp 0x453f
movq 0x18(%rax), %r13
cmpq 0x20(%rax), %r13
je 0x4400
movq %r15, %rdi
movq %r13, %rsi
callq 0x226c
addq $0x8, %r13
movq -0x30(%rbp), %rax
jmp 0x455e
movq -0x38(%rbp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x45ae
jmp 0x4594
movq %rax, %r14
movl $0x98, %esi
movq %r13, %rdi
callq 0x1100
jmp 0x45b1
jmp 0x45ae
jmp 0x45ae
jmp 0x45ae
jmp 0x45ae
movq %rax, %r14
movq -0x38(%rbp), %rdi
callq 0x2508
movq %r14, %rdi
callq 0x1180