// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/ziplist.c
// Function: _ziplistInsert
static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
    // 记录当前 ziplist 的长度
    size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen, prevlen = 0;
    size_t offset;
    int nextdiff = 0;
    unsigned char encoding = 0;
    long long value = 123456789; /* initialized to avoid warning. Using a value
                                    that is easy to see if for some reason
                                    we use it uninitialized. */
    zlentry entry, tail;

    /* Find out prevlen for the entry that is inserted. */
    if (p[0] != ZIP_END) {
        // 如果 p[0] 不指向列表末端，说明列表非空，并且 p 正指向列表的其中一个节点
        // 那么取出 p 所指向节点的信息，并将它保存到 entry 结构中
        // 然后用 prevlen 变量记录前置节点的长度
        // （当插入新节点之后 p 所指向的节点就成了新节点的前置节点）
        // T = O(1)
        entry = zipEntry(p);
        prevlen = entry.prevrawlen;
    } else {
        // 如果 p 指向表尾末端，那么程序需要检查列表是否为：
        // 1)如果 ptail 也指向 ZIP_END ，那么列表为空；
        // 2)如果列表不为空，那么 ptail 将指向列表的最后一个节点。
        unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
        if (ptail[0] != ZIP_END) {
            // 表尾节点为新节点的前置节点

            // 取出表尾节点的长度
            // T = O(1)
            prevlen = zipRawEntryLength(ptail);
        }
    }

    /* See if the entry can be encoded */
    // 尝试看能否将输入字符串转换为整数，如果成功的话：
    // 1)value 将保存转换后的整数值
    // 2)encoding 则保存适用于 value 的编码方式
    // 无论使用什么编码， reqlen 都保存节点值的长度
    // T = O(N)
    if (zipTryEncoding(s,slen,&value,&encoding)) {
        /* 'encoding' is set to the appropriate integer encoding */
        reqlen = zipIntSize(encoding);
    } else {
        /* 'encoding' is untouched, however zipEncodeLength will use the
         * string length to figure out how to encode it. */
        reqlen = slen;
    }
    /* We need space for both the length of the previous entry and
     * the length of the payload. */
    // 计算编码前置节点的长度所需的大小
    // T = O(1)
    reqlen += zipPrevEncodeLength(NULL,prevlen);
    // 计算编码当前节点值所需的大小
    // T = O(1)
    reqlen += zipEncodeLength(NULL,encoding,slen);

    /* When the insert position is not equal to the tail, we need to
     * make sure that the next entry can hold this entry's length in
     * its prevlen field. */
    // 只要新节点不是被添加到列表末端，
    // 那么程序就需要检查看 p 所指向的节点（的 header）能否编码新节点的长度。
    // nextdiff 保存了新旧编码之间的字节大小差，如果这个值大于 0 
    // 那么说明需要对 p 所指向的节点（的 header ）进行扩展
    // T = O(1)
    nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;

    /* Store offset because a realloc may change the address of zl. */
    // 因为重分配空间可能会改变 zl 的地址
    // 所以在分配之前，需要记录 zl 到 p 的偏移量，然后在分配之后依靠偏移量还原 p 
    offset = p-zl;
    // curlen 是 ziplist 原来的长度
    // reqlen 是整个新节点的长度
    // nextdiff 是新节点的后继节点扩展 header 的长度（要么 0 字节，要么 4 个字节）
    // T = O(N)
    zl = ziplistResize(zl,curlen+reqlen+nextdiff);
    p = zl+offset;

    /* Apply memory move when necessary and update tail offset. */
    if (p[0] != ZIP_END) {
        // 新元素之后还有节点，因为新元素的加入，需要对这些原有节点进行调整

        /* Subtract one because of the ZIP_END bytes */
        // 移动现有元素，为新元素的插入空间腾出位置
        // T = O(N)
        memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);

        /* Encode this entry's raw length in the next entry. */
        // 将新节点的长度编码至后置节点
        // p+reqlen 定位到后置节点
        // reqlen 是新节点的长度
        // T = O(1)
        zipPrevEncodeLength(p+reqlen,reqlen);

        /* Update offset for tail */
        // 更新到达表尾的偏移量，将新节点的长度也算上
        ZIPLIST_TAIL_OFFSET(zl) =
            intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);

        /* When the tail contains more than one entry, we need to take
         * "nextdiff" in account as well. Otherwise, a change in the
         * size of prevlen doesn't have an effect on the *tail* offset. */
        // 如果新节点的后面有多于一个节点
        // 那么程序需要将 nextdiff 记录的字节数也计算到表尾偏移量中
        // 这样才能让表尾偏移量正确对齐表尾节点
        // T = O(1)
        tail = zipEntry(p+reqlen);
        if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {
            ZIPLIST_TAIL_OFFSET(zl) =
                intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
        }
    } else {
        /* This element will be the new tail. */
        // 新元素是新的表尾节点
        ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);
    }

    /* When nextdiff != 0, the raw length of the next entry has changed, so
     * we need to cascade the update throughout the ziplist */
    // 当 nextdiff != 0 时，新节点的后继节点的（header 部分）长度已经被改变，
    // 所以需要级联地更新后续的节点
    if (nextdiff != 0) {
        offset = p-zl;
        // T  = O(N^2)
        zl = __ziplistCascadeUpdate(zl,p+reqlen);
        p = zl+offset;
    }

    /* Write the entry */
    // 一切搞定，将前置节点的长度写入新节点的 header
    p += zipPrevEncodeLength(p,prevlen);
    // 将节点值的长度写入新节点的 header
    p += zipEncodeLength(p,encoding,slen);
    // 写入节点值
    if (ZIP_IS_STR(encoding)) {
        // T = O(N)
        memcpy(p,s,slen);
    } else {
        // T = O(1)
        zipSaveInteger(p,value,encoding);
    }

    // 更新列表的节点数量计数器
    // T = O(1)
    ZIPLIST_INCR_LENGTH(zl,1);

    return zl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0x14(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r12
movl (%rdi), %ebp
movb $0x0, 0xb(%rsp)
movq $0x75bcd15, 0x48(%rsp)  # imm = 0x75BCD15
cmpb $-0x1, (%rsi)
je 0x2a8eb
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2aca4
movl 0x4(%r14), %eax
jmp 0x2a903
movl 0x4(%r12), %edi
cmpb $-0x1, (%r12,%rdi)
je 0x2ab40
addq %r12, %rdi
callq 0x2ad80
movl %eax, 0xc(%rsp)
leaq 0x48(%rsp), %rdx
leaq 0xb(%rsp), %r14
movq %rbx, 0x20(%rsp)
movq %rbx, %rdi
movl 0x14(%rsp), %ebx
movl %ebx, %esi
movq %r14, %rcx
callq 0x2b1fc
movl %eax, %ecx
movb (%r14), %r14b
movzbl %r14b, %r15d
movl %ebx, %eax
testl %ecx, %ecx
je 0x2a93e
movl %r15d, %edi
callq 0x2b426
movl %r15d, 0x10(%rsp)
movl %eax, %eax
xorl %ecx, %ecx
cmpl $0xfe, 0xc(%rsp)
setae %cl
xorl %edx, %edx
cmpl $0x4000, %ebx           # imm = 0x4000
setae %dl
movb %r14b, 0xa(%rsp)
cmpb $-0x40, %r14b
movl %ebx, %esi
leaq 0x2(%rdx,%rdx,2), %rbx
pushq $0x1
popq %rdx
cmovaeq %rdx, %rbx
leal 0x1(,%rcx,4), %ecx
cmpl $0x40, %esi
cmovbq %rdx, %rbx
addq %rcx, %rax
addq %rax, %rbx
movb (%r13), %al
cmpb $-0x1, %al
movq %rcx, 0x50(%rsp)
je 0x2a9b5
xorl %ecx, %ecx
cmpb $-0x2, %al
setne %cl
negl %ecx
orl $-0x5, %ecx
xorl %eax, %eax
cmpl $0xfe, %ebx
setae %al
leal (%rcx,%rax,4), %r15d
incl %r15d
jmp 0x2a9b8
xorl %r15d, %r15d
subq %r12, %r13
leal (%r15,%rbp), %esi
addl %ebx, %esi
movq %r12, %rdi
callq 0x2b602
leaq (%rax,%r13), %r12
cmpb $-0x1, (%rax,%r13)
je 0x2aa09
movq %rax, 0x18(%rsp)
movslq %r15d, %rax
leaq (%r12,%rbx), %r14
movq %r12, %rsi
subq %rax, %rsi
movq %r13, %rcx
notq %rcx
addq %rax, %rcx
addq %rcx, %rbp
movq %r14, %rdi
movq %rbp, %rdx
callq 0x1f780
cmpl $0xfd, %ebx
ja 0x2aa12
movl %ebx, %eax
jmp 0x2aa18
movl %r13d, 0x4(%rax)
movq %rax, %rbp
jmp 0x2aa4b
movl %ebx, 0x1(%r14)
movb $-0x2, %al
movq 0x18(%rsp), %rcx
movb %al, (%r14)
addl %ebx, 0x4(%rcx)
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2aca4
movl 0xc(%rbp), %eax
movl 0x10(%rbp), %ecx
movq 0x18(%rsp), %rbp
addq %r14, %rcx
cmpb $-0x1, (%rax,%rcx)
je 0x2aa4b
addl %r15d, 0x4(%rbp)
testl %r15d, %r15d
je 0x2aa7b
addq %rbx, %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2b616
testq %rax, %rax
movb 0xa(%rsp), %cl
movl 0x10(%rsp), %edx
je 0x2aaab
movq %rax, %rbp
addq %rax, %r13
movq %r13, %r12
movq 0x20(%rsp), %r14
jmp 0x2aa88
movq 0x20(%rsp), %r14
movb 0xa(%rsp), %cl
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %eax
cmpl $0xfd, %eax
ja 0x2aa9c
pushq $0x1
popq %rsi
movb %al, (%r12)
jmp 0x2aaba
movb $-0x2, (%r12)
movl %eax, 0x1(%r12)
pushq $0x5
popq %rsi
jmp 0x2aaba
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
movq 0x50(%rsp), %rsi
movl %esi, %r13d
addq %r12, %r13
cmpb $-0x41, %cl
jbe 0x2ab14
movb %cl, 0x28(%rsp)
movb %cl, (%r13)
movq 0x48(%rsp), %rax
cmpl $0xc0, %edx
je 0x2abf4
cmpl $0xd0, %edx
je 0x2abb2
cmpl $0xe0, %edx
je 0x2abc6
cmpl $0xf0, %edx
je 0x2abb8
cmpl $0xfe, %edx
jne 0x2abcc
movb %al, 0x1(%r13)
jmp 0x2ab90
movl 0x14(%rsp), %ebx
cmpl $0x3f, %ebx
ja 0x2ab25
movb %bl, 0x28(%rsp)
pushq $0x1
jmp 0x2ab6e
cmpl $0x3fff, %ebx           # imm = 0x3FFF
ja 0x2ab4d
movl %ebx, %eax
shrl $0x8, %eax
orb $0x40, %al
movb %al, 0x28(%rsp)
movb %bl, 0x29(%rsp)
pushq $0x2
jmp 0x2ab6e
movl $0x0, 0xc(%rsp)
jmp 0x2a907
movb $-0x80, 0x28(%rsp)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0x29(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2a(%rsp)
movb %bh, 0x2b(%rsp)
movb %bl, 0x2c(%rsp)
pushq $0x5
popq %r15
leaq 0x28(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x1fbd0
addq %r15, %r13
movl %ebx, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x1fbd0
movzwl 0x8(%rbp), %eax
cmpw $-0x1, %ax
je 0x2aba0
incl %eax
movw %ax, 0x8(%rbp)
movq %rbp, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, 0x1(%r13)
jmp 0x2ab90
movw %ax, 0x1(%r13)
shrl $0x10, %eax
movb %al, 0x3(%r13)
jmp 0x2ab90
movq %rax, 0x1(%r13)
jmp 0x2ab90
addb $0xf, %cl
cmpb $0xe, %cl
jb 0x2ab90
leaq 0x646e5(%rip), %rdi     # 0x8f2c0
leaq 0x620a3(%rip), %rsi     # 0x8cc85
movl $0x2ab, %edx            # imm = 0x2AB
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0
movw %ax, 0x1(%r13)
jmp 0x2ab90