// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zsetConvert
void zsetConvert(robj *zobj, int encoding) {
    zset *zs;
    zskiplistNode *node, *next;
    robj *ele;
    double score;

    if (zobj->encoding == encoding) return;

    // 从 ZIPLIST 编码转换为 SKIPLIST 编码
    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *zl = zobj->ptr;
        unsigned char *eptr, *sptr;
        unsigned char *vstr;
        unsigned int vlen;
        long long vlong;

        if (encoding != REDIS_ENCODING_SKIPLIST)
            redisPanic("Unknown target encoding");

        // 创建有序集合结构
        zs = zmalloc(sizeof(*zs));
        // 字典
        zs->dict = dictCreate(&zsetDictType,NULL);
        // 跳跃表
        zs->zsl = zslCreate();

        // 有序集合在 ziplist 中的排列：
        //
        // | member-1 | score-1 | member-2 | score-2 | ... |
        //
        // 指向 ziplist 中的首个节点（保存着元素成员）
        eptr = ziplistIndex(zl,0);
        redisAssertWithInfo(NULL,zobj,eptr != NULL);
        // 指向 ziplist 中的第二个节点（保存着元素分值）
        sptr = ziplistNext(zl,eptr);
        redisAssertWithInfo(NULL,zobj,sptr != NULL);

        // 遍历所有 ziplist 节点，并将元素的成员和分值添加到有序集合中
        while (eptr != NULL) {
            
            // 取出分值
            score = zzlGetScore(sptr);

            // 取出成员
            redisAssertWithInfo(NULL,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
            if (vstr == NULL)
                ele = createStringObjectFromLongLong(vlong);
            else
                ele = createStringObject((char*)vstr,vlen);

            /* Has incremented refcount since it was just created. */
            // 将成员和分值分别关联到跳跃表和字典中
            node = zslInsert(zs->zsl,score,ele);
            redisAssertWithInfo(NULL,zobj,dictAdd(zs->dict,ele,&node->score) == DICT_OK);
            incrRefCount(ele); /* Added to dictionary. */

            // 移动指针，指向下个元素
            zzlNext(zl,&eptr,&sptr);
        }

        // 释放原来的 ziplist
        zfree(zobj->ptr);

        // 更新对象的值，以及编码方式
        zobj->ptr = zs;
        zobj->encoding = REDIS_ENCODING_SKIPLIST;

    // 从 SKIPLIST 转换为 ZIPLIST 编码
    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {

        // 新的 ziplist
        unsigned char *zl = ziplistNew();

        if (encoding != REDIS_ENCODING_ZIPLIST)
            redisPanic("Unknown target encoding");

        /* Approach similar to zslFree(), since we want to free the skiplist at
         * the same time as creating the ziplist. */
        // 指向跳跃表
        zs = zobj->ptr;

        // 先释放字典，因为只需要跳跃表就可以遍历整个有序集合了
        dictRelease(zs->dict);

        // 指向跳跃表首个节点
        node = zs->zsl->header->level[0].forward;

        // 释放跳跃表表头
        zfree(zs->zsl->header);
        zfree(zs->zsl);

        // 遍历跳跃表，取出里面的元素，并将它们添加到 ziplist
        while (node) {

            // 取出解码后的值对象
            ele = getDecodedObject(node->obj);

            // 添加元素到 ziplist
            zl = zzlInsertAt(zl,NULL,ele,node->score);
            decrRefCount(ele);

            // 沿着跳跃表的第 0 层前进
            next = node->level[0].forward;
            zslFreeNode(node);
            node = next;
        }

        // 释放跳跃表
        zfree(zs);

        // 更新对象的值，以及对象的编码方式
        zobj->ptr = zl;
        zobj->encoding = REDIS_ENCODING_ZIPLIST;
    } else {
        redisPanic("Unknown sorted set encoding");
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl (%rdi), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl %esi, %eax
je 0x3ecad
movl %esi, %ebp
movq %rdi, %r13
cmpl $0x7, %eax
je 0x3ec11
cmpl $0x5, %eax
jne 0x3ed01
cmpl $0x7, %ebp
jne 0x3ed16
movq 0x8(%r13), %r15
movl $0x10, %edi
callq 0x29709
movq %rax, %r14
leaq 0x8adb2(%rip), %rdi     # 0xc98a0
xorl %esi, %esi
callq 0x21f97
movq %rax, (%r14)
callq 0x3cfc2
movq %rax, 0x8(%r14)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2bff5
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x3ed45
movq %r13, (%rsp)
movq %r15, %rdi
movq %rax, %rsi
callq 0x2c2b3
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3ed60
movq 0x8(%rsp), %r13
testq %r13, %r13
je 0x3ebef
leaq 0x28(%rsp), %rbp
leaq 0x8(%rsp), %rbx
leaq 0x18(%rsp), %r12
movq 0x18(%rsp), %rdi
callq 0x3ddbd
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x14(%rsp), %rdx
movq %rbp, %rcx
callq 0x2c351
testl %eax, %eax
je 0x3ecbc
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3eb95
movl 0x14(%rsp), %esi
callq 0x303b7
jmp 0x3eb9f
movq 0x28(%rsp), %rdi
callq 0x303c6
movq %rax, %r13
movq 0x8(%r14), %rdi
movsd 0x20(%rsp), %xmm0
movq %rax, %rsi
callq 0x3d096
movq (%r14), %rdi
leaq 0x8(%rax), %rdx
movq %r13, %rsi
callq 0x222b6
testl %eax, %eax
jne 0x3ecd8
movq %r13, %rdi
callq 0x3041b
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3df7d
movq 0x8(%rsp), %r13
testq %r13, %r13
jne 0x3eb53
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
callq 0x2984b
movq %r14, 0x8(%rbx)
movl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
andl (%rbx), %eax
orl $0x70, %eax
movl %eax, (%rbx)
jmp 0x3ecad
callq 0x2bbe0
cmpl $0x5, %ebp
jne 0x3ed2b
movq %rax, %rbx
movq 0x8(%r13), %r14
movq (%r14), %rdi
callq 0x227ed
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq 0x18(%rdi), %r15
callq 0x2984b
movq 0x8(%r14), %rdi
callq 0x2984b
testq %r15, %r15
je 0x3ec91
movq (%r15), %rdi
callq 0x30ac3
movq %rax, %r12
movsd 0x8(%r15), %xmm0
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x3e6b1
movq %rax, %rbx
movq %r12, %rdi
callq 0x30834
movq (%r15), %rdi
movq 0x18(%r15), %r12
callq 0x30834
movq %r15, %rdi
callq 0x2984b
movq %r12, %r15
testq %r12, %r12
jne 0x3ec4c
movq %r14, %rdi
callq 0x2984b
movq %rbx, 0x8(%r13)
movl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
andl (%r13), %eax
orl $0x50, %eax
movl %eax, (%r13)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5a52a(%rip), %rdx     # 0x991ed
leaq 0x5a4ec(%rip), %rcx     # 0x991b6
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x667, %r8d            # imm = 0x667
jmp 0x3ecf2
leaq 0x5a5f3(%rip), %rdx     # 0x992d2
leaq 0x5a4d0(%rip), %rcx     # 0x991b6
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x670, %r8d            # imm = 0x670
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0
leaq 0x5864b(%rip), %rdi     # 0x97353
leaq 0x5a4a7(%rip), %rsi     # 0x991b6
movl $0x6ad, %edx            # imm = 0x6AD
jmp 0x3ed3e
leaq 0x5a59b(%rip), %rdi     # 0x992b8
leaq 0x5a492(%rip), %rsi     # 0x991b6
movl $0x64c, %edx            # imm = 0x64C
jmp 0x3ed3e
leaq 0x5a586(%rip), %rdi     # 0x992b8
leaq 0x5a47d(%rip), %rsi     # 0x991b6
movl $0x685, %edx            # imm = 0x685
callq 0x4d0e5
jmp 0x3ecf7
leaq 0x5a4f3(%rip), %rdx     # 0x9923f
leaq 0x5a463(%rip), %rcx     # 0x991b6
xorl %edi, %edi
movq %r13, %rsi
movl $0x65b, %r8d            # imm = 0x65B
jmp 0x3ecf2
leaq 0x5a795(%rip), %rdx     # 0x994fc
leaq 0x5a448(%rip), %rcx     # 0x991b6
xorl %edi, %edi
movq (%rsp), %rsi
movl $0x65e, %r8d            # imm = 0x65E
jmp 0x3ecf2