// File: /workspace/github_c_makefile_star2_O3/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 %rcx, 0x20(%rsp)
movl %edx, %r13d
movq %rdi, %r15
leal (%r8,%r13), %eax
movl $0xfe, %ecx
xorl %edx, %edx
cmpl %ecx, %r13d
setae %dl
leal (%rax,%rdx,4), %eax
xorl %edx, %edx
movq %r8, 0x18(%rsp)
cmpl %ecx, %r8d
setae %dl
leal (%rax,%rdx,4), %r12d
addl $0x3, %r12d
testq %r9, %r9
movq %rsi, 0x10(%rsp)
je 0x391a0
movq %r9, %rbx
movl $0x0, (%r9)
leaq 0x4(%rsp), %rcx
movq %r15, %rdi
movl %r13d, %edx
callq 0x39349
testq %rax, %rax
je 0x39229
movq %rax, %rbp
movl $0x1, (%rbx)
jmp 0x391b8
leaq 0x4(%rsp), %rcx
movq %r15, %rdi
movl %r13d, %edx
callq 0x39349
movq %rax, %rbp
testq %rax, %rax
je 0x39229
movq %rbp, %rdi
callq 0x3944e
movl %eax, %ebx
movl %r12d, %r14d
subl %eax, %r14d
jbe 0x39266
subq %r15, %rbp
movq %r13, 0x8(%rsp)
movl 0x4(%rsp), %r13d
subl %ebx, %r13d
leal (%r12,%r13), %esi
movq %r15, %rdi
callq 0x3837d
movq %rax, %r15
leal (%r12,%r13), %eax
decl %eax
movb $-0x1, (%r15,%rax)
movl %ebp, %r13d
addq %r15, %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 0x39266
movl 0x4(%rsp), %ebx
leal (%rbx,%r12), %esi
movq %r15, %rdi
callq 0x3837d
movq %rax, %r15
leal (%rbx,%r12), %eax
decl %eax
movb $-0x1, (%r15,%rax)
movl 0x4(%rsp), %eax
leaq -0x1(%r15,%rax), %rbp
addl %r12d, %eax
movl %eax, 0x4(%rsp)
movb (%r15), %al
cmpb $-0x3, %al
ja 0x39263
incb %al
movb %al, (%r15)
movl %r12d, %ebx
movl %ebx, %r14d
subl %r12d, %r14d
cmpl $0x4, %r14d
jb 0x392c4
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), %ebx
subl %r14d, %ebx
movl %ebx, 0x4(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3837d
movq %rax, %r15
decl %ebx
movb $-0x1, (%rax,%rbx)
movl %r13d, %ebp
movq 0x8(%rsp), %r13
addq %rax, %rbp
xorl %r14d, %r14d
movq 0x10(%rsp), %rsi
cmpl $0xfd, %r13d
ja 0x392dc
movl $0x1, %ebx
movl %r13d, %eax
jmp 0x392e7
movl %r13d, 0x1(%rbp)
movl $0x5, %ebx
movb $-0x2, %al
movb %al, (%rbp)
addq %rbp, %rbx
movl %r13d, %r13d
movq %rbx, %rdi
movq %r13, %rdx
callq 0x26c50
addq %r13, %rbx
movq 0x18(%rsp), %rdx
cmpl $0xfd, %edx
ja 0x39314
movl $0x1, %eax
movl %edx, %ecx
jmp 0x3931e
movl %edx, 0x1(%rbx)
movl $0x5, %eax
movb $-0x2, %cl
movb %cl, (%rbx)
leaq (%rbx,%rax), %rdi
incq %rdi
movb %r14b, -0x1(%rdi)
movl %edx, %edx
movq 0x20(%rsp), %rsi
callq 0x26c50
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq