// File: /workspace/github_c_makefile_star2_O3/WojciechMula[P]pyahocorasick/src/Automaton_pickle.c
// Function: automaton_unpickle
static bool
automaton_unpickle(
    Automaton* automaton,
    PyObject* bytes_list,
    PyObject* values
) {
    TrieNode** id2node = NULL;

    TrieNode* node;
    TrieNode* dump;
    Pair* next;
    PyObject* bytes;
    PyObject* value;
    Py_ssize_t nodes_count;
    Py_ssize_t i;

    unsigned id;
    const uint8_t* data;
    const uint8_t* ptr;
    const uint8_t* end;
    unsigned k;
    size_t j;
    unsigned object_idx = 0;
    size_t index;
    size_t count;

    if (!automaton_unpickle__validate_bytes_list(bytes_list, &count)) {
        goto exception;
    }

    id2node = (TrieNode**)memory_alloc((count+1) * sizeof(TrieNode*));
    if (UNLIKELY(id2node == NULL)) {
        goto no_mem;
    }

    // 1. make nodes
    id = 1;
    for (k=0; k < PyList_GET_SIZE(bytes_list); k++) {
        bytes = PyList_GET_ITEM(bytes_list, k);
        data = (const uint8_t*)PyBytes_AS_STRING(bytes);

        nodes_count = *((Py_ssize_t*)data);

        ptr  = data + PICKLE_CHUNK_COUNTER_SIZE;
        end  = ptr + PyBytes_GET_SIZE(bytes) - PICKLE_CHUNK_COUNTER_SIZE;
        for (i=0; i < nodes_count; i++) {
            if (UNLIKELY(ptr + PICKLE_TRIENODE_SIZE > end)) {
                PyErr_Format(PyExc_ValueError,
                             "Data truncated [parsing header of node #%li]: chunk #%di @ offset %ld, expected at least %lu bytes",
                             i, k, ptr - data, PICKLE_TRIENODE_SIZE);
                goto exception;
            }

            dump = (TrieNode*)(ptr);
            node = (TrieNode*)memory_alloc(sizeof(TrieNode));
            if (LIKELY(node != NULL)) {
                node->output    = dump->output;
                node->fail      = dump->fail;
                node->n         = dump->n;
                node->eow       = dump->eow;
                node->next      = NULL;
            }
            else
                goto no_mem;

            ptr += PICKLE_TRIENODE_SIZE;

            id2node[id++] = node;

            if (node->n > 0) {
                if (UNLIKELY(ptr + node->n * sizeof(Pair) > end)) {
                    PyErr_Format(PyExc_ValueError,
                                "Data truncated [parsing children of node #%lu]: "
                                "chunk #%d @ offset %lu, expected at least %ld bytes",
                                 i, k, ptr - data + i, node->n * sizeof(Pair));

                    goto exception;
                }

                node->next = (Pair*)memory_alloc(node->n * sizeof(Pair));
                if (UNLIKELY(node->next == NULL)) {
                    goto no_mem;
                }

                next = (Pair*)(ptr);
                for (j=0; j < node->n; j++) {
                    node->next[j] = next[j];
                }

                ptr += node->n * sizeof(Pair);
            }
        }
    }

    // 2. restore pointers and references to pyobjects
    for (i=1; i < id; i++) {
        node = id2node[i];

        // references
        if (values and node->eow) {
            value = F(PyList_GetItem)(values, object_idx);
            if (value) {
                Py_INCREF(value);
                node->output.object = value;
                object_idx += 1;
            }
            else
                goto exception;
        }

        // pointers
        if (node->fail) {
            index = (size_t)(node->fail);
            if (LIKELY(index < count + 1)) {
                node->fail = id2node[index];
            } else {
                PyErr_Format(PyExc_ValueError,
                             "Node #%lu malformed: the fail link points to node #%lu, while there are %lu nodes",
                             i - 1, index, count);
                goto exception;
            }
        }

        for (j=0; j < node->n; j++) {
            index = (size_t)(node->next[j].child);
            if (LIKELY(index < count + 1)) {
                node->next[j].child = id2node[index];
            } else {
                PyErr_Format(PyExc_ValueError,
                             "Node #%lu malformed: next link #%lu points to node #%lu, while there are %lu nodes",
                             i - 1, j, index, count);
                goto exception;
            }
        }
    }

    automaton->root = id2node[1];

    memory_free(id2node);
    return 1;

no_mem:
    PyErr_NoMemory();
exception:
    // free memory
    if (id2node) {
        for (i=1; i < id; i++) {
            trienode_free(id2node[i]);
        }

        memory_free(id2node);
    }

    // If there is value list and some of its items were already
    // referenced, release them
    if (values) {
        for (i=0; i < object_idx; i++) {
            Py_XDECREF(F(PyList_GetItem)(values, i));
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x8(%rsp)
movq 0x8(%rsi), %rax
testb $0x2, 0xab(%rax)
je 0x85c8
movq %rsi, %r13
movq %rdi, %r12
movq 0x10(%rsi), %rbx
testq %rbx, %rbx
jle 0x8171
movq 0x7e52(%rip), %rax      # 0xff80
movq 0xa8(%rax), %rdx
movq 0x18(%r13), %rsi
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%rsi,%r14,8), %rcx
cmpq %rax, 0x8(%rcx)
jne 0x851d
btl $0x1b, %edx
jae 0x858a
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
jle 0x853d
addq %rcx, %r15
incq %r14
cmpq %r14, %rbx
jne 0x813f
jmp 0x8180
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %rbx, %r14
jl 0x850c
leaq 0x8(,%r15,8), %rdi
callq 0x3370
movq %rax, (%rsp)
testq %rax, %rax
je 0x8585
movq %r12, 0x30(%rsp)
movq %r15, 0x10(%rsp)
movq 0x8(%r13), %rax
testb $0x2, 0xab(%rax)
je 0x8318
movl $0x1, %r15d
xorl %edx, %edx
movq %r13, 0x38(%rsp)
cmpq %rdx, 0x10(%r13)
jle 0x8337
movq 0x18(%r13), %rcx
movq (%rcx,%rdx,8), %rsi
movq 0x8(%rsi), %rcx
testb $0x8, 0xab(%rcx)
je 0x85a9
movq 0x20(%rsi), %rcx
movq %rcx, 0x40(%rsp)
testq %rcx, %rcx
jle 0x8303
movq %rdx, 0x18(%rsp)
movq 0x10(%rsi), %rax
leaq 0x28(%rsi), %r14
movq %rsi, 0x20(%rsp)
addq %rsi, %rax
addq $0x20, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
leaq 0x18(%r14), %r13
cmpq 0x28(%rsp), %r13
ja 0x8405
movl $0x20, %edi
callq 0x3370
testq %rax, %rax
je 0x8470
movq %rax, %rbx
movq (%r14), %rax
movq %rax, (%rbx)
movq 0x8(%r14), %rax
movq %rax, 0x8(%rbx)
movl 0x10(%r14), %eax
leal 0x1(%r15), %ebp
movl %eax, 0x10(%rbx)
movb 0x14(%r14), %cl
movb %cl, 0x14(%rbx)
movq $0x0, 0x18(%rbx)
movl %r15d, %ecx
movq (%rsp), %rdx
movq %rbx, (%rdx,%rcx,8)
testq %rax, %rax
je 0x82df
shlq $0x2, %rax
leaq (%rax,%rax,2), %r9
leaq (%r9,%r13), %rax
cmpq 0x28(%rsp), %rax
ja 0x8439
movq %r9, %rdi
callq 0x3370
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x846d
cmpl $0x0, 0x10(%rbx)
je 0x82da
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%rbx), %rdx
movl 0x8(%r13,%rax), %esi
movl %esi, 0x8(%rdx,%rax)
movq (%r13,%rax), %rsi
movq %rsi, (%rdx,%rax)
incq %rcx
movl 0x10(%rbx), %edx
addq $0xc, %rax
cmpq %rdx, %rcx
jb 0x82ab
shlq $0x2, %rdx
leaq (%rdx,%rdx,2), %rax
jmp 0x82dc
xorl %eax, %eax
addq %rax, %r13
incq %r12
movq %r13, %r14
movl %ebp, %r15d
cmpq 0x40(%rsp), %r12
jne 0x8218
movq 0x38(%rsp), %r13
movq 0x8(%r13), %rax
movq 0x18(%rsp), %rdx
jmp 0x8306
movl %r15d, %ebp
incl %edx
testb $0x2, 0xab(%rax)
movl %ebp, %r15d
jne 0x81c2
leaq 0x3cad(%rip), %rdi      # 0xbfcc
leaq 0x3a00(%rip), %rsi      # 0xbd26
leaq 0x3cb8(%rip), %rcx      # 0xbfe5
movl $0x16b, %edx            # imm = 0x16B
callq 0x3130
cmpl $0x2, %r15d
jb 0x83e8
movl %r15d, %r14d
movq 0x10(%rsp), %rax
leaq 0x1(%rax), %r13
movl $0x1, %r12d
xorl %ebx, %ebx
movq (%rsp), %rax
movq (%rax,%r12,8), %rbp
cmpq $0x0, 0x8(%rsp)
je 0x8389
cmpb $0x0, 0x14(%rbp)
je 0x8389
movl %ebx, %esi
movq 0x8(%rsp), %rdi
callq 0x3360
testq %rax, %rax
je 0x84a6
incq (%rax)
movq %rax, (%rbp)
incl %ebx
movq 0x8(%rbp), %rcx
testq %rcx, %rcx
je 0x83a7
cmpq %rcx, %r13
jbe 0x855d
movq (%rsp), %rax
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rbp)
movl 0x10(%rbp), %eax
testq %rax, %rax
je 0x83dc
movq 0x18(%rbp), %rdx
addq $0x4, %rdx
xorl %ecx, %ecx
movq (%rdx), %r8
cmpq %r8, %r13
jbe 0x8483
movq (%rsp), %rsi
movq (%rsi,%r8,8), %rsi
movq %rsi, (%rdx)
incq %rcx
addq $0xc, %rdx
cmpq %rcx, %rax
jne 0x83b9
incq %r12
cmpq %r14, %r12
jne 0x8355
movq (%rsp), %rdi
movq 0x8(%rdi), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x28(%rcx)
callq 0x3070
movb $0x1, %al
jmp 0x850e
movq 0x7b7c(%rip), %rax      # 0xff88
movq (%rax), %rdi
subq 0x20(%rsp), %r14
addq $-0x20, %r14
leaq 0x3c04(%rip), %rsi      # 0xc023
movl $0x18, %r9d
movq %r12, %rdx
movq 0x18(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x3300
jmp 0x8475
movq 0x7b48(%rip), %rax      # 0xff88
movq (%rax), %rdi
movq %r12, %rax
subq 0x20(%rsp), %rax
leaq (%rax,%r13), %r8
addq $-0x20, %r8
leaq 0x3c2c(%rip), %rsi      # 0xc086
xorl %ebx, %ebx
movq %r12, %rdx
movq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x3300
jmp 0x84a9
movl %ebp, %r15d
callq 0x30e0
xorl %ebx, %ebx
movl %r15d, %ebp
cmpq $0x0, (%rsp)
jne 0x84a9
jmp 0x84d4
movq 0x7afe(%rip), %rax      # 0xff88
movq (%rax), %rdi
decq %r12
leaq 0x3ca5(%rip), %rsi      # 0xc13c
movq %r12, %rdx
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x3300
movl %r15d, %ebp
cmpl $0x2, %ebp
movq (%rsp), %r12
jb 0x84cc
movl %ebp, %r14d
movl $0x1, %r15d
movq (%r12,%r15,8), %rdi
callq 0x4ffe
incq %r15
cmpq %r15, %r14
jne 0x84bb
movq %r12, %rdi
callq 0x3070
cmpq $0x0, 0x8(%rsp)
je 0x850c
testl %ebx, %ebx
je 0x850c
movl %ebx, %r14d
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x3360
testq %rax, %rax
je 0x8504
decq (%rax)
jne 0x8504
movq %rax, %rdi
callq 0x30d0
incq %rbx
cmpq %rbx, %r14
jne 0x84e5
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7a64(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3ca5(%rip), %rsi      # 0xc1d3
movq %r14, %rdx
xorl %eax, %eax
callq 0x3300
jmp 0x8177
movq 0x7a44(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3cb7(%rip), %rsi      # 0xc205
movq %r14, %rdx
xorl %eax, %eax
callq 0x3300
jmp 0x8177
movq 0x7a24(%rip), %rax      # 0xff88
movq (%rax), %rdi
decq %r12
leaq 0x3b79(%rip), %rsi      # 0xc0ea
movq %r12, %rdx
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x3300
jmp 0x84a6
jmp 0x8470
leaq 0x107a(%rip), %rdi      # 0x960b
leaq 0x378e(%rip), %rsi      # 0xbd26
leaq 0x3bf0(%rip), %rcx      # 0xc18f
movl $0x134, %edx            # imm = 0x134
callq 0x3130
leaq 0x105b(%rip), %rdi      # 0x960b
leaq 0x376f(%rip), %rsi      # 0xbd26
leaq 0x3a27(%rip), %rcx      # 0xbfe5
movl $0x16d, %edx            # imm = 0x16D
callq 0x3130
leaq 0x39fd(%rip), %rdi      # 0xbfcc
leaq 0x3750(%rip), %rsi      # 0xbd26
leaq 0x3bb2(%rip), %rcx      # 0xc18f
movl $0x12b, %edx            # imm = 0x12B
callq 0x3130