// File: /workspace/github_c_makefile_star2_O1/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, 0x48(%rsp)
movl %edx, 0x14(%rsp)
movl %esi, (%rsp)
movq %rdi, %rbx
movq 0xb8(%rsp), %r12
movl 0xb0(%rsp), %r15d
movq %r9, %rdi
callq 0x7c33
movq %rbx, 0x8(%rsp)
movq 0x8(%rbx), %rbx
testb %al, %al
je 0x4836
testq %rbx, %rbx
je 0x4802
movl 0x24(%rbx), %ebp
testb $0x1, %bpl
je 0x47c9
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 (%rsp), %r8d
callq 0x4be4
testb %al, %al
jne 0x4a34
testb $0x2, %bpl
je 0x47fc
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 0x48(%rsp), %rdi
movl 0x14(%rsp), %r8d
callq 0x4be4
testb %al, %al
jne 0x4a34
movq 0x8(%rbx), %rbx
jmp 0x4791
movq 0x8(%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 (%rsp), %r9d
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4c20
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x484a
cmpl $0x1, 0x24(%rbx)
sbbl $-0x1, %ebp
movq 0x8(%rbx), %rbx
jmp 0x4838
movq %r14, %rdi
callq 0x7c22
movl %eax, %ebx
leal -0x1(%rbx), %eax
movl %eax, 0x4(%rsp)
cmpl %ebp, %eax
jg 0x4a77
movl %r15d, 0x10(%rsp)
movq %r13, 0x28(%rsp)
movq 0x8(%r14), %r13
leal (%rbx,%rbp), %edi
movq %r12, %rsi
callq 0x8565
movq %rax, %r15
movq %rbx, 0x40(%rsp)
testl %ebx, %ebx
movq %rax, 0x20(%rsp)
jle 0x4a18
movl 0x40(%rsp), %eax
movq %rax, 0x50(%rsp)
movl %ebp, %eax
movq %rax, 0x70(%rsp)
xorl %edx, %edx
movq %r14, 0x30(%rsp)
movq %r12, 0x18(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x10(%r15), %rax
movq (%rax,%rdx,8), %rax
movl $0x0, 0x20(%rax)
movq 0x18(%r13), %rcx
movq %rdx, 0x58(%rsp)
movl %edx, 0x28(%rcx)
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %r15
movl 0x18(%rcx), %ecx
movl %ecx, 0x28(%rax)
testl %ebp, %ebp
je 0x49f7
xorl %ebp, %ebp
movq %r13, 0x68(%rsp)
movl 0x24(%r15), %ebx
testl %ebx, %ebx
jne 0x48f9
movq 0x8(%r15), %r15
jmp 0x48eb
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 0x28(%rsp), %rdi
movl (%rsp), %r8d
callq 0x4be4
testb %al, %al
jne 0x4954
testb $0x2, %bl
je 0x49e5
movq 0x18(%r15), %rax
movl 0x18(%rax), %esi
movl 0x18(%r14), %edx
movq 0x18(%r13), %rax
movl 0x18(%rax), %ecx
movq 0x48(%rsp), %rdi
movl 0x14(%rsp), %r8d
callq 0x4be4
testb %al, %al
je 0x49e5
movq 0x18(%r12), %rdi
callq 0x1432b
movq %rax, %rbx
movq 0x18(%r12), %rdi
callq 0x1432b
movq %rax, %r12
movq %rax, (%rbx)
movq 0x20(%rsp), %r13
movq 0x10(%r13), %rax
movq 0x58(%rsp), %r14
movq (%rax,%r14,8), %rdi
movq %rdi, 0x10(%rbx)
movq (%rax,%r14,8), %rcx
incl 0x20(%rcx)
movq 0x50(%rsp), %rcx
addq %rbp, %rcx
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rbx)
movq %rbx, %rsi
callq 0x7cd0
movq %rbx, (%r12)
movq 0x10(%r13), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, 0x18(%r12)
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq %rdi, 0x10(%r12)
movq %r12, %rsi
movq 0x18(%rsp), %r12
movq 0x30(%rsp), %r14
callq 0x7cd0
incl 0x4(%r13)
movq 0x68(%rsp), %r13
movq 0x8(%r15), %r15
incq %rbp
cmpq 0x70(%rsp), %rbp
jne 0x48eb
movq 0x8(%r13), %r13
movq 0x58(%rsp), %rdx
incq %rdx
cmpq 0x50(%rsp), %rdx
movq 0x20(%rsp), %r15
movq 0x60(%rsp), %rbp
jne 0x48b2
movl 0x4(%rsp), %eax
cmpl %eax, 0x4(%r15)
jge 0x4a89
movq %r12, %rdi
movq %r15, %rsi
callq 0x14832
movq 0x28(%rsp), %r13
jmp 0x4a77
movq %r13, %rdi
callq 0x41d6
movq 0x8(%rsp), %rax
movl 0x18(%rax), %esi
movslq 0x18(%r14), %rbx
movl %r15d, %edi
callq 0x4cf4
movq %rax, %r13
movq $0x0, 0x10(%rax)
movq 0x8(%rax), %r14
movq (%r14,%rbx,8), %rax
movq (%rax,%rbx,8), %rdi
movl (%rsp), %esi
callq 0x4e00
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 0x40(%rsp), %rbx
movl %ebx, 0x8(%r15)
movq %r15, %rdi
callq 0x359e
movq %r15, %rdi
movq %r12, %rsi
callq 0x367c
cmpl %ebx, %eax
movq 0x28(%rsp), %r13
jne 0x4ad6
movq %r13, %rdi
callq 0x41d6
movq %r12, %rdi
movq %r15, %rsi
callq 0x14832
movq 0x8(%rsp), %rax
movl 0x18(%rax), %esi
movslq 0x18(%r14), %rbx
movl 0x10(%rsp), %edi
jmp 0x4a4b
movq %r12, 0x18(%rsp)
testl %ebx, %ebx
jle 0x4bd2
movq %r14, 0x30(%rsp)
movl %ebx, %edx
xorl %r12d, %r12d
movq %rdx, 0x38(%rsp)
movl 0x4(%r15), %ecx
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rax
subl 0x20(%rax), %ecx
cmpl 0x4(%rsp), %ecx
jl 0x4bc6
movq 0x8(%rax), %r14
movq $0x0, 0x8(%rax)
xorl %ebp, %ebp
testq %r14, %r14
je 0x4b45
movq %r14, %r15
movq 0x20(%rsp), %rbx
movq 0x18(%r15), %rax
movl 0x18(%rax), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x7e8d
incl %ebp
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0x4b26
jmp 0x4b4a
movq 0x20(%rsp), %rbx
subl %ebp, 0x4(%rbx)
movq %rbx, %rdi
callq 0x359e
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x367c
cmpl 0x4(%rsp), %eax
movq %rbx, %r15
jne 0x4b9a
movq 0x8(%rsp), %rax
movl 0x18(%rax), %edx
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rax
movl 0x28(%rax), %ecx
movq 0x30(%rsp), %rax
movl 0x18(%rax), %r8d
movq %r13, %rdi
movl 0x10(%rsp), %esi
movl (%rsp), %r9d
callq 0x4c20
movq %rax, %r13
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rax
movq %r14, 0x8(%rax)
testq %r14, %r14
je 0x4bbd
movq (%r14), %rsi
movq 0x10(%rsi), %rdi
callq 0x7cd0
movq 0x8(%r14), %r14
jmp 0x4ba6
addl %ebp, 0x4(%r15)
movq 0x38(%rsp), %rdx
incq %r12
cmpq %rdx, %r12
jne 0x4af2
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x14832
jmp 0x4a77