// File: /workspace/github_c_makefile_star2_O2/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, %ebp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %r15
leaq 0x88b0a(%rip), %r13     # 0xc7800
movq 0x50(%r13), %rdx
callq 0x2f9a9
testq %rax, %rax
je 0x3ed19
movq %rax, %r12
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
je 0x3ed28
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x3bc1e
movl (%r15), %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x3eeae
movl %eax, %eax
movq %rax, 0x10(%rsp)
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3ede1
cmpl $0x5, %eax
jne 0x3eeca
movq 0x8(%r12), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x2abfb
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3eee4
movq %rax, %r14
movl %ebp, 0xc(%rsp)
movq %r13, %rdi
movq %rax, %rsi
callq 0x2ae0d
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x3ef00
xorl %r12d, %r12d
leaq 0x20(%rsp), %rbp
testq %r14, %r14
je 0x3ee41
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
movq %r14, %rdi
callq 0x2b164
testl %eax, %eax
jne 0x3ee59
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x3b25c
movq 0x18(%rsp), %r14
incq %r12
jmp 0x3eda7
movq 0x8(%r12), %r12
movq 0x8(%r12), %r15
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
callq 0x2ef4c
movq %rax, %r14
movq 0x38(%rbx), %rax
movq %r14, 0x10(%rax)
movq (%r12), %rdi
movq %r14, %rsi
callq 0x21b64
testq %rax, %rax
je 0x3ee78
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r14, %rsi
callq 0x3ad26
testq %rax, %rax
je 0x3ef1c
testl %ebp, %ebp
je 0x3ee92
movq 0x10(%rsp), %rsi
subq %rax, %rsi
movq %rbx, %rdi
jmp 0x3ee9b
leaq 0x889b8(%rip), %rax     # 0xc7800
movq 0x50(%rax), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x3ed19
cmpl $0x0, 0xc(%rsp)
je 0x3ee68
notq %r12
addq 0x10(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2c794
jmp 0x3ed19
movq 0x50(%r13), %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c10d
decq %rax
movq %rbx, %rdi
movq %rax, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c794
leaq 0x503d4(%rip), %rdx     # 0x8f289
leaq 0x50307(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq %r15, %rsi
movl $0xe90, %r8d            # imm = 0xE90
jmp 0x3ef36
leaq 0x4e4d6(%rip), %rdi     # 0x8d3a7
leaq 0x502eb(%rip), %rsi     # 0x8f1c3
movl $0xeca, %edx            # imm = 0xECA
callq 0x490e8
jmp 0x3ef3b
leaq 0x50361(%rip), %rdx     # 0x8f24c
leaq 0x502d1(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq %r12, %rsi
movl $0xe97, %r8d            # imm = 0xE97
jmp 0x3ef36
leaq 0x50602(%rip), %rdx     # 0x8f509
leaq 0x502b5(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq %r12, %rsi
movl $0xe99, %r8d            # imm = 0xE99
jmp 0x3ef36
leaq 0x4b53c(%rip), %rdx     # 0x8a45f
leaq 0x50299(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq %r14, %rsi
movl $0xebe, %r8d            # imm = 0xEBE
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0