// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0x7bdf
movq 0xa3d7(%rip), %rdi      # 0x11fa8
callq 0x4a30
movq %rax, -0x8(%rbp)
jmp 0x804f
leaq -0x30(%rbp), %rdi
callq 0x3b90
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x7c3f
movq 0xa3fe(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x440c(%rip), %rsi      # 0xc010
leaq 0x6b8d(%rip), %rdx      # 0xe798
leaq 0x6bf3(%rip), %rcx      # 0xe805
movl $0x244, %r8d            # imm = 0x244
leaq 0x6bff(%rip), %r9       # 0xe81e
movb $0x0, %al
callq 0x31b0
movq 0xa3cb(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7c41
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
cmpl 0x10(%rcx), %eax
jae 0x7d1c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
callq 0x5dd0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x7cc5
movq 0xa378(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x4386(%rip), %rsi      # 0xc010
leaq 0x6b07(%rip), %rdx      # 0xe798
leaq 0x6b6d(%rip), %rcx      # 0xe805
movl $0x248, %r8d            # imm = 0x248
leaq 0x6a7f(%rip), %r9       # 0xe724
movb $0x0, %al
callq 0x31b0
movq 0xa345(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7cc7
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x10, %edi
callq 0x3b30
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x7d07
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x3c70
jmp 0x7d0c
jmp 0x8039
jmp 0x7d0e
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x7c48
jmp 0x7d1e
leaq -0x30(%rbp), %rdi
callq 0x3de0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x7d37
jmp 0x8006
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x68(%rbp), %rdi
callq 0x34b0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x8001
movq -0x40(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
callq 0x5dd0
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
callq 0x7410
movl %eax, -0x54(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x7dd9
movq 0xa264(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x4272(%rip), %rsi      # 0xc010
leaq 0x69f3(%rip), %rdx      # 0xe798
leaq 0x6a59(%rip), %rcx      # 0xe805
movl $0x263, %r8d            # imm = 0x263
leaq 0x696b(%rip), %r9       # 0xe724
movb $0x0, %al
callq 0x31b0
movq 0xa231(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7ddb
movl $0x10, %edi
callq 0x3b30
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x7e0b
movq -0x48(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x3c70
jmp 0x7e10
jmp 0x8039
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x7e6f
movq 0xa1ce(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x41dc(%rip), %rsi      # 0xc010
leaq 0x695d(%rip), %rdx      # 0xe798
leaq 0x69c3(%rip), %rcx      # 0xe805
movl $0x26e, %r8d            # imm = 0x26E
leaq 0x6d01(%rip), %r9       # 0xeb50
movb $0x0, %al
callq 0x31b0
movq 0xa19b(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7e71
jmp 0x7e73
cmpq $0x0, -0x48(%rbp)
jne 0x7ec6
movq 0xa177(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x4185(%rip), %rsi      # 0xc010
leaq 0x6906(%rip), %rdx      # 0xe798
leaq 0x696c(%rip), %rcx      # 0xe805
movl $0x26f, %r8d            # imm = 0x26F
leaq 0x687e(%rip), %r9       # 0xe724
movb $0x0, %al
callq 0x31b0
movq 0xa144(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7ec8
jmp 0x7eca
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpq 0x28(%rdx), %rcx
movb %al, -0x69(%rbp)
je 0x7ef5
movq -0x50(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x6510
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x7efe
jmp 0x7f64
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x7f5d
movq 0xa0e0(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x40ee(%rip), %rsi      # 0xc010
leaq 0x686f(%rip), %rdx      # 0xe798
leaq 0x68d5(%rip), %rcx      # 0xe805
movl $0x274, %r8d            # imm = 0x274
leaq 0x6c13(%rip), %r9       # 0xeb50
movb $0x0, %al
callq 0x31b0
movq 0xa0ad(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7f5f
jmp 0x7eca
movq -0x50(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x6510
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x7f96
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x7f98
movq -0x48(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x7fef
movq 0xa04e(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x405c(%rip), %rsi      # 0xc010
leaq 0x67dd(%rip), %rdx      # 0xe798
leaq 0x6843(%rip), %rcx      # 0xe805
movl $0x27b, %r8d            # imm = 0x27B
leaq 0x685f(%rip), %r9       # 0xe82e
movb $0x0, %al
callq 0x31b0
movq 0xa01b(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x7ff1
jmp 0x7ff3
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x7d53
jmp 0x7d1e
movq -0x10(%rbp), %rax
movl $0x2, 0x10(%rax)
movq -0x10(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
leaq -0x30(%rbp), %rdi
callq 0x3bc0
movq 0x9f92(%rip), %rdi      # 0x11fc0
callq 0x4a30
movq %rax, -0x8(%rbp)
jmp 0x804f
leaq -0x30(%rbp), %rdi
callq 0x3bc0
callq 0x30e0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)