// File: /workspace/github_c_makefile_star2_O1/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
cmpl 0x30(%rcx), %eax
jne 0x7bb6
movq %rdi, %rbx
movq 0x50(%rdi), %r14
testq %r14, %r14
jne 0x7be2
movq %rcx, (%rsp)
movl 0x5c(%rbx), %r12d
movl 0x64(%rbx), %r15d
incl %r12d
movl %r12d, 0x5c(%rbx)
cmpl %r15d, %r12d
jge 0x7bd3
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ebp, %ebp
testb %al, %al
je 0x7ac3
movq 0x8(%rbp), %rax
movl %r13d, 0x5c(%rbx)
movl %r13d, %r12d
xorl %ebp, %ebp
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x7ad6
movq 0x30(%rbx), %rax
movq 0x48(%rbx), %rdi
movslq %r12d, %rcx
movl (%rax,%rcx,4), %esi
callq 0x57b7
testq %rax, %rax
je 0x7af5
cmpb $0x0, 0x14(%rax)
je 0x7b65
movq %rax, 0x50(%rbx)
movl %r12d, 0x58(%rbx)
movq %rax, %r14
jmp 0x7b98
testq %r14, %r14
jne 0x7bde
movb $0x1, %al
testq %rbp, %rbp
jne 0x7ba5
movq 0x48(%rbx), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x7b57
movq 0x30(%rbx), %rcx
movslq %r12d, %rdx
movl (%rcx,%rdx,4), %ecx
movl 0x10(%rax), %esi
testq %rsi, %rsi
je 0x7b4e
movq 0x18(%rax), %rdx
shlq $0x2, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
cmpl %ecx, (%rdx,%rdi)
je 0x7b46
addq $0xc, %rdi
cmpq %rdi, %rsi
jne 0x7b36
jmp 0x7b4e
cmpq $0x0, 0x4(%rdx,%rdi)
jne 0x7bb0
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x7b20
movq %rax, 0x48(%rbx)
movq (%rsp), %rax
movq 0x28(%rax), %rax
jmp 0x7b98
movq %r14, %rcx
orq %rbp, %rcx
jne 0x7b98
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x7b96
movq (%rsp), %rdx
cmpq 0x28(%rdx), %rcx
je 0x7b96
cmpb $0x0, 0x14(%rcx)
movq %rax, %rbp
movl $0x0, %ecx
cmoveq %rcx, %rbp
cmovnel %r12d, %r13d
jmp 0x7b98
xorl %ebp, %ebp
movq %rax, 0x48(%rbx)
incl %r12d
movl %r12d, 0x5c(%rbx)
xorl %eax, %eax
cmpl %r15d, %r12d
jl 0x7aaa
jmp 0x7bd3
movq %rax, 0x48(%rbx)
jmp 0x7ba3
movq 0x83cb(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3e69(%rip), %rsi      # 0xba30
callq 0x3120
xorl %eax, %eax
jmp 0x7c58
testq %r14, %r14
movq (%rsp), %rcx
jne 0x7be2
jmp 0x7bcc
movq (%rsp), %rcx
movl 0x14(%rcx), %eax
cmpl $0xa, %eax
je 0x7bf4
cmpl $0x1e, %eax
je 0x7c06
cmpl $0x14, %eax
jne 0x7c1f
movl 0x58(%rbx), %esi
addl 0x60(%rbx), %esi
movq (%r14), %rdx
leaq 0x3cda(%rip), %rdi      # 0xb8de
jmp 0x7c16
movl 0x58(%rbx), %esi
addl 0x60(%rbx), %esi
movq (%r14), %rdx
leaq 0x4379(%rip), %rdi      # 0xbf8f
xorl %eax, %eax
callq 0x3330
jmp 0x7c37
movq 0x8362(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3e82(%rip), %rsi      # 0xbab2
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)
movq $0x0, 0x50(%rbx)
movl $0xffffffff, 0x58(%rbx) # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq