// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
callq 0x1fdb0
movq %rax, -0x48(%rbp)
movq $0xa, -0x60(%rbp)
movl $0x0, -0x70(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x3d060
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x2, %eax
je 0x3d060
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3d060
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
jne 0x3d062
jmp 0x3d084
leaq 0xa4665(%rip), %rdi     # 0xe16ce
leaq 0xa5a95(%rip), %rsi     # 0xe2b05
movl $0x2a7, %edx            # imm = 0x2A7
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
movq -0x10(%rbp), %rdx
movl $0x3, %eax
movl $0x2, %ecx
cmpq $0x0, %rdx
cmovel %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x3d1f8
movq -0x8(%rbp), %rax
movl 0x30(%rax), %eax
subl -0x20(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
leaq 0xa4523(%rip), %rsi     # 0xe15f7
callq 0x1f2f0
cmpl $0x0, %eax
jne 0x3d145
cmpl $0x2, -0x24(%rbp)
jl 0x3d145
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x60(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
je 0x3d114
jmp 0x3d7b9
cmpq $0x1, -0x60(%rbp)
jge 0x3d137
movq -0x8(%rbp), %rdi
leaq 0xd702a(%rip), %rax     # 0x114150
movq 0x80(%rax), %rsi
callq 0x35a70
jmp 0x3d7b9
movl -0x20(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x20(%rbp)
jmp 0x3d1f3
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rdi
leaq 0xaf0d1(%rip), %rsi     # 0xec231
callq 0x1f2f0
cmpl $0x0, %eax
jne 0x3d1d5
cmpl $0x2, -0x24(%rbp)
jl 0x3d1d5
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x3bdc0
movl %eax, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x2a, %ecx
movb %al, -0x101(%rbp)
jne 0x3d1ba
cmpl $0x1, -0x6c(%rbp)
sete %al
movb %al, -0x101(%rbp)
movb -0x101(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x70(%rbp)
movl -0x20(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x20(%rbp)
jmp 0x3d1f1
movq -0x8(%rbp), %rdi
leaq 0xd6f70(%rip), %rax     # 0x114150
movq 0x80(%rax), %rsi
callq 0x35a70
jmp 0x3d7b9
jmp 0x3d1f3
jmp 0x3d09c
movq $0x0, -0x78(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x3d21b
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x3d2c7
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x3d248
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x3d248
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x3d2c5
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x4, %eax
jne 0x3d280
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x3d280
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rax
shlq %rax
movq %rax, -0x60(%rbp)
jmp 0x3d2c3
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
jne 0x3d2c1
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
jne 0x3d2c1
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rax
shlq %rax
movq %rax, -0x60(%rbp)
jmp 0x3d2c3
jmp 0x3d2c5
jmp 0x3d2c7
cmpq $0x0, -0x78(%rbp)
je 0x3d336
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rcx
leaq -0x50a(%rip), %rdx      # 0x3cdf0
callq 0x24780
movq %rax, -0x18(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x18(%rbp)
movb %al, -0x102(%rbp)
je 0x3d327
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rax
cmpq -0x60(%rbp), %rax
setb %al
movb %al, -0x102(%rbp)
movb -0x102(%rbp), %al
testb $0x1, %al
jne 0x3d2e4
jmp 0x3d4b9
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x3d3ac
movl $0x0, -0x94(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x94(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
leaq -0xa0(%rbp), %rdx
callq 0x67a60
cmpb $0x0, %al
je 0x3d39f
movq -0x48(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x3a330
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x1ff90
jmp 0x3d34e
movq $0x0, -0x18(%rbp)
jmp 0x3d4b7
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3d3cc
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
jne 0x3d493
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
callq 0x32e50
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x3d489
movq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xb4(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
callq 0x33490
movq -0x48(%rbp), %rax
movq %rax, -0x118(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x3d443
movq -0xb0(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl %eax, %esi
callq 0x3a2e0
movq %rax, -0x120(%rbp)
jmp 0x3d456
movq -0xc0(%rbp), %rdi
callq 0x3a330
movq %rax, -0x120(%rbp)
movq -0x118(%rbp), %rdi
movq -0x120(%rbp), %rsi
callq 0x1ff90
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xa8(%rbp), %rsi
callq 0x33330
movq %rax, -0xa8(%rbp)
jmp 0x3d3e2
movq $0x0, -0x18(%rbp)
jmp 0x3d4b5
leaq 0xa4288(%rip), %rdi     # 0xe1722
leaq 0xa5664(%rip), %rsi     # 0xe2b05
movl $0x31b, %edx            # imm = 0x31B
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x3d4b7
jmp 0x3d4b9
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x3d6f0
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0xcc(%rbp)
cmpl $0x0, -0xcc(%rbp)
jne 0x3d624
cmpl $0x0, -0x70(%rbp)
je 0x3d624
movq -0xc8(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x3d533
movq -0xc8(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x3d59b
movq -0x68(%rbp), %rax
movq %rax, -0x138(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x12c(%rbp)
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x128(%rbp)
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3bdc0
movq -0x138(%rbp), %rdi
movl -0x12c(%rbp), %esi
movq -0x128(%rbp), %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x38f60
cmpl $0x0, %eax
jne 0x3d596
movl $0x1, -0xcc(%rbp)
jmp 0x3d622
movq -0xc8(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x1, %eax
jne 0x3d5b1
jmp 0x3d5d3
leaq 0xa418a(%rip), %rdi     # 0xe1742
leaq 0xa5546(%rip), %rsi     # 0xe2b05
movl $0x32e, %edx            # imm = 0x32E
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
leaq -0xf0(%rbp), %rdi
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rdx
movl $0x15, %esi
callq 0x39660
movl %eax, -0xf4(%rbp)
movq -0x68(%rbp), %rdi
movl -0x6c(%rbp), %esi
leaq -0xf0(%rbp), %rdx
movl -0xf4(%rbp), %ecx
xorl %r8d, %r8d
callq 0x38f60
cmpl $0x0, %eax
jne 0x3d620
movl $0x1, -0xcc(%rbp)
jmp 0x3d622
jmp 0x3d624
cmpl $0x0, -0xcc(%rbp)
jne 0x3d657
cmpq $0x0, -0x10(%rbp)
jne 0x3d657
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xc8(%rbp), %rsi
callq 0x3bf60
cmpl $0x0, %eax
je 0x3d657
movl $0x1, -0xcc(%rbp)
cmpl $0x0, -0xcc(%rbp)
je 0x3d679
movq -0xc8(%rbp), %rdi
callq 0x3aaa0
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x20170
cmpq $0x0, -0x10(%rbp)
je 0x3d6e3
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
je 0x3d69c
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x4, %eax
jne 0x3d6e3
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0xcc(%rbp)
je 0x3d6e1
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x3aaa0
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x20170
jmp 0x3d6e3
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x3d4c4
movq -0x8(%rbp), %rdi
movl $0x2, %esi
callq 0x364f0
leaq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x15, %esi
leaq 0xa7c2d(%rip), %rdx     # 0xe533f
movb $0x0, %al
callq 0x1f120
movl %eax, -0x1c(%rbp)
movslq -0x1c(%rbp), %rax
cmpq $0x15, %rax
jae 0x3d728
jmp 0x3d74a
leaq 0xa4038(%rip), %rdi     # 0xe1767
leaq 0xa53cf(%rip), %rsi     # 0xe2b05
movl $0x34f, %edx            # imm = 0x34F
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
movq -0x8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x36670
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x364f0
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x3d7b7
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x8(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0x36620
movq -0x100(%rbp), %rdi
callq 0x3aaa0
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x20170
jmp 0x3d76c
jmp 0x3d7b9
movq -0x48(%rbp), %rax
leaq -0x3154(%rip), %rcx     # 0x3a670
movq %rcx, 0x18(%rax)
movq -0x48(%rbp), %rdi
callq 0x1fe30
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw (%rax,%rax)