// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/zipmap.c
// Function: zipmapSet
unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update) {
    unsigned int zmlen, offset;
    unsigned int freelen, reqlen = zipmapRequiredLength(klen,vlen);
    unsigned int empty, vempty;
    unsigned char *p;

    freelen = reqlen;
    if (update) *update = 0;
    p = zipmapLookupRaw(zm,key,klen,&zmlen);
    if (p == NULL) {
        /* Key not found: enlarge */
        zm = zipmapResize(zm, zmlen+reqlen);
        p = zm+zmlen-1;
        zmlen = zmlen+reqlen;

        /* Increase zipmap length (this is an insert) */
        if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;
    } else {
        /* Key found. Is there enough space for the new value? */
        /* Compute the total length: */
        if (update) *update = 1;
        freelen = zipmapRawEntryLength(p);
        if (freelen < reqlen) {
            /* Store the offset of this key within the current zipmap, so
             * it can be resized. Then, move the tail backwards so this
             * pair fits at the current position. */
            offset = p-zm;
            zm = zipmapResize(zm, zmlen-freelen+reqlen);
            p = zm+offset;

            /* The +1 in the number of bytes to be moved is caused by the
             * end-of-zipmap byte. Note: the *original* zmlen is used. */
            memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
            zmlen = zmlen-freelen+reqlen;
            freelen = reqlen;
        }
    }

    /* We now have a suitable block where the key/value entry can
     * be written. If there is too much free space, move the tail
     * of the zipmap a few bytes to the front and shrink the zipmap,
     * as we want zipmaps to be very space efficient. */
    empty = freelen-reqlen;
    if (empty >= ZIPMAP_VALUE_MAX_FREE) {
        /* First, move the tail <empty> bytes to the front, then resize
         * the zipmap to be <empty> bytes smaller. */
        offset = p-zm;
        memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
        zmlen -= empty;
        zm = zipmapResize(zm, zmlen);
        p = zm+offset;
        vempty = 0;
    } else {
        vempty = empty;
    }

    /* Just write the key + value and we are done. */
    /* Key: */
    p += zipmapEncodeLength(p,klen);
    memcpy(p,key,klen);
    p += klen;
    /* Value: */
    p += zipmapEncodeLength(p,vlen);
    *p++ = vempty;
    memcpy(p,val,vlen);
    return zm;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %rbx
movq %rcx, %r13
movq %rdi, %r15
leal (%r8,%rdx), %eax
movl $0xfe, %ecx
xorl %edi, %edi
cmpl %ecx, %edx
setae %dil
leal (%rax,%rdi,4), %eax
xorl %edi, %edi
movq %r8, 0x20(%rsp)
cmpl %ecx, %r8d
setae %dil
leal (%rax,%rdi,4), %r12d
addl $0x3, %r12d
testq %r9, %r9
je 0x33859
andl $0x0, (%rbx)
leaq 0x4(%rsp), %rcx
movq %r15, %rdi
movq %rsi, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
callq 0x339ff
testq %rax, %rax
je 0x338e0
movq %rax, %rbp
testq %rbx, %rbx
je 0x33883
movl $0x1, (%rbx)
movq %rbp, %rdi
callq 0x33b16
movl %eax, %ebx
movl %r12d, %r14d
subl %eax, %r14d
jbe 0x33913
subq %r15, %rbp
movl 0x4(%rsp), %esi
addl %r14d, %esi
movq %r15, %rdi
callq 0x33b04
movq %rax, %r15
movq %r13, 0x8(%rsp)
movl %ebp, %r13d
addq %rax, %r13
leaq (%r12,%r13), %rdi
movl %ebx, %esi
addq %r13, %rsi
addl %ebp, %ebx
notl %ebx
addl 0x4(%rsp), %ebx
movq %rbx, %rdx
callq 0x267e0
addl %r14d, 0x4(%rsp)
movl %r12d, %ebx
movq %r13, %rbp
movq 0x8(%rsp), %r13
jmp 0x33913
movl 0x4(%rsp), %esi
addl %r12d, %esi
movq %r15, %rdi
callq 0x33b04
movq %rax, %r15
movl 0x4(%rsp), %eax
leaq (%r15,%rax), %rbp
decq %rbp
addl %r12d, %eax
movl %eax, 0x4(%rsp)
movb (%r15), %al
cmpb $-0x3, %al
ja 0x33910
incb %al
movb %al, (%r15)
movl %r12d, %ebx
movl %ebx, %r14d
subl %r12d, %r14d
cmpl $0x4, %r14d
jb 0x33968
movq %r13, 0x8(%rsp)
movq %rbp, %r13
subq %r15, %r13
leaq (%r12,%rbp), %rdi
movl %ebx, %esi
addq %rbp, %rsi
addl %r13d, %ebx
notl %ebx
addl 0x4(%rsp), %ebx
movq %rbx, %rdx
callq 0x267e0
movl 0x4(%rsp), %esi
subl %r14d, %esi
movl %esi, 0x4(%rsp)
movq %r15, %rdi
callq 0x33b04
movq %rax, %r15
movl %r13d, %ebp
movq 0x8(%rsp), %r13
addq %rax, %rbp
xorl %r14d, %r14d
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rcx
testq %rbp, %rbp
je 0x33987
cmpl $0xfd, %ecx
ja 0x3399c
pushq $0x1
popq %rax
movb %cl, (%rbp)
jmp 0x339a6
xorl %eax, %eax
cmpl $0xfe, %ecx
setae %al
leaq 0x1(,%rax,4), %rax
jmp 0x339a6
pushq $0x5
popq %rax
movb $-0x2, (%rbp)
movl %ecx, 0x1(%rbp)
addq %rax, %rbp
movl %ecx, %ebx
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x26c50
addq %rbx, %rbp
movq 0x20(%rsp), %rdx
cmpl $0xfd, %edx
ja 0x339cd
pushq $0x1
popq %rax
movl %edx, %ecx
jmp 0x339d5
movl %edx, 0x1(%rbp)
pushq $0x5
popq %rax
movb $-0x2, %cl
movb %cl, (%rbp)
leaq (%rax,%rbp), %rdi
incq %rdi
movb %r14b, -0x1(%rdi)
movl %edx, %edx
movq %r13, %rsi
callq 0x26c50
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq