// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zrankGenericCommand
void zrankGenericCommand(redisClient *c, int reverse) {
    robj *key = c->argv[1];
    robj *ele = c->argv[2];
    robj *zobj;
    unsigned long llen;
    unsigned long rank;

    // 有序集合
    if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL ||
        checkType(c,zobj,REDIS_ZSET)) return;

    // 元素数量
    llen = zsetLength(zobj);

    redisAssertWithInfo(c,ele,sdsEncodedObject(ele));

    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *zl = zobj->ptr;
        unsigned char *eptr, *sptr;

        eptr = ziplistIndex(zl,0);
        redisAssertWithInfo(c,zobj,eptr != NULL);
        sptr = ziplistNext(zl,eptr);
        redisAssertWithInfo(c,zobj,sptr != NULL);

        // 计算排名
        rank = 1;
        while(eptr != NULL) {
            if (ziplistCompare(eptr,ele->ptr,sdslen(ele->ptr)))
                break;
            rank++;
            zzlNext(zl,&eptr,&sptr);
        }

        if (eptr != NULL) {
            // ZRANK 还是 ZREVRANK ？
            if (reverse)
                addReplyLongLong(c,llen-rank);
            else
                addReplyLongLong(c,rank-1);
        } else {
            addReply(c,shared.nullbulk);
        }

    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
        zset *zs = zobj->ptr;
        zskiplist *zsl = zs->zsl;
        dictEntry *de;
        double score;

        // 从字典中取出元素
        ele = c->argv[2] = tryObjectEncoding(c->argv[2]);
        de = dictFind(zs->dict,ele);
        if (de != NULL) {

            // 取出元素的分值
            score = *(double*)dictGetVal(de);

            // 在跳跃表中计算该元素的排位
            rank = zslGetRank(zsl,score,ele);
            redisAssertWithInfo(c,ele,rank); /* Existing elements always have a rank. */

            // ZRANK 还是 ZREVRANK ？
            if (reverse)
                addReplyLongLong(c,llen-rank);
            else
                addReplyLongLong(c,rank-1);
        } else {
            addReply(c,shared.nullbulk);
        }

    } else {
        redisPanic("Unknown sorted set encoding");
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %r15
leaq 0x888ba(%rip), %rbp     # 0xca800
movq 0x50(%rbp), %rdx
callq 0x31475
testq %rax, %rax
je 0x41f6b
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x308e7
testl %eax, %eax
je 0x41f7a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x3ea49
movl (%r15), %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x42120
movl %eax, %eax
movq %rax, 0x18(%rsp)
movl (%r13), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
movl %r14d, 0xc(%rsp)
je 0x4204b
cmpl $0x5, %eax
jne 0x4213c
movq 0x8(%r13), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2bff5
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x42156
movq %r12, %rdi
movq %rax, %rsi
callq 0x2c2b3
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x42172
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
sete %r14b
je 0x420b2
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
callq 0x2c64f
movl $0x1, %r13d
testl %eax, %eax
jne 0x420ad
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x3df7d
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
sete %r14b
je 0x420b2
incq %r13
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
callq 0x2c64f
jmp 0x42012
movq 0x8(%r13), %r12
movq 0x8(%r12), %r15
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
callq 0x30971
movq %rax, %r14
movq 0x38(%rbx), %rax
movq %r14, 0x10(%rax)
movq (%r12), %rdi
movq %r14, %rsi
callq 0x224dc
testq %rax, %rax
je 0x420c3
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r14, %rsi
callq 0x3d98d
testq %rax, %rax
je 0x4218e
cmpl $0x0, 0xc(%rsp)
je 0x420f1
movq 0x18(%rsp), %rsi
subq %rax, %rsi
movq %rbx, %rdi
jmp 0x420fa
testb %r14b, %r14b
je 0x420dd
movq 0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0x2d6f0
jmp 0x41f6b
movq 0x50(%rbp), %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d6f0
cmpl $0x0, 0xc(%rsp)
je 0x4210d
movq 0x18(%rsp), %rsi
subq %r13, %rsi
movq %rbx, %rdi
jmp 0x42116
decq %rax
movq %rbx, %rdi
movq %rax, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2dea3
decq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2dea3
jmp 0x41f6b
leaq 0x57155(%rip), %rdx     # 0x9927c
leaq 0x57088(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r15, %rsi
movl $0xe90, %r8d            # imm = 0xE90
jmp 0x421a8
leaq 0x55210(%rip), %rdi     # 0x97353
leaq 0x5706c(%rip), %rsi     # 0x991b6
movl $0xeca, %edx            # imm = 0xECA
callq 0x4d0e5
jmp 0x421ad
leaq 0x570e2(%rip), %rdx     # 0x9923f
leaq 0x57052(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r13, %rsi
movl $0xe97, %r8d            # imm = 0xE97
jmp 0x421a8
leaq 0x57383(%rip), %rdx     # 0x994fc
leaq 0x57036(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r13, %rsi
movl $0xe99, %r8d            # imm = 0xE99
jmp 0x421a8
leaq 0x522ba(%rip), %rdx     # 0x9444f
leaq 0x5701a(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r14, %rsi
movl $0xebe, %r8d            # imm = 0xEBE
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0