// File: /workspace/github_c_makefile_star2_O1/WojciechMula[P]pyahocorasick/src/Automaton.c
// Function: automaton_items_create
static PyObject*
automaton_items_create(PyObject* self, PyObject* args, const ItemsType type) {
#define automaton ((Automaton*)self)
    PyObject* arg1 = NULL;
    PyObject* arg2 = NULL;
    PyObject* arg3 = NULL;
    TRIE_LETTER_TYPE* word = NULL;
    TRIE_LETTER_TYPE* tmp = NULL;
    Py_ssize_t wordlen = 0;

    TRIE_LETTER_TYPE wildcard;
    bool use_wildcard = false;
    PatternMatchType matchtype = MATCH_AT_LEAST_PREFIX;

    AutomatonItemsIter* iter;

    bool word_is_copy = false;
    bool tmp_is_copy = false;

    // arg 1: prefix/prefix pattern
    if (args)
        arg1 = F(PyTuple_GetItem)(args, 0);
    else
        arg1 = NULL;

    if (arg1) {
        arg1 = pymod_get_string(arg1, &word, &wordlen, &word_is_copy);
        if (arg1 == NULL)
            goto error;
    }
    else {
        PyErr_Clear();
        word = NULL;
        wordlen = 0;
    }

    // arg 2: wildcard
    if (args)
        arg2 = F(PyTuple_GetItem)(args, 1);
    else
        arg2 = NULL;

    if (arg2) {
        Py_ssize_t len = 0;

        arg2 = pymod_get_string(arg2, &tmp, &len, &tmp_is_copy);
        if (arg2 == NULL) {
            goto error;
        } else {
            if (len == 1) {
                wildcard = tmp[0];
                use_wildcard = true;
            }
            else {
                PyErr_SetString(PyExc_ValueError, "Wildcard must be a single character.");
                goto error;
            }
        }
    }
    else {
        PyErr_Clear();
        wildcard = 0;
        use_wildcard = false;
    }

    // arg3: matchtype
    matchtype = MATCH_AT_LEAST_PREFIX;
    if (args) {
        arg3 = F(PyTuple_GetItem)(args, 2);
        if (arg3) {
            Py_ssize_t val = F(PyNumber_AsSsize_t)(arg3, PyExc_OverflowError);
            if (val == -1 and PyErr_Occurred())
                goto error;

            switch ((PatternMatchType)val) {
                case MATCH_AT_LEAST_PREFIX:
                case MATCH_AT_MOST_PREFIX:
                case MATCH_EXACT_LENGTH:
                    matchtype = (PatternMatchType)val;
                    break;

                default:
                    PyErr_SetString(PyExc_ValueError,
                        "The optional how third argument must be one of: "
                        "MATCH_EXACT_LENGTH, MATCH_AT_LEAST_PREFIX or MATCH_AT_LEAST_PREFIX"
                    );
                    goto error;
            }
        }
        else {
            PyErr_Clear();
            if (use_wildcard)
                matchtype = MATCH_EXACT_LENGTH;
            else
                matchtype = MATCH_AT_LEAST_PREFIX;
        }
    }

    //
    iter = (AutomatonItemsIter*)automaton_items_iter_new(
                    automaton,
                    word,
                    wordlen,
                    use_wildcard,
                    wildcard,
                    matchtype);

    maybe_decref(word_is_copy, arg1)
    maybe_decref(tmp_is_copy, arg2)
    maybe_free(word_is_copy, word)
    maybe_free(tmp_is_copy, tmp)

    if (iter) {
        iter->type = type;
        return (PyObject*)iter;
    }
    else
        return NULL;


error:
    maybe_decref(word_is_copy, arg1)
    maybe_decref(tmp_is_copy, arg2)
    maybe_free(word_is_copy, word)
    maybe_free(tmp_is_copy, tmp)
    return NULL;
#undef automaton
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, 0x30(%rsp)
movb %al, 0xf(%rsp)
movb %al, 0xe(%rsp)
testq %rsi, %rsi
je 0x590d
movq %r13, %rdi
xorl %esi, %esi
callq 0x3140
testq %rax, %rax
je 0x593c
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0xf(%rsp), %rcx
movq %rax, %rdi
callq 0x4864
movq %rax, %r14
testq %rax, %rax
jne 0x594e
xorl %r15d, %r15d
xorl %r14d, %r14d
jmp 0x5b33
callq 0x3190
xorl %r14d, %r14d
movq %r14, 0x20(%rsp)
movq %r14, 0x30(%rsp)
testq %r13, %r13
je 0x5962
movl $0x1, %esi
movq %r13, %rdi
callq 0x3140
jmp 0x5964
xorl %eax, %eax
testq %rax, %rax
movq %rbx, 0x38(%rsp)
je 0x59ae
leaq 0x40(%rsp), %rdx
movq $0x0, (%rdx)
leaq 0x18(%rsp), %rsi
leaq 0xe(%rsp), %rcx
movq %rax, %rdi
callq 0x4864
movq %rax, %r15
testq %rax, %rax
je 0x59d8
cmpq $0x1, 0x40(%rsp)
jne 0x59c2
movq 0x18(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movb $0x1, %bpl
movb $0x1, %al
jmp 0x59dc
callq 0x3190
xorl %r15d, %r15d
movl $0x0, 0x14(%rsp)
xorl %ebp, %ebp
jmp 0x59e4
movq 0xa5bf(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x40d4(%rip), %rsi      # 0x9aa7
callq 0x3120
xorl %ebp, %ebp
xorl %eax, %eax
testb %al, %al
je 0x5b33
testq %r13, %r13
je 0x5a35
movl $0x2, %esi
movq %r13, %rdi
callq 0x3140
testq %rax, %rax
je 0x5a3c
movq 0xa5b6(%rip), %rcx      # 0xffb8
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x3060
movq %rax, %rbx
cmpq $-0x1, %rax
je 0x5a4c
movb $0x1, %al
cmpl $0x3, %ebx
jb 0x5a5d
movq 0xa564(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x409e(%rip), %rsi      # 0x9acc
callq 0x3120
jmp 0x5a56
movl $0x2, %ebx
jmp 0x5a65
callq 0x3190
movl %ebp, %eax
xorb $0x1, %al
movzbl %al, %ebx
addl %ebx, %ebx
jmp 0x5a65
callq 0x31f0
testq %rax, %rax
je 0x5a16
movl $0x2, %ebx
xorl %eax, %eax
testb %al, %al
je 0x5b33
movl %r12d, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %r12
leaq 0xac68(%rip), %rdi      # 0x106e8
callq 0x32b0
testq %rax, %rax
je 0x5be1
movq %rax, %r13
movq 0x38(%rsp), %rcx
movq %rcx, 0x10(%rax)
movl 0x30(%rcx), %eax
movl %eax, 0x18(%r13)
xorl %eax, %eax
movq %rax, 0x20(%r13)
movl $0x0, 0x40(%r13)
movq %rax, 0x48(%r13)
movq %rax, 0x58(%r13)
movb %bpl, 0x60(%r13)
movq %rcx, %rbp
movl 0x14(%rsp), %eax
movl %eax, 0x64(%r13)
movl %ebx, 0x68(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%r13)
incq (%rcx)
movl 0x20(%rcx), %eax
leal 0x4(,%rax,4), %eax
movslq %eax, %rdi
callq 0x3370
movq %rax, 0x48(%r13)
testq %rax, %rax
je 0x5bd9
cmpq $0x0, 0x28(%rsp)
je 0x5b90
leaq (,%r12,4), %rbx
movq %rbx, %rdi
callq 0x3370
movq %rax, 0x58(%r13)
testq %rax, %rax
je 0x5bd9
movq %r12, 0x50(%r13)
movq %rax, %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
callq 0x31e0
jmp 0x5b98
testq %r14, %r14
je 0x5b4c
cmpb $0x0, 0xf(%rsp)
jne 0x5b4c
decq (%r14)
jne 0x5b4c
movq %r14, %rdi
callq 0x30d0
testq %r15, %r15
je 0x5b65
cmpb $0x0, 0xe(%rsp)
jne 0x5b65
decq (%r15)
jne 0x5b65
movq %r15, %rdi
callq 0x30d0
cmpb $0x1, 0xf(%rsp)
jne 0x5b76
movq 0x20(%rsp), %rdi
callq 0x3070
cmpb $0x1, 0xe(%rsp)
jne 0x5c47
movq 0x18(%rsp), %rdi
callq 0x3070
jmp 0x5c47
movq $0x0, 0x50(%r13)
movl $0x20, %edi
callq 0x3370
testq %rax, %rax
je 0x5bd9
movq %r13, %rcx
addq $0x30, %rcx
xorl %edx, %edx
movq %rdx, (%rax)
movq 0x28(%rbp), %rsi
movq %rsi, 0x8(%rax)
movq %rdx, 0x18(%rax)
movq (%rcx), %rdx
testq %rdx, %rdx
je 0x5bcc
movq %rdx, (%rax)
jmp 0x5bd0
movq %rax, 0x38(%r13)
movl 0x10(%rsp), %ebx
movq %rax, (%rcx)
jmp 0x5be8
movq %r13, %rdi
callq 0x40f4
xorl %r13d, %r13d
movl 0x10(%rsp), %ebx
testq %r14, %r14
je 0x5c01
cmpb $0x0, 0xf(%rsp)
jne 0x5c01
decq (%r14)
jne 0x5c01
movq %r14, %rdi
callq 0x30d0
testq %r15, %r15
je 0x5c1a
cmpb $0x0, 0xe(%rsp)
jne 0x5c1a
decq (%r15)
jne 0x5c1a
movq %r15, %rdi
callq 0x30d0
cmpb $0x1, 0xf(%rsp)
jne 0x5c2b
movq 0x28(%rsp), %rdi
callq 0x3070
cmpb $0x1, 0xe(%rsp)
jne 0x5c3c
movq 0x18(%rsp), %rdi
callq 0x3070
testq %r13, %r13
je 0x5c47
movl %ebx, 0x40(%r13)
jmp 0x5c4a
xorl %r13d, %r13d
movq %r13, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq