// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl -0xc(%rbp), %eax
jne 0x50135
jmp 0x504b1
movq -0x8(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x5, %eax
jne 0x5037a
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
cmpl $0x7, -0xc(%rbp)
je 0x5017e
leaq 0x9322d(%rip), %rdi     # 0xe3390
leaq 0x93124(%rip), %rsi     # 0xe328e
movl $0x64c, %edx            # imm = 0x64C
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
movl $0x10, %edi
callq 0x2e130
movq %rax, -0x18(%rbp)
leaq 0xc271d(%rip), %rdi     # 0x1128b0
xorl %eax, %eax
movl %eax, %esi
callq 0x231a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
callq 0x4d060
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x32e50
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x501ce
jmp 0x501f9
movq -0x8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq 0x9313a(%rip), %rdx     # 0xe3317
leaq 0x930aa(%rip), %rcx     # 0xe328e
movl $0x65b, %r8d            # imm = 0x65B
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x33330
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x50213
jmp 0x5023e
movq -0x8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq 0x933b2(%rip), %rdx     # 0xe35d4
leaq 0x93065(%rip), %rcx     # 0xe328e
movl $0x65e, %r8d            # imm = 0x65E
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
jmp 0x50240
cmpq $0x0, -0x48(%rbp)
je 0x5034b
movq -0x50(%rbp), %rdi
callq 0x4ee80
movsd %xmm0, -0x38(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x33490
cmpl $0x0, %eax
je 0x50275
jmp 0x502a0
movq -0x8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq 0x93041(%rip), %rdx     # 0xe32c5
leaq 0x93003(%rip), %rcx     # 0xe328e
movl $0x667, %r8d            # imm = 0x667
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
cmpq $0x0, -0x58(%rbp)
jne 0x502b6
movq -0x68(%rbp), %rdi
callq 0x3a330
movq %rax, -0x30(%rbp)
jmp 0x502c8
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
callq 0x3a2e0
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movsd -0x38(%rbp), %xmm0
movq -0x30(%rbp), %rsi
callq 0x4d210
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
callq 0x236d0
cmpl $0x0, %eax
jne 0x50301
jmp 0x5032c
movq -0x8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq 0x9309a(%rip), %rdx     # 0xe33aa
leaq 0x92f77(%rip), %rcx     # 0xe328e
movl $0x670, %r8d            # imm = 0x670
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x30(%rbp), %rdi
callq 0x3a3f0
movq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4f100
jmp 0x50240
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2e3a0
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
andl $0xffffff0f, %ecx       # imm = 0xFFFFFF0F
orl $0x70, %ecx
movl %ecx, (%rax)
jmp 0x504b1
movq -0x8(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
jne 0x5048d
callq 0x32a00
movq %rax, -0x70(%rbp)
cmpl $0x5, -0xc(%rbp)
je 0x503c0
leaq 0x92feb(%rip), %rdi     # 0xe3390
leaq 0x92ee2(%rip), %rsi     # 0xe328e
movl $0x685, %edx            # imm = 0x685
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x24000
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
callq 0x2e3a0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2e3a0
cmpq $0x0, -0x20(%rbp)
je 0x50465
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x3aef0
movq %rax, -0x30(%rbp)
movq -0x70(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorl %eax, %eax
movl %eax, %esi
callq 0x4fb00
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3aaa0
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4d120
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x50408
movq -0x18(%rbp), %rdi
callq 0x2e3a0
movq -0x70(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
andl $0xffffff0f, %ecx       # imm = 0xFFFFFF0F
orl $0x50, %ecx
movl %ecx, (%rax)
jmp 0x504af
leaq 0x91007(%rip), %rdi     # 0xe149b
leaq 0x92df3(%rip), %rsi     # 0xe328e
movl $0x6ad, %edx            # imm = 0x6AD
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x504b1
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)