// File: /workspace/github_c_makefile_star2_O1/pwelke[P]GraphMiningTools/executables/outerplanaritytest.c
// Function: main
int main(int argc, char** argv) {

    /* object pools */
    struct ListPool *lp;
    struct VertexPool *vp;
    struct ShallowGraphPool *sgp;
    struct GraphPool *gp;

    /* pointer to the current graph which is returned by the input iterator  */
    struct Graph* g = NULL;

    char extendToHamiltonianBlocks = 0;
    char spiderWeb = 0;
    char shortcutting = 0;
    char isFirstGraph = 1;

    /* parse command line arguments */
    int arg;
    const char* validArgs = "hlsw";
    for (arg=getopt(argc, argv, validArgs); arg!=-1; arg=getopt(argc, argv, validArgs)) {
        switch (arg) {
        case 'h': // Help
            printHelp();
            return EXIT_SUCCESS;
        case 'l': // List Hamiltonian cycles in non-outerplanar Hamiltonian blocks
            extendToHamiltonianBlocks = 1;
            break;
        case 'w': // spider Web connectivity
            spiderWeb = 1;
            break;
        case 's':
            shortcutting = 1;
            break;
        case '?':
            return EXIT_FAILURE;
            break;
        }
    }

    /* init object pools */
    lp = createListPool(10000);
    vp = createVertexPool(10000);
    sgp = createShallowGraphPool(1000, lp);
    gp = createGraphPool(100, vp, lp);

    /* initialize the stream to read graphs from 
        check if there is a filename present in the command line arguments 
        if so, open the file, if not, read from stdin */
    if (optind < argc) {
        char* filename = argv[optind];
        /* if the present filename is not '-' then init a file iterator for that file name */
        if (strcmp(filename, "-") != 0) {
            createFileIterator(filename, gp);
        } else {
            createStdinIterator(gp);
        }
    } else {
        createStdinIterator(gp);
    }

    printf("[");

    /* iterate over all graphs in the database */
    while ((g = iterateFile())) {

        // list handling for graphs
        if (isFirstGraph) {
            isFirstGraph = 0;
        } else {
            printf(",");
        }

        /* if there was an error reading some graph the returned n will be -1 */
        if (g->n != -1) {
            char isOp = 1;
            char firstCC = 1;
            printf("\n{\"graph\": %i, \"ccs\": [", g->number);
            struct Graph* connectedComponents = listConnectedComponents(g, gp);
            for (struct Graph* comp=connectedComponents; comp!=NULL; comp=comp->next) {
                if (firstCC) {
                    firstCC = 0;
                    putc('{', stdout);
                } else {
                    printf(", {");
                }

                if (comp->m > 0) {
                    int* originalIDs = malloc(comp->n * sizeof(int));
                    for (int i=0; i<comp->n; ++i) {
                        originalIDs[i] = comp->vertices[i]->lowPoint;
                        comp->vertices[i]->lowPoint = 0;
                    }
                    
                    struct ShallowGraph* biconnectedComponents = listBiconnectedComponents(comp, sgp);
                    char isOPComp = listHamiltonianCycles(biconnectedComponents, extendToHamiltonianBlocks, originalIDs, gp, sgp);
                    isOp = isOp && isOPComp;

                    if (shortcutting || spiderWeb) {
                        struct BBTree* bbTree = createFancyBlockAndBridgeTree(biconnectedComponents, comp, &originalIDs, gp, sgp);
                        
                        printBlockVertices(bbTree, originalIDs);
                        printBBTreeEdges(bbTree);
                        
                        if (shortcutting) {
                            addShortcutEdges(bbTree, 0);
                        } 
                        if (spiderWeb) {
                            addSpiderWebEdges(bbTree, sgp);
                        }
                        dumpFancyBBTree(gp, sgp, bbTree);
                    } else {
                        /* cleanup */
                        dumpShallowGraphCycle(sgp, biconnectedComponents);
                    }
                    free(originalIDs);
                } else {
                    printf("\"hamiltonianCycles\": {}, \"isOuterplanar\": true");
                    if (shortcutting || spiderWeb) {
                        printf(", \"blocks\": {}, \"CATree\": []");
                        if (shortcutting) {
                            printf(", \"shortcutEdges\": []");
                        }
                        if (spiderWeb) {
                            printf(", \"spiderweb\": {}");

                        }
                    }
                }
                putc('}', stdout);
            }
            dumpGraphList(gp, connectedComponents);
            putc(']', stdout);
            if (isOp) {
                printf(", \"isOuterplanar\": true}");
            } else {
                printf(", \"isOuterplanar\": false}");
            }
        } 

        /* garbage collection */
        dumpGraph(gp, g);
    }

    printf("\n]\n");

    /* global garbage collection */
    destroyFileIterator();
    freeGraphPool(gp);
    freeShallowGraphPool(sgp);
    freeListPool(lp);
    freeVertexPool(vp);

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movl %edi, %r13d
movb $0x1, %al
movl %eax, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
leaq 0x1f0b(%rip), %r14      # 0x216d0
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movl %r13d, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x11e0
cmpl $0x6b, %eax
jg 0x1f7fa
cmpl $-0x1, %eax
je 0x1f826
cmpl $0x3f, %eax
je 0x1f8d2
cmpl $0x68, %eax
jne 0x1f7cf
jmp 0x1f8a0
cmpl $0x6c, %eax
je 0x1f811
cmpl $0x73, %eax
je 0x1f816
cmpl $0x77, %eax
jne 0x1f7cf
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x1f7cf
movb $0x1, %bpl
jmp 0x1f7cf
movb $0x1, %al
movl %eax, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x1f7cf
movl $0x2710, %edi           # imm = 0x2710
callq 0x141a4
movq %rax, %r14
movl $0x2710, %edi           # imm = 0x2710
callq 0x1442b
movq %rax, %r12
movl $0x3e8, %edi            # imm = 0x3E8
movq %r14, %rsi
callq 0x14921
movq %rax, %r15
movl $0x64, %edi
movq %r12, 0x28(%rsp)
movq %r12, %rsi
movq %r14, 0x30(%rsp)
movq %r14, %rdx
callq 0x14652
movq %rax, %r14
movq 0xd764(%rip), %rax      # 0x2cfd8
movslq (%rax), %rax
cmpl %r13d, %eax
jge 0x1f8dd
movq (%rbx,%rax,8), %rbx
leaq 0x1e4e(%rip), %rsi      # 0x216d5
movq %rbx, %rdi
callq 0x1140
testl %eax, %eax
je 0x1f8dd
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd20d
jmp 0x1f8e5
xorl %r12d, %r12d
leaq 0x1836(%rip), %rbx      # 0x210e0
movq 0xd717(%rip), %r14      # 0x2cfc8
xorl %r15d, %r15d
movzbl (%r15,%rbx), %edi
movq (%r14), %rsi
callq 0x1110
incq %r15
cmpq $0x4a7, %r15            # imm = 0x4A7
jne 0x1f8b4
jmp 0x1fbce
movl $0x1, %r12d
jmp 0x1fbce
movq %r14, %rdi
callq 0xd25a
movl $0x5b, %edi
callq 0x1040
xorl %eax, %eax
callq 0xd362
testq %rax, %rax
je 0x1fb94
movq %rax, %rbx
cmpb $0x0, 0x8(%rsp)
setne %al
movl 0x4(%rsp), %ecx
orb %al, %cl
movl %ecx, 0x4(%rsp)
movq 0xd6ad(%rip), %r12      # 0x2cfc8
movsbl %bpl, %eax
movl %eax, 0x24(%rsp)
xorl %eax, %eax
movq %r14, 0x18(%rsp)
testb $0x1, %al
je 0x1f938
movl $0x2c, %edi
callq 0x1040
cmpl $-0x1, (%rbx)
je 0x1fb74
movl 0x8(%rbx), %esi
leaq 0x1d8c(%rip), %rdi      # 0x216d7
xorl %eax, %eax
callq 0x10b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb490
movq %rax, %rsi
testq %rax, %rax
je 0x1fb50
movq %rbx, 0x40(%rsp)
movb $0x1, %bpl
xorl %eax, %eax
movq %rsi, 0x38(%rsp)
movq %rsi, %r14
testb $0x1, %al
je 0x1f98f
leaq 0x1d69(%rip), %rdi      # 0x216ef
xorl %eax, %eax
callq 0x10b0
jmp 0x1f99d
movq (%r12), %rsi
movl $0x7b, %edi
callq 0x1130
cmpl $0x0, 0x4(%r14)
jle 0x1fa89
movslq (%r14), %rbx
leaq (,%rbx,4), %rdi
callq 0x1190
movq %rax, 0x10(%rsp)
testq %rbx, %rbx
jle 0x1f9e4
movq 0x10(%r14), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl 0x20(%rsi), %edi
movl %edi, (%rax,%rdx,4)
movl $0x0, 0x20(%rsi)
incq %rdx
movslq (%r14), %rsi
cmpq %rsi, %rdx
jl 0x1f9c8
movq %r14, %rdi
movq %r15, %rsi
callq 0xb2bf
movq %rax, %rbx
movq 0x10(%rsp), %rdx
movq %rax, %rdi
movl 0x24(%rsp), %esi
movq 0x18(%rsp), %r12
movq %r12, %rcx
movq %r15, %r8
callq 0x1f0f5
testb %bpl, %bpl
setne %bpl
testb %al, %al
setne %r13b
testb $0x1, 0x4(%rsp)
je 0x1fad8
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x17af4
movq %rax, %rbx
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x1f62a
movq %rbx, %rdi
callq 0x1f5c5
testb $0x1, 0xc(%rsp)
jne 0x1fa65
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1f4f9
cmpb $0x0, 0x8(%rsp)
je 0x1fa77
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1f395
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x17453
jmp 0x1fae3
leaq 0x1c63(%rip), %rdi      # 0x216f3
xorl %eax, %eax
callq 0x10b0
testb $0x1, 0x4(%rsp)
je 0x1faf7
leaq 0x1c7d(%rip), %rdi      # 0x21722
xorl %eax, %eax
callq 0x10b0
testb $0x1, 0xc(%rsp)
jne 0x1fac1
leaq 0x1c85(%rip), %rdi      # 0x2173f
xorl %eax, %eax
callq 0x10b0
cmpb $0x0, 0x8(%rsp)
je 0x1faf7
leaq 0x1c86(%rip), %rdi      # 0x21755
xorl %eax, %eax
callq 0x10b0
jmp 0x1faf7
movq %r15, %rdi
movq %rbx, %rsi
callq 0x14b75
andb %r13b, %bpl
movq 0x10(%rsp), %rdi
callq 0x1030
movq 0xd4d1(%rip), %r12      # 0x2cfc8
movq (%r12), %rsi
movl $0x7d, %edi
callq 0x1130
movq 0x18(%r14), %r14
movb $0x1, %al
testq %r14, %r14
jne 0x1f97b
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x148f4
movq (%r12), %rsi
movl $0x5d, %edi
callq 0x1130
testb %bpl, %bpl
leaq 0x1c29(%rip), %rdi      # 0x21767
leaq 0x1c3b(%rip), %rax      # 0x21780
cmoveq %rax, %rdi
movq 0x40(%rsp), %rbx
jmp 0x1fb6d
movq %r14, %rdi
callq 0x148f4
movq (%r12), %rsi
movl $0x5d, %edi
callq 0x1130
leaq 0x1bfa(%rip), %rdi      # 0x21767
xorl %eax, %eax
callq 0x10b0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14832
xorl %eax, %eax
callq 0xd362
movq %rax, %rbx
movb $0x1, %al
testq %rbx, %rbx
jne 0x1f92a
leaq 0x1bff(%rip), %rdi      # 0x2179a
callq 0x1070
xorl %eax, %eax
callq 0xd277
movq %r14, %rdi
callq 0x14789
movq %r15, %rdi
callq 0x14a5c
movq 0x30(%rsp), %rdi
callq 0x142d9
movq 0x28(%rsp), %rdi
callq 0x14560
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq