// File: /workspace/github_c_makefile_star2_O3/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 0x889aa(%rip), %r13     # 0xca800
movq 0x50(%r13), %rdx
callq 0x31745
testq %rax, %rax
je 0x41e7b
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x30bbf
testl %eax, %eax
je 0x41e8a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x3eadd
movl (%r15), %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x4200a
movl %eax, %eax
movq %rax, 0x10(%rsp)
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x41f55
cmpl $0x5, %eax
jne 0x42026
movq 0x8(%r12), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x2c28b
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x42040
movq %rax, %r14
movl %ebp, 0xc(%rsp)
movq %r13, %rdi
movq %rax, %rsi
callq 0x2c558
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x4205c
xorl %r12d, %r12d
leaq 0x20(%rsp), %rbp
movq 0x8(%r15), %rsi
movl -0x8(%rsi), %edx
movq %r14, %rdi
callq 0x2c8fd
testl %eax, %eax
jne 0x41fb5
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x3e09f
movq 0x18(%rsp), %r14
incq %r12
testq %r14, %r14
jne 0x41f09
leaq 0x888bc(%rip), %rax     # 0xca800
movq 0x50(%rax), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x41e7b
movq 0x8(%r12), %r12
movq 0x8(%r12), %r15
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rdi
callq 0x30c49
movq %rax, %r14
movq 0x38(%rbx), %rax
movq %r14, 0x10(%rax)
movq (%r12), %rdi
movq %r14, %rsi
callq 0x226ac
testq %rax, %rax
je 0x41fd4
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r14, %rsi
callq 0x3da08
testq %rax, %rax
je 0x42078
testl %ebp, %ebp
je 0x41fee
movq 0x10(%rsp), %rsi
subq %rax, %rsi
movq %rbx, %rdi
jmp 0x41ff7
cmpl $0x0, 0xc(%rsp)
je 0x41fc4
notq %r12
addq 0x10(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2e133
jmp 0x41e7b
movq 0x50(%r13), %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d97c
decq %rax
movq %rbx, %rdi
movq %rax, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2e133
leaq 0x572b4(%rip), %rdx     # 0x992c5
leaq 0x571e7(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq %r15, %rsi
movl $0xe90, %r8d            # imm = 0xE90
jmp 0x42092
leaq 0x55386(%rip), %rdi     # 0x973b3
leaq 0x571cb(%rip), %rsi     # 0x991ff
movl $0xeca, %edx            # imm = 0xECA
callq 0x4cae0
jmp 0x42097
leaq 0x57241(%rip), %rdx     # 0x99288
leaq 0x571b1(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq %r12, %rsi
movl $0xe97, %r8d            # imm = 0xE97
jmp 0x42092
leaq 0x574e2(%rip), %rdx     # 0x99545
leaq 0x57195(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq %r12, %rsi
movl $0xe99, %r8d            # imm = 0xE99
jmp 0x42092
leaq 0x52420(%rip), %rdx     # 0x9449f
leaq 0x57179(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq %r14, %rsi
movl $0xebe, %r8d            # imm = 0xEBE
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0