// File: /workspace/github_c_makefile_star2_O1/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 0x10(%rsp), %rdx
callq 0x3f8b1
testl %eax, %eax
je 0x41457
leaq 0x57f61(%rip), %rsi     # 0x993a2
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d9d7
leaq 0x893a2(%rip), %r13     # 0xca800
movq 0x20(%r13), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31475
testq %rax, %rax
je 0x41489
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x308e7
testl %eax, %eax
je 0x41498
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x4157d
cmpl $0x5, %eax
jne 0x4161d
movq 0x8(%r12), %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x3e149
movq %rax, (%rsp)
testq %rax, %rax
movq %rax, 0x28(%rsp)
je 0x415ea
movq %r15, %rdi
movq %rax, %rsi
callq 0x2c2b3
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x3ddbd
movsd 0x18(%rsp), %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
ucomisd %xmm0, %xmm1
movl 0x24(%rsp), %r14d
setbe %al
setb %cl
testl %r14d, %r14d
cmovel %ecx, %eax
testb %al, %al
jne 0x41637
cmpq $0x0, (%rsp)
je 0x415f6
xorl %ebp, %ebp
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq %rsp, %r12
leaq 0x8(%rsp), %r13
movq 0x8(%rsp), %rdi
callq 0x3ddbd
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %al
setb %cl
testl %r14d, %r14d
cmovel %ecx, %eax
testb %al, %al
jne 0x415f8
incl %ebp
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3df7d
cmpq $0x0, (%rsp)
jne 0x41538
jmp 0x415f8
movq 0x8(%r12), %rax
movq 0x8(%rax), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x3d4e8
testq %rax, %rax
je 0x415e6
movsd 0x8(%rax), %xmm0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3d98d
movq 0x10(%r14), %rcx
subq %rax, %rcx
movabsq $-0xffffffff, %rbp      # imm = 0xFFFFFFFF00000001
addq %rcx, %rbp
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x3d5ad
testq %rax, %rax
je 0x41604
movsd 0x8(%rax), %xmm0
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3d98d
addl %eax, %ebp
subl 0x10(%r14), %ebp
jmp 0x41604
xorl %ebp, %ebp
jmp 0x41604
movq 0x20(%r13), %rsi
movq %rbx, %rdi
callq 0x2d6f0
xorl %ebp, %ebp
cmpq $0x0, 0x28(%rsp)
je 0x41489
movslq %ebp, %rsi
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2dea3
leaq 0x55d2f(%rip), %rdi     # 0x97353
leaq 0x57b8b(%rip), %rsi     # 0x991b6
movl $0xd43, %edx            # imm = 0xD43
callq 0x4d0e5
jmp 0x41656
leaq 0x57ecb(%rip), %rdx     # 0x99509
leaq 0x57b71(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r12, %rsi
movl $0xd0b, %r8d            # imm = 0xD0B
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0