// File: /workspace/github_c_makefile_star2_O3/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), %r13
cmpl 0x30(%r13), %eax
jne 0x79c6
movq %rdi, %rbx
movq 0x50(%rdi), %rbp
testq %rbp, %rbp
je 0x79e3
movl 0x14(%r13), %eax
cmpl $0xa, %eax
je 0x79b0
cmpl $0x1e, %eax
je 0x7b2f
cmpl $0x14, %eax
jne 0x7b49
movl 0x58(%rbx), %esi
addl 0x60(%rbx), %esi
movq (%rbp), %rdx
leaq 0x3f2d(%rip), %rdi      # 0xb8ee
jmp 0x7b40
movq 0x85bb(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x4069(%rip), %rsi      # 0xba40
callq 0x3120
xorl %eax, %eax
jmp 0x7b82
movl 0x5c(%rbx), %r11d
movl 0x64(%rbx), %r14d
incl %r11d
movl %r11d, 0x5c(%rbx)
cmpl %r14d, %r11d
jge 0x79dc
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
xorl %eax, %eax
xorl %r15d, %r15d
movq %rbx, (%rsp)
testb %al, %al
je 0x7a22
movq 0x8(%r15), %rax
movl %r12d, 0x5c(%rbx)
movl %r12d, %r11d
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x7a3f
movq 0x30(%rbx), %rax
movq 0x48(%rbx), %rdi
movslq %r11d, %rcx
movl (%rax,%rcx,4), %esi
movl %r11d, %ebx
callq 0x57ab
movl %ebx, %r11d
movq (%rsp), %rbx
testq %rax, %rax
je 0x7a5a
cmpb $0x0, 0x14(%rax)
je 0x7a71
movq %rax, 0x50(%rbx)
movl %r11d, 0x58(%rbx)
movq %rax, %rbp
jmp 0x7b11
testq %rbp, %rbp
jne 0x7995
testq %r15, %r15
je 0x7aa8
movb $0x1, %al
xorl %ebp, %ebp
jmp 0x7b1e
movq %rbp, %rcx
orq %r15, %rcx
jne 0x7b11
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x7b0c
cmpq 0x28(%r13), %rcx
je 0x7b0c
cmpb $0x0, 0x14(%rcx)
movq %rax, %r15
movl $0x0, %ecx
cmoveq %rcx, %r15
cmovnel %r11d, %r12d
xorl %ebp, %ebp
jmp 0x7b11
movq 0x48(%rbx), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x7af6
movq 0x30(%rbx), %rcx
movslq %r11d, %rdx
movl (%rcx,%rdx,4), %ecx
movl 0x10(%rax), %esi
testq %rsi, %rsi
je 0x7aed
movq 0x18(%rax), %rdx
shlq $0x2, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
cmpl %ecx, (%rdx,%rdi)
je 0x7ae5
addq $0xc, %rdi
cmpq %rdi, %rsi
jne 0x7ad5
jmp 0x7aed
cmpq $0x0, 0x4(%rdx,%rdi)
jne 0x7b29
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x7abf
movq 0x28(%r13), %rax
movq %rax, 0x48(%rbx)
incl %r11d
movl %r11d, 0x5c(%rbx)
xorl %ebp, %ebp
xorl %r15d, %r15d
jmp 0x7b1c
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rax, 0x48(%rbx)
incl %r11d
movl %r11d, 0x5c(%rbx)
xorl %eax, %eax
cmpl %r14d, %r11d
jl 0x7a08
jmp 0x7b91
movq %rax, 0x48(%rbx)
jmp 0x7b05
movl 0x58(%rbx), %esi
addl 0x60(%rbx), %esi
movq (%rbp), %rdx
leaq 0x445f(%rip), %rdi      # 0xbf9f
xorl %eax, %eax
callq 0x3330
jmp 0x7b61
movq 0x8438(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3f68(%rip), %rsi      # 0xbac2
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
testq %rbp, %rbp
jne 0x7995
jmp 0x79dc