// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0xbf60f(%rip), %rax     # 0x114150
movq 0x50(%rax), %rdx
callq 0x3c0c0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x54b6b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x3, %edx
callq 0x3abb0
cmpl $0x0, %eax
je 0x54b70
jmp 0x54e56
movq -0x28(%rbp), %rdi
callq 0x50080
movl %eax, %eax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x54ba1
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x54ba3
jmp 0x54bce
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x8e7a2(%rip), %rdx     # 0xe3354
leaq 0x8e6d5(%rip), %rcx     # 0xe328e
movl $0xe90, %r8d            # imm = 0xE90
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x5, %eax
jne 0x54d28
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x32e50
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x54c07
jmp 0x54c32
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq 0x8e701(%rip), %rdx     # 0xe3317
leaq 0x8e671(%rip), %rcx     # 0xe328e
movl $0xe97, %r8d            # imm = 0xE97
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 0x54c4c
jmp 0x54c77
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq 0x8e979(%rip), %rdx     # 0xe35d4
leaq 0x8e62c(%rip), %rcx     # 0xe328e
movl $0xe99, %r8d            # imm = 0xE99
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq $0x1, -0x38(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x54cdc
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4cfd0
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl %eax, %edx
callq 0x33a20
cmpl $0x0, %eax
je 0x54cbd
jmp 0x54cdc
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x4f100
jmp 0x54c7f
cmpq $0x0, -0x48(%rbp)
je 0x54d0f
cmpl $0x0, -0xc(%rbp)
je 0x54cfc
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
subq -0x38(%rbp), %rsi
callq 0x36480
jmp 0x54d0d
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
subq $0x1, %rsi
callq 0x36480
jmp 0x54d23
movq -0x8(%rbp), %rdi
leaq 0xbf436(%rip), %rax     # 0x114150
movq 0x50(%rax), %rsi
callq 0x35a70
jmp 0x54e56
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
jne 0x54e32
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rax), %rdi
callq 0x3acc0
movq -0x8(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq %rax, 0x10(%rcx)
movq %rax, -0x20(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x23aa0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x54e1c
movq -0x68(%rbp), %rax
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x60(%rbp), %rdi
movsd -0x70(%rbp), %xmm0
movq -0x20(%rbp), %rsi
callq 0x4e640
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x54dc5
jmp 0x54df0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x896f3(%rip), %rdx     # 0xde4c7
leaq 0x8e4b3(%rip), %rcx     # 0xe328e
movl $0xebe, %r8d            # imm = 0xEBE
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
cmpl $0x0, -0xc(%rbp)
je 0x54e09
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
subq -0x38(%rbp), %rsi
callq 0x36480
jmp 0x54e1a
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
subq $0x1, %rsi
callq 0x36480
jmp 0x54e30
movq -0x8(%rbp), %rdi
leaq 0xbf329(%rip), %rax     # 0x114150
movq 0x50(%rax), %rsi
callq 0x35a70
jmp 0x54e54
leaq 0x8c662(%rip), %rdi     # 0xe149b
leaq 0x8e44e(%rip), %rsi     # 0xe328e
movl $0xeca, %edx            # imm = 0xECA
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x54e56
addq $0x80, %rsp
popq %rbp
retq
nop