// File: /workspace/github_c_makefile_star2_O2/WojciechMula[P]pyahocorasick/src/AutomatonSearchIterLong.c
// Function: automaton_search_iter_long_next
static PyObject*
automaton_search_iter_long_next(PyObject* self) {
    PyObject* output = NULL;
    TrieNode* next;
    TrieNode* fail_node = NULL; //< the node to fail over
    int fail_index = -1;        //< the index to fail over
    uint8_t fail_flag = false;  //< whether this iteration is triggered by failover

    if (iter->version != iter->automaton->version) {
        PyErr_SetString(PyExc_ValueError, "underlaying automaton has changed, iterator is not valid anymore");
        return NULL;
    }

return_output:
    if (iter->last_node) {
        Py_XDECREF(output); // Release any existing reference to avoid memory leak
        output = automaton_build_output_iter_long(self);

        // start over, as we don't want overlapped results
        // Note: this leads to quadratic complexity in the worst case
        iter->state      = iter->automaton->root;
        iter->index      = iter->last_index;

        iter->last_node  = NULL;
        iter->last_index = -1;

        return output;
    }

    iter->index += 1;
    while (iter->index < iter->end) {
        if (fail_flag) {
            // when failover start from fail node instead of next
            next = fail_node->fail;
            iter->index = fail_index;
            fail_node = NULL;
            fail_index = -1;
            fail_flag = false;
        } else {
            next = trienode_get_next(iter->state, iter->input.word[iter->index]);
        }
        if (next) {
            if (next->eow) {
                // save the last node on the path
                iter->last_node  = next;
                iter->last_index = iter->index;
            } else if (!iter->last_node && !fail_node && next->fail && next->fail != iter->automaton->root && next->fail->eow) {
                fail_node = next;
                fail_index = iter->index;
            }

            iter->state = next;
            iter->index += 1;
        } else {
            if (iter->last_node) {
                goto return_output;
            } else {
                if (fail_node) {
                  // failover
                  fail_flag = true;
                  continue;
                }
                while (true) {
                    iter->state = iter->state->fail;
                    if (iter->state == NULL) {
                        iter->state = iter->automaton->root;
                        iter->index += 1;
                        break;
                    } else if (trienode_get_next(iter->state, iter->input.word[iter->index])) {
                        break;
                    }
                }
            }
        }
    } // while 

    if (iter->last_node) {
        goto return_output;
    }
    
    return NULL;    // StopIteration
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x18(%rdi), %eax
movq 0x10(%rdi), %rcx
movq %rcx, (%rsp)
cmpl 0x30(%rcx), %eax
jne 0x76d9
movq %rdi, %rbx
movq 0x50(%rdi), %r15
pushq $-0x1
popq %rbp
xorl %r12d, %r12d
xorl %eax, %eax
testq %r15, %r15
jne 0x76f3
movl 0x5c(%rbx), %r13d
movl 0x64(%rbx), %r14d
incl %r13d
movl %r13d, 0x5c(%rbx)
xorl %r15d, %r15d
cmpl %r14d, %r13d
jge 0x76ce
testb %al, %al
je 0x7604
movq 0x8(%r12), %rax
movl %ebp, 0x5c(%rbx)
movl %ebp, %r13d
xorl %r12d, %r12d
pushq $-0x1
popq %rbp
jmp 0x7617
movq 0x30(%rbx), %rax
movq 0x48(%rbx), %rdi
movslq %r13d, %rcx
movl (%rax,%rcx,4), %esi
callq 0x4d53
testq %rax, %rax
je 0x7632
cmpb $0x0, 0x14(%rax)
je 0x767a
movq %rax, 0x50(%rbx)
movl %r13d, 0x58(%rbx)
movq %rax, %r15
jmp 0x76bc
testq %r15, %r15
jne 0x75c7
movb $0x1, %al
movl $0x0, %r15d
testq %r12, %r12
jne 0x75e4
movq 0x48(%rbx), %r15
movslq %r13d, %r12
movq 0x8(%r15), %r15
movq %r15, 0x48(%rbx)
testq %r15, %r15
je 0x76ae
movq 0x30(%rbx), %rax
movl (%rax,%r12,4), %esi
movq %r15, %rdi
callq 0x4d53
testq %rax, %rax
je 0x764b
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x75e4
movq %r15, %rcx
orq %r12, %rcx
jne 0x76bc
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x76b6
movq (%rsp), %rdx
cmpq 0x28(%rdx), %rcx
je 0x76b6
cmpb $0x0, 0x14(%rcx)
movq %rax, %r12
movl $0x0, %ecx
cmoveq %rcx, %r12
cmovnel %r13d, %ebp
xorl %r15d, %r15d
jmp 0x76bc
movq (%rsp), %rax
movq 0x28(%rax), %rax
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rax, 0x48(%rbx)
incl %r13d
movl %r13d, 0x5c(%rbx)
xorl %eax, %eax
jmp 0x75e4
testq %r15, %r15
jne 0x75c9
jmp 0x76ef
movq 0x68a8(%rip), %rax      # 0xdf88
movq (%rax), %rdi
leaq 0x3346(%rip), %rsi      # 0xaa30
callq 0x3120
xorl %eax, %eax
jmp 0x7767
movq (%rsp), %rax
movl 0x14(%rax), %eax
cmpl $0xa, %eax
je 0x7709
cmpl $0x1e, %eax
je 0x771b
cmpl $0x14, %eax
jne 0x7734
movl 0x58(%rbx), %esi
addl 0x60(%rbx), %esi
movq (%r15), %rdx
leaq 0x31c5(%rip), %rdi      # 0xa8de
jmp 0x772b
movl 0x58(%rbx), %esi
addl 0x60(%rbx), %esi
movq (%r15), %rdx
leaq 0x3864(%rip), %rdi      # 0xaf8f
xorl %eax, %eax
callq 0x3330
jmp 0x774c
movq 0x684d(%rip), %rax      # 0xdf88
movq (%rax), %rdi
leaq 0x336d(%rip), %rsi      # 0xaab2
callq 0x3120
xorl %eax, %eax
movq 0x10(%rbx), %rcx
movq 0x28(%rcx), %rcx
movq %rcx, 0x48(%rbx)
movl 0x58(%rbx), %ecx
movl %ecx, 0x5c(%rbx)
andq $0x0, 0x50(%rbx)
orl $-0x1, 0x58(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq