// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
movq %r9, %r12
movq %r8, 0x10(%rsp)
movq %rcx, 0x48(%rsp)
movl %edx, 0x34(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
movq 0xc8(%rsp), %r13
movl 0xc0(%rsp), %r14d
movq %r9, %rdi
callq 0x5a99
testb %al, %al
je 0x2992
movq %rbx, %r15
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
je 0x2b95
movl 0x24(%rbx), %ebp
testb $0x1, %bpl
je 0x2941
movq 0x18(%rbx), %rax
movl 0x18(%rax), %esi
movl 0x18(%r12), %edx
movq 0x8(%r12), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r8d
callq 0x2d20
testb %al, %al
jne 0x2972
testb $0x2, %bpl
je 0x2900
movq 0x18(%rbx), %rax
movl 0x18(%rax), %esi
movl 0x18(%r12), %edx
movq 0x8(%r12), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x48(%rsp), %rdi
movl 0x34(%rsp), %r8d
callq 0x2d20
testb %al, %al
je 0x2900
movq 0x10(%rsp), %rdi
callq 0x2411
movl 0x18(%r15), %edx
movl 0x18(%r12), %ecx
xorl %edi, %edi
movl %r14d, %esi
movl %ecx, %r8d
jmp 0x2bb2
movq %rbx, 0x38(%rsp)
leaq 0x8(%rbx), %rax
xorl %ebx, %ebx
movq %rax, 0x68(%rsp)
movq (%rax), %rax
testq %rax, %rax
je 0x29b7
cmpl $0x1, 0x24(%rax)
sbbl $-0x1, %ebx
addq $0x8, %rax
jmp 0x29a2
movq %r12, %rdi
callq 0x5a8a
movl %eax, %r15d
leal -0x1(%r15), %eax
movl %eax, 0x1c(%rsp)
cmpl %ebx, %eax
jg 0x2be2
movl %r14d, 0x30(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
leal (%r15,%rbx), %edi
movq %r13, %rsi
callq 0x630e
movq %rax, %r14
movslq %r15d, %rax
movq %rax, 0x70(%rsp)
xorl %edx, %edx
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %r15d, %eax
movq %rax, 0x40(%rsp)
movl %ebx, %eax
movq %rax, 0x80(%rsp)
movq %r14, 0x28(%rsp)
movq %r12, 0x50(%rsp)
movq %r13, 0x78(%rsp)
movq %r15, 0x60(%rsp)
cmpq 0x40(%rsp), %rdx
je 0x2bcd
movq 0x10(%r14), %rax
movq (%rax,%rdx,8), %rax
andl $0x0, 0x20(%rax)
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq %rdx, 0x58(%rsp)
movl %edx, 0x28(%rcx)
movq 0x68(%rsp), %rdx
movq (%rdx), %rbx
movl 0x18(%rcx), %ecx
movl %ecx, 0x28(%rax)
xorl %ebp, %ebp
cmpq 0x80(%rsp), %rbp
jne 0x2a74
jmp 0x2b75
movq 0x8(%rbx), %rbx
movl 0x24(%rbx), %r15d
testl %r15d, %r15d
je 0x2a70
testb $0x1, %r15b
je 0x2aae
movq 0x18(%rbx), %rax
movl 0x18(%rax), %esi
movl 0x18(%r12), %edx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %r8d
callq 0x2d20
testb %al, %al
jne 0x2ae7
testb $0x2, %r15b
je 0x2b69
movq 0x18(%rbx), %rax
movl 0x18(%rax), %esi
movl 0x18(%r12), %edx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movq 0x48(%rsp), %rdi
movl 0x34(%rsp), %r8d
callq 0x2d20
testb %al, %al
je 0x2b69
movq 0x18(%r13), %rdi
callq 0x11307
movq %rax, %r15
movq 0x18(%r13), %rdi
callq 0x11307
movq %rax, %r12
movq %rax, (%r15)
movq 0x10(%r14), %rax
movq 0x58(%rsp), %r13
movq (%rax,%r13,8), %rdi
movq %rdi, 0x10(%r15)
incl 0x20(%rdi)
movq 0x70(%rsp), %rcx
leaq (%rcx,%rbp), %r14
movq (%rax,%r14,8), %rax
movq %rax, 0x18(%r15)
movq %r15, %rsi
callq 0x5b2c
movq %r15, (%r12)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r13,8), %rcx
movq %rcx, 0x18(%r12)
movq (%rax,%r14,8), %rdi
movq 0x28(%rsp), %r14
movq %rdi, 0x10(%r12)
movq %r12, %rsi
movq 0x50(%rsp), %r12
movq 0x78(%rsp), %r13
callq 0x5b2c
incl 0x4(%r14)
movq 0x8(%rbx), %rbx
incq %rbp
jmp 0x2a61
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rdx
incq %rdx
movq 0x60(%rsp), %r15
jmp 0x2a29
movl 0x18(%r15), %edx
movq 0x8(%r12), %rax
movq 0x18(%rax), %rax
movl 0x18(%rax), %ecx
movl 0x18(%r12), %r8d
movq 0x10(%rsp), %rdi
movl %r14d, %esi
movl 0xc(%rsp), %r9d
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d4e
movl 0x1c(%rsp), %eax
cmpl %eax, 0x4(%r14)
jge 0x2bf9
movq %r13, %rdi
movq %r14, %rsi
callq 0x1166e
movq 0x10(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r15d, 0x8(%r14)
movq %r14, %rdi
callq 0x192e
movq %r14, %rdi
movq %r13, %rsi
callq 0x19fe
cmpl %r15d, %eax
jne 0x2c42
movq 0x10(%rsp), %rdi
callq 0x2411
movq %r13, %rdi
movq %r14, %rsi
callq 0x1166e
movq 0x38(%rsp), %rax
movl 0x18(%rax), %edx
movl 0x18(%r12), %ecx
xorl %edi, %edi
movl 0x30(%rsp), %esi
jmp 0x298a
xorl %ebx, %ebx
movl 0x30(%rsp), %r12d
cmpq 0x40(%rsp), %rbx
je 0x2bd7
movl 0x4(%r14), %ecx
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rax
subl 0x20(%rax), %ecx
cmpl 0x1c(%rsp), %ecx
jl 0x2d18
movq 0x8(%rax), %r14
andq $0x0, 0x8(%rax)
xorl %ebp, %ebp
movq %r14, %r15
testq %r15, %r15
je 0x2c97
movq 0x18(%r15), %rax
movl 0x18(%rax), %esi
movq 0x28(%rsp), %rdi
movl %ebx, %edx
callq 0x5cd1
incl %ebp
movq 0x8(%r15), %r15
jmp 0x2c77
movq 0x28(%rsp), %r15
subl %ebp, 0x4(%r15)
movq %r15, %rdi
callq 0x192e
movq %r15, %rdi
movq %r13, %rsi
callq 0x19fe
cmpl 0x1c(%rsp), %eax
jne 0x2cec
movq 0x38(%rsp), %rax
movl 0x18(%rax), %edx
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %rax
movl 0x28(%rax), %ecx
movq 0x50(%rsp), %rax
movl 0x18(%rax), %r8d
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movl 0xc(%rsp), %r9d
callq 0x2d4e
movq %rax, 0x10(%rsp)
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %rax
movq %r14, 0x8(%rax)
testq %r14, %r14
je 0x2d0f
movq (%r14), %rsi
movq 0x10(%rsi), %rdi
callq 0x5b2c
movq 0x8(%r14), %r14
jmp 0x2cf8
movq 0x28(%rsp), %r14
addl %ebp, 0x4(%r14)
incq %rbx
jmp 0x2c49