// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x68, %rsp
movq %rdi, %r14
cmpl $0x0, 0x1c(%rdi)
je 0x706d
leaq 0x28(%r14), %rbx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq 0x28(%r14), %rdi
leaq 0xf1c(%rip), %rsi       # 0x7f59
movq %r15, %rdx
callq 0x56a1
cmpq $0x0, 0x10(%r15)
je 0x7089
movq (%rbx), %rdi
leaq 0xf54(%rip), %rsi       # 0x7faa
leaq 0x40(%rsp), %rdx
callq 0x56a1
callq 0x30e0
xorl %r14d, %r14d
jmp 0x7237
movq 0x8(%r14), %rsi
leaq 0x4c8f(%rip), %rdi      # 0xbd07
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x3330
movl 0x14(%r14), %ebp
movq 0x48(%rsp), %r15
movq $0x0, (%rsp)
movb $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x29(%rsp)
testq %r15, %r15
je 0x7265
xorl %edi, %edi
callq 0x3030
movq %rax, (%rsp)
testq %rax, %rax
je 0x724d
cmpl $0x1e, %ebp
jne 0x70e8
xorl %edi, %edi
callq 0x3030
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x7245
leaq 0x8(%r15), %rax
cmpq $0x1000001, %r15        # imm = 0x1000001
movl $0x1000000, %ecx        # imm = 0x1000000
cmovbq %rax, %rcx
movq %rsp, %rdi
movq %rcx, 0x10(%rdi)
setae 0x8(%rdi)
callq 0x8121
testb %al, %al
je 0x724d
movq (%rbx), %rdi
leaq 0xeb3(%rip), %rsi       # 0x7fd1
movq %rsp, %r15
movq %r15, %rdx
callq 0x56a1
cmpb $0x0, 0x38(%r15)
jne 0x724d
movq 0x28(%rsp), %rax
cmpq 0x10(%rsp), %rax
jae 0x71b8
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x726a
movq 0x8(%rdi), %rax
testb $0x2, 0xab(%rax)
je 0x726f
movq 0x10(%rdi), %r15
decq %r15
movq %r15, %rsi
callq 0x3360
testq %rax, %rax
je 0x724d
movq 0x8(%rax), %rcx
testb $0x8, 0xab(%rcx)
je 0x728e
addq $0x20, %rax
movq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x30c0
testq %rax, %rax
je 0x724d
movq (%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3210
testl %eax, %eax
js 0x724d
movl 0x14(%r14), %r8d
cmpl $0x1e, %r8d
je 0x71d1
movq 0x8df7(%rip), %rax      # 0xffc0
movq %rax, 0x30(%rsp)
incq (%rax)
movq 0x8(%r14), %rsi
movl 0x10(%r14), %ecx
movl 0x18(%r14), %r9d
movl 0x1c(%r14), %r10d
movl 0x20(%r14), %r11d
movq (%rsp), %rdx
subq $0x8, %rsp
leaq 0x4b17(%rip), %rdi      # 0xbd0b
xorl %eax, %eax
pushq 0x38(%rsp)
pushq %r11
pushq %r10
callq 0x3330
addq $0x20, %rsp
movq %rax, %r14
movq 0x30(%rsp), %rax
cmpq 0x8daa(%rip), %rax      # 0xffc0
jne 0x7221
movq $0x0, 0x30(%rsp)
testq %r14, %r14
je 0x724d
movq (%rbx), %rdi
leaq 0xebb(%rip), %rsi       # 0x80eb
xorl %edx, %edx
callq 0x56a1
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %rdi
callq 0x4408
movq %rsp, %rsi
leaq 0x60(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x441c
movq (%r14), %r14
jmp 0x7237
callq 0x4468
callq 0x43c6
leaq 0x4b9f(%rip), %rdi      # 0xbe15
leaq 0x4ac1(%rip), %rsi      # 0xbd3e
leaq 0x4bb0(%rip), %rcx      # 0xbe34
movl $0x4a, %edx
callq 0x3130
leaq 0x2366(%rip), %rdi      # 0x95fb
leaq 0x4aa2(%rip), %rsi      # 0xbd3e
leaq 0x4b91(%rip), %rcx      # 0xbe34
movl $0x51, %edx
callq 0x3130