// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 $0x78, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
callq 0x26e0c
movq %rax, 0x30(%rsp)
movq $0xa, 0x20(%rsp)
testq %r14, %r14
movq %r14, 0x8(%rsp)
movq %r15, 0x18(%rsp)
je 0x3b95c
movl (%r14), %eax
andl $0xf, %eax
addl $-0x2, %eax
cmpl $0x3, %eax
jae 0x3bdb8
pushq $0x3
jmp 0x3b95e
pushq $0x2
popq %rbp
movl %ebp, %r13d
negl %r13d
movq $0x0, 0x40(%rsp)
pushq $0xa
popq %r15
movq $0x0, 0x38(%rsp)
movl $0x0, 0x2c(%rsp)
movq %rbx, 0x10(%rsp)
movq %rbx, %rax
movslq 0x38(%rbx), %rbx
cmpq %rbx, %rbp
jge 0x3ba52
addl %r13d, %ebx
movq 0x40(%rax), %r14
movq (%r14,%rbp,8), %rax
movq 0x8(%rax), %r12
movq %r12, %rdi
leaq 0x81fc8(%rip), %rsi     # 0xbd979
callq 0x262f0
testl %eax, %eax
jne 0x3b9f1
cmpl $0x2, %ebx
jl 0x3b9f1
movq 0x8(%r14,%rbp,8), %rsi
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3ab5e
testl %eax, %eax
movq 0x30(%rsp), %r14
jne 0x3baa5
movq 0x20(%rsp), %r15
testq %r15, %r15
jg 0x3ba40
jmp 0x3ba88
movq %r12, %rdi
leaq 0x8658a(%rip), %rsi     # 0xc1f85
callq 0x262f0
testl %eax, %eax
jne 0x3ba88
cmpl $0x2, %ebx
jl 0x3ba88
movq 0x8(%r14,%rbp,8), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0x3b1a7
movq %rax, %rcx
movq %rbx, 0x40(%rsp)
cmpb $0x2a, (%rbx)
setne %al
movq %rcx, 0x38(%rsp)
cmpl $0x1, %ecx
setne %cl
orb %al, %cl
movzbl %cl, %eax
movl %eax, 0x2c(%rsp)
addq $0x2, %rbp
addl $-0x2, %r13d
movq 0x10(%rsp), %rbx
jmp 0x3b988
movq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x30(%rsp), %r14
je 0x3bac7
movl (%rax), %eax
cmpb $0x73, %al
movq 0x10(%rsp), %rbx
je 0x3bae0
movzbl %al, %ecx
cmpl $0x24, %ecx
je 0x3bad5
cmpl $0x22, %ecx
jne 0x3bb5a
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r12
jmp 0x3baf4
leaq 0xbe881(%rip), %rax     # 0xfa310
movq 0x80(%rax), %rsi
movq 0x10(%rsp), %rdi
callq 0x36987
movq 0x30(%rsp), %r14
leaq -0x17a8(%rip), %rax     # 0x3a304
movq %rax, 0x18(%r14)
movq %r14, %rdi
callq 0x26e2a
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rax
movq (%rax), %r12
jmp 0x3baf4
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r12
jmp 0x3baec
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %r12
addq %r15, %r15
movq %r15, 0x20(%rsp)
testq %r12, %r12
je 0x3bb53
leaq 0x50(%rsp), %r13
movq %r14, (%r13)
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r13)
imulq $-0xa, %r15, %rbx
leaq -0x35f(%rip), %rbp      # 0x3b7b7
movq 0x18(%rsp), %rsi
movq %r12, %rdi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x2ad3b
movq %rax, %rsi
testq %rax, %rax
je 0x3bb44
testq %rbx, %rbx
je 0x3bb44
movq 0x28(%r14), %rax
incq %rbx
cmpq 0x20(%rsp), %rax
jb 0x3bb1b
movq %rsi, 0x18(%rsp)
movq 0x10(%rsp), %rbx
jmp 0x3bc22
movq 0x8(%rsp), %rax
movl (%rax), %eax
andl $0xf, %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x3bbd9
movq 0x8(%rsp), %r15
movq 0x8(%r15), %rdi
xorl %esi, %esi
callq 0x35aca
movq %rax, %rbp
leaq 0x4c(%rsp), %r13
leaq 0x70(%rsp), %r12
testq %rbp, %rbp
je 0x3bc19
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x35be8
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3bbb3
movl 0x4c(%rsp), %esi
callq 0x39e29
jmp 0x3bbbd
movq 0x70(%rsp), %rdi
callq 0x39e38
movq %r14, %rdi
movq %rax, %rsi
callq 0x26ecc
movq 0x8(%r15), %rdi
movq %rbp, %rsi
callq 0x35b60
movq %rax, %rbp
jmp 0x3bb82
cmpl $0x2, %eax
movq 0x8(%rsp), %r15
jne 0x3bdcd
xorl %ebp, %ebp
leaq 0x50(%rsp), %r12
movq 0x8(%r15), %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x5a9c0
testb %al, %al
je 0x3bc19
incl %ebp
movq 0x50(%rsp), %rdi
callq 0x39e38
movq %r14, %rdi
movq %rax, %rsi
callq 0x26ecc
jmp 0x3bbee
movq $0x0, 0x18(%rsp)
movq (%r14), %r15
testq %r15, %r15
je 0x3bd44
movq %r15, %rbp
movq 0x8(%r15), %r13
movq 0x10(%r15), %r12
cmpl $0x0, 0x2c(%rsp)
je 0x3bc6c
movl (%r12), %eax
movl %eax, %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x3bc7c
movq 0x8(%r12), %r15
movq %r15, %rdi
callq 0x3b1a7
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movl %eax, %ecx
jmp 0x3bcae
movb $0x1, %cl
cmpq $0x0, 0x8(%rsp)
jne 0x3bd06
jmp 0x3bcc4
andl $0xf0, %eax
cmpl $0x10, %eax
jne 0x3bd98
movq 0x8(%r12), %rdx
leaq 0x50(%rsp), %r15
movq %r15, %rdi
pushq $0x15
popq %rsi
callq 0x39796
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x3929c
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
jne 0x3bcdc
testl %eax, %eax
je 0x3bcdc
movq 0x10(%rbx), %rdi
movq %r12, %rsi
callq 0x3ae0a
movq %r13, %r15
testl %eax, %eax
jne 0x3bce2
jmp 0x3bc25
movb $0x1, %cl
testl %eax, %eax
jne 0x3bcf7
movq %r12, %rdi
callq 0x3a265
movq %r14, %rdi
movq %rbp, %rsi
callq 0x26fa8
xorl %ecx, %ecx
movq %r13, %r15
cmpq $0x0, 0x8(%rsp)
je 0x3bc25
movq 0x8(%rsp), %rax
movl (%rax), %eax
andl $0xf, %eax
addl $-0x3, %eax
movq %r13, %r15
cmpl $0x1, %eax
ja 0x3bc25
movq 0x8(%r13), %r15
testb %cl, %cl
jne 0x3bc25
movq 0x10(%r13), %rdi
callq 0x3a265
movq %r14, %rdi
movq %r13, %rsi
callq 0x26fa8
jmp 0x3bc25
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x370d9
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x37206
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0x370d9
movq (%r14), %r15
testq %r15, %r15
je 0x3baa5
movq 0x10(%r15), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x36fd9
movq %r12, %rdi
callq 0x3a265
movq %r14, %rdi
movq %r15, %rsi
callq 0x26fa8
jmp 0x3bd68
leaq 0x76a78(%rip), %rdi     # 0xb2817
leaq 0x78329(%rip), %rsi     # 0xb40cf
movl $0x26d, %edx            # imm = 0x26D
callq 0x57ac8
pushq $0x1
popq %rdi
callq 0x263c0
leaq 0x769ea(%rip), %rdi     # 0xb27a9
leaq 0x78309(%rip), %rsi     # 0xb40cf
movl $0x1f7, %edx            # imm = 0x1F7
jmp 0x3bdab
leaq 0x76a23(%rip), %rdi     # 0xb27f7
leaq 0x782f4(%rip), %rsi     # 0xb40cf
movl $0x25a, %edx            # imm = 0x25A
callq 0x57b45
jmp 0x3bdb0