// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/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;
    // 按 key 在 zipmap 中查找节点
    // T = O(N^2)
    p = zipmapLookupRaw(zm,key,klen,&zmlen);
    if (p == NULL) {

        /* Key not found: enlarge */
        // key 不存在，扩展 zipmap

        // T = O(N)
        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: */
        // 键已经存在，检查旧的值空间大小能否满足新值
        // 如果不满足的话，扩展 zipmap 并移动数据

        if (update) *update = 1;
        // T = O(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. */
            // 如果已有空间不满足新值所需空间，那么对 zipmap 进行扩展
            // T = O(N)
            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. */
            // 向后移动数据，为节点空出足以存放新值的空间
            // T = O(N)
            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;
        // 前移数据，覆盖空余空间
        // T = O(N)
        memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
        zmlen -= empty;
        // 缩小 zipmap ，移除多余的空间
        // T = O(N)
        zm = zipmapResize(zm, zmlen);
        p = zm+offset;
        vempty = 0;
    } else {
        vempty = empty;
    }

    /* Just write the key + value and we are done. */

    /* Key: */
    // 写入键
    // T = O(N)
    p += zipmapEncodeLength(p,klen);
    memcpy(p,key,klen);
    p += klen;

    /* Value: */
    // 写入值
    // T = O(N)
    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 0x2a38e
movl $0x0, (%rbx)
leaq 0x4(%rsp), %rcx
movq %r15, %rdi
movq %rsi, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
callq 0x2a563
testq %rax, %rax
je 0x2a429
movq %rax, %rbp
testq %rbx, %rbx
je 0x2a3b8
movl $0x1, (%rbx)
movq %rbp, %rdi
callq 0x2a676
movl %eax, %ebx
movl %r12d, %r14d
subl %eax, %r14d
jbe 0x2a466
subq %r15, %rbp
movq %r13, 0x8(%rsp)
movl 0x4(%rsp), %r13d
subl %ebx, %r13d
leal (%r12,%r13), %esi
movq %r15, %rdi
callq 0x297b5
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 0x1f780
addl %r14d, 0x4(%rsp)
movl %r12d, %ebx
movq %r13, %rbp
movq 0x8(%rsp), %r13
jmp 0x2a466
movl 0x4(%rsp), %ebx
leal (%rbx,%r12), %esi
movq %r15, %rdi
callq 0x297b5
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 0x2a463
incb %al
movb %al, (%r15)
movl %r12d, %ebx
movl %ebx, %r14d
subl %r12d, %r14d
cmpl $0x4, %r14d
jb 0x2a4c4
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 0x1f780
movl 0x4(%rsp), %ebx
subl %r14d, %ebx
movl %ebx, 0x4(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x297b5
movq %rax, %r15
decl %ebx
movb $-0x1, (%rax,%rbx)
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 0x2a4e5
cmpl $0xfd, %ecx
ja 0x2a4fa
movb %cl, (%rbp)
movl $0x1, %eax
jmp 0x2a506
xorl %eax, %eax
cmpl $0xfe, %ecx
setae %al
leaq 0x1(,%rax,4), %rax
jmp 0x2a506
movb $-0x2, (%rbp)
movl %ecx, 0x1(%rbp)
movl $0x5, %eax
addq %rax, %rbp
movl %ecx, %ebx
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x1fbd0
addq %rbx, %rbp
movq 0x20(%rsp), %rcx
cmpl $0xfd, %ecx
ja 0x2a530
movb %cl, (%rbp)
movl $0x1, %eax
jmp 0x2a53c
movb $-0x2, (%rbp)
movl %ecx, 0x1(%rbp)
movl $0x5, %eax
leaq (%rax,%rbp), %rdi
incq %rdi
movb %r14b, -0x1(%rdi)
movl %ecx, %edx
movq %r13, %rsi
callq 0x1fbd0
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq