// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/ziplist.c
// Function: _ziplistInsert
unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
    size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen;
    unsigned int prevlensize, 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 tail;

    /* Find out prevlen for the entry that is inserted. */
    if (p[0] != ZIP_END) {
        ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
    } else {
        unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
        if (ptail[0] != ZIP_END) {
            prevlen = zipRawEntryLength(ptail);
        }
    }

    /* See if the entry can be encoded */
    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. */
    reqlen += zipPrevEncodeLength(NULL,prevlen);
    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. */
    int forcelarge = 0;
    nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
    if (nextdiff == -4 && reqlen < 4) {
        nextdiff = 0;
        forcelarge = 1;
    }

    /* Store offset because a realloc may change the address of zl. */
    offset = p-zl;
    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 */
        memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);

        /* Encode this entry's raw length in the next entry. */
        if (forcelarge)
            zipPrevEncodeLengthForceLarge(p+reqlen,reqlen);
        else
            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. */
        zipEntry(p+reqlen, &tail);
        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 */
    if (nextdiff != 0) {
        offset = p-zl;
        zl = __ziplistCascadeUpdate(zl,p+reqlen);
        p = zl+offset;
    }

    /* Write the entry */
    p += zipPrevEncodeLength(p,prevlen);
    p += zipEncodeLength(p,encoding,slen);
    if (ZIP_IS_STR(encoding)) {
        memcpy(p,s,slen);
    } else {
        zipSaveInteger(p,value,encoding);
    }
    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, %ebp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
movl (%rdi), %r12d
movb $0x0, 0x5(%rsp)
movq $0x75bcd15, 0x20(%rsp)  # imm = 0x75BCD15
movzbl (%rsi), %r14d
cmpl $0xfe, %r14d
je 0x3e62f
cmpl $0xff, %r14d
jne 0x3e638
movl 0x4(%r13), %edi
cmpb $-0x1, (%r13,%rdi)
je 0x3e635
addq %r13, %rdi
callq 0x3df51
movl %eax, %r14d
jmp 0x3e638
movl 0x1(%r15), %r14d
jmp 0x3e638
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdx
leaq 0x5(%rsp), %rcx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3e03f
movl %ebp, %ecx
testl %eax, %eax
je 0x3e6a6
movzbl 0x5(%rsp), %eax
cmpl $0xdf, %eax
jle 0x3e67a
cmpl $0xe0, %eax
je 0x3e68f
cmpl $0xf0, %eax
je 0x3e696
movl $0x1, %ecx
cmpl $0xfe, %eax
je 0x3e6a6
jmp 0x3e6a4
cmpl $0xc0, %eax
je 0x3e69d
cmpl $0xd0, %eax
jne 0x3e6a4
movl $0x4, %ecx
jmp 0x3e6a6
movl $0x8, %ecx
jmp 0x3e6a6
movl $0x3, %ecx
jmp 0x3e6a6
movl $0x2, %ecx
jmp 0x3e6a6
xorl %ecx, %ecx
movq %rbx, 0x28(%rsp)
movl %ecx, %eax
xorl %ecx, %ecx
movl %r14d, 0x8(%rsp)
cmpl $0xfe, %r14d
setae %cl
leal 0x1(,%rcx,4), %ecx
movq %rcx, 0x10(%rsp)
addq %rcx, %rax
movb 0x5(%rsp), %dl
xorl %ecx, %ecx
cmpl $0x4000, %ebp           # imm = 0x4000
setae %cl
movb %dl, 0x7(%rsp)
cmpb $-0x40, %dl
leaq 0x2(%rcx,%rcx,2), %r14
movl $0x1, %ecx
cmovaeq %rcx, %r14
movl %ebp, 0xc(%rsp)
cmpl $0x40, %ebp
cmovbq %rcx, %r14
addq %rax, %r14
movb (%r15), %cl
xorl %edx, %edx
movl $0x0, %eax
cmpb $-0x1, %cl
je 0x3e72c
xorl %eax, %eax
cmpb $-0x2, %cl
setne %al
negl %eax
orl $-0x5, %eax
xorl %ecx, %ecx
cmpl $0xfe, %r14d
setae %cl
leal (%rax,%rcx,4), %eax
incl %eax
cmpl $-0x4, %eax
setne %cl
cmpq $0x4, %r14
setae %sil
orb %cl, %sil
movb %sil, 0x6(%rsp)
cmovnel %eax, %edx
movq %rdx, 0x18(%rsp)
subq %r13, %r15
leal (%r12,%r14), %ebp
leal (%rdx,%rbp), %ebx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3b349
movq 0x18(%rsp), %rcx
movq %rax, %r13
movl %ebx, (%rax)
leal (%rcx,%rbp), %eax
decl %eax
movb $-0x1, (%r13,%rax)
leaq (%r15,%r13), %rbx
cmpb $-0x1, (%r13,%r15)
movq %r15, 0x30(%rsp)
je 0x3e7f6
movslq %ecx, %rax
leaq (%rbx,%r14), %rbp
movq %rbx, %rsi
subq %rax, %rsi
notq %r15
addq %r15, %r12
addq %rax, %r12
movq %rbp, %rdi
movq %r12, %rdx
callq 0x267f0
cmpb $0x0, 0x6(%rsp)
je 0x3e7bc
cmpl $0xfd, %r14d
ja 0x3e7bc
movb %r14b, (%rbp)
jmp 0x3e7c4
movb $-0x2, (%rbp)
movl %r14d, 0x1(%rbp)
addl %r14d, 0x4(%r13)
leaq 0x38(%rsp), %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3e1c2
movl 0xc(%r12), %eax
movl 0x10(%r12), %ecx
addq %rbp, %rcx
cmpb $-0x1, (%rax,%rcx)
movq 0x18(%rsp), %rcx
je 0x3e7fa
addl %ecx, 0x4(%r13)
jmp 0x3e7fa
movl %r15d, 0x4(%r13)
testl %ecx, %ecx
movl 0xc(%rsp), %ebp
je 0x3e81b
addq %r14, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3e31b
movq %rax, %r13
movq 0x30(%rsp), %rbx
addq %rax, %rbx
movl 0x8(%rsp), %eax
movq 0x10(%rsp), %rcx
testq %rbx, %rbx
je 0x3e844
cmpl $0xfd, %eax
ja 0x3e839
movb %al, (%rbx)
movl $0x1, %ecx
jmp 0x3e844
movb $-0x2, (%rbx)
movl %eax, 0x1(%rbx)
movl $0x5, %ecx
movl %ecx, %eax
addq %rax, %rbx
movzbl 0x7(%rsp), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x3de59
movl %eax, %edi
addq %rbx, %rdi
movb 0x5(%rsp), %al
cmpb $-0x41, %al
jbe 0x3e874
movq 0x20(%rsp), %rsi
movzbl %al, %edx
callq 0x3e0c4
jmp 0x3e880
movl %ebp, %edx
movq 0x28(%rsp), %rsi
callq 0x26c60
movzwl 0x8(%r13), %eax
cmpw $-0x1, %ax
je 0x3e892
incl %eax
movw %ax, 0x8(%r13)
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq