// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/quicklist.c
// Function: quicklistInsert
REDIS_STATIC void _quicklistInsert(quicklist *quicklist, quicklistEntry *entry,
                                   void *value, const size_t sz, int after) {
    int full = 0, at_tail = 0, at_head = 0, full_next = 0, full_prev = 0;
    int fill = quicklist->fill;
    quicklistNode *node = entry->node;
    quicklistNode *new_node = NULL;

    if (!node) {
        /* we have no reference node, so let's create only node in the list */
        D("No node given!");
        new_node = quicklistCreateNode();
        new_node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_HEAD);
        __quicklistInsertNode(quicklist, NULL, new_node, after);
        new_node->count++;
        quicklist->count++;
        return;
    }

    /* Populate accounting flags for easier boolean checks later */
    if (!_quicklistNodeAllowInsert(node, fill, sz)) {
        D("Current node is full with count %d with requested fill %lu",
          node->count, fill);
        full = 1;
    }

    if (after && (entry->offset == node->count)) {
        D("At Tail of current ziplist");
        at_tail = 1;
        if (!_quicklistNodeAllowInsert(node->next, fill, sz)) {
            D("Next node is full too.");
            full_next = 1;
        }
    }

    if (!after && (entry->offset == 0)) {
        D("At Head");
        at_head = 1;
        if (!_quicklistNodeAllowInsert(node->prev, fill, sz)) {
            D("Prev node is full too.");
            full_prev = 1;
        }
    }

    /* Now determine where and how to insert the new element */
    if (!full && after) {
        D("Not full, inserting after current position.");
        quicklistDecompressNodeForUse(node);
        unsigned char *next = ziplistNext(node->zl, entry->zi);
        if (next == NULL) {
            node->zl = ziplistPush(node->zl, value, sz, ZIPLIST_TAIL);
        } else {
            node->zl = ziplistInsert(node->zl, next, value, sz);
        }
        node->count++;
        quicklistNodeUpdateSz(node);
        quicklistRecompressOnly(quicklist, node);
    } else if (!full && !after) {
        D("Not full, inserting before current position.");
        quicklistDecompressNodeForUse(node);
        node->zl = ziplistInsert(node->zl, entry->zi, value, sz);
        node->count++;
        quicklistNodeUpdateSz(node);
        quicklistRecompressOnly(quicklist, node);
    } else if (full && at_tail && node->next && !full_next && after) {
        /* If we are: at tail, next has free space, and inserting after:
         *   - insert entry at head of next node. */
        D("Full and tail, but next isn't full; inserting next node head");
        new_node = node->next;
        quicklistDecompressNodeForUse(new_node);
        new_node->zl = ziplistPush(new_node->zl, value, sz, ZIPLIST_HEAD);
        new_node->count++;
        quicklistNodeUpdateSz(new_node);
        quicklistRecompressOnly(quicklist, new_node);
    } else if (full && at_head && node->prev && !full_prev && !after) {
        /* If we are: at head, previous has free space, and inserting before:
         *   - insert entry at tail of previous node. */
        D("Full and head, but prev isn't full, inserting prev node tail");
        new_node = node->prev;
        quicklistDecompressNodeForUse(new_node);
        new_node->zl = ziplistPush(new_node->zl, value, sz, ZIPLIST_TAIL);
        new_node->count++;
        quicklistNodeUpdateSz(new_node);
        quicklistRecompressOnly(quicklist, new_node);
    } else if (full && ((at_tail && node->next && full_next && after) ||
                        (at_head && node->prev && full_prev && !after))) {
        /* If we are: full, and our prev/next is full, then:
         *   - create new node and attach to quicklist */
        D("\tprovisioning new node...");
        new_node = quicklistCreateNode();
        new_node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_HEAD);
        new_node->count++;
        quicklistNodeUpdateSz(new_node);
        __quicklistInsertNode(quicklist, node, new_node, after);
    } else if (full) {
        /* else, node is full we need to split it. */
        /* covers both after and !after cases */
        D("\tsplitting node...");
        quicklistDecompressNodeForUse(node);
        new_node = _quicklistSplitNode(node, entry->offset, after);
        new_node->zl = ziplistPush(new_node->zl, value, sz,
                                   after ? ZIPLIST_HEAD : ZIPLIST_TAIL);
        new_node->count++;
        quicklistNodeUpdateSz(new_node);
        __quicklistInsertNode(quicklist, node, new_node, after);
        _quicklistMergeNodes(quicklist, node);
    }

    quicklist->count++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %r13d
