// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movb $0x0, -0x2d(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
cmpl 0x30(%rcx), %eax
je 0xa01e
movq 0x7f86(%rip), %rax      # 0x11f88
movq (%rax), %rdi
leaq 0x4ac8(%rip), %rsi      # 0xead4
callq 0x3120
movq $0x0, -0x8(%rbp)
jmp 0xa255
jmp 0xa020
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xa087
movq -0x18(%rbp), %rdi
callq 0xa260
movq -0x10(%rbp), %rdi
callq 0xa290
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x5c(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x58(%rax) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa255
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x64(%rcx), %eax
jge 0xa23d
cmpb $0x0, -0x2d(%rbp)
je 0xa0d9
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x5c(%rax)
movq $0x0, -0x28(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movb $0x0, -0x2d(%rbp)
jmp 0xa0fd
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x5c(%rcx), %rcx
movl (%rax,%rcx,4), %esi
callq 0x6510
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xa1a4
movq -0x20(%rbp), %rax
cmpb $0x0, 0x14(%rax)
je 0xa12e
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x58(%rax)
jmp 0xa186
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0xa184
cmpq $0x0, -0x28(%rbp)
jne 0xa184
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xa184
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
cmpq 0x28(%rcx), %rax
je 0xa184
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
je 0xa184
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0xa186
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
jmp 0xa238
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xa1b4
jmp 0xa020
cmpq $0x0, -0x28(%rbp)
je 0xa1c4
movb $0x1, -0x2d(%rbp)
jmp 0xa094
jmp 0xa1c6
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0xa208
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x5c(%rax)
jmp 0xa234
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x5c(%rcx), %rcx
movl (%rax,%rcx,4), %esi
callq 0x6510
cmpq $0x0, %rax
je 0xa230
jmp 0xa234
jmp 0xa232
jmp 0xa1c6
jmp 0xa236
jmp 0xa238
jmp 0xa094
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0xa24d
jmp 0xa020
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nop