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

	bool found_active = false;
	int rand_connection_id;
	for(int i=0; i<15; i++){
		rand_connection_id = randint(0, connections.size()-1);
		if(connections[rand_connection_id]->is_active){
			found_active = true;
			break;
		}
	}
	if(!found_active){ 
		cout << "no active connection found"<< endl;
		return false;


	}

	Connection *chosen_con = connections[rand_connection_id];
	Node *init_node = chosen_con->in_node;
	Node *end_node = chosen_con->out_node;

	chosen_con->is_active = false;


	bool found = false;
	int new_node_innov_number, new_in_connection_innov_number, new_out_connection_innov_number;


	vector<Node*>::iterator nodePtr;

	for(nodePtr=all_nodes->begin(); nodePtr != all_nodes->end(); ++nodePtr){
		// this will only find those nodes that haven't gotten any new connections since added
		// (Stanley's implementation only looks for nodes mutated in the current generation)
		if((*nodePtr)->in_connections.size()==1 && (*nodePtr)->out_connections.size()==1){

			if((*nodePtr)->in_connections[0]->in_node->hist_marking==init_node->hist_marking && (*nodePtr)->out_connections[0]->out_node->hist_marking==end_node->hist_marking){
				found = true;
				new_node_innov_number = (*nodePtr)->hist_marking;
				new_in_connection_innov_number = (*nodePtr)->in_connections[0]->hist_marking;
				new_out_connection_innov_number = (*nodePtr)->out_connections[0]->hist_marking;
			}

		}

	}

	if(!found){
		new_node_innov_number = *innov_num;
		(*innov_num)++;
		new_in_connection_innov_number = *innov_num;
		(*innov_num)++;
		new_out_connection_innov_number = *innov_num;
		(*innov_num)++;
	}


	Node *new_node = new Node(HIDDEN, new_node_innov_number, *generation);
	
	Connection *new_in_connection = new Connection(init_node, new_node, new_in_connection_innov_number, *generation);
	Connection *new_out_connection = new Connection(new_node, end_node, new_out_connection_innov_number, *generation);
	
	new_in_connection->weight = 1.0;
	new_out_connection->weight = chosen_con->weight;


	connections.push_back(new_in_connection);
	connections.push_back(new_out_connection);

	// all_connections->push_back(new_in_connection);
	// all_connections->push_back(new_out_connection);

	nodes.insert(nodes.begin()+(nodes.size()-(*out_size)), new_node); // add before the output nodes
	// all_nodes->push_back(new_node);

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r13
movq %rdi, %rbx
movq 0x68(%rdi), %r14
subq 0x60(%rdi), %r14
shrq $0x3, %r14
callq 0x1040
cltd
idivl %r14d
movslq %edx, %rax
movq 0x60(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movb $0x1, %r14b
cmpb $0x0, 0x18(%rax)
jne 0x5ee4
xorl %eax, %eax
movl %eax, %r14d
cmpl $0xe, %eax
je 0x5edc
movq 0x68(%rbx), %r15
subq 0x60(%rbx), %r15
shrq $0x3, %r15
callq 0x1040
cltd
idivl %r15d
movslq %edx, %rax
movq 0x60(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
leal 0x1(%r14), %eax
cmpb $0x1, 0x18(%rcx)
jne 0x5eaa
cmpl $0xe, %r14d
setb %r14b
testb %r14b, %r14b
je 0x5f7a
movslq %edx, %rax
movq 0x60(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq (%rax), %r12
movq 0x8(%rax), %r10
movq %rax, -0x50(%rbp)
movb $0x0, 0x18(%rax)
movq (%r13), %rcx
movq 0x8(%r13), %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
je 0x5fc1
movq (%rcx), %rsi
movq 0x18(%rsi), %rdi
movq 0x20(%rsi), %r8
subq %rdi, %r8
cmpq $0x8, %r8
jne 0x5f6f
movq 0x30(%rsi), %r8
movq 0x38(%rsi), %r9
subq %r8, %r9
cmpq $0x8, %r9
jne 0x5f6f
movq (%rdi), %rdi
movq (%rdi), %r9
movl 0x4(%r9), %r9d
cmpl 0x4(%r12), %r9d
jne 0x5f6f
movq (%r8), %r8
movq 0x8(%r8), %r9
movl 0x4(%r9), %r9d
cmpl 0x4(%r10), %r9d
jne 0x5f6f
movl 0x4(%rsi), %r13d
movl 0x1c(%rdi), %r15d
movl 0x1c(%r8), %r11d
movb $0x1, %al
addq $0x8, %rcx
cmpq %rdx, %rcx
jne 0x5f1a
jmp 0x5fc1
movq 0x4057(%rip), %rbx      # 0x9fd8
leaq 0x12a3(%rip), %rsi      # 0x722b
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x1120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x10a0
jmp 0x6105
movq %r10, -0x58(%rbp)
movb %r14b, -0x29(%rbp)
testb $0x1, %al
jne 0x5fe2
movq 0x78(%rbx), %rax
movl (%rax), %r13d
leal 0x1(%r13), %r15d
leal 0x2(%r13), %r11d
leal 0x3(%r13), %ecx
movl %ecx, (%rax)
movl %r11d, -0x30(%rbp)
movl $0x48, %edi
callq 0x1100
movq %rax, %r14
movq 0x90(%rbx), %rax
movl (%rax), %ecx
movq %r14, %rdi
movl $0x2, %esi
movl %r13d, %edx
callq 0x664e
movq %r14, -0x48(%rbp)
movl $0x28, %edi
callq 0x1100
movq %rax, %r13
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl %r15d, %ecx
callq 0x67b6
movq %r13, -0x40(%rbp)
movl $0x28, %edi
callq 0x1100
movq %rax, %r12
movq -0x48(%rbp), %rsi
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %r12, %rdi
movq -0x58(%rbp), %rdx
movl -0x30(%rbp), %ecx
callq 0x67b6
leaq 0x60(%rbx), %r14
movq %r12, -0x38(%rbp)
movq -0x40(%rbp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movsd %xmm0, 0x10(%r12)
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x60a4
movq %rax, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x60b0
leaq -0x40(%rbp), %rdx
movq %r14, %rdi
callq 0x2bd8
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x60c8
movq -0x38(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x60d4
leaq -0x38(%rbp), %rdx
movq %r14, %rdi
callq 0x2bd8
movb -0x29(%rbp), %r14b
leaq 0x18(%rbx), %rdi
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movq 0x88(%rbx), %rdx
movslq (%rdx), %rdx
subq %rdx, %rcx
leaq (%rax,%rcx,8), %rsi
leaq -0x48(%rbp), %rdx
callq 0x6468
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x613c
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
jmp 0x613c
movq %rax, %rbx
movl $0x48, %esi
movq %r14, %rdi
callq 0x1110
movq %rbx, %rdi
callq 0x11a0
nop