// File: /workspace/github_c_makefile_star2_O3/WojciechMula[P]pyahocorasick/src/Automaton.c
// Function: automaton_add_word
static PyObject*
automaton_add_word(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)
    // argument
    PyObject* py_value = NULL;
    struct Input input;

    Py_ssize_t integer = 0;
    TrieNode* node;
    bool new_word;

    if (!prepare_input_from_tuple(self, args, 0, &input)) {
        return NULL;
    }

    switch (automaton->store) {
        case STORE_ANY:
            py_value = F(PyTuple_GetItem)(args, 1);
            if (not py_value) {
                PyErr_SetString(PyExc_ValueError, "A value object is required as second argument.");
                goto py_exception;
            }
            break;

        case STORE_INTS:
            py_value = F(PyTuple_GetItem)(args, 1);
            if (py_value) {
                if (F(PyNumber_Check)(py_value)) {
                    integer = F(PyNumber_AsSsize_t)(py_value, PyExc_ValueError);
                    if (integer == -1 and PyErr_Occurred())
                        goto py_exception;
                }
                else {
                    PyErr_SetString(PyExc_TypeError, "An integer value is required as second argument.");
                    goto py_exception;
                }
            }
            else {
                // default
                PyErr_Clear();
                integer = automaton->count + 1;
            }
            break;

        case STORE_LENGTH:
            integer = input.wordlen;
            break;

        default:
            PyErr_SetString(PyExc_SystemError, "Invalid value for this key: see documentation for supported values.");
            goto py_exception;
    }

    node = NULL;
    new_word = false;

    if (input.wordlen > 0) {
        node = trie_add_word(automaton, input.word, input.wordlen, &new_word);

        if (node == NULL) {
            PyErr_NoMemory();
            goto py_exception;
        }
    }

    destroy_input(&input);

    if (node) {
        switch (automaton->store) {
            case STORE_ANY:
                if (not new_word and node->eow)
                    // replace
                    Py_DECREF(node->output.object);

                Py_INCREF(py_value);
                node->output.object = py_value;
                break;

            default:
                node->output.integer = integer;
        } // switch

        if (new_word) {
            automaton->version += 1; // change version only when new word appeared
            if (input.wordlen > automaton->longest_word)
                automaton->longest_word = (int)input.wordlen;

            Py_RETURN_TRUE;
        }
        else {
            Py_RETURN_FALSE;
        }
    }

    Py_RETURN_FALSE;

py_exception:
    destroy_input(&input);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorl %r15d, %r15d
