// File: /workspace/github_c_makefile_star2_O3/WojciechMula[P]pyahocorasick/src/Automaton_pickle.c
// Function: automaton___reduce__
static PyObject*
automaton___reduce__(PyObject* self, PyObject* args) {
#define automaton ((Automaton*)self)

#define MB ((size_t)(1024*1024))

    const size_t array_size = 16*MB;

    DumpState   state;
    PickleData  data;
    PyObject*   tuple;

    // 0. for an empty automaton do nothing
    if (automaton->count == 0) {
        // the class constructor feed with an empty argument build an empty automaton
        return F(Py_BuildValue)("O()", Py_TYPE(self));
    }

    // 1. numerate nodes
    state.id        = 0;
    state.failed_on = NULL;
    state.total_size = 0;

    trie_traverse(automaton->root, pickle_dump_replace_fail_with_id, &state);
    if (state.failed_on) {
        // revert changes (partial)
        trie_traverse(automaton->root, pickle_dump_revert_replace, &state);

        // and set error
        PyErr_NoMemory();
        return NULL;
    }

    // 2. gather data
    if (!pickle_data__init(&data, automaton->store, state.total_size, array_size))
        goto exception;

    trie_traverse(automaton->root, pickle_dump_save, &data);
    if (UNLIKELY(data.error)) {
        goto exception;
    }

    if (UNLIKELY(!pickle_data__shrink_last_buffer(&data))) {
        goto exception;
    }

    if (automaton->store != STORE_ANY) { // always pickle a Python object
        data.values = Py_None;
        Py_INCREF(data.values);
    }

    /* 3: save tuple:
        * binary data
        * automaton->kind
        * automaton->store
        * automaton->key_type
        * automaton->count
        * automaton->longest_word
        * list of values
    */

    tuple = F(Py_BuildValue)(
        "O(OiiiiiO)",
        Py_TYPE(self),
        data.bytes_list,
        automaton->kind,
        automaton->store,
        automaton->key_type,
        automaton->count,
        automaton->longest_word,
        data.values
    );

    if (data.values == Py_None) {
        data.values = NULL;
    }

    if (UNLIKELY(tuple == NULL)) {
        goto exception;
    }

    // revert all changes
    trie_traverse(automaton->root, pickle_dump_undo_replace, NULL);

    return tuple;

exception:
    // revert all changes
    trie_traverse(automaton->root, pickle_dump_undo_replace, NULL);

    // and free memory
    pickle_data__cleanup(&data);
    return NULL;
#undef automaton
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rdi, %r14
cmpl $0x0, 0x1c(%rdi)
je 0x6fbe
leaq 0x28(%r14), %rbx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq 0x28(%r14), %rdi
leaq 0xf01(%rip), %rsi       # 0x7e8f
movq %r15, %rdx
callq 0x5696
cmpq $0x0, 0x10(%r15)
je 0x6fdc
movq (%rbx), %rdi
leaq 0xf39(%rip), %rsi       # 0x7ee0
leaq 0x40(%rsp), %rdx
callq 0x5696
callq 0x30e0
xorl %r14d, %r14d
jmp 0x718b
movq 0x8(%r14), %rsi
leaq 0x4d4e(%rip), %rdi      # 0xbd17
xorl %eax, %eax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3330
movl 0x14(%r14), %ebp
movq 0x48(%rsp), %r12
movb $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x29(%rsp)
testq %r12, %r12
je 0x71bb
xorl %edi, %edi
callq 0x3030
movq %rax, (%rsp)
testq %rax, %rax
je 0x71a3
cmpl $0x1e, %ebp
jne 0x7036
movq %rax, %r15
xorl %edi, %edi
callq 0x3030
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x719b
leaq 0x8(%r12), %rax
cmpq $0x1000001, %r12        # imm = 0x1000001
movl $0x1000000, %ecx        # imm = 0x1000000
cmovbq %rax, %rcx
movq %rsp, %rdi
movq %rcx, 0x10(%rdi)
setae 0x8(%rdi)
callq 0x8054
testb %al, %al
je 0x71a3
movq (%rbx), %rdi
leaq 0xe9a(%rip), %rsi       # 0x7f07
movq %rsp, %r15
movq %r15, %rdx
callq 0x5696
cmpb $0x0, 0x38(%r15)
jne 0x71a3
movq 0x28(%rsp), %rax
cmpq 0x10(%rsp), %rax
jae 0x7107
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x71c0
movq 0x8(%rdi), %rax
testb $0x2, 0xab(%rax)
je 0x71c5
movq 0x10(%rdi), %r15
decq %r15
movq %r15, %rsi
callq 0x3360
testq %rax, %rax
je 0x71a3
movq 0x8(%rax), %rcx
testb $0x8, 0xab(%rcx)
je 0x71e4
addq $0x20, %rax
movq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x30c0
testq %rax, %rax
je 0x71a3
movq (%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3210
testl %eax, %eax
js 0x71a3
movl 0x14(%r14), %r8d
cmpl $0x1e, %r8d
jne 0x7118
movq 0x30(%rsp), %r10
jmp 0x7127
movq 0x8ea1(%rip), %r10      # 0xffc0
movq %r10, 0x30(%rsp)
incq (%r10)
movq 0x8(%r14), %rsi
movq (%rsp), %rdx
movl 0x10(%r14), %ecx
movl 0x18(%r14), %r9d
movl 0x1c(%r14), %r11d
movl 0x20(%r14), %r14d
subq $0x8, %rsp
leaq 0x4bd1(%rip), %rdi      # 0xbd1b
xorl %eax, %eax
pushq %r10
pushq %r14
pushq %r11
callq 0x3330
addq $0x20, %rsp
movq %rax, %r14
movq 0x30(%rsp), %rax
cmpq 0x8e56(%rip), %rax      # 0xffc0
jne 0x7175
movq $0x0, 0x30(%rsp)
testq %r14, %r14
je 0x71a3
movq (%rbx), %rdi
leaq 0xe9a(%rip), %rsi       # 0x801e
xorl %edx, %edx
callq 0x5696
movq %r14, %rax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x42b9
movq %rsp, %rsi
leaq 0x58(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x42ca
movq (%r14), %r14
jmp 0x718b
callq 0x4316
callq 0x4277
leaq 0x4c59(%rip), %rdi      # 0xbe25
leaq 0x4b7b(%rip), %rsi      # 0xbd4e
leaq 0x4c6a(%rip), %rcx      # 0xbe44
movl $0x4a, %edx
callq 0x3130
leaq 0x2420(%rip), %rdi      # 0x960b
leaq 0x4b5c(%rip), %rsi      # 0xbd4e
leaq 0x4c4b(%rip), %rcx      # 0xbe44
movl $0x51, %edx
callq 0x3130