// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 $0x28, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3ab5e
testl %eax, %eax
je 0x47036
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rbp
movq %rbp, %r15
negq %r15
testq %rbp, %rbp
movl %ebp, %r14d
movq %r14, %rax
cmovsq %r15, %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xb32ae(%rip), %rax     # 0xfa310
movq 0x68(%rax), %rdx
movq %rbx, %rdi
callq 0x3aee5
testq %rax, %rax
je 0x47027
movq %rax, %r12
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3a311
testl %eax, %eax
jne 0x47027
movq %r12, %rdi
callq 0x461ae
cmpq $0x0, 0x18(%rsp)
je 0x47149
testq %rbp, %rbp
js 0x47161
movq %rax, %r13
cmpq %rax, %r14
jae 0x471b3
leaq 0xb1cf8(%rip), %rdi     # 0xf8db0
xorl %esi, %esi
callq 0x2a155
movq %rax, %r15
leaq (%r14,%r14,2), %rax
cmpq %r13, %rax
jbe 0x471d0
movq %r12, %rdi
callq 0x45fc4
movq %rax, %r12
leaq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r14
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
movq %rbp, %rdx
callq 0x46049
cmpl $0x6, %eax
je 0x4710e
cmpl $-0x1, %eax
je 0x47235
movq 0x8(%rsp), %rdi
callq 0x39f59
jmp 0x47118
movq 0x10(%rsp), %rdi
callq 0x39e38
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2a3ed
testl %eax, %eax
je 0x470e4
leaq 0x6b5fc(%rip), %rdi     # 0xb272c
leaq 0x6d90d(%rip), %rsi     # 0xb4a44
movl $0x2be, %edx            # imm = 0x2BE
callq 0x57ac8
pushq $0x1
popq %rdi
callq 0x263c0
leaq 0xb31c0(%rip), %rax     # 0xfa310
movq 0x68(%rax), %rsi
movq %rbx, %rdi
callq 0x36987
jmp 0x47027
movq %rbx, %rdi
movq %r15, %rsi
callq 0x370d9
leaq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
testq %rbp, %rbp
je 0x47027
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x46138
cmpl $0x6, %eax
jne 0x471a1
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x37206
jmp 0x471ae
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x36fd9
incq %rbp
jmp 0x47176
movq 0x40(%rbx), %rsi
addq $0x8, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x46a59
jmp 0x47027
xorl %ebp, %ebp
movq 0x18(%rsp), %r14
cmpq %r14, %rbp
jae 0x47267
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x46138
cmpl $0x6, %eax
jne 0x47203
movq 0x10(%rsp), %rdi
callq 0x39e38
jmp 0x4720d
movq 0x8(%rsp), %rdi
callq 0x39f59
movq %rax, %r13
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2a3ed
testl %eax, %eax
je 0x47230
movq %r13, %rdi
callq 0x3a265
jmp 0x471d7
incq %rbp
jmp 0x471d7
movq %r12, %rdi
callq 0x4602d
movq 0x48(%r15), %rax
addq 0x28(%r15), %rax
cmpq %r13, %rax
jne 0x472ac
cmpq %r14, %r13
jbe 0x47267
movq %r15, %rdi
callq 0x2aaf6
movq (%rax), %rsi
movq %r15, %rdi
callq 0x2a6dc
decq %r13
jmp 0x4724a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x370d9
movq %r15, %rdi
callq 0x2a99f
movq %rax, %r14
movq %r14, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x47297
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x36fd9
jmp 0x4727d
movq %r14, %rdi
callq 0x2aa94
movq %r15, %rdi
callq 0x2a8da
jmp 0x47027
leaq 0x6d85c(%rip), %rdi     # 0xb4b0f
leaq 0x6d78a(%rip), %rsi     # 0xb4a44
movl $0x2c1, %edx            # imm = 0x2C1
jmp 0x4713c