// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zcountCommand
void zcountCommand(redisClient *c) {
    robj *key = c->argv[1];
    robj *zobj;
    zrangespec range;
    int count = 0;

    /* Parse the range arguments */
    // 分析并读入范围参数
    if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) {
        addReplyError(c,"min or max is not a float");
        return;
    }

    /* Lookup the sorted set */
    // 取出有序集合
    if ((zobj = lookupKeyReadOrReply(c, key, shared.czero)) == NULL ||
        checkType(c, zobj, REDIS_ZSET)) return;

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

        /* Use the first element in range as the starting point */
        // 指向指定范围内第一个元素的成员
        eptr = zzlFirstInRange(zl,&range);

        /* No "first" element */
        // 没有任何元素在这个范围内，直接返回
        if (eptr == NULL) {
            addReply(c, shared.czero);
            return;
        }

        /* First element is in range */
        // 取出分值
        sptr = ziplistNext(zl,eptr);
        score = zzlGetScore(sptr);
        redisAssertWithInfo(c,zobj,zslValueLteMax(score,&range));

        /* Iterate over elements in range */
        // 遍历范围内的所有元素
        while (eptr) {

            score = zzlGetScore(sptr);

            /* Abort when the node is no longer in range. */
            // 如果分值不符合范围，跳出
            if (!zslValueLteMax(score,&range)) {
                break;

            // 分值符合范围，增加 count 计数器
            // 然后指向下一个元素
            } else {
                count++;
                zzlNext(zl,&eptr,&sptr);
            }
        }

    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
        zset *zs = zobj->ptr;
        zskiplist *zsl = zs->zsl;
        zskiplistNode *zn;
        unsigned long rank;

        /* Find first element in range */
        // 指向指定范围内第一个元素
        zn = zslFirstInRange(zsl, &range);

        /* Use rank of first element, if any, to determine preliminary count */
        // 如果有至少一个元素在范围内，那么执行以下代码
        if (zn != NULL) {
            // 确定范围内第一个元素的排位
            rank = zslGetRank(zsl, zn->score, zn->obj);

            count = (zsl->length - (rank - 1));

            /* Find last element in range */
            // 指向指定范围内的最后一个元素
            zn = zslLastInRange(zsl, &range);

            /* Use rank of last element, if any, to determine the actual count */
            // 如果范围内的最后一个元素不为空，那么执行以下代码
            if (zn != NULL) {
                // 确定范围内最后一个元素的排位
                rank = zslGetRank(zsl, zn->score, zn->obj);

                // 这里计算的就是第一个和最后一个两个元素之间的元素数量
                // （包括这两个元素）
                count -= (zsl->length - rank);
            }
        }

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

    addReplyLongLong(c, count);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r14
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x20(%rsp), %rdx
callq 0x3f8e5
testl %eax, %eax
je 0x41428
leaq 0x57fd9(%rip), %rsi     # 0x993eb
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2dc63
leaq 0x893d1(%rip), %r12     # 0xca800
movq 0x20(%r12), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31745
testq %rax, %rax
je 0x4145b
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x30bbf
testl %eax, %eax
je 0x4146a
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r15), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x4153a
cmpl $0x5, %eax
jne 0x415d1
movq 0x8(%r15), %r14
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3e26b
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x415bf
movq %r14, %rdi
movq %rax, %rsi
callq 0x2c558
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x3ded9
movsd 0x28(%rsp), %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
movsd %xmm1, 0x18(%rsp)
ucomisd %xmm0, %xmm1
movl 0x34(%rsp), %ebp
setbe %al
setb %cl
testl %ebp, %ebp
cmovel %ecx, %eax
testb %al, %al
jne 0x415eb
xorl %r13d, %r13d
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r12
movq 0x8(%rsp), %rdi
callq 0x3ded9
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %al
setb %cl
testl %ebp, %ebp
cmovel %ecx, %eax
testb %al, %al
jne 0x415a6
incl %r13d
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3e09f
cmpq $0x0, 0x10(%rsp)
jne 0x414f4
jmp 0x415a6
movq 0x8(%r15), %rax
movq 0x8(%rax), %r14
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3d576
testq %rax, %rax
je 0x415a3
movsd 0x8(%rax), %xmm0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3da08
movq 0x10(%r14), %rcx
subq %rax, %rcx
movabsq $-0xffffffff, %r13      # imm = 0xFFFFFFFF00000001
addq %rcx, %r13
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3d63a
testq %rax, %rax
je 0x415a6
movsd 0x8(%rax), %xmm0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3da08
addl %eax, %r13d
subl 0x10(%r14), %r13d
jmp 0x415a6
xorl %r13d, %r13d
movslq %r13d, %rsi
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2e133
movq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x4145b
leaq 0x55ddb(%rip), %rdi     # 0x973b3
leaq 0x57c20(%rip), %rsi     # 0x991ff
movl $0xd43, %edx            # imm = 0xD43
callq 0x4cae0
jmp 0x4160a
leaq 0x57f60(%rip), %rdx     # 0x99552
leaq 0x57c06(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq %r15, %rsi
movl $0xd0b, %r8d            # imm = 0xD0B
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0