// File: /workspace/github_c_makefile_star2_O3/pwelke[P]GraphMiningTools/cactustree.c
// Function: characteristics
struct Characteristics *characteristics(struct Vertex *w, const int treeID, const int wTreeID, struct Characteristics *oldCharacteristics, struct Characteristics *newCharacteristics, struct Vertex *patternVertex, int patternSize, struct GraphPool *gPool){
    struct Characteristics *ret=newCharacteristics;
    struct VertexList *edgehlp, *edgehlp2, *edgehlp3, *edgehlp4, *tmprm=NULL;
    struct Graph *bipartite;
    int i=0, j=0, numTree=0, numPattern, matchingSize, numRem=0;

    //TODO single vertex pattern.
    //printf("gV %i pV %i\n",w->number, patternVertex->number);
    if(isLeaf(patternVertex)){
        //printf("leaf %i\n",w->number);
        for(edgehlp=w->neighborhood; edgehlp; edgehlp=edgehlp->next)
            if(((edgehlp->flag & 1) && (checkCharacteristic(newCharacteristics,edgehlp->endPoint->number,patternVertex->number, patternVertex->neighborhood->endPoint->number, treeID)))
                    ||((edgehlp->flag & 2) && (checkCharacteristic(oldCharacteristics,edgehlp->endPoint->number,patternVertex->number, patternVertex->neighborhood->endPoint->number, wTreeID)))){
                freeCharacteristics(ret);
                return insertCharacteristic(NULL, patternSize, w->number, patternVertex->number, patternVertex->number, treeID);
            }
        return insertCharacteristic(ret,patternSize,w->number, patternVertex->neighborhood->endPoint->number, patternVertex->number, treeID);
    }
    