movq %rcx, %r12
movq %rdx, %r15
movq %rdi, %rbp
movq 0x8(%rsi), %r14
testq %r14, %r14
je 0x27ef3
movq %rsi, %rax
movq %r15, 0x10(%rsp)
movswl 0x1c(%rbp), %r15d
movq %r14, %rdi
movl %r15d, %esi
movq %r12, (%rsp)
movq %r12, %rdx
movq %rax, 0x20(%rsp)
callq 0x27586
movl %eax, %ebx
movq 0x20(%rsp), %rax
movl 0x2c(%rax), %r12d
testl %r13d, %r13d
movq %rbp, 0x8(%rsp)
movl %r13d, 0x1c(%rsp)
je 0x27f2d
movl 0x1c(%r14), %ebp
movzwl %bp, %eax
cmpl %eax, %r12d
jne 0x27f43
movq 0x8(%r14), %rdi
movl %r15d, %esi
movq (%rsp), %rdx
callq 0x27586
testl %ebx, %ebx
jne 0x27f4b
movq 0x8(%r14), %rbx
testq %rbx, %rbx
je 0x28062
testl %eax, %eax
je 0x28062
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%rbx), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x27eac
movq %rbx, %rdi
callq 0x2732d
orb $0x10, 0x1e(%rbx)
movq 0x10(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
xorl %ecx, %ecx
callq 0x35aa8
movq %rax, 0x10(%rbx)
incw 0x1c(%rbx)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%rbx)
movl $0x130000, %eax         # imm = 0x130000
andl 0x1c(%rbx), %eax
cmpl $0x110000, %eax         # imm = 0x110000
movq 0x8(%rsp), %rbp
jne 0x28176
movq %rbx, %rdi
jmp 0x28058
callq 0x2722c
movq %rax, %rbx
callq 0x3540c
movq %rax, %rdi
movq %r15, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0x35aa8
movq %rax, 0x10(%rbx)
movq %rbp, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movl %r13d, %ecx
callq 0x274c9
incw 0x1c(%rbx)
jmp 0x28176
testl %r12d, %r12d
je 0x27fd8
testl %ebx, %ebx
jne 0x27fef
jmp 0x280e8
testl %ebx, %ebx
je 0x280e8
andl $0x30000, %ebp          # imm = 0x30000
cmpl $0x20000, %ebp          # imm = 0x20000
jne 0x27f66
movq %r14, %rdi
callq 0x2732d
orb $0x10, 0x1e(%r14)
movq 0x10(%r14), %rdi
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x35b60
movq 0x10(%r14), %rdi
testq %rax, %rax
movq (%rsp), %r8
movq 0x10(%rsp), %rdx
je 0x27f97
movq %rax, %rsi
movl %r8d, %ecx
callq 0x35c73
jmp 0x27fa5
pushq $0x1
popq %rcx
movq %rdx, %rsi
movl %r8d, %edx
callq 0x35aa8
movq 0x8(%rsp), %rbp
movq %rax, 0x10(%r14)
incw 0x1c(%r14)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%r14)
movl $0x130000, %eax         # imm = 0x130000
andl 0x1c(%r14), %eax
cmpl $0x110000, %eax         # imm = 0x110000
je 0x28055
jmp 0x28176
movq (%r14), %rdi
movl %r15d, %esi
movq (%rsp), %rdx
callq 0x27586
testl %ebx, %ebx
je 0x280c0
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%r14), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x2800c
movq %r14, %rdi
callq 0x2732d
orb $0x10, 0x1e(%r14)
movq 0x10(%r14), %rdi
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x10(%rsp), %rdx
movq (%rsp), %rcx
callq 0x35c73
movq %rax, 0x10(%r14)
incw 0x1c(%r14)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%r14)
movl $0x130000, %eax         # imm = 0x130000
andl 0x1c(%r14), %eax
cmpl $0x110000, %eax         # imm = 0x110000
movq 0x8(%rsp), %rbp
jne 0x28176
movq %r14, %rdi
callq 0x272ac
jmp 0x28176
testl %eax, %eax
sete %al
testq %rbx, %rbx
setne %cl
testb %cl, %al
je 0x280e8
callq 0x2722c
movq %rax, %rbx
callq 0x3540c
movq %rax, %rdi
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
xorl %ecx, %ecx
callq 0x35aa8
movq %rax, 0x10(%rbx)
incw 0x1c(%rbx)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%rbx)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl 0x1c(%rsp), %ecx
callq 0x274c9
jmp 0x28176
testl %eax, %eax
sete %cl
movq (%r14), %r12
testq %r12, %r12
sete %dl
orb %cl, %dl
je 0x28189
testq %r12, %r12
sete %cl
testl %eax, %eax
setne %al
xorl %r12d, %r12d
orb %cl, %al
je 0x28071
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%r14), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x2810e
movq %r14, %rdi
callq 0x2732d
orb $0x10, 0x1e(%r14)
movq 0x20(%rsp), %rax
movl 0x2c(%rax), %r12d
xorl %ebx, %ebx
movl 0x1c(%rsp), %r13d
testl %r13d, %r13d
sete %bl
movq %r14, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x27d23
movq %rax, %r15
movq 0x10(%rax), %rdi
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
movl %ebx, %ecx
callq 0x35aa8
movq %rax, 0x10(%r15)
incw 0x1c(%r15)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%r15)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %r13d, %ecx
callq 0x274c9
movq %rbp, %rdi
movq %r14, %rsi
callq 0x27c40
incq 0x10(%rbp)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%r12), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x281a8
movq %r12, %rdi
callq 0x2732d
orb $0x10, 0x1e(%r12)
movq 0x10(%r12), %rdi
pushq $0x1
popq %rcx
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
callq 0x35aa8
movq %rax, 0x10(%r12)
incw 0x1c(%r12)
movq %rax, %rdi
callq 0x35f52
movl %eax, 0x18(%r12)
movl $0x130000, %eax         # imm = 0x130000
andl 0x1c(%r12), %eax
cmpl $0x110000, %eax         # imm = 0x110000
movq 0x8(%rsp), %rbp
jne 0x28176
movq %r12, %rdi
jmp 0x28058