// File: /workspace/github_cpp_makefile_star2_O2/pgrouchy[P]NoiseWorld/mathNet.cpp
// Function: Genome::removeUnusedEquations()
Genome* Genome::removeUnusedEquations() {
    Genome *tempParent;
    Tree *tempTree;
    list<Tree*>::iterator curTree1;
    list<TreeID*>::iterator curTreeID;
    list<int> oldIDs, newIDs;
    int i, j, numNotConnected = 0;
    bool isConnected[this->num_trees];

    tempParent = new Genome(this);

    for (i = 0; i < NUM_OUTPUTS; i++) { // we will never remove the original output equations
        isConnected[i] = true;
    }
    for (i = NUM_OUTPUTS; i < this->num_trees; i++) {
        isConnected[i] = true;
            
        if (this->numRefsToTree(i) == 0) { // this tree is not directly or indirectly referenced by an original output equation
            numNotConnected++;
            isConnected[i] = false;
        }
    }
    if (numNotConnected > 0) { // remove unconnected trees

        // delete all tree ids, will need to reorder
        curTreeID = tempParent->tree_id_lookup.begin();
        for (curTree1 = tempParent->trees.begin(); curTree1 != tempParent->trees.end(); ++curTree1) {
            delete (*curTree1);
            delete (*curTreeID);
            ++curTreeID;
        }

        // delete all trees, will need to reorder
        tempParent->trees.clear();
        tempParent->tree_id_lookup.clear();
        tempParent->num_trees = 0;
        curTree1 = this->trees.begin();
        curTreeID = this->tree_id_lookup.begin();
        oldIDs.clear();
        newIDs.clear();
        for (i = 0; i < this->num_trees; i++) {
            if (i < NUM_OUTPUTS) { // put original equations back
                tempParent->trees.push_back(new Tree((*curTree1)));
                tempParent->tree_id_lookup.push_back(new TreeID((*curTreeID)));
                tempParent->num_trees++;
            } else if (isConnected[i]) { // put connected equations back (leaving out unconnected equation tree)
                tempTree = new Tree((*curTree1));
                oldIDs.push_back(i); // keep track of order changes
                newIDs.push_back(tempParent->num_trees); // keep track of order changes
                tempParent->trees.push_back(tempTree);
                tempParent->tree_id_lookup.push_back(new TreeID((*curTreeID)));
                tempParent->num_trees++;

            }
            ++curTree1;
            ++curTreeID;
        }

        // resize initial value array as the number of trees has changed
        delete [] tempParent->initialVal;
        tempParent->initialVal = new double[tempParent->num_trees];
        j = 0;
        for (i = 0; i < this->num_trees; i++) {
            if (isConnected[i]) {
                tempParent->initialVal[j] = this->initialVal[i];
                j++;
            }
        }

        // reorder references to internal variables as trees have been reordered
        tempParent->updateIDrefs(oldIDs, newIDs);
    }
    return tempParent;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r14
leaq -0x88(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq %rsp, %r12
leaq -0x70(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movl 0x34(%rdi), %eax
movq %rsp, %rbx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl $0x90, %edi
callq 0x21f0
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
callq 0x4846
movw $0x101, (%rbx)          # imm = 0x101
xorl %r15d, %r15d
pushq $0x2
popq %rsi
movl %esi, -0x2c(%rbp)
cmpl 0x34(%r14), %esi
jge 0x822e
movslq %esi, %rax
movb $0x1, (%rbx,%rax)
movq %r14, %rdi
callq 0x68fa
movslq -0x2c(%rbp), %rsi
testl %eax, %eax
jne 0x822a
incl %r15d
movb $0x0, (%rbx,%rsi)
incl %esi
jmp 0x8203
testl %r15d, %r15d
jle 0x8436
movq %r12, -0x58(%rbp)
leaq 0x18(%r13), %r12
movq %r12, -0x48(%rbp)
movq %r13, -0x38(%rbp)
movq (%r13), %r13
cmpq -0x38(%rbp), %r13
je 0x8280
movq (%r12), %r12
movq 0x10(%r13), %r15
testq %r15, %r15
je 0x8266
movq %r15, %rdi
callq 0x4a1a
movq %r15, %rdi
pushq $0x38
popq %rsi
callq 0x2200
movq 0x10(%r12), %rdi
pushq $0x8
popq %rsi
callq 0x2200
jmp 0x8247
movq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x4a7a
movq -0x48(%rbp), %rdi
callq 0x4a92
andl $0x0, 0x34(%r15)
addq $0x34, %r15
movq %r15, -0x50(%rbp)
movq (%r14), %r13
movq 0x18(%r14), %r12
leaq -0x88(%rbp), %rdi
callq 0x8a1a
leaq -0x70(%rbp), %rdi
callq 0x8a1a
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
cmpl 0x34(%r14), %eax
jge 0x83af
cmpl $0x1, %eax
jg 0x8320
pushq $0x38
popq %rdi
callq 0x21f0
movq %rax, %r15
movq 0x10(%r13), %rsi
movq %rax, %rdi
callq 0x4afe
movq %r15, -0x40(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x8a32
pushq $0x8
popq %rdi
callq 0x21f0
movq 0x10(%r12), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4bb8
jmp 0x8397
movl %eax, %eax
cmpb $0x1, (%rbx,%rax)
jne 0x839d
pushq $0x38
popq %rdi
callq 0x21f0
movq %rax, %r15
movq 0x10(%r13), %rsi
movq %rax, %rdi
callq 0x4afe
movq %r15, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x89fa
leaq -0x70(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x89fa
movq -0x38(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4bac
pushq $0x8
popq %rdi
callq 0x21f0
movq 0x10(%r12), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4bb8
movq -0x50(%rbp), %rax
incl (%rax)
movq (%r13), %r13
movq (%r12), %r12
movl -0x2c(%rbp), %eax
incl %eax
jmp 0x82c0
movq -0x38(%rbp), %r13
movq 0x80(%r13), %rdi
testq %rdi, %rdi
je 0x83c4
callq 0x2230
movq -0x50(%rbp), %rax
movslq (%rax), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq -0x58(%rbp), %r12
movq %rax, 0x80(%r13)
movl 0x34(%r14), %ecx
movq 0x80(%r14), %rdx
xorl %esi, %esi
testl %ecx, %ecx
cmovlel %esi, %ecx
xorl %edi, %edi
cmpq %rsi, %rcx
je 0x8420
cmpb $0x1, (%rbx,%rsi)
jne 0x841b
movsd (%rdx,%rsi,8), %xmm0
movslq %edi, %rdi
movsd %xmm0, (%rax,%rdi,8)
incl %edi
incq %rsi
jmp 0x8401
movl %ecx, -0x2c(%rbp)
leaq -0x88(%rbp), %rsi
leaq -0x70(%rbp), %rdx
movq %r13, %rdi
callq 0x6afa
movq %r12, %rsp
leaq -0x70(%rbp), %rdi
callq 0x8d4a
leaq -0x88(%rbp), %rdi
callq 0x8d4a
movq %r13, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x90, %esi
movq %r13, %rdi
jmp 0x847c
jmp 0x8489
jmp 0x8473
jmp 0x8489
movq %rax, %r14
pushq $0x38
popq %rsi
movq %r15, %rdi
callq 0x2200
jmp 0x848c
jmp 0x8489
jmp 0x8489
jmp 0x8489
movq %rax, %r14
leaq -0x70(%rbp), %rdi
callq 0x8d4a
leaq -0x88(%rbp), %rdi
callq 0x8d4a
movq %r14, %rdi
callq 0x2310
nop