// File: 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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0xd0(%rbp)
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movb $0x0, -0x21(%rbp)
callq 0x3930
leaq -0x30(%rbp), %rdi
callq 0x39b0
leaq -0x38(%rbp), %rdi
callq 0x1800
movq -0xc0(%rbp), %rdi
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
movq %rax, -0x40(%rbp)
callq 0x4210
movq -0xc0(%rbp), %rdi
movq %rax, -0x48(%rbp)
callq 0x4280
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
leaq -0x106f(%rip), %rdx     # 0xc7b0
callq 0x4310
jmp 0xd826
jmp 0xd828
movq -0xd0(%rbp), %rdi
callq 0x17e0
movq -0xc0(%rbp), %rcx
movslq 0x40(%rcx), %rcx
cmpq %rcx, %rax
jae 0xdbfa
movq -0xc0(%rbp), %rdi
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
callq 0x17e0
movsd -0xe0(%rbp), %xmm0
movq -0xc0(%rbp), %rdi
movq %rax, %xmm1
movaps 0x7804(%rip), %xmm2     # 0x15080
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x7808(%rip), %xmm2     # 0x15090
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xd8(%rbp)
callq 0x17e0
movl -0xd8(%rbp), %edi
movl %eax, %esi
decl %esi
callq 0xe160
movl %eax, -0xd4(%rbp)
jmp 0xd8c1
movl -0xd4(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl $0x98, %edi
callq 0x10e0
movq %rax, -0xe8(%rbp)
jmp 0xd8dd
movq -0xc0(%rbp), %rdi
movslq -0x6c(%rbp), %rsi
callq 0x4360
movq -0xe8(%rbp), %rdi
movq (%rax), %rsi
callq 0x11610
jmp 0xd8fe
movq -0xe8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x11f50
jmp 0xd91c
movq -0x68(%rbp), %rdi
addq $0x60, %rdi
callq 0x1820
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rdi
addq $0x60, %rdi
callq 0x1890
movq %rax, -0x80(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1850
testb $0x1, %al
jne 0xd959
jmp 0xd9be
movq -0x20(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x18c0
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
callq 0x39d0
jmp 0xd97e
jmp 0xd980
leaq -0x38(%rbp), %rdi
callq 0x18e0
jmp 0xd935
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xdc2c
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0x98, %esi
callq 0x10f0
jmp 0xdc2c
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x3a40
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x3ab0
movq %rax, -0x90(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3a70
testb $0x1, %al
jne 0xda07
jmp 0xda39
movq -0x18(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3b50
movq -0xf8(%rbp), %rdi
movq %rax, %rsi
callq 0x3ae0
jmp 0xda2c
jmp 0xda2e
leaq -0x30(%rbp), %rdi
callq 0x3b70
jmp 0xd9dd
movq -0xd0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3da0
jmp 0xda4b
movq -0xc0(%rbp), %rax
cmpl $0x5, 0x40(%rax)
jle 0xdbf5
movq -0xd0(%rbp), %rdi
callq 0x17e0
movq -0xc0(%rbp), %rcx
movslq 0x40(%rcx), %rcx
cmpq %rcx, %rax
jae 0xdbf5
movl $0x98, %edi
callq 0x10e0
movq %rax, -0x100(%rbp)
jmp 0xda8f
movq -0xc0(%rbp), %rdi
callq 0x17e0
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x4360
movq -0x100(%rbp), %rdi
movq (%rax), %rsi
callq 0x11610
jmp 0xdabe
movq -0xd0(%rbp), %rdi
movq -0x100(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x3da0
jmp 0xdadb
movq -0x68(%rbp), %rdi
addq $0x60, %rdi
callq 0x1820
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rdi
addq $0x60, %rdi
callq 0x1890
movq %rax, -0xa0(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1850
testb $0x1, %al
jne 0xdb24
jmp 0xdb78
movq -0x20(%rbp), %rax
movq %rax, -0x108(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x18c0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x39d0
jmp 0xdb49
jmp 0xdb4b
leaq -0x38(%rbp), %rdi
callq 0x18e0
jmp 0xdafa
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0x98, %esi
callq 0x10f0
jmp 0xdc2c
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x3a40
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
callq 0x3ab0
movq %rax, -0xb0(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3a70
testb $0x1, %al
jne 0xdbc1
jmp 0xdbf3
movq -0x18(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3b50
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x3ae0
jmp 0xdbe6
jmp 0xdbe8
leaq -0x30(%rbp), %rdi
callq 0x3b70
jmp 0xdb97
jmp 0xdbf5
jmp 0xd828
leaq -0xb8(%rbp), %rdi
callq 0x41f0
movb $0x1, -0x21(%rbp)
testb $0x1, -0x21(%rbp)
jne 0xdc1c
movq -0xd0(%rbp), %rdi
callq 0x3f30
movq -0xc8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x3f30
movq -0x58(%rbp), %rdi
callq 0x1180
nopw %cs:(%rax,%rax)