// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/db.c
// Function: scanGenericCommand
void scanGenericCommand(redisClient *c, robj *o, unsigned long cursor) {
    int rv;
    int i, j;
    char buf[REDIS_LONGSTR_SIZE];
    list *keys = listCreate();
    listNode *node, *nextnode;
    long count = 10;
    sds pat;
    int patlen, use_pattern = 0;
    dict *ht;

    /* Object must be NULL (to iterate keys names), or the type of the object
     * must be Set, Sorted Set, or Hash. */
    // 输入类型检查
    redisAssert(o == NULL || o->type == REDIS_SET || o->type == REDIS_HASH ||
                o->type == REDIS_ZSET);

    /* Set i to the first option argument. The previous one is the cursor. */
    // 设置第一个选项参数的索引位置
    // 0    1      2      3  
    // SCAN OPTION <op_arg>         SCAN 命令的选项值从索引 2 开始
    // HSCAN <key> OPTION <op_arg>  而其他 *SCAN 命令的选项值从索引 3 开始
    i = (o == NULL) ? 2 : 3; /* Skip the key argument if needed. */

    /* Step 1: Parse options. */
    // 分析选项参数
    while (i < c->argc) {
        j = c->argc - i;

        // COUNT <number>
        if (!strcasecmp(c->argv[i]->ptr, "count") && j >= 2) {
            if (getLongFromObjectOrReply(c, c->argv[i+1], &count, NULL)
                != REDIS_OK)
            {
                goto cleanup;
            }

            if (count < 1) {
                addReply(c,shared.syntaxerr);
                goto cleanup;
            }

            i += 2;

        // MATCH <pattern>
        } else if (!strcasecmp(c->argv[i]->ptr, "match") && j >= 2) {
            pat = c->argv[i+1]->ptr;
            patlen = sdslen(pat);

            /* The pattern always matches if it is exactly "*", so it is
             * equivalent to disabling it. */
            use_pattern = !(pat[0] == '*' && patlen == 1);

            i += 2;

        // error
        } else {
            addReply(c,shared.syntaxerr);
            goto cleanup;
        }
    }

    /* Step 2: Iterate the collection.
     *
     * Note that if the object is encoded with a ziplist, intset, or any other
     * representation that is not a hash table, we are sure that it is also
     * composed of a small number of elements. So to avoid taking state we
     * just return everything inside the object in a single call, setting the
     * cursor to zero to signal the end of the iteration. */
     // 如果对象的底层实现为 ziplist 、intset 而不是哈希表，
     // 那么这些对象应该只包含了少量元素，
     // 为了保持不让服务器记录迭代状态的设计
     // 我们将 ziplist 或者 intset 里面的所有元素都一次返回给调用者
     // 并向调用者返回游标（cursor） 0

    /* Handle the case of a hash table. */
    ht = NULL;
    if (o == NULL) {
        // 迭代目标为数据库
        ht = c->db->dict;
    } else if (o->type == REDIS_SET && o->encoding == REDIS_ENCODING_HT) {
        // 迭代目标为 HT 编码的集合
        ht = o->ptr;
    } else if (o->type == REDIS_HASH && o->encoding == REDIS_ENCODING_HT) {
        // 迭代目标为 HT 编码的哈希
        ht = o->ptr;
        count *= 2; /* We return key / value for this type. */
    } else if (o->type == REDIS_ZSET && o->encoding == REDIS_ENCODING_SKIPLIST) {
        // 迭代目标为 HT 编码的跳跃表
        zset *zs = o->ptr;
        ht = zs->dict;
        count *= 2; /* We return key / value for this type. */
    }

    if (ht) {
        void *privdata[2];

        /* We pass two pointers to the callback: the list to which it will
         * add new elements, and the object containing the dictionary so that
         * it is possible to fetch more data in a type-dependent way. */
        // 我们向回调函数传入两个指针：
        // 一个是用于记录被迭代元素的列表
        // 另一个是字典对象
        // 从而实现类型无关的数据提取操作
        privdata[0] = keys;
        privdata[1] = o;
        do {
            cursor = dictScan(ht, cursor, scanCallback, privdata);
        } while (cursor && listLength(keys) < count);
    } else if (o->type == REDIS_SET) {
        int pos = 0;
        int64_t ll;

        while(intsetGet(o->ptr,pos++,&ll))
            listAddNodeTail(keys,createStringObjectFromLongLong(ll));
        cursor = 0;
    } else if (o->type == REDIS_HASH || o->type == REDIS_ZSET) {
        unsigned char *p = ziplistIndex(o->ptr,0);
        unsigned char *vstr;
        unsigned int vlen;
        long long vll;

        while(p) {
            ziplistGet(p,&vstr,&vlen,&vll);
            listAddNodeTail(keys,
                (vstr != NULL) ? createStringObject((char*)vstr,vlen) :
                                 createStringObjectFromLongLong(vll));
            p = ziplistNext(o->ptr,p);
        }
        cursor = 0;
    } else {
        redisPanic("Not handled encoding in SCAN.");
    }

    /* Step 3: Filter elements. */
    node = listFirst(keys);
    while (node) {
        robj *kobj = listNodeValue(node);
        nextnode = listNextNode(node);
        int filter = 0;

        /* Filter element if it does not match the pattern. */
        if (!filter && use_pattern) {
            if (sdsEncodedObject(kobj)) {
                if (!stringmatchlen(pat, patlen, kobj->ptr, sdslen(kobj->ptr), 0))
                    filter = 1;
            } else {
                char buf[REDIS_LONGSTR_SIZE];
                int len;

                redisAssert(kobj->encoding == REDIS_ENCODING_INT);
                len = ll2string(buf,sizeof(buf),(long)kobj->ptr);
                if (!stringmatchlen(pat, patlen, buf, len, 0)) filter = 1;
            }
        }

        /* Filter element if it is an expired key. */
        if (!filter && o == NULL && expireIfNeeded(c->db, kobj)) filter = 1;

        /* Remove the element and its associted value if needed. */
        if (filter) {
            decrRefCount(kobj);
            listDelNode(keys, node);
        }

        /* If this is a hash or a sorted set, we have a flat list of
         * key-value elements, so if this element was filtered, remove the
         * value, or skip it if it was not filtered: we only match keys. */
        if (o && (o->type == REDIS_ZSET || o->type == REDIS_HASH)) {
            node = nextnode;
            nextnode = listNextNode(node);
            if (filter) {
                kobj = listNodeValue(node);
                decrRefCount(kobj);
                listDelNode(keys, node);
            }
        }
        node = nextnode;
    }

    /* Step 4: Reply to the client. */
    addReplyMultiBulkLen(c, 2);
    rv = snprintf(buf, sizeof(buf), "%lu", cursor);
    redisAssert(rv < sizeof(buf));
    addReplyBulkCBuffer(c, buf, rv);

    addReplyMultiBulkLen(c, listLength(keys));
    while ((node = listFirst(keys)) != NULL) {
        robj *kobj = listNodeValue(node);
        addReplyBulk(c, kobj);
        decrRefCount(kobj);
        listDelNode(keys, node);
    }

cleanup:
    listSetFreeMethod(keys,decrRefCountVoid);
    listRelease(keys);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x28(%rsp)
movq %rsi, %r15
movq %rdi, %r14
callq 0x203fc
movq %rax, %rbx
movq $0xa, 0x18(%rsp)
testq %r15, %r15
movq %r15, 0x68(%rsp)
je 0x321dd
movl (%r15), %eax
andl $0xf, %eax
addl $-0x2, %eax
movl $0x3, %r12d
cmpl $0x3, %eax
jb 0x321e3
leaq 0x65402(%rip), %rdi     # 0x975ce
leaq 0x668c6(%rip), %rsi     # 0x98a99
movl $0x2a7, %edx            # imm = 0x2A7
jmp 0x32662
movl $0x2, %r12d
movl 0x30(%r14), %r15d
cmpl %r15d, %r12d
movq %r14, 0x8(%rsp)
jge 0x322fb
movq %rbx, 0x30(%rsp)
movl %r12d, %r13d
negl %r12d
movq $0x0, 0x58(%rsp)
movl $0xa, %eax
movq %rax, 0x10(%rsp)
leal (%r15,%r12), %ebp
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rbx
movq (%rbx,%r13,8), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
leaq 0x652c7(%rip), %rsi     # 0x974f9
callq 0x1f2e0
testl %eax, %eax
jne 0x32281
cmpl $0x2, %ebp
jl 0x32281
movq 0x8(%rbx,%r13,8), %rsi
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
movq 0x30(%rsp), %rbx
jne 0x32358
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jle 0x3233b
movq 0x8(%rsp), %rax
movl 0x30(%rax), %r15d
jmp 0x322cc
movq %r14, %rdi
leaq 0x6fcd2(%rip), %rsi     # 0xa1f5d
callq 0x1f2e0
testl %eax, %eax
jne 0x3233b
cmpl $0x2, %ebp
jl 0x3233b
movq 0x8(%rbx,%r13,8), %rax
movq 0x8(%rax), %rax
movl -0x8(%rax), %ecx
movq %rax, 0x60(%rsp)
cmpb $0x2a, (%rax)
setne %al
movl %ecx, 0x24(%rsp)
cmpl $0x1, %ecx
setne %cl
orb %al, %cl
movzbl %cl, %eax
movq %rax, 0x58(%rsp)
addq $0x2, %r13
addl $-0x2, %r12d
cmpl %r13d, %r15d
jg 0x32213
movq 0x10(%rsp), %rdx
addq %rdx, %rdx
cmpl $0x0, 0x58(%rsp)
sete 0x10(%rsp)
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r14
jmp 0x32305
movb $0x1, 0x10(%rsp)
movl $0x14, %edx
movq 0x28(%rsp), %rsi
movq 0x68(%rsp), %r15
testq %r15, %r15
je 0x32332
movl (%r15), %eax
cmpb $0x73, %al
je 0x32380
movzbl %al, %ecx
cmpl $0x24, %ecx
je 0x3237a
cmpl $0x22, %ecx
jne 0x323d3
movq 0x8(%r15), %r14
jmp 0x3238c
movq 0x8(%r14), %rax
movq (%rax), %r14
jmp 0x3238c
leaq 0x984be(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x2d97c
movq 0x30(%rsp), %rbx
leaq -0x1afb(%rip), %rax     # 0x30864
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x2041c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %r14
jmp 0x32387
movq 0x8(%r15), %rax
movq (%rax), %r14
movq %rdx, 0x18(%rsp)
testq %r14, %r14
je 0x323d0
leaq 0x40(%rsp), %r12
movq %rbx, (%r12)
movq %r15, 0x8(%r12)
leaq -0x379(%rip), %rbp      # 0x3202d
movq %r14, %rdi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x22d87
movq %rax, %rsi
testq %rax, %rax
je 0x324a7
movq 0x28(%rbx), %rax
cmpq 0x18(%rsp), %rax
jb 0x323a6
jmp 0x324a7
movl (%r15), %eax
andl $0xf, %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x32455
movq 0x8(%r15), %rdi
xorl %esi, %esi
callq 0x2c28b
testq %rax, %rax
je 0x324a5
movq %rax, %r14
leaq 0x40(%rsp), %r12
leaq 0x3c(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x2c5f6
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3242a
movl 0x3c(%rsp), %esi
callq 0x3068f
jmp 0x32434
movq 0x70(%rsp), %rdi
callq 0x3069e
movq %rbx, %rdi
movq %rax, %rsi
callq 0x204cb
movq 0x8(%r15), %rdi
movq %r14, %rsi
callq 0x2c558
movq %rax, %r14
testq %rax, %rax
jne 0x32404
jmp 0x324a5
cmpl $0x2, %eax
jne 0x32671
movq 0x8(%r15), %rdi
leaq 0x40(%rsp), %rdx
xorl %esi, %esi
callq 0x4f48d
testb %al, %al
je 0x324a5
movl $0x1, %ebp
leaq 0x40(%rsp), %r14
movq 0x40(%rsp), %rdi
callq 0x3069e
movq %rbx, %rdi
movq %rax, %rsi
callq 0x204cb
movq 0x8(%r15), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x4f48d
incl %ebp
testb %al, %al
jne 0x3247c
xorl %esi, %esi
movq %rsi, 0x28(%rsp)
movq (%rbx), %rbp
testq %rbp, %rbp
je 0x325bb
movq 0x8(%rbp), %r12
movq 0x10(%rbp), %r14
cmpb $0x0, 0x10(%rsp)
je 0x324e5
testq %r15, %r15
je 0x32547
movl (%r15), %eax
andl $0xf, %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x325b3
movq 0x8(%r12), %rbp
jmp 0x324af
movl (%r14), %eax
movl %eax, %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x32504
movq 0x8(%r14), %rdx
movl -0x8(%rdx), %ecx
movq 0x60(%rsp), %rdi
movl 0x24(%rsp), %esi
jmp 0x32536
andl $0xf0, %eax
cmpl $0x10, %eax
jne 0x3263a
movq 0x8(%r14), %rdx
movl $0x15, %esi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
callq 0x300c7
movq 0x60(%rsp), %rdi
movl 0x24(%rsp), %esi
movq %r13, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x2fc6c
testq %r15, %r15
jne 0x3255e
testl %eax, %eax
je 0x3255e
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
callq 0x31673
testl %eax, %eax
jne 0x32564
jmp 0x325b3
movb $0x1, %cl
testl %eax, %eax
jne 0x32579
movq %r14, %rdi
callq 0x30b0c
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x205b1
xorl %ecx, %ecx
testq %r15, %r15
je 0x325b3
movl (%r15), %eax
andl $0xf, %eax
addl $-0x3, %eax
cmpl $0x2, %eax
jae 0x325b3
movq 0x8(%r12), %rbp
testb %cl, %cl
jne 0x324af
movq 0x10(%r12), %rdi
callq 0x30b0c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x205b1
jmp 0x324af
movq %r12, %rbp
jmp 0x324af
movl $0x2, %esi
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x2e168
leaq 0x68d13(%rip), %rdx     # 0x9b2e7
leaq 0x40(%rsp), %rdi
movl $0x15, %esi
movq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0x1f120
cmpl $0x15, %eax
jae 0x3264f
movl %eax, %edx
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x2e237
movq 0x28(%rbx), %rsi
movq %r12, %rdi
callq 0x2e168
movq (%rbx), %r14
testq %r14, %r14
je 0x32358
movq 0x10(%r14), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x2e204
movq %r15, %rdi
callq 0x30b0c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x205b1
jmp 0x3260a
leaq 0x65001(%rip), %rdi     # 0x97642
leaq 0x66451(%rip), %rsi     # 0x98a99
movl $0x32e, %edx            # imm = 0x32E
jmp 0x32662
leaq 0x65011(%rip), %rdi     # 0x97667
leaq 0x6643c(%rip), %rsi     # 0x98a99
movl $0x34f, %edx            # imm = 0x34F
callq 0x4ca32
movl $0x1, %edi
callq 0x1f3b0
leaq 0x64faa(%rip), %rdi     # 0x97622
leaq 0x6641a(%rip), %rsi     # 0x98a99
movl $0x31b, %edx            # imm = 0x31B
callq 0x4cae0
jmp 0x32667