// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: genericZrangebyscoreCommand
void genericZrangebyscoreCommand(redisClient *c, int reverse) {
    zrangespec range;
    robj *key = c->argv[1];
    robj *zobj;
    long offset = 0, limit = -1;
    int withscores = 0;
    unsigned long rangelen = 0;
    void *replylen = NULL;
    int minidx, maxidx;

    /* Parse the range arguments. */
    if (reverse) {
        /* Range is given as [max,min] */
        maxidx = 2; minidx = 3;
    } else {
        /* Range is given as [min,max] */
        minidx = 2; maxidx = 3;
    }

    // 分析并读入范围
    if (zslParseRange(c->argv[minidx],c->argv[maxidx],&range) != REDIS_OK) {
        addReplyError(c,"min or max is not a float");
        return;
    }

    /* Parse optional extra arguments. Note that ZCOUNT will exactly have
     * 4 arguments, so we'll never enter the following code path. */
    // 分析并读入可选参数
    if (c->argc > 4) {
        int remaining = c->argc - 4;
        int pos = 4;

        while (remaining) {
            if (remaining >= 1 && !strcasecmp(c->argv[pos]->ptr,"withscores")) {
                pos++; remaining--;
                withscores = 1;
            } else if (remaining >= 3 && !strcasecmp(c->argv[pos]->ptr,"limit")) {
                if ((getLongFromObjectOrReply(c, c->argv[pos+1], &offset, NULL) != REDIS_OK) ||
                    (getLongFromObjectOrReply(c, c->argv[pos+2], &limit, NULL) != REDIS_OK)) return;
                pos += 3; remaining -= 3;
            } else {
                addReply(c,shared.syntaxerr);
                return;
            }
        }
    }

    /* Ok, lookup the key and get the range */
    // 取出有序集合对象
    if ((zobj = lookupKeyReadOrReply(c,key,shared.emptymultibulk)) == NULL ||
        checkType(c,zobj,REDIS_ZSET)) return;

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

        /* If reversed, get the last node in range as starting point. */
        // 迭代的方向
        if (reverse) {
            eptr = zzlLastInRange(zl,&range);
        } else {
            eptr = zzlFirstInRange(zl,&range);
        }

        /* No "first" element in the specified interval. */
        // 没有元素在指定范围之内
        if (eptr == NULL) {
            addReply(c, shared.emptymultibulk);
            return;
        }

        /* Get score pointer for the first element. */
        redisAssertWithInfo(c,zobj,eptr != NULL);
        sptr = ziplistNext(zl,eptr);

        /* We don't know in advance how many matching elements there are in the
         * list, so we push this object that will represent the multi-bulk
         * length in the output buffer, and will "fix" it later */
        replylen = addDeferredMultiBulkLength(c);

        /* If there is an offset, just traverse the number of elements without
         * checking the score because that is done in the next loop. */
        // 跳过 offset 指定数量的元素
        while (eptr && offset--) {
            if (reverse) {
                zzlPrev(zl,&eptr,&sptr);
            } else {
                zzlNext(zl,&eptr,&sptr);
            }
        }

        // 遍历并返回所有在范围内的元素
        while (eptr && limit--) {

            // 分值
            score = zzlGetScore(sptr);

            /* Abort when the node is no longer in range. */
            // 检查分值是否符合范围
            if (reverse) {
                if (!zslValueGteMin(score,&range)) break;
            } else {
                if (!zslValueLteMax(score,&range)) break;
            }

            /* We know the element exists, so ziplistGet should always succeed */
            redisAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));

            rangelen++;
            if (vstr == NULL) {
                addReplyBulkLongLong(c,vlong);
            } else {
                addReplyBulkCBuffer(c,vstr,vlen);
            }

            if (withscores) {
                addReplyDouble(c,score);
            }

            /* Move to next node */
            if (reverse) {
                zzlPrev(zl,&eptr,&sptr);
            } else {
                zzlNext(zl,&eptr,&sptr);
            }
        }

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

        /* If reversed, get the last node in range as starting point. */
        // 方向
        if (reverse) {
            ln = zslLastInRange(zsl,&range);
        } else {
            ln = zslFirstInRange(zsl,&range);
        }

        /* No "first" element in the specified interval. */
        // 没有值在指定范围之内
        if (ln == NULL) {
            addReply(c, shared.emptymultibulk);
            return;
        }

        /* We don't know in advance how many matching elements there are in the
         * list, so we push this object that will represent the multi-bulk
         * length in the output buffer, and will "fix" it later */
        replylen = addDeferredMultiBulkLength(c);

        /* If there is an offset, just traverse the number of elements without
         * checking the score because that is done in the next loop. */
        // 跳过 offset 参数指定的元素数量
        while (ln && offset--) {
            if (reverse) {
                ln = ln->backward;
            } else {
                ln = ln->level[0].forward;
            }
        }

        // 遍历并返回所有在范围内的元素
        while (ln && limit--) {
            /* Abort when the node is no longer in range. */
            if (reverse) {
                if (!zslValueGteMin(ln->score,&range)) break;
            } else {
                if (!zslValueLteMax(ln->score,&range)) break;
            }

            rangelen++;
            addReplyBulk(c,ln->obj);

            if (withscores) {
                addReplyDouble(c,ln->score);
            }

            /* Move to next node */
            if (reverse) {
                ln = ln->backward;
            } else {
                ln = ln->level[0].forward;
            }
        }
    } else {
        redisPanic("Unknown sorted set encoding");
    }

    if (withscores) {
        rangelen *= 2;
    }

    setDeferredMultiBulkLength(c, replylen, rangelen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r14
movq $0x0, 0x40(%rsp)
movq $-0x1, 0x68(%rsp)
xorl %ecx, %ecx
testl %esi, %esi
sete %cl
movq 0x10(%rax,%rcx,8), %rsi
xorl $0x3, %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x48(%rsp), %rdx
callq 0x3f8b1
testl %eax, %eax
je 0x40eef
leaq 0x584cc(%rip), %rsi     # 0x993a2
movq %rbx, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d9d7
movq %r14, 0x28(%rsp)
movl %ebp, 0x4(%rsp)
movl 0x30(%rbx), %r13d
movq $0x0, 0x30(%rsp)
cmpl $0x5, %r13d
jl 0x40fd0
addl $-0x4, %r13d
movl $0x4, %r12d
movq $0x0, 0x30(%rsp)
leaq 0x585b8(%rip), %r15     # 0x994e1
movq 0x38(%rbx), %r14
movl %r12d, %r12d
movq (%r14,%r12,8), %rax
movq 0x8(%rax), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1f2e0
testl %eax, %eax
je 0x40fb0
cmpl $0x3, %r13d
jl 0x41049
movq %rbp, %rdi
leaq 0x589e1(%rip), %rsi     # 0x9993c
callq 0x1f2e0
testl %eax, %eax
jne 0x41049
movq 0x8(%r14,%r12,8), %rsi
movq %rbx, %rdi
leaq 0x40(%rsp), %rdx
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x41391
movq 0x38(%rbx), %rax
movq 0x10(%rax,%r12,8), %rsi
movq %rbx, %rdi
leaq 0x68(%rsp), %rdx
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x41391
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
movl $0x3, %ecx
jmp 0x40fc4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
movl $0x1, %edx
movq %rdx, 0x30(%rsp)
addl %ecx, %r12d
addl %eax, %r13d
jne 0x40f29
leaq 0x89829(%rip), %r15     # 0xca800
movq 0x68(%r15), %rdx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x31475
testq %rax, %rax
je 0x41391
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x308e7
testl %eax, %eax
jne 0x41391
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x41064
cmpl $0x5, %eax
movl 0x4(%rsp), %r13d
jne 0x413e0
movq 0x8(%r12), %r14
leaq 0x48(%rsp), %rsi
movq %r14, %rdi
testl %r13d, %r13d
je 0x410dd
callq 0x3e21e
jmp 0x410e2
leaq 0x897b0(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq %rbx, %rdi
callq 0x2d6f0
jmp 0x41391
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
movl 0x4(%rsp), %ebp
leaq 0x48(%rsp), %rsi
testl %ebp, %ebp
je 0x41081
callq 0x3d5ad
jmp 0x41086
callq 0x3d4e8
movq %rax, %r13
testq %rax, %rax
je 0x410ca
xorl %r14d, %r14d
testl %ebp, %ebp
sete %r14b
movq %rbx, %rdi
callq 0x2dc0a
movq %rax, %rsi
movq 0x40(%rsp), %rax
leaq 0x10(,%r14,8), %rcx
movq %r13, %r12
subq $0x1, %rax
jb 0x4129f
movq (%r12,%rcx), %r12
testq %r12, %r12
jne 0x410b2
jmp 0x412a6
movq 0x68(%r15), %rsi
movq %rbx, %rdi
callq 0x2d6f0
xorl %esi, %esi
jmp 0x41373
callq 0x3e149
movq %rax, %rbp
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x41289
movq %r14, %rdi
movq %rbp, 0x20(%rsp)
movq %rbp, %rsi
callq 0x2c2b3
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
callq 0x2dc0a
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x41151
leaq 0x10(%rsp), %r15
leaq 0x38(%rsp), %rbp
subq $0x1, 0x40(%rsp)
jb 0x41151
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
testl %r13d, %r13d
je 0x41144
callq 0x3e004
jmp 0x41149
callq 0x3df7d
cmpq $0x0, 0x10(%rsp)
jne 0x41127
movq 0x10(%rsp), %rbp
testq %rbp, %rbp
je 0x413a3
movl 0x58(%rsp), %r13d
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x70(%rsp)
xorl %r15d, %r15d
subq $0x1, 0x68(%rsp)
jb 0x413a6
movq 0x38(%rsp), %rdi
callq 0x3ddbd
movapd %xmm0, %xmm1
cmpl $0x0, 0x4(%rsp)
je 0x411bd
xorl %eax, %eax
xorl %ecx, %ecx
ucomisd 0x60(%rsp), %xmm1
setbe %al
setb %cl
testl %r13d, %r13d
jmp 0x411d6
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
setbe %al
setb %cl
cmpl $0x0, 0x70(%rsp)
cmovel %ecx, %eax
testb %al, %al
jne 0x413a6
movsd %xmm1, 0x28(%rsp)
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
leaq 0x74(%rsp), %rdx
leaq 0x78(%rsp), %rcx
callq 0x2c351
testl %eax, %eax
je 0x413b7
movq 0x80(%rsp), %rsi
testq %rsi, %rsi
je 0x41224
movl 0x74(%rsp), %edx
movq %rbx, %rdi
callq 0x2dfa7
jmp 0x41231
movq 0x78(%rsp), %rsi
movq %rbx, %rdi
callq 0x2dfe7
cmpl $0x0, 0x30(%rsp)
movsd 0x28(%rsp), %xmm0
je 0x41246
movq %rbx, %rdi
callq 0x2dd0b
cmpl $0x0, 0x4(%rsp)
je 0x41261
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x3e004
jmp 0x41273
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x3df7d
movq 0x10(%rsp), %rbp
incq %r15
testq %rbp, %rbp
jne 0x41187
jmp 0x413a6
movq 0x68(%r15), %rsi
movq %rbx, %rdi
callq 0x2d6f0
xorl %esi, %esi
xorl %r15d, %r15d
jmp 0x413b0
movq $-0x1, %rax
movq %rax, 0x40(%rsp)
testq %r12, %r12
je 0x41373
movq %r13, 0x18(%rsp)
movq %rsi, 0x8(%rsp)
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movl 0x58(%rsp), %ebp
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movl 0x5c(%rsp), %r13d
leaq 0x10(,%rax,8), %r14
xorl %r15d, %r15d
subq $0x1, 0x68(%rsp)
jb 0x41367
movsd 0x8(%r12), %xmm0
cmpl $0x0, 0x4(%rsp)
je 0x4131b
xorl %eax, %eax
xorl %ecx, %ecx
ucomisd 0x28(%rsp), %xmm0
setbe %al
setb %cl
testl %ebp, %ebp
jmp 0x41332
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x60(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %al
setb %cl
testl %r13d, %r13d
cmovel %ecx, %eax
testb %al, %al
jne 0x41367
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x2df74
cmpl $0x0, 0x30(%rsp)
je 0x4135b
movsd 0x8(%r12), %xmm0
movq %rbx, %rdi
callq 0x2dd0b
movq (%r12,%r14), %r12
incq %r15
testq %r12, %r12
jne 0x412f1
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %r13
jmp 0x41376
xorl %r15d, %r15d
testq %r13, %r13
je 0x41391
cmpl $0x0, 0x30(%rsp)
setne %cl
shlq %cl, %r15
movq %rbx, %rdi
movq %r15, %rdx
callq 0x2dc46
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rbp
testq %rbp, %rbp
jne 0x4137b
jmp 0x41391
leaq 0x57e2f(%rip), %rdx     # 0x991ed
leaq 0x57df1(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r12, %rsi
movl $0xc82, %r8d            # imm = 0xC82
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0
leaq 0x55f6c(%rip), %rdi     # 0x97353
leaq 0x57dc8(%rip), %rsi     # 0x991b6
movl $0xcd3, %edx            # imm = 0xCD3
callq 0x4d0e5
jmp 0x413d6