// File: /workspace/github_c_makefile_star2_O3/WojciechMula[P]pyahocorasick/src/Automaton.c
// Function: automaton_make_automaton
static PyObject*
automaton_make_automaton(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)

    AutomatonQueueItem* item;
    List queue;
    unsigned i;

    TrieNode* node;
    TrieNode* child;
    TrieNode* state;
    TRIE_LETTER_TYPE letter;


    if (automaton->kind != TRIE)
        Py_RETURN_FALSE;

    list_init(&queue);

    // 1. setup nodes at first level: they fail back to the root
    ASSERT(automaton->root);

    for (i=0; i < automaton->root->n; i++) {
        TrieNode* child = trienode_get_ith_unsafe(automaton->root, i);
        ASSERT(child);
        // fail edges go to the root
        // every other letters loop on root - implicit (see automaton_next)
        child->fail = automaton->root;

        item = (AutomatonQueueItem*)list_item_new(sizeof(AutomatonQueueItem));
        if (item) {
            item->node = child;
            list_append(&queue, (ListItem*)item);
        }
        else
            goto no_mem;
    }

    // 2. make links
    while (true) {
        AutomatonQueueItem* item = (AutomatonQueueItem*)list_pop_first(&queue);
        if (item == NULL)
            break;
        else {
            node = item->node;
            memory_free(item);
        }

        for (i=0; i < node->n; i++) {
            child  = trienode_get_ith_unsafe(node, i);
            letter = trieletter_get_ith_unsafe(node, i);
            ASSERT(child);

            item = (AutomatonQueueItem*)list_item_new(sizeof(AutomatonQueueItem));
            if (item) {
                item->node = child;
                list_append(&queue, (ListItem*)item);
            }
            else
                goto no_mem;

            state = node->fail;
            ASSERT(state);
            ASSERT(child);
            while (state != automaton->root and\
                   not trienode_get_next(state, letter)) {

                state = state->fail;
                ASSERT(state);
            }

            child->fail = trienode_get_next(state, letter);
            if (child->fail == NULL)
                child->fail = automaton->root;

            ASSERT(child->fail);
        }
    }

    automaton->kind = AHOCORASICK;
    automaton->version += 1;
    list_delete(&queue);
    Py_RETURN_NONE;
#undef automaton

no_mem:
    list_delete(&queue);
    PyErr_NoMemory();
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x1, 0x10(%rdi)
jne 0x689b
movq %rdi, %rbp
movq 0x28(%rdi), %r15
testq %r15, %r15
je 0x68ef
cmpl $0x0, 0x10(%r15)
je 0x6888
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r15, %rdi
movq %r14, %rsi
callq 0x572c
testq %rax, %rax
je 0x68db
movq %rax, %r13
movq %r15, 0x8(%rax)
movl $0x10, %edi
callq 0x3370
testq %rax, %rax
je 0x68a7
movq $0x0, (%rax)
movq %r13, 0x8(%rax)
movq %rax, %rdi
testq %rbx, %rbx
je 0x6714
movq %rax, (%rbx)
movq %r12, %rdi
incq %r14
movq 0x28(%rbp), %r15
movl 0x10(%r15), %ecx
movq %rax, %rbx
movq %rdi, %r12
cmpq %rcx, %r14
jb 0x66cd
testq %rdi, %rdi
je 0x6888
movq %rbp, (%rsp)
movq (%rdi), %r12
movq 0x8(%rdi), %r14
testq %r12, %r12
movq %rax, %rbx
cmoveq %r12, %rbx
callq 0x3070
cmpl $0x0, 0x10(%r14)
je 0x6879
xorl %r15d, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x572c
movq %rax, %r13
movq %r14, %rdi
movq %r15, %rsi
callq 0x5ff9
testq %r13, %r13
je 0x68e0
movl %eax, %ebp
movl $0x10, %edi
callq 0x3370
testq %rax, %rax
je 0x68a7
movq $0x0, (%rax)
movq %r13, 0x8(%rax)
movq %rax, %rdi
testq %rbx, %rbx
je 0x67ab
movq %rax, (%rbx)
movq %r12, %rdi
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x68e5
movq (%rsp), %rcx
movq 0x28(%rcx), %rcx
movq 0x18(%rsi), %rdx
movl 0x10(%rsi), %r8d
cmpq %rcx, %rsi
je 0x6808
testq %r8, %r8
je 0x67fa
leaq (,%r8,4), %r9
leaq (%r9,%r9,2), %r9
xorl %r10d, %r10d
cmpl %ebp, (%rdx,%r10)
je 0x67f2
addq $0xc, %r10
cmpq %r10, %r9
jne 0x67e1
jmp 0x67fa
cmpq $0x0, 0x4(%rdx,%r10)
jne 0x6808
movq 0x8(%rsi), %rsi
testq %rsi, %rsi
jne 0x67c0
jmp 0x68d6
testq %r8, %r8
je 0x6827
shlq $0x2, %r8
leaq (%r8,%r8,2), %rsi
xorl %r8d, %r8d
cmpl %ebp, (%rdx,%r8)
je 0x685e
addq $0xc, %r8
cmpq %r8, %rsi
jne 0x6818
movq %r13, %rdx
addq $0x8, %rdx
movq $0x0, 0x8(%r13)
movq (%rsp), %rbp
movq %rcx, (%rdx)
testq %rcx, %rcx
je 0x68ea
incq %r15
movl 0x10(%r14), %ecx
movq %rax, %rbx
movq %rdi, %r12
cmpq %rcx, %r15
jb 0x675b
jmp 0x687f
movq 0x4(%rdx,%r8), %rdx
movq %rdx, 0x8(%r13)
testq %rdx, %rdx
movq (%rsp), %rbp
jne 0x6846
addq $0x8, %r13
movq %r13, %rdx
jmp 0x683a
movq %r12, %rdi
movq %rbx, %rax
testq %rdi, %rdi
jne 0x6737
movl $0x2, 0x10(%rbp)
incl 0x30(%rbp)
movq 0x9727(%rip), %rax      # 0xffc0
jmp 0x68a2
movq 0x9706(%rip), %rax      # 0xffa8
incq (%rax)
jmp 0x68c7
testq %r12, %r12
je 0x68c0
movq (%r12), %rbx
movq %r12, %rdi
callq 0x3070
movq %rbx, %r12
testq %rbx, %rbx
jne 0x68ac
callq 0x30e0
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x40df
callq 0x41ef
callq 0x41ab
callq 0x4167
callq 0x4123
callq 0x4233