// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/db.c
// Function: scanGenericCommand
void scanGenericCommand(client *c, robj *o, unsigned long cursor) {
    int i, j;
    list *keys = listCreate();
    listNode *node, *nextnode;
    long count = 10;
    sds pat = NULL;
    int patlen = 0, 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. */
    serverAssert(o == NULL || o->type == OBJ_SET || o->type == OBJ_HASH ||
                o->type == OBJ_ZSET);

    /* Set i to the first option argument. The previous one is the cursor. */
    i = (o == NULL) ? 2 : 3; /* Skip the key argument if needed. */

    /* Step 1: Parse options. */
    while (i < c->argc) {
        j = c->argc - i;
        if (!strcasecmp(c->argv[i]->ptr, "count") && j >= 2) {
            if (getLongFromObjectOrReply(c, c->argv[i+1], &count, NULL)
                != C_OK)
            {
                goto cleanup;
            }

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

            i += 2;
        } 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;
        } 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. */

    /* Handle the case of a hash table. */
    ht = NULL;
    if (o == NULL) {
        ht = c->db->dict;
    } else if (o->type == OBJ_SET && o->encoding == OBJ_ENCODING_HT) {
        ht = o->ptr;
    } else if (o->type == OBJ_HASH && o->encoding == OBJ_ENCODING_HT) {
        ht = o->ptr;
        count *= 2; /* We return key / value for this type. */
    } else if (o->type == OBJ_ZSET && o->encoding == OBJ_ENCODING_SKIPLIST) {
        zset *zs = o->ptr;
        ht = zs->dict;
        count *= 2; /* We return key / value for this type. */
    }

    if (ht) {
        void *privdata[2];
        /* We set the max number of iterations to ten times the specified
         * COUNT, so if the hash table is in a pathological state (very
         * sparsely populated) we avoid to block too much time at the cost
         * of returning no or very few elements. */
        long maxiterations = count*10;

        /* 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 &&
              maxiterations-- &&
              listLength(keys) < (unsigned long)count);
    } else if (o->type == OBJ_SET) {
        int pos = 0;
        int64_t ll;

        while(intsetGet(o->ptr,pos++,&ll))
            listAddNodeTail(keys,createStringObjectFromLongLong(ll));
        cursor = 0;
    } else if (o->type == OBJ_HASH || o->type == OBJ_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 {
        serverPanic("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[LONG_STR_SIZE];
                int len;

                serverAssert(kobj->encoding == OBJ_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 == OBJ_ZSET || o->type == OBJ_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);
    addReplyBulkLongLong(c,cursor);

    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, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
callq 0x2e33c
movq %rbx, %rcx
movq %rax, %rbx
movq $0xa, 0x8(%rsp)
testq %rcx, %rcx
je 0x45c56
movl (%rcx), %eax
andl $0xf, %eax
addl $-0x2, %eax
movl $0x3, %ebp
cmpl $0x3, %eax
jb 0x45c5b
leaq 0x7e304(%rip), %rdi     # 0xc3f49
leaq 0x7fd43(%rip), %rsi     # 0xc598f
movl $0x1f7, %edx            # imm = 0x1F7
jmp 0x46134
movl $0x2, %ebp
movl 0x38(%r15), %r12d
cmpl %r12d, %ebp
movq %rcx, (%rsp)
jge 0x45da0
movq %rbx, 0x30(%rsp)
movl %ebp, %r13d
negl %ebp
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
addl %ebp, %r12d
movq %r15, %rbx
movq 0x40(%r15), %r15
movq (%r15,%r13,8), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
leaq 0x89f80(%rip), %rsi     # 0xcfc2d
callq 0x26300
testl %eax, %eax
jne 0x45cee
cmpl $0x2, %r12d
jl 0x45cee
movq 0x8(%r15,%r13,8), %rsi
movq %rbx, %rdi
leaq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x44ba1
testl %eax, %eax
movq 0x30(%rsp), %r12
jne 0x460fe
cmpq $0x0, 0x8(%rsp)
jg 0x45d74
jmp 0x45dde
movq %r14, %rdi
leaq 0x8e67d(%rip), %rsi     # 0xd4375
callq 0x26300
testl %eax, %eax
jne 0x45dde
cmpl $0x2, %r12d
jl 0x45dde
movq 0x8(%r15,%r13,8), %rax
movq 0x8(%rax), %rcx
movzbl -0x1(%rcx), %edx
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x45d52
leaq 0x7e14f(%rip), %rsi     # 0xc3e7c
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
shrl $0x3, %edx
jmp 0x45d54
movq -0x11(%rcx), %rdx
jmp 0x45d54
movzwl -0x5(%rcx), %edx
jmp 0x45d54
movl -0x9(%rcx), %edx
jmp 0x45d54
movzbl -0x3(%rcx), %edx
jmp 0x45d54
xorl %edx, %edx
movq %rcx, 0x28(%rsp)
cmpb $0x2a, (%rcx)
setne %al
movq %rdx, 0x20(%rsp)
cmpl $0x1, %edx
setne %cl
orb %al, %cl
movzbl %cl, %eax
movq %rax, 0x18(%rsp)
addq $0x2, %r13
movq %rbx, %r15
movl 0x38(%rbx), %r12d
addl $-0x2, %ebp
cmpl %r13d, %r12d
jg 0x45c91
cmpl $0x0, 0x18(%rsp)
sete 0x18(%rsp)
movq 0x30(%rsp), %rbx
movq (%rsp), %rcx
jmp 0x45db7
movb $0x1, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
testq %rcx, %rcx
je 0x45dca
movzbl (%rcx), %eax
cmpl $0x22, %eax
jne 0x45dd3
movq 0x8(%rcx), %r14
jmp 0x45e14
movq 0x10(%r15), %rax
movq (%rax), %r14
jmp 0x45e14
cmpl $0x24, %eax
jne 0x45dfe
movq 0x8(%rcx), %r14
jmp 0x45e0a
leaq 0xc14cb(%rip), %rax     # 0x1072b0
movq 0x80(%rax), %rsi
movq %rbx, %rdi
callq 0x3fc19
movq 0x30(%rsp), %r12
jmp 0x460fe
cmpl $0x73, %eax
jne 0x45e11
movq 0x8(%rcx), %rax
movq (%rax), %r14
shlq 0x8(%rsp)
jmp 0x45e14
xorl %r14d, %r14d
testq %r14, %r14
je 0x45e75
movq 0x8(%rsp), %rax
leaq 0x40(%rsp), %r12
movq %rbx, (%r12)
movq %rcx, 0x8(%r12)
addq %rax, %rax
leaq (%rax,%rax,4), %rbp
negq %rbp
leaq -0x3d3(%rip), %r13      # 0x45a6a
movq 0x10(%rsp), %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %r12, %rcx
callq 0x3248d
movq %rax, %rsi
testq %rax, %rax
je 0x45e6b
testq %rbp, %rbp
je 0x45e6b
movq 0x28(%rbx), %rax
incq %rbp
cmpq 0x8(%rsp), %rax
jb 0x45e42
movq %rsi, 0x10(%rsp)
jmp 0x45f5f
movl (%rcx), %eax
andl $0xf, %eax
movq %rcx, %rdx
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x45f00
movq 0x8(%rdx), %rdi
xorl %esi, %esi
callq 0x3ea40
testq %rax, %rax
je 0x45f56
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 0x3eb60
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x45ed1
movl 0x3c(%rsp), %esi
callq 0x43ba8
jmp 0x45edb
movq 0x60(%rsp), %rdi
callq 0x43bb7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2e40f
movq (%rsp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
callq 0x3ead6
movq %rax, %r14
testq %rax, %rax
jne 0x45eab
jmp 0x45f56
cmpl $0x2, %eax
jne 0x46143
movq (%rsp), %r12
movq 0x8(%r12), %rdi
leaq 0x40(%rsp), %rdx
xorl %esi, %esi
callq 0x68a3c
testb %al, %al
je 0x45f56
movl $0x1, %ebp
leaq 0x40(%rsp), %r14
movq 0x40(%rsp), %rdi
callq 0x43bb7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2e40f
movq 0x8(%r12), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x68a3c
incl %ebp
testb %al, %al
jne 0x45f2c
movq $0x0, 0x10(%rsp)
movq (%rbx), %r12
testq %r12, %r12
movq (%rsp), %rcx
je 0x4609f
movq 0x8(%r12), %r13
movq 0x10(%r12), %r14
cmpb $0x0, 0x18(%rsp)
je 0x45f87
xorl %eax, %eax
jmp 0x46026
movl (%r14), %eax
shrl $0x4, %eax
testb $0x7, %al
jne 0x45fb8
movq 0x8(%r14), %rdx
movzbl -0x1(%rdx), %ecx
movl %ecx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x46009
leaq 0x7dee6(%rip), %rsi     # 0xc3e90
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
shrl $0x3, %ecx
jmp 0x4600b
movl (%r14), %eax
movl $0xf0, %ecx
andl %ecx, %eax
cmpl $0x10, %eax
jne 0x46121
movq 0x8(%r14), %rdx
movl $0x15, %esi
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
callq 0x433e5
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
movl %eax, %ecx
jmp 0x46015
movq -0x11(%rdx), %rcx
jmp 0x4600b
movzwl -0x5(%rdx), %ecx
jmp 0x4600b
movl -0x9(%rdx), %ecx
jmp 0x4600b
movzbl -0x3(%rdx), %ecx
jmp 0x4600b
xorl %ecx, %ecx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
xorl %r8d, %r8d
callq 0x42e7c
testl %eax, %eax
sete %al
movq (%rsp), %rcx
movzbl %al, %ebp
testq %rcx, %rcx
jne 0x46044
testb %al, %al
jne 0x46044
movq 0x10(%r15), %rdi
movq %r14, %rsi
callq 0x44eae
movq (%rsp), %rcx
movl %eax, %ebp
testl %ebp, %ebp
je 0x4605f
movq %r14, %rdi
callq 0x440f3
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2e4f4
movq (%rsp), %rcx
testq %rcx, %rcx
je 0x46093
movl (%rcx), %eax
andl $0xf, %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x46093
movq 0x8(%r13), %r12
testl %ebp, %ebp
je 0x46096
movq 0x10(%r13), %rdi
callq 0x440f3
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2e4f4
movq (%rsp), %rcx
jmp 0x46096
movq %r13, %r12
testq %r12, %r12
jne 0x45f6f
movl $0x2, %esi
movq %r15, %rdi
callq 0x4063b
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x407de
movq 0x28(%rbx), %rsi
movq %r15, %rdi
callq 0x4063b
movq (%rbx), %r14
testq %r14, %r14
movq %rbx, %r12
je 0x460fe
movq %r15, %rbx
movq 0x10(%r14), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x40537
movq %r15, %rdi
callq 0x440f3
movq %r12, %rdi
movq %r14, %rsi
callq 0x2e4f4
movq (%r12), %r14
testq %r14, %r14
jne 0x460d3
leaq -0x1f6a(%rip), %rax     # 0x4419b
movq %rax, 0x18(%r12)
movq %r12, %rdi
callq 0x2e360
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7de8f(%rip), %rdi     # 0xc3fb7
leaq 0x7f860(%rip), %rsi     # 0xc598f
movl $0x26d, %edx            # imm = 0x26D
callq 0x654e1
movl $0x1, %edi
callq 0x263d0
leaq 0x7de4d(%rip), %rdi     # 0xc3f97
leaq 0x7f83e(%rip), %rsi     # 0xc598f
movl $0x25a, %edx            # imm = 0x25A
callq 0x6558d
jmp 0x46139