// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/t_set.c
// Function: srandmemberWithCountCommand
void srandmemberWithCountCommand(client *c) {
    long l;
    unsigned long count, size;
    int uniq = 1;
    robj *set, *ele;
    int64_t llele;
    int encoding;

    dict *d;

    if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != C_OK) return;
    if (l >= 0) {
        count = (unsigned) l;
    } else {
        /* A negative count means: return the same elements multiple times
         * (i.e. don't remove the extracted element after every extraction). */
        count = -l;
        uniq = 0;
    }

    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk))
        == NULL || checkType(c,set,OBJ_SET)) return;
    size = setTypeSize(set);

    /* If count is zero, serve it ASAP to avoid special cases later. */
    if (count == 0) {
        addReply(c,shared.emptymultibulk);
        return;
    }

    /* CASE 1: The count was negative, so the extraction method is just:
     * "return N random elements" sampling the whole set every time.
     * This case is trivial and can be served without auxiliary data
     * structures. */
    if (!uniq) {
        addReplyMultiBulkLen(c,count);
        while(count--) {
            encoding = setTypeRandomElement(set,&ele,&llele);
            if (encoding == OBJ_ENCODING_INTSET) {
                addReplyBulkLongLong(c,llele);
            } else {
                addReplyBulk(c,ele);
            }
        }
        return;
    }

    /* CASE 2:
     * The number of requested elements is greater than the number of
     * elements inside the set: simply return the whole set. */
    if (count >= size) {
        sunionDiffGenericCommand(c,c->argv+1,1,NULL,SET_OP_UNION);
        return;
    }

    /* For CASE 3 and CASE 4 we need an auxiliary dictionary. */
    d = dictCreate(&setDictType,NULL);

    /* CASE 3:
     * The number of elements inside the set is not greater than
     * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.
     * In this case we create a set from scratch with all the elements, and
     * subtract random elements to reach the requested number of elements.
     *
     * This is done because if the number of requsted elements is just
     * a bit less than the number of elements in the set, the natural approach
     * used into CASE 3 is highly inefficient. */
    if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) {
        setTypeIterator *si;

        /* Add all the elements into the temporary dictionary. */
        si = setTypeInitIterator(set);
        while((encoding = setTypeNext(si,&ele,&llele)) != -1) {
            int retval = DICT_ERR;

            if (encoding == OBJ_ENCODING_INTSET) {
                retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL);
            } else {
                retval = dictAdd(d,dupStringObject(ele),NULL);
            }
            serverAssert(retval == DICT_OK);
        }
        setTypeReleaseIterator(si);
        serverAssert(dictSize(d) == size);

        /* Remove random elements to reach the right count. */
        while(size > count) {
            dictEntry *de;

            de = dictGetRandomKey(d);
            dictDelete(d,dictGetKey(de));
            size--;
        }
    }

    /* CASE 4: We have a big set compared to the requested number of elements.
     * In this case we can simply get random elements from the set and add
     * to the temporary set, trying to eventually get enough unique elements
     * to reach the specified count. */
    else {
        unsigned long added = 0;

        while(added < count) {
            encoding = setTypeRandomElement(set,&ele,&llele);
            if (encoding == OBJ_ENCODING_INTSET) {
                ele = createStringObjectFromLongLong(llele);
            } else {
                ele = dupStringObject(ele);
            }
            /* Try to add the object to the dictionary. If it already exists
             * free it, otherwise increment the number of objects we have
             * in the result dictionary. */
            if (dictAdd(d,ele,NULL) == DICT_OK)
                added++;
            else
                decrRefCount(ele);
        }
    }

    /* CASE 3 & 4: send the result to the user. */
    {
        dictIterator *di;
        dictEntry *de;

        addReplyMultiBulkLen(c,count);
        di = dictGetIterator(d);
        while((de = dictNext(di)) != NULL)
            addReplyBulk(c,dictGetKey(de));
        dictReleaseIterator(di);
        dictRelease(d);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x44ba1
testl %eax, %eax
je 0x52c6a
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %r15
movq %r15, %rax
negq %rax
testq %r15, %r15
movl %r15d, %r14d
cmovsq %rax, %r14
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xb4622(%rip), %rbp     # 0x1072b0
movq 0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x44f89
testq %rax, %rax
je 0x52c5b
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x441ab
testl %eax, %eax
jne 0x52c5b
movq %r12, %rdi
callq 0x51cf6
testq %r14, %r14
je 0x52d6e
testq %r15, %r15
js 0x52d7f
movq %rax, %r13
cmpq %rax, %r14
jae 0x52dca
leaq 0xb302d(%rip), %rdi     # 0x105d10
xorl %esi, %esi
callq 0x31774
movq %rax, %r15
leaq (%r14,%r14,2), %rax
cmpq %r13, %rax
jbe 0x52de9
movq %r12, %rdi
callq 0x51af7
movq %rax, %r12
leaq 0x8(%rsp), %rbp
movq %r12, %rdi
movq %rsp, %rsi
movq %rbp, %rdx
callq 0x51b83
cmpl $0x6, %eax
je 0x52d31
cmpl $-0x1, %eax
je 0x52e87
movq (%rsp), %rdi
callq 0x43cf8
jmp 0x52d3b
movq 0x8(%rsp), %rdi
callq 0x43bb7
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x31a95
testl %eax, %eax
je 0x52d0a
leaq 0x71179(%rip), %rdi     # 0xc3ecc
leaq 0x736d6(%rip), %rsi     # 0xc6430
movl $0x2be, %edx            # imm = 0x2BE
callq 0x654e1
movl $0x1, %edi
callq 0x263d0
movq 0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0x3fc19
jmp 0x52c5b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4063b
movq %rsp, %r15
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x51c7b
cmpl $0x6, %eax
jne 0x52db4
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x407de
jmp 0x52dc0
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x40537
decq %r14
jne 0x52d92
jmp 0x52c5b
movq 0x40(%rbx), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x525ea
jmp 0x52c5b
xorl %ebp, %ebp
movq %r12, %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x51c7b
cmpl $0x6, %eax
jne 0x52e0c
movq 0x8(%rsp), %rdi
callq 0x43bb7
jmp 0x52e15
movq (%rsp), %rdi
callq 0x43cf8
movq %rax, %r13
movq %rax, (%rsp)
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x31a95
testl %eax, %eax
je 0x52e37
movq %r13, %rdi
callq 0x440f3
jmp 0x52e3a
incq %rbp
cmpq %r14, %rbp
jb 0x52deb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4063b
movq %r15, %rdi
callq 0x320bb
movq %rax, %r14
movq %rax, %rdi
callq 0x32116
testq %rax, %rax
je 0x52e72
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x40537
movq %r14, %rdi
jmp 0x52e58
movq %r14, %rdi
callq 0x321d3
movq %r15, %rdi
callq 0x31ff3
jmp 0x52c5b
movq %r12, %rdi
callq 0x51b67
movq 0x48(%r15), %rax
addq 0x28(%r15), %rax
cmpq %r13, %rax
jne 0x52eb9
cmpq %r14, %r13
jbe 0x52e3f
movq %r15, %rdi
callq 0x32237
movq (%rax), %rsi
movq %r15, %rdi
callq 0x31dba
decq %r13
jmp 0x52e9c
leaq 0x7363b(%rip), %rdi     # 0xc64fb
leaq 0x73569(%rip), %rsi     # 0xc6430
movl $0x2c1, %edx            # imm = 0x2C1
jmp 0x52d5f