// File: /workspace/github_c_makefile_star2_O0/WojciechMula[P]pyahocorasick/src/trie.c
// Function: trie_remove_word
static PyObject*
trie_remove_word(Automaton* automaton, const TRIE_LETTER_TYPE* word, const size_t wordlen) {

    PyObject* object;
    TrieNode* node;
    TrieNode* tmp;
    TrieNode* last_multiway;
    unsigned last_multiway_index;
    unsigned i;

    if (automaton->root == NULL) {
        return NULL;
    }

    node = automaton->root;

    last_multiway = node;
    last_multiway_index = 0;
    for (i=0; i < wordlen; i++) {
        const TRIE_LETTER_TYPE letter = word[i];

        node = trienode_get_next(node, letter);
        if (node == NULL) {
            return NULL;
        }

        // Save the last node along path which has more children
        // or is a terminating node.
        if (node->n > 1 || (node->n == 1 && node->eow)) {
            last_multiway = node;
            last_multiway_index = i + 1;
        }
    }

    if (node->eow != true) {
        return NULL;
    }

    object = node->output.object;

    if (trienode_is_leaf(node)) {
        // Remove a linear list that starts at the last_multiway node
        // and ends at the last [found] one.

        // 1. Unlink the tail from the trie
        node = trienode_get_next(last_multiway, word[last_multiway_index]);
        ASSERT(node != NULL);

        if (UNLIKELY(trienode_unset_next_pointer(last_multiway, node) == MEMORY_ERROR)) {
            PyErr_NoMemory();
            return NULL;
        }

        // 2. Free the tail (reference to value from the last element was already saved)
        for (i = last_multiway_index + 1; i < wordlen; i++) {
            tmp = trienode_get_next(node, word[i]);
            ASSERT(tmp->n <= 1);
            trienode_free(node);
            node = tmp;
        }

        trienode_free(node);

    } else {
        // just unmark the terminating node
        node->eow = false;
    }

    automaton->kind = TRIE;
    return object;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x90cc
movq $0x0, -0x8(%rbp)
jmp 0x9302
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x9167
movq -0x18(%rbp), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x6510
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x9128
movq $0x0, -0x8(%rbp)
jmp 0x9302
movq -0x30(%rbp), %rax
cmpl $0x1, 0x10(%rax)
ja 0x9149
movq -0x30(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x915a
movq -0x30(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
je 0x915a
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x915c
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x90ee
movq -0x30(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x1, %eax
je 0x9181
movq $0x0, -0x8(%rbp)
jmp 0x9302
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x92e7
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %esi
callq 0x6510
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x9204
movq 0x8e39(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x2e47(%rip), %rsi      # 0xc010
leaq 0x33fa(%rip), %rdx      # 0xc5ca
leaq 0x557b(%rip), %rcx      # 0xe752
movl $0x70, %r8d
leaq 0x557f(%rip), %r9       # 0xe763
movb $0x0, %al
callq 0x31b0
movq 0x8e06(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x9206
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x9310
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x9238
callq 0x30e0
movq $0x0, -0x8(%rbp)
jmp 0x9302
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x92dc
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,4), %esi
callq 0x6510
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jbe 0x92bb
movq 0x8d82(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x2d90(%rip), %rsi      # 0xc010
leaq 0x3343(%rip), %rdx      # 0xc5ca
leaq 0x54c4(%rip), %rcx      # 0xe752
movl $0x7a, %r8d
leaq 0x54d5(%rip), %r9       # 0xe770
movb $0x0, %al
callq 0x31b0
movq 0x8d4f(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
callq 0x3230
movl $0x1, %edi
callq 0x3310
jmp 0x92bd
movq -0x30(%rbp), %rdi
callq 0x51a0
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9241
movq -0x30(%rbp), %rdi
callq 0x51a0
jmp 0x92ef
movq -0x30(%rbp), %rax
movb $0x0, 0x14(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)