// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq 0x8(%rsi), %rax
testb $0x2, 0xab(%rax)
je 0x86ea
movq %rdx, %r15
movq %rdi, %rbp
movq %rsi, 0x28(%rsp)
movq 0x10(%rsi), %rbx
movl $0x0, %eax
movl $0x0, %r12d
movl $0x0, %r14d
testq %rbx, %rbx
cmovgq %rbx, %rax
jle 0x8258
movq 0x28(%rsp), %rcx
movq 0x18(%rcx), %rdx
xorl %r14d, %r14d
movq 0x7d64(%rip), %rsi      # 0xff80
xorl %r12d, %r12d
movq (%rdx,%r14,8), %rcx
movq 0x8(%rcx), %rdi
cmpq %rsi, %rdi
jne 0x863a
testb $0x8, 0xab(%rdi)
je 0x86ac
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
jle 0x865a
addq %rcx, %r12
incq %r14
cmpq %r14, %rbx
jne 0x821f
movq %rax, %r14
movq $0x0, 0x10(%rsp)
cmpq %rbx, %r14
movq %r15, 0x20(%rsp)
jge 0x8273
xorl %r13d, %r13d
jmp 0x8593
leaq 0x8(,%r12,8), %rdi
callq 0x3370
movq %rax, %r13
testq %rax, %rax
je 0x86a7
movq %rbp, 0x48(%rsp)
movq %r12, 0x18(%rsp)
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
testb $0x2, 0xab(%rax)
je 0x840f
movl $0x1, %ebp
xorl %edx, %edx
movq %r13, 0x8(%rsp)
cmpq %rdx, 0x10(%rcx)
jle 0x842e
movq 0x18(%rcx), %rax
movq (%rax,%rdx,8), %rcx
movq 0x8(%rcx), %rax
testb $0x8, 0xab(%rax)
je 0x86cb
movq %rdx, 0x30(%rsp)
movq 0x20(%rcx), %rax
movq %rax, 0x50(%rsp)
testq %rax, %rax
jle 0x83e7
movq 0x10(%rcx), %rax
leaq 0x28(%rcx), %r15
movq %rcx, 0x38(%rsp)
addq %rcx, %rax
addq $0x20, %rax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
leaq 0x18(%r15), %r13
cmpq 0x40(%rsp), %r13
ja 0x84ff
movl $0x20, %edi
callq 0x3370
testq %rax, %rax
je 0x857d
movq %rax, %rbx
movq (%r15), %rax
movq %rax, (%rbx)
movq 0x8(%r15), %rax
movq %rax, 0x8(%rbx)
movl 0x10(%r15), %eax
leal 0x1(%rbp), %r14d
movl %eax, 0x10(%rbx)
movb 0x14(%r15), %cl
movb %cl, 0x14(%rbx)
movq $0x0, 0x18(%rbx)
movl %ebp, %ecx
movq 0x8(%rsp), %rdx
movq %rbx, (%rdx,%rcx,8)
testq %rax, %rax
je 0x83d1
shlq $0x2, %rax
leaq (%rax,%rax,2), %r9
leaq (%r9,%r13), %rax
cmpq 0x40(%rsp), %rax
ja 0x853c
movq %r9, %rdi
callq 0x3370
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x857a
movl 0x10(%rbx), %edx
testq %rdx, %rdx
je 0x83c8
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 0x83a3
leaq (%rdx,%rdx,2), %rax
leaq (%r13,%rax,4), %r13
incq %r12
movq %r13, %r15
movl %r14d, %ebp
cmpq 0x50(%rsp), %r12
jne 0x830e
jmp 0x83ea
movl %ebp, %r14d
movq 0x30(%rsp), %rdx
incl %edx
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
testb $0x2, 0xab(%rax)
movl %r14d, %ebp
movq 0x8(%rsp), %r13
jne 0x82b8
leaq 0x3ba6(%rip), %rdi      # 0xbfbc
leaq 0x38f9(%rip), %rsi      # 0xbd16
leaq 0x3bb1(%rip), %rcx      # 0xbfd5
movl $0x16b, %edx            # imm = 0x16B
callq 0x3130
cmpl $0x2, %ebp
jb 0x84e3
movl %ebp, %ebx
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %r14
movl $0x1, %r12d
xorl %r9d, %r9d
movq (%r13,%r12,8), %r15
cmpq $0x0, 0x20(%rsp)
je 0x8489
cmpb $0x0, 0x14(%r15)
je 0x8489
movl %r9d, %esi
movq 0x20(%rsp), %rdi
movq %r9, %r13
callq 0x3360
testq %rax, %rax
je 0x8589
incq (%rax)
movq %rax, (%r15)
incl %r13d
movq %r13, %r9
movq 0x8(%rsp), %r13
movq 0x8(%r15), %rcx
testq %rcx, %rcx
je 0x84a4
cmpq %rcx, %r14
jbe 0x867a
movq (%r13,%rcx,8), %rax
movq %rax, 0x8(%r15)
movl 0x10(%r15), %eax
testq %rax, %rax
je 0x84d7
movq 0x18(%r15), %rdx
addq $0x4, %rdx
xorl %ecx, %ecx
movq (%rdx), %r8
cmpq %r8, %r14
jbe 0x860d
movq (%r13,%r8,8), %rsi
movq %rsi, (%rdx)
incq %rcx
addq $0xc, %rdx
cmpq %rcx, %rax
jne 0x84b7
incq %r12
cmpq %rbx, %r12
jne 0x844b
movq 0x8(%r13), %rax
movq 0x48(%rsp), %rcx
movq %rax, 0x28(%rcx)
movq %r13, %rdi
callq 0x3070
movb $0x1, %al
jmp 0x85fe
movq 0x7a82(%rip), %rax      # 0xff88
movq (%rax), %rdi
subq 0x38(%rsp), %r15
addq $-0x20, %r15
leaq 0x3afa(%rip), %rsi      # 0xc013
movq $0x0, 0x10(%rsp)
movl $0x18, %r9d
movq %r12, %rdx
movq 0x30(%rsp), %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x3300
jmp 0x858e
movq 0x7a45(%rip), %rax      # 0xff88
movq (%rax), %rdi
movq %r12, %rax
subq 0x38(%rsp), %rax
leaq (%rax,%r13), %r8
addq $-0x20, %r8
leaq 0x3b19(%rip), %rsi      # 0xc076
movq $0x0, 0x10(%rsp)
movq %r12, %rdx
movq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x3300
movl %r14d, %ebp
jmp 0x858e
movl %r14d, %ebp
movq 0x8(%rsp), %r13
callq 0x30e0
jmp 0x8593
movq %r13, 0x10(%rsp)
movq 0x8(%rsp), %r13
testq %r13, %r13
je 0x85bf
cmpl $0x2, %ebp
jb 0x85b7
movl %ebp, %ebx
movl $0x1, %r14d
movq (%r13,%r14,8), %rdi
callq 0x514e
incq %r14
cmpq %r14, %rbx
jne 0x85a5
movq %r13, %rdi
callq 0x3070
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x85fc
movq 0x10(%rsp), %rax
testl %eax, %eax
je 0x85fc
movl %eax, %r14d
xorl %ebx, %ebx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3360
testq %rax, %rax
je 0x85f4
decq (%rax)
jne 0x85f4
movq %rax, %rdi
callq 0x30d0
incq %rbx
cmpq %rbx, %r14
jne 0x85d7
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r9, 0x10(%rsp)
movq 0x796f(%rip), %rax      # 0xff88
movq (%rax), %rdi
decq %r12
leaq 0x3b06(%rip), %rsi      # 0xc12c
movq %r12, %rdx
movq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0x3300
jmp 0x8593
movq 0x7947(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3b78(%rip), %rsi      # 0xc1c3
movq %r14, %rdx
xorl %eax, %eax
callq 0x3300
jmp 0x8258
movq 0x7927(%rip), %rax      # 0xff88
movq (%rax), %rdi
leaq 0x3b8a(%rip), %rsi      # 0xc1f5
movq %r14, %rdx
xorl %eax, %eax
callq 0x3300
jmp 0x8258
movq %r9, 0x10(%rsp)
movq 0x7902(%rip), %rax      # 0xff88
movq (%rax), %rdi
decq %r12
leaq 0x3a47(%rip), %rsi      # 0xc0da
movq %r12, %rdx
movq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x3300
jmp 0x8593
jmp 0x8582
leaq 0xf48(%rip), %rdi       # 0x95fb
leaq 0x365c(%rip), %rsi      # 0xbd16
leaq 0x3abe(%rip), %rcx      # 0xc17f
movl $0x134, %edx            # imm = 0x134
callq 0x3130
leaq 0xf29(%rip), %rdi       # 0x95fb
leaq 0x363d(%rip), %rsi      # 0xbd16
leaq 0x38f5(%rip), %rcx      # 0xbfd5
movl $0x16d, %edx            # imm = 0x16D
callq 0x3130
leaq 0x38cb(%rip), %rdi      # 0xbfbc
leaq 0x361e(%rip), %rsi      # 0xbd16
leaq 0x3a80(%rip), %rcx      # 0xc17f
movl $0x12b, %edx            # imm = 0x12B
callq 0x3130