// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1fd8c
movq %rax, 0x28(%rsp)
movq $0xa, 0x18(%rsp)
testq %rbx, %rbx
movq %rbx, 0x10(%rsp)
movq %r15, 0x8(%rsp)
je 0x30365
movl (%rbx), %eax
andl $0xf, %eax
addl $-0x2, %eax
cmpl $0x3, %eax
jae 0x3078a
pushq $0x3
jmp 0x30367
pushq $0x2
popq %r13
movl %r13d, %r15d
negl %r15d
movq $0x0, 0x68(%rsp)
pushq $0xa
popq %rax
movq %rax, 0x30(%rsp)
movslq 0x30(%r14), %rbx
cmpq %rbx, %r13
jge 0x3043f
addl %r15d, %ebx
movq %r14, %rbp
movq 0x38(%r14), %r14
movq (%r14,%r13,8), %rax
movq 0x8(%rax), %r12
movq %r12, %rdi
leaq 0x5d144(%rip), %rsi     # 0x8d4ed
callq 0x1f2e0
testl %eax, %eax
jne 0x303ec
cmpl $0x2, %ebx
jl 0x303ec
movq 0x8(%r14,%r13,8), %rsi
movq %rbp, %rdi
leaq 0x18(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
movq 0x28(%rsp), %rbx
jne 0x30487
movq 0x18(%rsp), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
jg 0x3042f
jmp 0x3046c
movq %r12, %rdi
leaq 0x67b0f(%rip), %rsi     # 0x97f05
callq 0x1f2e0
testl %eax, %eax
jne 0x3046c
cmpl $0x2, %ebx
jl 0x3046c
movq 0x8(%r14,%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, 0x68(%rsp)
addq $0x2, %r13
addl $-0x2, %r15d
movq %rbp, %r14
jmp 0x30380
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x304a9
movl (%r15), %eax
cmpb $0x73, %al
movq 0x28(%rsp), %rbx
je 0x304bd
movzbl %al, %ecx
cmpl $0x24, %ecx
je 0x304b7
cmpl $0x22, %ecx
jne 0x3051d
movq 0x8(%r15), %r12
jmp 0x304d1
leaq 0x9738d(%rip), %rax     # 0xc7800
movq 0x80(%rax), %rsi
movq %rbp, %rdi
callq 0x2c10d
movq 0x28(%rsp), %rbx
leaq -0x188c(%rip), %rax     # 0x2ec02
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x1fdaa
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rax
movq (%rax), %r12
movq 0x28(%rsp), %rbx
jmp 0x304d1
movq 0x8(%r15), %r12
jmp 0x304c4
movq 0x8(%r15), %rax
movq (%rax), %r12
movq 0x30(%rsp), %rax
addq %rax, %rax
movq %rax, 0x18(%rsp)
testq %r12, %r12
je 0x3051a
leaq 0x40(%rsp), %r13
movq %rbx, (%r13)
movq %r15, 0x8(%r13)
leaq -0x31d(%rip), %rbp      # 0x301cd
movq 0x8(%rsp), %rsi
movq %r12, %rdi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x221be
movq %rax, %rsi
testq %rax, %rax
je 0x30510
movq 0x28(%rbx), %rax
cmpq 0x18(%rsp), %rax
jb 0x304ef
movq %rsi, 0x8(%rsp)
jmp 0x305db
movl (%r15), %eax
andl $0xf, %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x30597
movq 0x8(%r15), %rdi
xorl %esi, %esi
callq 0x2abfb
movq %rax, %rbp
leaq 0x3c(%rsp), %r13
leaq 0x70(%rsp), %r12
testq %rbp, %rbp
je 0x305d2
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x2aea9
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x30571
movl 0x3c(%rsp), %esi
callq 0x2ea81
jmp 0x3057b
movq 0x70(%rsp), %rdi
callq 0x2ea90
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1fe4c
movq 0x8(%r15), %rdi
movq %rbp, %rsi
callq 0x2ae0d
movq %rax, %rbp
jmp 0x30540
cmpl $0x2, %eax
jne 0x307b4
xorl %ebp, %ebp
leaq 0x40(%rsp), %r12
movq 0x8(%r15), %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x4b608
testb %al, %al
je 0x305d2
incl %ebp
movq 0x40(%rsp), %rdi
callq 0x2ea90
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1fe4c
jmp 0x305a7
movq $0x0, 0x8(%rsp)
movq (%rbx), %r15
testq %r15, %r15
je 0x306f0
movq %r15, %r13
movq 0x8(%r15), %rbp
movq 0x10(%r15), %r12
cmpl $0x0, 0x68(%rsp)
je 0x3061f
movl (%r12), %eax
movl %eax, %ecx
shrl $0x4, %ecx
testb $0x7, %cl
movq 0x10(%rsp), %r15
jne 0x3062f
movq 0x8(%r12), %rdx
movl -0x8(%rdx), %ecx
movq 0x60(%rsp), %rdi
movl 0x24(%rsp), %esi
jmp 0x3065f
movb $0x1, %cl
cmpq $0x0, 0x10(%rsp)
jne 0x306b2
jmp 0x30670
andl $0xf0, %eax
cmpl $0x10, %eax
jne 0x3076a
movq 0x8(%r12), %rdx
leaq 0x40(%rsp), %rdi
pushq $0x15
popq %rsi
callq 0x2e511
movq 0x60(%rsp), %rdi
movl 0x24(%rsp), %esi
leaq 0x40(%rsp), %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x2e0ec
testq %r15, %r15
jne 0x30688
testl %eax, %eax
je 0x30688
movq 0x8(%r14), %rdi
movq %r12, %rsi
callq 0x2f8d7
movq %rbp, %r15
testl %eax, %eax
jne 0x3068e
jmp 0x305de
movb $0x1, %cl
testl %eax, %eax
jne 0x306a3
movq %r12, %rdi
callq 0x2ee1f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1ff28
xorl %ecx, %ecx
movq %rbp, %r15
cmpq $0x0, 0x10(%rsp)
je 0x305de
movq 0x10(%rsp), %rax
movl (%rax), %eax
andl $0xf, %eax
addl $-0x3, %eax
movq %rbp, %r15
cmpl $0x1, %eax
ja 0x305de
movq 0x8(%rbp), %r15
testb %cl, %cl
jne 0x305de
movq 0x10(%rbp), %rdi
callq 0x2ee1f
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1ff28
jmp 0x305de
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x2c7c7
leaq 0x60ba5(%rip), %rdx     # 0x912a7
leaq 0x40(%rsp), %rdi
pushq $0x15
popq %rsi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x1f120
cmpl $0x15, %eax
jae 0x3079f
movl %eax, %edx
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x2c87d
movq 0x28(%rbx), %rsi
movq %r14, %rdi
callq 0x2c7c7
movq (%rbx), %r15
testq %r15, %r15
je 0x30487
movq 0x10(%r15), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x2c84a
movq %r12, %rdi
callq 0x2ee1f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1ff28
jmp 0x3073a
leaq 0x5ceb3(%rip), %rdi     # 0x8d624
leaq 0x5e2e5(%rip), %rsi     # 0x8ea5d
movl $0x32e, %edx            # imm = 0x32E
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0
leaq 0x5ce1f(%rip), %rdi     # 0x8d5b0
leaq 0x5e2c5(%rip), %rsi     # 0x8ea5d
movl $0x2a7, %edx            # imm = 0x2A7
jmp 0x3077d
leaq 0x5cea3(%rip), %rdi     # 0x8d649
leaq 0x5e2b0(%rip), %rsi     # 0x8ea5d
movl $0x34f, %edx            # imm = 0x34F
jmp 0x3077d
leaq 0x5ce49(%rip), %rdi     # 0x8d604
leaq 0x5e29b(%rip), %rsi     # 0x8ea5d
movl $0x31b, %edx            # imm = 0x31B
callq 0x490e8
jmp 0x30782