// File: /workspace/github_c_makefile_star2_O3/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 0x41718
testl %eax, %eax
je 0x4f514
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 0xb2dd0(%rip), %rax     # 0x102310
movq 0x68(%rax), %rdx
movq %rbx, %rdi
callq 0x41af9
testq %rax, %rax
je 0x4f505
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x40d47
testl %eax, %eax
jne 0x4f505
movq %r12, %rdi
callq 0x4e5b5
cmpq $0x0, 0x18(%rsp)
je 0x4f62b
testq %rbp, %rbp
js 0x4f643
movq %rax, %r13
cmpq %rax, %r14
jae 0x4f691
leaq 0xb1818(%rip), %rdi     # 0x100db0
xorl %esi, %esi
callq 0x2e68c
movq %rax, %r15
leaq (%r14,%r14,2), %rax
cmpq %r13, %rax
jbe 0x4f6b0
movq %r12, %rdi
callq 0x4e3b6
movq %rax, %r12
leaq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r14
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
movq %rbp, %rdx
callq 0x4e442
cmpl $0x6, %eax
je 0x4f5ee
cmpl $-0x1, %eax
je 0x4f757
movq 0x8(%rsp), %rdi
callq 0x40894
jmp 0x4f5f8
movq 0x10(%rsp), %rdi
callq 0x40753
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2e9b8
testl %eax, %eax
je 0x4f5c4
leaq 0x71920(%rip), %rdi     # 0xc0f30
leaq 0x73e99(%rip), %rsi     # 0xc34b0
movl $0x2be, %edx            # imm = 0x2BE
callq 0x61908
movl $0x1, %edi
callq 0x263c0
leaq 0xb2cde(%rip), %rax     # 0x102310
movq 0x68(%rax), %rsi
movq %rbx, %rdi
callq 0x3c854
jmp 0x4f505
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d274
leaq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4e53a
cmpl $0x6, %eax
jne 0x4f67a
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3d415
jmp 0x4f687
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x3d170
incq %rbp
jne 0x4f658
jmp 0x4f505
movq 0x40(%rbx), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4eeb2
jmp 0x4f505
xorl %ebp, %ebp
movq 0x18(%rsp), %r14
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x4e53a
cmpl $0x6, %eax
jne 0x4f6da
movq 0x10(%rsp), %rdi
callq 0x40753
jmp 0x4f6e4
movq 0x8(%rsp), %rdi
callq 0x40894
movq %rax, %r13
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2e9b8
testl %eax, %eax
je 0x4f707
movq %r13, %rdi
callq 0x40c8f
jmp 0x4f70a
incq %rbp
cmpq %r14, %rbp
jb 0x4f6b7
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3d274
movq %r15, %rdi
callq 0x2efbb
movq %rax, %r14
movq %rax, %rdi
callq 0x2f015
testq %rax, %rax
je 0x4f742
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x3d170
movq %r14, %rdi
jmp 0x4f728
movq %r14, %rdi
callq 0x2f0dd
movq %r15, %rdi
callq 0x2eef3
jmp 0x4f505
movq %r12, %rdi
callq 0x4e426
movq 0x48(%r15), %rax
addq 0x28(%r15), %rax
cmpq %r13, %rax
jne 0x4f789
cmpq %r14, %r13
jbe 0x4f70f
movq %r15, %rdi
callq 0x2f141
movq (%rax), %rsi
movq %r15, %rdi
callq 0x2ecb5
decq %r13
jmp 0x4f76c
leaq 0x73deb(%rip), %rdi     # 0xc357b
leaq 0x73d19(%rip), %rsi     # 0xc34b0
movl $0x2c1, %edx            # imm = 0x2C1
jmp 0x4f61c