// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdi, %rbx
movq 0x68(%rdi), %r14
subq 0x60(%rdi), %r14
shrq $0x3, %r14
callq 0x1040
cltd
idivl %r14d
movslq %edx, %rcx
movq 0x60(%rbx), %rax
movq (%rax,%rcx,8), %r13
movb $0x1, %cl
cmpb $0x0, 0x18(%r13)
jne 0x5dde
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $0xd, %r14d
je 0x5f9d
movq 0x68(%rbx), %r12
subq %rax, %r12
shrq $0x3, %r12
callq 0x1040
cltd
idivl %r12d
movslq %edx, %rcx
movq 0x60(%rbx), %rax
movq (%rax,%rcx,8), %r13
incl %r14d
cmpb $0x0, 0x18(%r13)
je 0x5da4
cmpl $0xe, %r14d
setb %cl
movl %ecx, -0x2c(%rbp)
movq (%r13), %r14
movq 0x8(%r13), %r10
movb $0x0, 0x18(%r13)
movq (%r15), %rax
movq 0x8(%r15), %rcx
cmpq %rcx, %rax
je 0x5e5e
xorl %edx, %edx
movq (%rax), %rsi
movq 0x18(%rsi), %rdi
movq 0x20(%rsi), %r8
subq %rdi, %r8
cmpq $0x8, %r8
jne 0x5e50
movq 0x30(%rsi), %r8
movq 0x38(%rsi), %r9
subq %r8, %r9
cmpq $0x8, %r9
jne 0x5e50
movq (%rdi), %rdi
movq (%rdi), %r9
movl 0x4(%r9), %r9d
cmpl 0x4(%r14), %r9d
jne 0x5e50
movq (%r8), %r8
movq 0x8(%r8), %r9
movl 0x4(%r9), %r9d
cmpl 0x4(%r10), %r9d
jne 0x5e50
movl 0x4(%rsi), %r12d
movl 0x1c(%rdi), %r15d
movl 0x1c(%r8), %r11d
movb $0x1, %dl
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x5dfc
testb $0x1, %dl
jne 0x5e76
movq 0x78(%rbx), %rax
movl (%rax), %r12d
leal 0x1(%r12), %r15d
leal 0x2(%r12), %r11d
leal 0x3(%r12), %ecx
movl %ecx, (%rax)
movl %r15d, -0x34(%rbp)
movl %r11d, -0x30(%rbp)
movq %r10, -0x40(%rbp)
movl $0x48, %edi
callq 0x1100
movq %rax, %r15
movq 0x90(%rbx), %rax
movl (%rax), %ecx
movq %r15, %rdi
movl $0x2, %esi
movl %r12d, %edx
callq 0x6512
movq %r15, -0x58(%rbp)
movl $0x28, %edi
callq 0x1100
movq %rax, %r12
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl -0x34(%rbp), %ecx
callq 0x666a
movq %r12, -0x50(%rbp)
movl $0x28, %edi
callq 0x1100
movq %rax, %r14
movq 0x90(%rbx), %rax
movl (%rax), %r8d
movq %r14, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
movl -0x30(%rbp), %ecx
callq 0x666a
leaq 0x60(%rbx), %r15
movq %r14, -0x48(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x10(%r12)
movsd 0x10(%r13), %xmm0
movsd %xmm0, 0x10(%r14)
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x5f3f
movq %r12, (%rsi)
movq 0x68(%rbx), %rsi
addq $0x8, %rsi
movq %rsi, 0x68(%rbx)
jmp 0x5f4f
leaq -0x50(%rbp), %rdx
movq %r15, %rdi
callq 0x2bbe
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x5f5f
movq %r14, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x5f6b
leaq -0x48(%rbp), %rdx
movq %r15, %rdi
callq 0x2bbe
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 -0x58(%rbp), %rdx
callq 0x632c
movl -0x2c(%rbp), %eax
jmp 0x5fe1
movq 0x4034(%rip), %rbx      # 0x9fd8
leaq 0x1280(%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
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x28, %esi
movq %r14, %rdi
jmp 0x6015
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x6015
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0x1110
movq %rbx, %rdi
callq 0x11a0