// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movl -0x14(%rbp), %edi
movl -0x24(%rbp), %esi
callq 0x3d4a0
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3d294
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
leaq -0x34(%rbp), %rcx
callq 0x3d4e0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x3d2ff
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
addl -0x40(%rbp), %esi
callq 0x3d620
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
movl -0x34(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xfe, %eax
jge 0x3d2fa
movq -0x8(%rbp), %rax
movb (%rax), %cl
addb $0x1, %cl
movb %cl, (%rax)
jmp 0x3d395
cmpq $0x0, -0x30(%rbp)
je 0x3d310
movq -0x30(%rbp), %rax
movl $0x1, (%rax)
movq -0x50(%rbp), %rdi
callq 0x3d660
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x3d393
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
subl -0x3c(%rbp), %esi
addl -0x40(%rbp), %esi
callq 0x3d620
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl -0x38(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movl -0x40(%rbp), %eax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movl -0x3c(%rbp), %eax
addq %rax, %rsi
movl -0x34(%rbp), %eax
movl -0x38(%rbp), %ecx
addl -0x3c(%rbp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x267e0
movl -0x34(%rbp), %eax
subl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x3d395
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x4, -0x44(%rbp)
jb 0x3d40f
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
movl -0x40(%rbp), %eax
addq %rax, %rdi
movq -0x50(%rbp), %rsi
movl -0x3c(%rbp), %eax
addq %rax, %rsi
movl -0x34(%rbp), %eax
movl -0x38(%rbp), %ecx
addl -0x3c(%rbp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x267e0
movl -0x44(%rbp), %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x3d620
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl -0x38(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x48(%rbp)
jmp 0x3d415
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x50(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x3d6a0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %eax
movl %eax, %edx
callq 0x26c60
movl -0x14(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x3d6a0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl -0x48(%rbp), %eax
movb %al, %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %eax
movl %eax, %edx
callq 0x26c60
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop