// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r13
callq 0x2038c
movq %rbx, %rsi
movq %rax, %rbx
movq $0xa, 0x20(%rsp)
testq %rsi, %rsi
je 0x31ef1
movl (%rsi), %eax
andl $0xf, %eax
addl $-0x2, %eax
movl $0x3, %r15d
cmpl $0x3, %eax
jb 0x31ef7
leaq 0x6567c(%rip), %rdi     # 0x9755c
leaq 0x66b56(%rip), %rsi     # 0x98a3d
movl $0x2a7, %edx            # imm = 0x2A7
jmp 0x32366
movl $0x2, %r15d
movl 0x30(%r13), %r12d
cmpl %r12d, %r15d
movq %r13, 0x30(%rsp)
movq %rsi, 0x8(%rsp)
jge 0x31ff2
movq %rbp, 0x10(%rsp)
movl %r15d, %ebp
negl %r15d
movq $0x0, 0x18(%rsp)
addl %r15d, %r12d
movq 0x38(%r13), %r13
movq (%r13,%rbp,8), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
leaq 0x6555d(%rip), %rsi     # 0x97499
callq 0x1f2e0
testl %eax, %eax
jne 0x31f76
cmpl $0x2, %r12d
jl 0x31f76
movq 0x8(%r13,%rbp,8), %rsi
movq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x32036
cmpq $0x0, 0x20(%rsp)
jg 0x31fc2
jmp 0x3201e
movq %r14, %rdi
leaq 0x6ff4d(%rip), %rsi     # 0xa1ecd
callq 0x1f2e0
testl %eax, %eax
jne 0x3201e
cmpl $0x2, %r12d
jl 0x3201e
movq 0x8(%r13,%rbp,8), %rax
movq 0x8(%rax), %rax
movl -0x8(%rax), %ecx
movq %rax, 0x58(%rsp)
cmpb $0x2a, (%rax)
setne %al
movl %ecx, 0x2c(%rsp)
cmpl $0x1, %ecx
setne %cl
orb %al, %cl
movzbl %cl, %eax
movq %rax, 0x18(%rsp)
addq $0x2, %rbp
movq 0x30(%rsp), %r13
movl 0x30(%r13), %r12d
addl $-0x2, %r15d
cmpl %ebp, %r12d
jg 0x31f22
cmpl $0x0, 0x18(%rsp)
sete 0x18(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rbp
jmp 0x31ff7
movb $0x1, 0x18(%rsp)
testq %rsi, %rsi
je 0x3200a
movzbl (%rsi), %eax
cmpl $0x22, %eax
jne 0x32013
movq 0x8(%rsi), %r14
jmp 0x3206e
movq 0x8(%r13), %rax
movq (%rax), %r14
jmp 0x3206e
cmpl $0x24, %eax
jne 0x32058
movq 0x8(%rsi), %r14
jmp 0x32064
leaq 0x987db(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq 0x30(%rsp), %rdi
callq 0x2d6f0
leaq -0x1ab1(%rip), %rax     # 0x3058c
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x203b0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x73, %eax
jne 0x3206b
movq 0x8(%rsi), %rax
movq (%rax), %r14
shlq 0x20(%rsp)
jmp 0x3206e
xorl %r14d, %r14d
testq %r14, %r14
je 0x320b4
leaq 0x40(%rsp), %r15
movq %rbx, (%r15)
movq %rsi, 0x8(%r15)
leaq -0x343(%rip), %r12      # 0x31d43
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x22bc4
movq %rax, %rbp
testq %rax, %rax
je 0x320aa
movq 0x28(%rbx), %rax
cmpq 0x20(%rsp), %rax
jb 0x32086
movq %rbp, 0x10(%rsp)
jmp 0x321a7
movl (%rsi), %eax
andl $0xf, %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x3214b
movq 0x8(%rsi), %rdi
xorl %esi, %esi
callq 0x2bff5
testq %rax, %rax
je 0x3213b
movq %rax, %r14
leaq 0x40(%rsp), %r12
leaq 0x3c(%rsp), %r13
leaq 0x60(%rsp), %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x2c351
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3210d
movl 0x3c(%rsp), %esi
callq 0x303b7
jmp 0x32117
movq 0x60(%rsp), %rdi
callq 0x303c6
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2045f
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
callq 0x2c2b3
movq %rax, %r14
testq %rax, %rax
jne 0x320e7
movq $0x0, 0x10(%rsp)
movq 0x30(%rsp), %r13
jmp 0x321a7
cmpl $0x2, %eax
jne 0x32375
movq 0x8(%rsi), %rdi
leaq 0x40(%rsp), %rdx
movq %rsi, %r15
xorl %esi, %esi
callq 0x4f9dc
testb %al, %al
je 0x3219e
movl $0x1, %ebp
leaq 0x40(%rsp), %r14
movq 0x40(%rsp), %rdi
callq 0x303c6
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2045f
movq 0x8(%r15), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x4f9dc
incl %ebp
testb %al, %al
jne 0x32175
movq $0x0, 0x10(%rsp)
movq (%rbx), %rbp
testq %rbp, %rbp
movq 0x8(%rsp), %rcx
je 0x322c4
movq 0x8(%rbp), %r12
movq 0x10(%rbp), %r14
cmpb $0x0, 0x18(%rsp)
je 0x321cb
xorl %eax, %eax
jmp 0x32242
movl (%r14), %eax
shrl $0x4, %eax
testb $0x7, %al
jne 0x321f9
movq 0x8(%r14), %rdx
movl -0x8(%rdx), %ecx
movq 0x58(%rsp), %rdi
movl 0x2c(%rsp), %esi
xorl %r8d, %r8d
callq 0x2f97c
movq 0x8(%rsp), %rcx
testl %eax, %eax
sete %al
jmp 0x32242
movl (%r14), %eax
movl $0xf0, %ecx
andl %ecx, %eax
cmpl $0x10, %eax
jne 0x3233e
movq 0x8(%r14), %rdx
movl $0x15, %esi
leaq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x2fe12
movq 0x58(%rsp), %rdi
movl 0x2c(%rsp), %esi
movq %r15, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x2f97c
testl %eax, %eax
sete %al
movq 0x8(%rsp), %rcx
movzbl %al, %r15d
testq %rcx, %rcx
jne 0x32263
testb %al, %al
jne 0x32263
movq 0x8(%r13), %rdi
movq %r14, %rsi
callq 0x313a3
movq 0x8(%rsp), %rcx
movl %eax, %r15d
testl %r15d, %r15d
je 0x32280
movq %r14, %rdi
callq 0x30834
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x20544
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x322b8
movl (%rcx), %eax
andl $0xf, %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x322b8
movq 0x8(%r12), %rbp
testl %r15d, %r15d
je 0x322bb
movq 0x10(%r12), %rdi
callq 0x30834
movq %rbx, %rdi
movq %r12, %rsi
callq 0x20544
movq 0x8(%rsp), %rcx
jmp 0x322bb
movq %r12, %rbp
testq %rbp, %rbp
jne 0x321b8
movl $0x2, %esi
movq %r13, %rdi
callq 0x2ded8
leaq 0x68fcf(%rip), %rdx     # 0x9b2a7
leaq 0x40(%rsp), %rdi
movl $0x15, %esi
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x1f120
cmpl $0x15, %eax
jae 0x32353
movl %eax, %edx
leaq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0x2dfa7
movq 0x28(%rbx), %rsi
movq %r13, %rdi
callq 0x2ded8
movq (%rbx), %r14
testq %r14, %r14
je 0x32036
movq 0x10(%r14), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x2df74
movq %r15, %rdi
callq 0x30834
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20544
jmp 0x3230e
leaq 0x6528b(%rip), %rdi     # 0x975d0
leaq 0x666f1(%rip), %rsi     # 0x98a3d
movl $0x32e, %edx            # imm = 0x32E
jmp 0x32366
leaq 0x6529b(%rip), %rdi     # 0x975f5
leaq 0x666dc(%rip), %rsi     # 0x98a3d
movl $0x34f, %edx            # imm = 0x34F
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0
leaq 0x65234(%rip), %rdi     # 0x975b0
leaq 0x666ba(%rip), %rsi     # 0x98a3d
movl $0x31b, %edx            # imm = 0x31B
callq 0x4d0e5
jmp 0x3236b