// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 $0x18, %rsp
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, %rbx
movq 0x8(%rsi), %r14
testq %r14, %r14
jne 0x2f238
movl $0x20, %edi
callq 0x3b29d
movq %rax, %r14
movq $0x0, 0x10(%rax)
movl $0x0, 0x18(%rax)
movl $0xffe00000, %eax       # imm = 0xFFE00000
andl 0x1c(%r14), %eax
orl $0x90000, %eax          # imm = 0x90000
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movl %eax, 0x1c(%r14)
callq 0x3e2e3
movq %rax, %rdi
movq %r12, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x3ea1e
movq %rax, 0x10(%r14)
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x2eacc
incw 0x1c(%r14)
incq 0x10(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r13
movswl 0x1c(%rbx), %esi
movq %r14, %rdi
movl %esi, 0xc(%rsp)
movq %r15, %rdx
callq 0x26d30
movl %eax, %esi
testl %eax, %eax
sete %r9b
movb $0x1, %dl
testl %ebp, %ebp
je 0x2f26b
movzwl 0x1c(%r14), %eax
cmpl %eax, 0x2c(%r13)
je 0x2f48e
xorl %r11d, %r11d
movb $0x1, %r10b
movb $0x1, %r8b
testl %ebp, %ebp
jne 0x2f283
cmpl $0x0, 0x2c(%r13)
je 0x2f4de
xorl %eax, %eax
testl %ebp, %ebp
je 0x2f2cc
testl %esi, %esi
je 0x2f2cc
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%r14), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x2f2aa
movq %r14, %rdi
callq 0x2e916
orb $0x10, 0x1e(%r14)
movq 0x10(%r14), %rdi
movq 0x10(%r13), %rsi
callq 0x3ead6
movq 0x10(%r14), %rdi
testq %rax, %rax
je 0x2f448
movq %rax, %rsi
jmp 0x2f3b4
testl %ebp, %ebp
setne %cl
orb %r9b, %cl
je 0x2f38f
testl %esi, %esi
setne %cl
orb %r8b, %cl
je 0x2f3c4
testl %esi, %esi
setne %cl
orb %dl, %cl
je 0x2f40c
testl %esi, %esi
jne 0x2f225
testb %r8b, %r8b
je 0x2f4c4
testb %dl, %dl
je 0x2f529
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%r14), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x2f32b
movq %r14, %rdi
callq 0x2e916
orb $0x10, 0x1e(%r14)
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movl %eax, 0xc(%rsp)
movl 0x2c(%r13), %esi
movq %r14, %rdi
movl %ebp, %edx
callq 0x2f0b8
movq %rax, %r13
movq 0x10(%rax), %rdi
movq %r12, %rsi
movl %r15d, %edx
movl 0xc(%rsp), %ecx
callq 0x3ea1e
movq %rax, 0x10(%r13)
incw 0x1c(%r13)
movq %rax, %rdi
callq 0x3ef6d
movl %eax, 0x18(%r13)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x2eacc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2efcf
jmp 0x2f225
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%r14), %eax
cmpl $0x20000, %eax          # imm = 0x20000
jne 0x2f3ac
movq %r14, %rdi
callq 0x2e916
orb $0x10, 0x1e(%r14)
movq 0x10(%r14), %rdi
movq 0x10(%r13), %rsi
movq %r12, %rdx
movl %r15d, %ecx
callq 0x3ebf0
jmp 0x2f458
movq 0x8(%r14), %rdi
testq %rdi, %rdi
setne %cl
testl %ebp, %ebp
je 0x2f2e8
andb %cl, %r10b
je 0x2f2e8
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%rdi), %eax
cmpl $0x20000, %eax          # imm = 0x20000
movq %rdi, %r14
jne 0x2f3fe
callq 0x2e916
movq %r14, %rdi
orb $0x10, 0x1e(%r14)
movq 0x10(%rdi), %rdi
movq %r12, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
jmp 0x2f453
movq (%r14), %rdi
testq %rdi, %rdi
sete %cl
testl %ebp, %ebp
jne 0x2f2f5
orb %al, %cl
jne 0x2f2f5
movl $0x30000, %eax          # imm = 0x30000
andl 0x1c(%rdi), %eax
cmpl $0x20000, %eax          # imm = 0x20000
movq %rdi, %r14
jne 0x2f444
movq %r14, %rdi
callq 0x2e916
orb $0x10, 0x1e(%r14)
movq 0x10(%r14), %rdi
movq %r12, %rsi
movl %r15d, %edx
movl $0x1, %ecx
callq 0x3ea1e
movq %rax, 0x10(%r14)
incw 0x1c(%r14)
movq %rax, %rdi
callq 0x3ef6d
movl %eax, 0x18(%r14)
movl $0x130000, %eax         # imm = 0x130000
andl 0x1c(%r14), %eax
cmpl $0x110000, %eax         # imm = 0x110000
jne 0x2f225
movq %r14, %rdi
callq 0x2e893
jmp 0x2f225
movq 0x8(%r14), %rdi
movl %esi, 0x10(%rsp)
movl 0xc(%rsp), %esi
movq %r15, %rdx
movb %r9b, 0xa(%rsp)
callq 0x26d30
movb 0xa(%rsp), %r9b
movl 0x10(%rsp), %esi
movb $0x1, %dl
testl %eax, %eax
sete %r11b
setne %r10b
xorl %r8d, %r8d
jmp 0x2f274
cmpq $0x0, 0x8(%r14)
setne %cl
testl %ebp, %ebp
je 0x2f306
andb %cl, %r11b
jne 0x2f540
jmp 0x2f306
movl %r11d, 0x14(%rsp)
movq (%r14), %rdi
movl %esi, 0x10(%rsp)
movl 0xc(%rsp), %esi
movq %r15, %rdx
movl %r8d, 0xc(%rsp)
movb %r9b, 0xa(%rsp)
movb %r10b, 0xb(%rsp)
callq 0x26d30
movb 0xb(%rsp), %r10b
movb 0xa(%rsp), %r9b
movl 0xc(%rsp), %r8d
movl 0x14(%rsp), %r11d
movl 0x10(%rsp), %esi
testl %eax, %eax
sete %al
xorl %edx, %edx
jmp 0x2f285
cmpq $0x0, (%r14)
setne %cl
testl %ebp, %ebp
jne 0x2f30e
andb %cl, %al
je 0x2f30e
callq 0x2e809
movq %rax, %r13
callq 0x3e2e3
movq %rax, %rdi
movq %r12, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0x3ea1e
movq %rax, 0x10(%r13)
incw 0x1c(%r13)
movq %rax, %rdi
callq 0x3ef6d
movl %eax, 0x18(%r13)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x2eacc
jmp 0x2f225