// File: /workspace/github_c_makefile_star2_O2/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 0x3c9cf
testl %eax, %eax
je 0x3e373
leaq 0x51052(%rip), %rsi     # 0x8f3af
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c2da
leaq 0x89486(%rip), %r13     # 0xc7800
movq 0x20(%r13), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f9a9
testq %rax, %rax
je 0x3e3a3
movq %rax, %r15
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
je 0x3e3b2
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 0x3e489
cmpl $0x5, %eax
jne 0x3e51d
movq 0x8(%r15), %r14
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3b41a
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x3e50c
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
callq 0x2ae0d
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x3b09e
movsd 0x28(%rsp), %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
movsd %xmm1, 0x18(%rsp)
ucomisd %xmm0, %xmm1
movl 0x34(%rsp), %r13d
setbe %al
setb %cl
testl %r13d, %r13d
cmovel %ecx, %eax
testb %al, %al
jne 0x3e537
xorl %ebp, %ebp
leaq 0x8(%rsp), %r15
testq %r12, %r12
je 0x3e4f3
movq 0x8(%rsp), %rdi
callq 0x3b09e
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %al
setb %cl
testl %r13d, %r13d
cmovel %ecx, %eax
testb %al, %al
jne 0x3e4f3
incl %ebp
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x3b25c
movq 0x10(%rsp), %r12
jmp 0x3e43b
movq 0x8(%r15), %rax
movq 0x8(%rax), %r14
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3a92b
testq %rax, %rax
je 0x3e4f1
movsd 0x8(%rax), %xmm0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3ad26
movq 0x10(%r14), %rcx
subq %rax, %rcx
movabsq $-0xffffffff, %rbp      # imm = 0xFFFFFFFF00000001
addq %rcx, %rbp
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3a9e1
testq %rax, %rax
je 0x3e4f3
movsd 0x8(%rax), %xmm0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3ad26
addl %eax, %ebp
subl 0x10(%r14), %ebp
jmp 0x3e4f3
xorl %ebp, %ebp
movslq %ebp, %rsi
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c794
movq 0x20(%r13), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x3e3a3
leaq 0x4ee83(%rip), %rdi     # 0x8d3a7
leaq 0x50c98(%rip), %rsi     # 0x8f1c3
movl $0xd43, %edx            # imm = 0xD43
callq 0x490e8
jmp 0x3e556
leaq 0x50fd8(%rip), %rdx     # 0x8f516
leaq 0x50c7e(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq %r15, %rsi
movl $0xd0b, %r8d            # imm = 0xD0B
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0