// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x1000000, -0x20(%rbp) # imm = 0x1000000
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x89b4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x6407(%rip), %rdi      # 0xedab
movb $0x0, %al
callq 0x3330
movq %rax, -0x8(%rbp)
jmp 0x8b64
movq $0x0, -0x38(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
leaq 0x1e25(%rip), %rsi      # 0xa800
leaq -0x38(%rbp), %rdx
callq 0x59e0
cmpq $0x0, -0x28(%rbp)
je 0x8a15
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
leaq 0x1eb6(%rip), %rsi      # 0xa8b0
leaq -0x38(%rbp), %rdx
callq 0x59e0
callq 0x30e0
movq $0x0, -0x8(%rbp)
jmp 0x8b64
movq -0x10(%rbp), %rax
movl 0x14(%rax), %esi
movq -0x30(%rbp), %rdx
leaq -0x78(%rbp), %rdi
movl $0x1000000, %ecx        # imm = 0x1000000
callq 0xa910
cmpl $0x0, %eax
jne 0x8a38
jmp 0x8b3b
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
leaq 0x2079(%rip), %rsi      # 0xaac0
leaq -0x78(%rbp), %rdx
callq 0x59e0
testb $0x1, -0x40(%rbp)
je 0x8a5b
jmp 0x8b3b
leaq -0x78(%rbp), %rdi
callq 0xad20
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x8a78
jmp 0x8b3b
movq -0x10(%rbp), %rax
cmpl $0x1e, 0x14(%rax)
je 0x8a96
movq 0x9537(%rip), %rax      # 0x11fc0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x5820
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x78(%rbp), %rdx
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0x14(%rax), %r8d
movq -0x10(%rbp), %rax
movl 0x18(%rax), %r9d
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %r11d
movq -0x10(%rbp), %rax
movl 0x20(%rax), %r10d
movq -0x48(%rbp), %rax
leaq 0x62db(%rip), %rdi      # 0xedaf
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x3330
movq %rax, -0x80(%rbp)
movq 0x94cc(%rip), %rax      # 0x11fc0
cmpq %rax, -0x48(%rbp)
jne 0x8b02
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x80(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x8b19
jmp 0x8b3b
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
leaq 0x23e8(%rip), %rsi      # 0xaf10
xorl %eax, %eax
movl %eax, %edx
callq 0x59e0
movq -0x80(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x8b64
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
leaq 0x23c6(%rip), %rsi      # 0xaf10
xorl %eax, %eax
movl %eax, %edx
callq 0x59e0
leaq -0x78(%rbp), %rdi
callq 0xaf60
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)