// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x1c(%rdi)
je 0x6bc8
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movq 0x28(%rbx), %rdi
leaq 0xea1(%rip), %rsi       # 0x7a38
movq %r14, %rdx
callq 0x4a41
cmpq $0x0, 0x10(%r14)
je 0x6be4
movq 0x28(%rbx), %rdi
leaq 0xed4(%rip), %rsi       # 0x7a85
leaq 0x40(%rsp), %rdx
callq 0x4a41
callq 0x30e0
xorl %r14d, %r14d
jmp 0x6dc1
movq 0x8(%rbx), %rsi
leaq 0x4134(%rip), %rdi      # 0xad07
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x3330
movl 0x14(%rbx), %ebp
movq 0x48(%rsp), %r15
movb $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movups %xmm0, 0x20(%rsp)
movups %xmm0, 0x29(%rsp)
testq %r15, %r15
je 0x6dcf
xorl %edi, %edi
callq 0x3030
movq %rax, (%rsp)
testq %rax, %rax
je 0x6d99
cmpl $0x1e, %ebp
jne 0x6c3d
movq %rax, %r14
xorl %edi, %edi
callq 0x3030
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x6d91
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 0x7c65
testb %al, %al
je 0x6d99
movq 0x28(%rbx), %rdi
leaq 0xe36(%rip), %rsi       # 0x7aaa
movq %rsp, %r14
movq %r14, %rdx
callq 0x4a41
cmpb $0x0, 0x38(%r14)
jne 0x6d99
movq 0x28(%rsp), %rax
cmpq 0x10(%rsp), %rax
jae 0x6d0e
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x6e10
movq 0x8(%rdi), %rax
testb $0x2, 0xab(%rax)
je 0x6e3a
movq 0x10(%rdi), %r14
decq %r14
movq %r14, %rsi
callq 0x3360
testq %rax, %rax
je 0x6d99
movq 0x8(%rax), %rcx
testb $0x8, 0xab(%rcx)
je 0x6e53
addq $0x20, %rax
movq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x30c0
testq %rax, %rax
je 0x6d99
movq (%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3210
testl %eax, %eax
js 0x6d99
movl 0x14(%rbx), %r8d
cmpl $0x1e, %r8d
jne 0x6d1f
movq 0x30(%rsp), %r10
jmp 0x6d2e
movq 0x729a(%rip), %r10      # 0xdfc0
movq %r10, 0x30(%rsp)
incq (%r10)
movq 0x8(%rbx), %rsi
movq (%rsp), %rdx
movl 0x10(%rbx), %ecx
movl 0x18(%rbx), %r9d
movl 0x1c(%rbx), %r11d
movl 0x20(%rbx), %r14d
subq $0x8, %rsp
leaq 0x3fbb(%rip), %rdi      # 0xad0b
xorl %eax, %eax
pushq %r10
pushq %r14
pushq %r11
callq 0x3330
addq $0x20, %rsp
movq %rax, %r14
movq 0x30(%rsp), %rax
cmpq 0x7250(%rip), %rax      # 0xdfc0
jne 0x6d78
andq $0x0, 0x30(%rsp)
testq %r14, %r14
je 0x6d99
movq 0x28(%rbx), %rdi
leaq 0xe71(%rip), %rsi       # 0x7bf9
xorl %edx, %edx
callq 0x4a41
jmp 0x6dc1
movq %r14, %rdi
callq 0x37d5
movq 0x28(%rbx), %rdi
leaq 0xe55(%rip), %rsi       # 0x7bf9
xorl %r14d, %r14d
xorl %edx, %edx
callq 0x4a41
movq (%rsp), %rdi
callq 0x7776
movq 0x30(%rsp), %rdi
callq 0x7776
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x7222(%rip), %rbx      # 0xdff8
movq (%rbx), %rdi
leaq 0x1250(%rip), %rsi      # 0x8030
leaq 0x3f57(%rip), %rdx      # 0xad3e
leaq 0x3f69(%rip), %rcx      # 0xad57
leaq 0x3f74(%rip), %r9       # 0xad69
pushq $0x63
popq %r8
xorl %eax, %eax
callq 0x31b0
movq (%rbx), %rdi
callq 0x3230
pushq $0x1
popq %rdi
callq 0x3310
movq 0x71e1(%rip), %rbx      # 0xdff8
movq (%rbx), %rdi
leaq 0x120f(%rip), %rsi      # 0x8030
leaq 0x3f16(%rip), %rdx      # 0xad3e
leaq 0x3fb5(%rip), %rcx      # 0xade4
leaq 0x3fce(%rip), %r9       # 0xae04
pushq $0x48
jmp 0x6df7
leaq 0x3fd4(%rip), %rdi      # 0xae15
leaq 0x3ef6(%rip), %rsi      # 0xad3e
leaq 0x3fe5(%rip), %rcx      # 0xae34
pushq $0x4a
jmp 0x6e6a
leaq 0x17a1(%rip), %rdi      # 0x85fb
leaq 0x3edd(%rip), %rsi      # 0xad3e
leaq 0x3fcc(%rip), %rcx      # 0xae34
pushq $0x51
popq %rdx
callq 0x3130