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

	Node *in_node;
	Node *out_node;


	int i = 0; // counter in case no new connection is possible
	bool duplicate = false;

	vector<Connection*>::iterator conPtr;


	do{

		int in_node_id = randint(0, nodes.size()-(*out_size)-1);
		// cout << "ma1" << endl;

		in_node = nodes[in_node_id];
		// cout << "ma2" << endl;

		int out_node_id;

		do{

			out_node_id = randint(*in_size, nodes.size()-1);
			i++;

		}while(in_node_id==out_node_id);

		out_node = nodes[out_node_id];

		duplicate = false;

		for(conPtr=out_node->in_connections.begin(); conPtr != out_node->in_connections.end(); ++conPtr){

			if((*conPtr)->in_node==in_node) duplicate = true; // proposed connection already exists

		}
			// cout << "ma3" << endl;

		for(conPtr=out_node->out_connections.begin(); conPtr != out_node->out_connections.end(); ++conPtr){

			if((*conPtr)->out_node==in_node) duplicate = true; // proposed opposite connection already exists, prevents simple loops

		}


	}while(i<20 && duplicate==true);

	if(duplicate==true) return false; // couldn't find any new possible connection


	int new_innov_number;

	bool found = false;


	for(conPtr=all_connections->begin(); conPtr != all_connections->end(); ++conPtr){
		// give identical mutations same hist_marking
		// could also check for mutation hist gen? 
		// (https://www.cs.ucf.edu/~kstanley/neat.html#FAQ1  - see "record of innovation" Q
		if((*conPtr)->in_node->hist_marking==in_node->hist_marking && (*conPtr)->out_node->hist_marking==out_node->hist_marking){
			new_innov_number = (*conPtr)->hist_marking;
			found = true;
			break;
		}

	}

	if(!found){
		new_innov_number = *innov_num;
		(*innov_num)++;
	}

	Connection *new_connection = new Connection(in_node, out_node, new_innov_number, *generation);

	connections.push_back(new_connection);
	// (*all_connections).push_back(new_connection);
	// cout << "ma3" << endl;

	in_node->out_connections.push_back(new_connection);
	out_node->in_connections.push_back(new_connection);

	return true;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, -0x30(%rbp)
movq %rdi, %rbx
movq 0x18(%rdi), %rax
xorl %r12d, %r12d
movq 0x20(%rbx), %r14
subq %rax, %r14
movq 0x88(%rbx), %rax
shrq $0x3, %r14
subl (%rax), %r14d
callq 0x1030
cltd
idivl %r14d
movl %edx, %r15d
movslq %edx, %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %r14
movq 0x20(%rbx), %rsi
movq 0x80(%rbx), %rax
movl (%rax), %edi
subq 0x18(%rbx), %rsi
shrq $0x3, %rsi
decl %esi
callq 0x46bd
incl %r12d
cmpl %eax, %r15d
je 0x5c6f
movslq %eax, %rcx
movq 0x18(%rbx), %rax
movq (%rax,%rcx,8), %r13
movq 0x18(%r13), %rdx
movq 0x20(%r13), %rsi
leal -0x1(%r12), %ecx
xorl %r15d, %r15d
cmpq %rsi, %rdx
je 0x5cc6
movq (%rdx), %rdi
cmpq %r14, (%rdi)
sete %dil
orb %dil, %r15b
addq $0x8, %rdx
jmp 0x5cae
movq 0x30(%r13), %rdx
movq 0x38(%r13), %rsi
cmpq %rsi, %rdx
je 0x5ce7
movq (%rdx), %rdi
cmpq %r14, 0x8(%rdi)
sete %dil
orb %dil, %r15b
addq $0x8, %rdx
jmp 0x5cce
cmpl $0x13, %ecx
setl %cl
testb %r15b, %cl
jne 0x5c43
testb $0x1, %r15b
jne 0x5daa
leaq 0x18(%r13), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
je 0x5d3a
movq (%rax), %rdx
movq (%rdx), %rsi
movl 0x4(%rsi), %esi
cmpl 0x4(%r14), %esi
jne 0x5d34
movq 0x8(%rdx), %rsi
movl 0x4(%rsi), %esi
cmpl 0x4(%r13), %esi
je 0x5d4a
addq $0x8, %rax
jmp 0x5d13
movq 0x78(%rbx), %rax
movl (%rax), %ecx
movq %rcx, -0x30(%rbp)
incl %ecx
movl %ecx, (%rax)
jmp 0x5d51
movl 0x1c(%rdx), %eax
movq %rax, -0x30(%rbp)
pushq $0x28
popq %rdi
callq 0x10f0
movq %rax, %r12
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq -0x30(%rbp), %rcx
callq 0x6216
leaq -0x40(%rbp), %r13
movq %r12, (%r13)
addq $0x60, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2248
addq $0x30, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2248
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x2248
notb %r15b
andb $0x1, %r15b
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x1180
nop