// File: /workspace/github_c_makefile_star2_O3/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, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
callq 0x2b17c
movq %rax, %rbx
movq $0xa, 0x20(%rsp)
testq %r14, %r14
movq %r14, 0x38(%rsp)
je 0x427c0
movl (%r14), %eax
andl $0xf, %eax
addl $-0x2, %eax
movl $0x3, %r12d
cmpl $0x3, %eax
jb 0x427c6
leaq 0x7e7fe(%rip), %rdi     # 0xc0fad
leaq 0x80259(%rip), %rsi     # 0xc2a0f
movl $0x1f7, %edx            # imm = 0x1F7
jmp 0x42cdf
movl $0x2, %r12d
movl 0x38(%rbp), %esi
cmpl %esi, %r12d
movq %rbp, 0x8(%rsp)
jge 0x4292e
movq %rbx, 0x10(%rsp)
movl %r12d, %ebp
negl %r12d
movq $0x0, 0x40(%rsp)
movl $0xa, %eax
movq %rax, 0x48(%rsp)
xorl %edx, %edx
movq $0x0, 0x18(%rsp)
movq %rdx, %r15
movq %rsi, (%rsp)
leal (%rsi,%r12), %ebx
movq 0x8(%rsp), %rax
movq 0x40(%rax), %r13
movq (%r13,%rbp,8), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
leaq 0x8a506(%rip), %rsi     # 0xccd2d
callq 0x262f0
testl %eax, %eax
jne 0x4287b
cmpl $0x2, %ebx
jl 0x4287b
movq 0x8(%r13,%rbp,8), %rsi
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x41718
testl %eax, %eax
movq 0x10(%rsp), %rbx
jne 0x42993
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
jle 0x42976
movq 0x8(%rsp), %rax
movl 0x38(%rax), %esi
movq %r15, %rdx
jmp 0x428ff
movq %r14, %rdi
leaq 0x8ec28(%rip), %rsi     # 0xd14ad
callq 0x262f0
testl %eax, %eax
jne 0x42976
cmpl $0x2, %ebx
jl 0x42976
movq 0x8(%r13,%rbp,8), %rax
movq 0x8(%rax), %rcx
movzbl -0x1(%rcx), %edx
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x428de
leaq 0x7e613(%rip), %rsi     # 0xc0ecc
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
shrl $0x3, %edx
jmp 0x428e0
movq -0x11(%rcx), %rdx
jmp 0x428e0
movzwl -0x5(%rcx), %edx
jmp 0x428e0
movl -0x9(%rcx), %edx
jmp 0x428e0
movzbl -0x3(%rcx), %edx
jmp 0x428e0
xorl %edx, %edx
movq (%rsp), %rsi
movq %rcx, 0x18(%rsp)
cmpb $0x2a, (%rcx)
setne %al
cmpl $0x1, %edx
setne %cl
orb %al, %cl
movzbl %cl, %eax
movq %rax, 0x40(%rsp)
addq $0x2, %rbp
addl $-0x2, %r12d
cmpl %ebp, %esi
jg 0x42800
cmpl $0x0, 0x40(%rsp)
sete (%rsp)
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %rbp
movq 0x18(%rsp), %rcx
movq 0x48(%rsp), %rsi
jmp 0x4293b
movb $0x1, (%rsp)
xorl %ecx, %ecx
movl $0xa, %esi
xorl %edx, %edx
movq 0x38(%rsp), %r15
testq %r15, %r15
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
je 0x4296d
movl (%r15), %eax
cmpb $0x73, %al
je 0x429bb
movzbl %al, %ecx
cmpl $0x24, %ecx
je 0x429b5
cmpl $0x22, %ecx
jne 0x42a29
movq 0x8(%r15), %r14
jmp 0x429ca
movq 0x10(%rbp), %rax
movq (%rax), %r14
jmp 0x429ca
leaq 0xbf993(%rip), %rax     # 0x102310
movq 0x80(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x3c854
movq 0x10(%rsp), %rbx
leaq -0x1c63(%rip), %rax     # 0x40d37
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x2b19c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %r14
jmp 0x429c2
movq 0x8(%r15), %rax
movq (%rax), %r14
addq %rsi, %rsi
movq %rsi, 0x20(%rsp)
testq %r14, %r14
je 0x42a26
leaq 0x50(%rsp), %r12
movq %rbx, (%r12)
movq %r15, 0x8(%r12)
addq %rsi, %rsi
leaq (%rsi,%rsi,4), %r15
negq %r15
leaq -0x419(%rip), %r13      # 0x425d5
movq 0x28(%rsp), %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %r12, %rcx
callq 0x2f38d
movq %rax, %rsi
testq %rax, %rax
je 0x42a1c
testq %r15, %r15
je 0x42a1c
movq 0x28(%rbx), %rax
incq %r15
cmpq 0x20(%rsp), %rax
jb 0x429f3
movq 0x38(%rsp), %r15
jmp 0x42b02
movl (%r15), %eax
andl $0xf, %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0x42aab
movq 0x8(%r15), %rdi
xorl %esi, %esi
callq 0x3b680
testq %rax, %rax
je 0x42afb
movq %rax, %r14
leaq 0x50(%rsp), %r12
leaq 0x34(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x3b7cf
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x42a80
movl 0x34(%rsp), %esi
callq 0x40744
jmp 0x42a8a
movq 0x70(%rsp), %rdi
callq 0x40753
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2b24b
movq 0x8(%r15), %rdi
movq %r14, %rsi
callq 0x3b745
movq %rax, %r14
testq %rax, %rax
jne 0x42a5a
jmp 0x42afb
cmpl $0x2, %eax
jne 0x42cee
movq 0x8(%r15), %rdi
leaq 0x50(%rsp), %rdx
xorl %esi, %esi
callq 0x64e29
testb %al, %al
je 0x42afb
movl $0x1, %ebp
leaq 0x50(%rsp), %r14
movq 0x50(%rsp), %rdi
callq 0x40753
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2b24b
movq 0x8(%r15), %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x64e29
incl %ebp
testb %al, %al
jne 0x42ad2
xorl %esi, %esi
movq 0x8(%rsp), %rbp
movq %rsi, 0x28(%rsp)
movq (%rbx), %r12
testq %r12, %r12
je 0x42c76
movq 0x8(%r12), %r13
movq 0x10(%r12), %r14
cmpb $0x0, (%rsp)
je 0x42b44
testq %r15, %r15
je 0x42c09
movl (%r15), %eax
andl $0xf, %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x42c6e
movq 0x8(%r13), %r12
jmp 0x42b0a
movl (%r14), %eax
movl %eax, %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x42b7c
movq 0x8(%r14), %rdx
movzbl -0x1(%rdx), %ecx
movl %ecx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x42bec
leaq 0x7e372(%rip), %rsi     # 0xc0ee0
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
jmpq *%rax
shrl $0x3, %ecx
jmp 0x42bee
movq %r13, 0x8(%rsp)
andl $0xf0, %eax
cmpl $0x10, %eax
jne 0x42ccc
movq %rbx, %r13
movq %r15, %rbx
movq %rbp, %r15
movq 0x8(%r14), %rdx
movl $0x15, %esi
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x3ff83
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x3fa30
movq %r15, %rbp
movq %rbx, %r15
movq %r13, %rbx
movq 0x8(%rsp), %r13
jmp 0x42c00
movq -0x11(%rdx), %rcx
jmp 0x42bee
movzwl -0x5(%rdx), %ecx
jmp 0x42bee
movl -0x9(%rdx), %ecx
jmp 0x42bee
movzbl -0x3(%rdx), %ecx
jmp 0x42bee
xorl %ecx, %ecx
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
xorl %r8d, %r8d
callq 0x3fa30
testq %r15, %r15
jne 0x42c1b
testl %eax, %eax
je 0x42c1b
movq 0x10(%rbp), %rdi
movq %r14, %rsi
callq 0x41a1e
testl %eax, %eax
jne 0x42c21
jmp 0x42c6e
movb $0x1, %cl
testl %eax, %eax
jne 0x42c36
movq %r14, %rdi
callq 0x40c8f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2b331
xorl %ecx, %ecx
testq %r15, %r15
je 0x42c6e
movl (%r15), %eax
andl $0xf, %eax
addl $-0x3, %eax
cmpl $0x2, %eax
jae 0x42c6e
movq 0x8(%r13), %r12
testb %cl, %cl
jne 0x42b0a
movq 0x10(%r13), %rdi
callq 0x40c8f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2b331
jmp 0x42b0a
movq %r13, %r12
jmp 0x42b0a
movl $0x2, %esi
movq %rbp, %rdi
callq 0x3d274
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
callq 0x3d415
movq 0x28(%rbx), %rsi
movq %rbp, %rdi
callq 0x3d274
movq (%rbx), %r14
testq %r14, %r14
je 0x42993
movq 0x10(%r14), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3d170
movq %r15, %rdi
callq 0x40c8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2b331
jmp 0x42c9c
leaq 0x7e348(%rip), %rdi     # 0xc101b
leaq 0x7fd35(%rip), %rsi     # 0xc2a0f
movl $0x26d, %edx            # imm = 0x26D
callq 0x61908
movl $0x1, %edi
callq 0x263c0
leaq 0x7e306(%rip), %rdi     # 0xc0ffb
leaq 0x7fd13(%rip), %rsi     # 0xc2a0f
movl $0x25a, %edx            # imm = 0x25A
callq 0x619b4
jmp 0x42ce4