movq %rsi, %rdi
xorl %esi, %esi
callq 0x3140
testq %rax, %rax
je 0x6328
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x45cd
testb %al, %al
je 0x6325
movl 0x14(%rbx), %eax
cmpl $0xa, %eax
je 0x608f
cmpl $0x14, %eax
je 0x6082
cmpl $0x1e, %eax
jne 0x60d5
movl $0x1, %esi
movq %r14, %rdi
callq 0x3140
testq %rax, %rax
je 0x62bf
movq %rax, %r15
xorl %r12d, %r12d
jmp 0x60fa
movq 0x28(%rsp), %rax
xorl %r15d, %r15d
movq %rax, %r12
jmp 0x60ff
movl $0x1, %esi
movq %r14, %rdi
callq 0x3140
testq %rax, %rax
je 0x60eb
movq %rax, %r15
movq %rax, %rdi
callq 0x32e0
testl %eax, %eax
je 0x62f6
movq 0x9ecd(%rip), %rax      # 0xff88
movq (%rax), %rsi
movq %r15, %rdi
callq 0x3060
cmpq $-0x1, %rax
je 0x633a
movq %rax, %r12
jmp 0x60fa
movq 0x9e94(%rip), %rax      # 0xff70
movq (%rax), %rdi
leaq 0x5578(%rip), %rsi      # 0xb65e
jmp 0x6307
callq 0x3190
movslq 0x1c(%rbx), %r12
incq %r12
xorl %r15d, %r15d
movq 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
jle 0x6129
movq 0x30(%rsp), %r14
cmpl $0x0, 0x10(%rbx)
movq 0x28(%rbx), %r13
je 0x6138
movq %r12, 0x10(%rsp)
movq %r15, 0x8(%rsp)
movq %rbx, 0x18(%rsp)
jmp 0x617c
leaq 0x28(%rsp), %rdi
callq 0x48e4
jmp 0x62b3
testq %r13, %r13
jne 0x635a
movl $0x20, %edi
callq 0x3370
testq %rax, %rax
je 0x630e
movq %rax, %r13
movq %r12, 0x10(%rsp)
movq %r15, 0x8(%rsp)
xorl %eax, %eax
movq %rax, 0x18(%r13)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq %rax, 0xd(%r13)
movq %rbx, 0x18(%rsp)
movq %r13, 0x28(%rbx)
xorl %eax, %eax
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
movl (%r14,%rax,4), %ebp
movq %r13, %rdi
movl %ebp, %esi
callq 0x57ab
movq %rax, %r12
testq %rax, %rax
jne 0x6242
movl $0x20, %edi
callq 0x3370
testq %rax, %rax
je 0x6316
movq %rax, %r12
xorl %eax, %eax
movq %rax, 0x18(%r12)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq %rax, 0xd(%r12)
testq %r13, %r13
je 0x6350
movq 0x18(%r13), %rdi
movl 0x10(%r13), %r15d
testq %r15, %r15
je 0x6209
leaq (,%r15,4), %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
cmpl %ebp, (%rdi,%rcx)
je 0x61fd
addq $0xc, %rcx
cmpq %rcx, %rax
jne 0x61ed
jmp 0x6209
cmpq $0x0, 0x4(%rdi,%rcx)
jne 0x6355
movslq %r15d, %rax
leaq (%rax,%rax,2), %rax
leaq 0xc(,%rax,4), %rsi
callq 0x3150
testq %rax, %rax
je 0x62ec
movq %rax, 0x18(%r13)
movslq %r15d, %rcx
leaq (%rcx,%rcx,2), %rcx
movl %ebp, (%rax,%rcx,4)
movq %r12, 0x4(%rax,%rcx,4)
incl 0x10(%r13)
movq 0x20(%rsp), %r15
incl %ebx
movq %rbx, %rax
movq %r12, %r13
cmpq %rbx, %r15
ja 0x6185
movb 0x14(%r12), %bl
testb %bl, %bl
movq 0x18(%rsp), %r14
jne 0x626b
movb $0x1, 0x14(%r12)
incl 0x1c(%r14)
movl $0x1, 0x10(%r14)
leaq 0x28(%rsp), %rdi
callq 0x48e4
cmpl $0x1e, 0x14(%r14)
movq 0x10(%rsp), %rax
jne 0x62ab
testb %bl, %bl
je 0x62a3
cmpb $0x0, 0x14(%r12)
je 0x62a3
movq (%r12), %rdi
decq (%rdi)
jne 0x62a3
callq 0x30d0
movq 0x8(%rsp), %rax
incq (%rax)
movq %rax, (%r12)
testb %bl, %bl
je 0x62d2
movq 0x9cee(%rip), %r15      # 0xffa8
incq (%r15)
jmp 0x6328
movq 0x9cc2(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x532e(%rip), %rsi      # 0xb5fe
jmp 0x6307
incl 0x30(%r14)
movslq 0x20(%r14), %rax
cmpq %rax, %r15
jle 0x62e3
movl %r15d, 0x20(%r14)
movq 0x9cde(%rip), %r15      # 0xffc8
jmp 0x62ba
movq %r12, %rdi
callq 0x3070
jmp 0x6316
movq 0x9c93(%rip), %rax      # 0xff90
movq (%rax), %rdi
leaq 0x5326(%rip), %rsi      # 0xb62d
callq 0x3120
jmp 0x631b
movq $0x0, 0x28(%rbx)
callq 0x30e0
leaq 0x28(%rsp), %rdi
callq 0x48e4
xorl %r15d, %r15d
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x31f0
testq %rax, %rax
jne 0x631b
movq $-0x1, %r12
jmp 0x60fa
callq 0x409b
callq 0x4057
callq 0x4015