    //printf("Checking neighbours of gV\n");
    for(edgehlp=w->neighborhood; edgehlp;edgehlp=edgehlp->next){
        //printf("\t(%i %i)" ,edgehlp->endPoint->number, edgehlp->flag);
        if((edgehlp->flag)){//part of the tree
           ++numTree;
        }
    }
    //printf("\n");
    numPattern=degree(patternVertex);
    if(numPattern-1>numTree) return ret;
    edgehlp=patternVertex->neighborhood;
    bipartite=createGraph(numPattern+numTree, gPool);
    //printf("\n nP %i nt %i  T %i wT %i\n",numPattern, numTree, treeID, wTreeID);
    for(;i<numPattern;++i){
        bipartite->vertices[i]->lowPoint =0;
        //setting vertexnumber for neighborhood.
        edgehlp->endPoint->d=i;
        edgehlp2=w->neighborhood;
        bipartite->vertices[i]->d=edgehlp->endPoint->number;
        for(j=0;j<numTree;++j){
            while(!(edgehlp2->flag)) edgehlp2=edgehlp2->next;
            /*
            printf("checking edge ");
            if(edgehlp2) printf(" (gN %i %i) ", edgehlp2->endPoint->number ,edgehlp2->flag);
            if((edgehlp)) printf(" (pN %i) ", edgehlp->endPoint->number);
            printf("\n");
            */
            if( (((edgehlp2->flag & 1) && checkCharacteristic(newCharacteristics,edgehlp2->endPoint->number,patternVertex->number, edgehlp->endPoint->number,treeID))
                    ||((edgehlp2->flag & 2) && checkCharacteristic(oldCharacteristics, edgehlp2->endPoint->number, patternVertex->number, edgehlp->endPoint->number,wTreeID)))){
                edgehlp3=getVertexList(gPool->listPool);
                edgehlp4=getVertexList(gPool->listPool);
                edgehlp3->label = (char *)edgehlp4;
                edgehlp3->startPoint=bipartite->vertices[i];
                ++(bipartite->vertices[i]->lowPoint);
                edgehlp3->endPoint=bipartite->vertices[numPattern+j];
                addEdge(edgehlp3->startPoint,edgehlp3);
                edgehlp4->label = (char *)edgehlp3;
                edgehlp4->endPoint=bipartite->vertices[i];
                edgehlp4->startPoint=bipartite->vertices[numPattern+j];
                addEdge(edgehlp4->startPoint,edgehlp4);
                ++(bipartite->m);
                //printf("bedge %i,%i\t\n",i,numPattern+j);
            }
            edgehlp2=edgehlp2->next;
        }
        //printf("%i=%i\n",degree(bipartite->vertices[i]),bipartite->vertices[i]->lowPoint);
        edgehlp=edgehlp->next;
    }
    //printf("\n");
    if(bipartite->m<numPattern-1){
        dumpGraph(gPool, bipartite);
        return ret;
    }
    bipartite->number=numPattern;
    initBipartite(bipartite);
    matchingSize=bipartiteMatchingFastAndDirty(bipartite, gPool); 
    //printf("\nmatching size is %i\n", matchingSize);
    if(matchingSize==numPattern){//we have a matching that covers.
        //printf("\n\n\nAND IT MATCHES!!!!111elf\n\n\n");
        freeCharacteristics(ret);
        dumpGraph(gPool,bipartite);
        return insertCharacteristic(NULL, patternSize, w->number, patternVertex->number, patternVertex->number, treeID);
    }
    // now start removing neighbours.
    /*
    int p;
    struct VertexList *debug;
    printf("\n complete bipartie\n");
        for(p=0;p<numPattern+numTree;++p){
            printf("%i",bipartite->vertices[p]->number);
            for(debug=bipartite->vertices[p]->neighborhood;debug;debug=debug->next) printf("  (%i cb  %i)", debug->startPoint->number, debug->endPoint->number);
        }
    */
    for(j=0;j<numPattern;++j){
        //printf("rem vert %i ",j);
        //printf("%i %i \n",bipartite->vertices[j]->lowPoint , degree(bipartite->vertices[i]));
        if(bipartite->m - bipartite->vertices[j]->lowPoint >= numPattern-1){
            tmprm=bipartite->vertices[j]->neighborhood;
            bipartite->vertices[j]->neighborhood=NULL;
            for(edgehlp=tmprm;edgehlp;edgehlp=edgehlp->next){
                deleteEdge(bipartite, edgehlp->endPoint->number, j);
                ++numRem;
            }
            
            bipartite->m -= numRem;
            /*
            printf("after removal " );
            for(p=0;p<numPattern+numTree;++p){
                printf("%i",bipartite->vertices[p]->number);
                for(debug=bipartite->vertices[p]->neighborhood;debug;debug=debug->next) printf("  (%i ar %i)", debug->startPoint->number, debug->endPoint->number);
            }
            */
            initBipartite(bipartite);
            matchingSize=bipartiteMatchingFastAndDirty(bipartite, gPool);
            //printf("\nmatching size is %i\n", matchingSize);
            if(matchingSize ==numPattern-1){//we have a matching that covers.
                //printf("\nsmall matching\n");
                ret=insertCharacteristic(ret, patternSize, w->number, bipartite->vertices[j]->d, patternVertex->number, treeID);
            }
        
        //reinsert removed Edges.
            bipartite->vertices[j]->neighborhood=tmprm;
            for(edgehlp=tmprm;edgehlp;edgehlp=edgehlp->next){
                edgehlp2=(struct VertexList *)edgehlp->label;
                addEdge(edgehlp2->startPoint,edgehlp2);
            }
            /*
            printf("\nAfter reinsertion: \n");
            for(p=0;p<numPattern+numTree;++p){
                printf("%i",bipartite->vertices[p]->number);
                for(debug=bipartite->vertices[p]->neighborhood;debug;debug=debug->next) printf("  (%i ai %i)", debug->startPoint->number, debug->endPoint->number);
            }
            */
            bipartite->m += numRem;
            numRem=0;
        }
    }
    dumpGraph(gPool,bipartite);
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r14
movq %r8, %r13
movq %rcx, 0x50(%rsp)
movl %edx, 0x2c(%rsp)
movl %esi, 0x4(%rsp)
movq %rdi, %rbx
movq 0xb8(%rsp), %r12
movl 0xb0(%rsp), %r15d
movq %r9, %rdi
callq 0x7c5f
movq %rbx, 0x18(%rsp)
movq 0x8(%rbx), %rbx
testb %al, %al
je 0x4833
testq %rbx, %rbx
je 0x47fe
movl 0x24(%rbx), %ebp
testb $0x1, %bpl
je 0x47c5
movq 0x18(%rbx), %rax
movl 0x18(%rax), %esi
movl 0x18(%r14), %edx
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq %r13, %rdi
movl 0x4(%rsp), %r8d
callq 0x4bfa
testb %al, %al
jne 0x4a2b
testb $0x2, %bpl
je 0x47f8
movq 0x18(%rbx), %rax
movl 0x18(%rax), %esi
movl 0x18(%r14), %edx
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x50(%rsp), %rdi
movl 0x2c(%rsp), %r8d
callq 0x4bfa
testb %al, %al
jne 0x4a2b
movq 0x8(%rbx), %rbx
jmp 0x478c
movq 0x18(%rsp), %rax
movl 0x18(%rax), %edx
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movl 0x18(%r14), %r8d
movq %r13, %rdi
movl %r15d, %esi
movl 0x4(%rsp), %r9d
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4c36
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x4847
cmpl $0x1, 0x24(%rbx)
sbbl $-0x1, %ebp
movq 0x8(%rbx), %rbx
jmp 0x4835
movq %r14, %rdi
callq 0x7c4e
movl %eax, %ebx
leal -0x1(%rbx), %eax
movl %eax, 0x14(%rsp)
cmpl %ebp, %eax
jg 0x4a6f
movl %r15d, 0x10(%rsp)
movq %r13, 0x38(%rsp)
movq 0x8(%r14), %r13
leal (%rbx,%rbp), %edi
movq %r12, %rsi
callq 0x85e3
movq %rax, 0x8(%rsp)
movq %rbx, 0x48(%rsp)
testl %ebx, %ebx
jle 0x4a0b
movl 0x48(%rsp), %eax
movq %rax, 0x58(%rsp)
movl %ebp, %eax
movq %rax, 0x70(%rsp)
xorl %edx, %edx
movq %r14, 0x40(%rsp)
movq %r12, 0x30(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%rdx,8), %rax
movl $0x0, 0x20(%rax)
movq 0x18(%r13), %rcx
movq %rdx, 0x20(%rsp)
movl %edx, 0x28(%rcx)
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %r15
movl 0x18(%rcx), %ecx
movl %ecx, 0x28(%rax)
testl %ebp, %ebp
je 0x49ef
xorl %ebp, %ebp
movq %r13, 0x68(%rsp)
movl 0x24(%r15), %ebx
testl %ebx, %ebx
jne 0x48f8
movq 0x8(%r15), %r15
jmp 0x48ea
testb $0x1, %bl
je 0x4922
movq 0x18(%r15), %rax
movl 0x18(%rax), %esi
movl 0x18(%r14), %edx
movq 0x18(%r13), %rax
movl 0x18(%rax), %ecx
movq 0x38(%rsp), %rdi
movl 0x4(%rsp), %r8d
callq 0x4bfa
testb %al, %al
jne 0x4954
testb $0x2, %bl
je 0x49dd
movq 0x18(%r15), %rax
movl 0x18(%rax), %esi
movl 0x18(%r14), %edx
movq 0x18(%r13), %rax
movl 0x18(%rax), %ecx
movq 0x50(%rsp), %rdi
movl 0x2c(%rsp), %r8d
callq 0x4bfa
testb %al, %al
je 0x49dd
movq 0x18(%r12), %rdi
callq 0x141f1
movq %rax, %rbx
movq 0x18(%r12), %rdi
callq 0x141f1
movq %rax, %r12
movq %rax, (%rbx)
movq 0x8(%rsp), %r13
movq 0x10(%r13), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %rdi, 0x10(%rbx)
incl 0x20(%rdi)
movq 0x58(%rsp), %rcx
leaq (%rcx,%rbp), %r14
movq (%rax,%r14,8), %rax
movq %rax, 0x18(%rbx)
movq %rbx, %rsi
callq 0x7cfe
movq %rbx, (%r12)
movq 0x10(%r13), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, 0x18(%r12)
movq (%rax,%r14,8), %rdi
movq %rdi, 0x10(%r12)
movq %r12, %rsi
movq 0x30(%rsp), %r12
movq 0x40(%rsp), %r14
callq 0x7cfe
incl 0x4(%r13)
movq 0x68(%rsp), %r13
movq 0x8(%r15), %r15
incq %rbp
cmpq 0x70(%rsp), %rbp
jne 0x48ea
movq 0x8(%r13), %r13
movq 0x20(%rsp), %rdx
incq %rdx
cmpq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rbp
jne 0x48ac
movq 0x8(%rsp), %rbx
movl 0x14(%rsp), %eax
cmpl %eax, 0x4(%rbx)
jge 0x4a81
movq %r12, %rdi
movq %rbx, %rsi
callq 0x14700
movq 0x38(%rsp), %r13
jmp 0x4a6f
movq %r13, %rdi
callq 0x41e4
movq 0x18(%rsp), %rax
movl 0x18(%rax), %esi
movslq 0x18(%r14), %rbx
movl %r15d, %edi
callq 0x4d0a
movq %rax, %r13
movq $0x0, 0x10(%rax)
movq 0x8(%rax), %r14
movq (%r14,%rbx,8), %rax
movq (%rax,%rbx,8), %rdi
movl 0x4(%rsp), %esi
callq 0x4e16
movq (%r14,%rbx,8), %rcx
movq %rax, (%rcx,%rbx,8)
movq %r13, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rsp), %rbp
movl %ebp, 0x8(%rbx)
movq %rbx, %rdi
callq 0x35c2
movq %rbx, %rdi
movq %r12, %rsi
callq 0x36a0
cmpl %ebp, %eax
movq 0x38(%rsp), %r13
jne 0x4acd
movq %r13, %rdi
callq 0x41e4
movq %r12, %rdi
movq %rbx, %rsi
callq 0x14700
movq 0x18(%rsp), %rax
movl 0x18(%rax), %esi
movslq 0x18(%r14), %rbx
movl 0x10(%rsp), %edi
jmp 0x4a42
movq %r12, 0x30(%rsp)
testl %ebp, %ebp
movl 0x10(%rsp), %r15d
jle 0x4be6
movq %r14, 0x40(%rsp)
movl %ebp, %edi
movq 0x8(%rsp), %rsi
movl 0x4(%rsi), %eax
xorl %r12d, %r12d
movq %rdi, 0x20(%rsp)
movq 0x10(%rsi), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, %edx
subl 0x20(%rcx), %edx
cmpl 0x14(%rsp), %edx
jl 0x4bd8
movq 0x8(%rcx), %r14
movq $0x0, 0x8(%rcx)
xorl %ebp, %ebp
testq %r14, %r14
je 0x4b4f
movq %r14, %r15
movq 0x8(%rsp), %rbx
movq 0x18(%r15), %rax
movl 0x18(%rax), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x7ed8
incl %ebp
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0x4b28
movl 0x4(%rbx), %eax
movl 0x10(%rsp), %r15d
jmp 0x4b54
movq 0x8(%rsp), %rbx
subl %ebp, %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
callq 0x35c2
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0x36a0
cmpl 0x14(%rsp), %eax
jne 0x4ba3
movq 0x18(%rsp), %rax
movl 0x18(%rax), %edx
movq 0x10(%rbx), %rax
movq (%rax,%r12,8), %rax
movl 0x28(%rax), %ecx
movq 0x40(%rsp), %rax
movl 0x18(%rax), %r8d
movq %r13, %rdi
movl %r15d, %esi
movl 0x4(%rsp), %r9d
callq 0x4c36
movq %rax, %r13
movq 0x10(%rbx), %rax
movq (%rax,%r12,8), %rax
movq %r14, 0x8(%rax)
testq %r14, %r14
je 0x4bc6
movq (%r14), %rsi
movq 0x10(%rsi), %rdi
callq 0x7cfe
movq 0x8(%r14), %r14
jmp 0x4baf
movq 0x8(%rsp), %rsi
addl 0x4(%rsi), %ebp
movl %ebp, 0x4(%rsi)
movl %ebp, %eax
movq 0x20(%rsp), %rdi
incq %r12
cmpq %rdi, %r12
jne 0x4af6
jmp 0x4beb
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x14700
jmp 0x4a6f