// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x18(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xb970
testb $0x1, %al
jne 0xb2b2
jmp 0xb8d7
movq -0xa8(%rbp), %rdi
addq $0x1, %rdi
shlq $0x3, %rdi
callq 0x3450
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb2e4
jmp 0xb8d2
movl $0x1, -0x64(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2000000, %esi        # imm = 0x2000000
callq 0x4450
cmpl $0x0, %eax
je 0xb31b
jmp 0xb33a
leaq 0x3d9a(%rip), %rdi      # 0xf0bc
leaq 0x3a91(%rip), %rsi      # 0xedba
movl $0x16b, %edx            # imm = 0x16B
leaq 0x3da0(%rip), %rcx      # 0xf0d5
callq 0x3130
movq -0xb0(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0xb6ab
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2000000, %esi        # imm = 0x2000000
callq 0x4450
cmpl $0x0, %eax
je 0xb368
jmp 0xb387
leaq 0x3d4d(%rip), %rdi      # 0xf0bc
leaq 0x3a44(%rip), %rsi      # 0xedba
movl $0x16c, %edx            # imm = 0x16C
leaq 0x3d53(%rip), %rcx      # 0xf0d5
callq 0x3130
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x84(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x8000000, %esi        # imm = 0x8000000
callq 0x4450
cmpl $0x0, %eax
je 0xb3b6
jmp 0xb3d5
leaq 0x127a(%rip), %rdi      # 0xc637
leaq 0x39f6(%rip), %rsi      # 0xedba
movl $0x16d, %edx            # imm = 0x16D
leaq 0x3d05(%rip), %rcx      # 0xf0d5
callq 0x3130
movq -0x48(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x8000000, %esi        # imm = 0x8000000
callq 0x4450
cmpl $0x0, %eax
je 0xb41c
jmp 0xb43b
leaq 0x1214(%rip), %rdi      # 0xc637
leaq 0x3990(%rip), %rsi      # 0xedba
movl $0x172, %edx            # imm = 0x172
leaq 0x3c9f(%rip), %rcx      # 0xf0d5
callq 0x3130
movq -0xb8(%rbp), %rax
movq -0x48(%rbp), %rcx
addq 0x10(%rcx), %rax
addq $-0x8, %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0xb695
movq -0x78(%rbp), %rax
addq $0x18, %rax
cmpq -0x80(%rbp), %rax
seta %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb4bc
movq 0x6afd(%rip), %rax      # 0x11f88
movq (%rax), %rdi
movq -0x60(%rbp), %rdx
movl -0x84(%rbp), %ecx
movq -0x78(%rbp), %r8
movq -0x70(%rbp), %rax
subq %rax, %r8
leaq 0x3c69(%rip), %rsi      # 0xf113
movl $0x18, %r9d
movb $0x0, %al
callq 0x3300
jmp 0xb8d7
movq -0x78(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x20, %edi
callq 0x3450
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
setne %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb52f
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x38(%rbp), %rax
movb 0x14(%rax), %cl
movq -0x30(%rbp), %rax
movb %cl, 0x14(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
jmp 0xb534
jmp 0xb8d2
movq -0x78(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x78(%rbp)
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rax
movl -0x64(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x64(%rbp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jbe 0xb682
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpq -0x80(%rbp), %rax
seta %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb5ce
movq 0x69f4(%rip), %rax      # 0x11f88
movq (%rax), %rdi
movq -0x60(%rbp), %rdx
movl -0x84(%rbp), %ecx
movq -0x78(%rbp), %r8
movq -0x70(%rbp), %rax
subq %rax, %r8
addq -0x60(%rbp), %r8
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
imulq $0xc, %rax, %r9
leaq 0x3bb4(%rip), %rsi      # 0xf176
movb $0x0, %al
callq 0x3300
jmp 0xb8d7
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
imulq $0xc, %rax, %rdi
callq 0x3450
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb607
jmp 0xb8d2
movq -0x78(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0xb66f
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
imulq $0xc, -0x90(%rbp), %rcx
addq %rcx, %rax
movq -0x40(%rbp), %rcx
imulq $0xc, -0x90(%rbp), %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0xb61a
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
imulq $0xc, %rax, %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0xb684
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xb45a
jmp 0xb697
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xb2f5
movq $0x1, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl -0x64(%rbp), %ecx
cmpq %rcx, %rax
jge 0xb8b0
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb72f
movq -0x30(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
je 0xb72f
movq -0x20(%rbp), %rdi
movl -0x94(%rbp), %eax
movl %eax, %esi
callq 0x3360
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0xb728
movq -0x50(%rbp), %rdi
callq 0x5820
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xb72d
jmp 0xb8d7
jmp 0xb72f
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xb7c0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
setb %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb78b
movq -0x28(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0xb7be
movq 0x67f6(%rip), %rax      # 0x11f88
movq (%rax), %rdi
movq -0x60(%rbp), %rdx
subq $0x1, %rdx
movq -0xa0(%rbp), %rcx
movq -0xa8(%rbp), %r8
leaq 0x3a28(%rip), %rsi      # 0xf1da
movb $0x0, %al
callq 0x3300
jmp 0xb8d7
jmp 0xb7c0
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0xb89d
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
imulq $0xc, -0x90(%rbp), %rcx
addq %rcx, %rax
movq 0x4(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
setb %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xb84d
movq -0x28(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
imulq $0xc, -0x90(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x4(%rax)
jmp 0xb884
movq 0x6734(%rip), %rax      # 0x11f88
movq (%rax), %rdi
movq -0x60(%rbp), %rdx
subq $0x1, %rdx
movq -0x90(%rbp), %rcx
movq -0xa0(%rbp), %r8
movq -0xa8(%rbp), %r9
leaq 0x39b1(%rip), %rsi      # 0xf22c
movb $0x0, %al
callq 0x3300
jmp 0xb8d7
jmp 0xb886
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0xb7cb
jmp 0xb89f
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xb6b3
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rdi
callq 0x34b0
movb $0x1, -0x1(%rbp)
jmp 0xb961
callq 0x30e0
cmpq $0x0, -0x28(%rbp)
je 0xb91a
movq $0x1, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl -0x64(%rbp), %ecx
cmpq %rcx, %rax
jge 0xb911
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x51a0
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xb8e6
movq -0x28(%rbp), %rdi
callq 0x34b0
cmpq $0x0, -0x20(%rbp)
je 0xb95d
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl -0x94(%rbp), %ecx
cmpq %rcx, %rax
jge 0xb95b
movq -0x20(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x3360
movq %rax, %rdi
callq 0xa260
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xb929
jmp 0xb95d
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xc0, %rsp
popq %rbp
retq
nop