// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 $0x68, %rsp
movl %ecx, %ebx
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r13
movl (%rdi), %r15d
movb $0x0, 0x7(%rsp)
movq $0x75bcd15, 0x20(%rsp)  # imm = 0x75BCD15
movzbl (%rsi), %eax
cmpl $0xfe, %eax
je 0x35714
cmpl $0xff, %eax
jne 0x3571b
movl 0x4(%r13), %edi
cmpb $-0x1, (%r13,%rdi)
je 0x35719
addq %r13, %rdi
callq 0x35133
jmp 0x3571b
movl 0x1(%rbp), %eax
jmp 0x3571b
xorl %eax, %eax
movq %r15, 0x40(%rsp)
movl %eax, 0xc(%rsp)
leaq 0x20(%rsp), %rdx
leaq 0x7(%rsp), %r15
movq %r12, 0x10(%rsp)
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rcx
callq 0x351c0
movl %eax, %ecx
movb (%r15), %r15b
movzbl %r15b, %r14d
movl %ebx, %eax
testl %ecx, %ecx
je 0x35757
movl %r14d, %edi
callq 0x35008
movl %r14d, 0x1c(%rsp)
movl %eax, %eax
xorl %ecx, %ecx
cmpl $0xfe, 0xc(%rsp)
setae %cl
leal 0x1(,%rcx,4), %ecx
movq %rcx, 0x28(%rsp)
addq %rcx, %rax
xorl %ecx, %ecx
cmpl $0x4000, %ebx           # imm = 0x4000
setae %cl
movb %r15b, 0x6(%rsp)
cmpb $-0x40, %r15b
leaq 0x2(%rcx,%rcx,2), %r12
pushq $0x1
popq %rcx
cmovaeq %rcx, %r12
movl %ebx, 0x8(%rsp)
cmpl $0x40, %ebx
cmovbq %rcx, %r12
addq %rax, %r12
movb (%rbp), %cl
xorl %edx, %edx
movl $0x0, %eax
cmpb $-0x1, %cl
je 0x357d5
xorl %eax, %eax
cmpb $-0x2, %cl
setne %al
negl %eax
orl $-0x5, %eax
xorl %ecx, %ecx
cmpl $0xfe, %r12d
setae %cl
leal (%rax,%rcx,4), %eax
incl %eax
cmpl $-0x4, %eax
setne %cl
cmpq $0x4, %r12
setae %bl
orb %cl, %bl
cmovnel %eax, %edx
subq %r13, %rbp
movq 0x40(%rsp), %r14
leal (%r14,%r12), %esi
addl %edx, %esi
movq %r13, %rdi
movl %edx, %r13d
callq 0x3542d
movq %rbp, %rcx
movq %rax, %rbp
leaq (%rax,%rcx), %r15
cmpb $-0x1, (%rax,%rcx)
movq %rcx, 0x38(%rsp)
je 0x35891
movq %rbp, 0x30(%rsp)
movl %r13d, 0x18(%rsp)
movslq %r13d, %rax
leaq (%r15,%r12), %rbp
movq %r15, %rsi
subq %rax, %rsi
notq %rcx
addq %rcx, %r14
addq %rax, %r14
movq %rbp, %rdi
movq %r14, %rdx
callq 0x267e0
testb %bl, %bl
je 0x35853
cmpl $0xfd, %r12d
ja 0x35853
movb %r12b, (%rbp)
jmp 0x3585b
movb $-0x2, (%rbp)
movl %r12d, 0x1(%rbp)
movq 0x30(%rsp), %r14
addl %r12d, 0x4(%r14)
leaq 0x48(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3532d
movl 0xc(%rbx), %eax
movl 0x10(%rbx), %ecx
addq %rbp, %rcx
movq %r14, %rbp
cmpb $-0x1, (%rax,%rcx)
movl 0x18(%rsp), %r13d
je 0x35894
addl %r13d, 0x4(%rbp)
jmp 0x35894
movl %ecx, 0x4(%rbp)
testl %r13d, %r13d
je 0x358be
addq %r12, %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x35441
testq %rax, %rax
movl 0x8(%rsp), %r14d
je 0x358e7
movq %rax, %rbp
movq 0x38(%rsp), %r15
addq %rax, %r15
jmp 0x358c3
movl 0x8(%rsp), %r14d
movq 0x10(%rsp), %r12
movl 0xc(%rsp), %eax
cmpl $0xfd, %eax
ja 0x358da
movb %al, (%r15)
pushq $0x1
jmp 0x358e4
movb $-0x2, (%r15)
movl %eax, 0x1(%r15)
pushq $0x5
popq %rax
jmp 0x358f6
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x10(%rsp), %r12
movq 0x28(%rsp), %rax
movl %eax, %ebx
addq %r15, %rbx
movq %rbx, %rdi
movl 0x1c(%rsp), %r15d
movl %r15d, %esi
movl %r14d, %edx
callq 0x35047
movl %eax, %edi
addq %rbx, %rdi
cmpb $-0x41, 0x6(%rsp)
jbe 0x35929
movq 0x20(%rsp), %rsi
movl %r15d, %edx
callq 0x35243
jmp 0x35934
movl %r14d, %edx
movq %r12, %rsi
callq 0x26c50
movzwl 0x8(%rbp), %eax
cmpw $-0x1, %ax
je 0x35944
incl %eax
movw %ax, 0x8(%rbp)
movq %rbp, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq