// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x10(%rax), %rsi
leaq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x49f20
cmpl $0x0, %eax
je 0x5d35d
jmp 0x5d6ae
cmpq $0x0, -0x10(%rbp)
jl 0x5d370
movq -0x10(%rbp), %rax
movl %eax, %eax
movq %rax, -0x18(%rbp)
jmp 0x5d381
xorl %eax, %eax
subq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x11b8a8(%rip), %rax    # 0x178c40
movq 0x68(%rax), %rdx
callq 0x4a730
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x5d3c2
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x2, %edx
callq 0x490a0
cmpl $0x0, %eax
je 0x5d3c7
jmp 0x5d6ae
movq -0x30(%rbp), %rdi
callq 0x5bf20
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5d3f4
movq -0x8(%rbp), %rdi
leaq 0x11b85a(%rip), %rax    # 0x178c40
movq 0x68(%rax), %rsi
callq 0x42cc0
jmp 0x5d6ae
cmpl $0x0, -0x24(%rbp)
jne 0x5d459
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x437f0
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x18(%rbp)
cmpq $0x0, %rax
je 0x5d454
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x5be60
movl %eax, -0x44(%rbp)
cmpl $0x6, -0x44(%rbp)
jne 0x5d445
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x439f0
jmp 0x5d452
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x43630
jmp 0x5d407
jmp 0x5d6ae
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x5d489
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rsi
addq $0x8, %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x5cb60
jmp 0x5d6ae
leaq 0x119920(%rip), %rdi    # 0x176db0
xorl %eax, %eax
movl %eax, %esi
callq 0x2d330
movq %rax, -0x50(%rbp)
imulq $0x3, -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x5d5e4
movq -0x30(%rbp), %rdi
callq 0x5bbf0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x5bce0
movl %eax, -0x44(%rbp)
cmpl $-0x1, %eax
je 0x5d568
movl $0x1, -0x5c(%rbp)
cmpl $0x6, -0x44(%rbp)
jne 0x5d50f
movq -0x50(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rdi
callq 0x486f0
movq -0x88(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2d8c0
movl %eax, -0x5c(%rbp)
jmp 0x5d539
movq -0x50(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rdi
callq 0x48970
movq -0x90(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2d8c0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x5d541
jmp 0x5d563
leaq 0xde344(%rip), %rdi     # 0x13b88c
leaq 0xe05c9(%rip), %rsi     # 0x13db18
movl $0x2be, %edx            # imm = 0x2BE
callq 0x76ce0
movl $0x1, %edi
callq 0x263c0
jmp 0x5d4b9
movq -0x58(%rbp), %rdi
callq 0x5bca0
movq -0x50(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
addq 0x48(%rcx), %rax
cmpq -0x20(%rbp), %rax
jne 0x5d589
jmp 0x5d5ab
leaq 0xe0653(%rip), %rdi     # 0x13dbe3
leaq 0xe0581(%rip), %rsi     # 0x13db18
movl $0x2c1, %edx            # imm = 0x2C1
callq 0x76ce0
movl $0x1, %edi
callq 0x263c0
jmp 0x5d5ad
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x5d5e2
movq -0x50(%rbp), %rdi
callq 0x2e6a0
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rdi
movq -0x68(%rbp), %rax
movq (%rax), %rsi
callq 0x2de50
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x5d5ad
jmp 0x5d65d
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x5d65b
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x5be60
movl %eax, -0x44(%rbp)
cmpl $0x6, -0x44(%rbp)
jne 0x5d61f
movq -0x40(%rbp), %rdi
callq 0x486f0
movq %rax, -0x38(%rbp)
jmp 0x5d62c
movq -0x38(%rbp), %rdi
callq 0x48970
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2d8c0
cmpl $0x0, %eax
jne 0x5d650
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x5d659
movq -0x38(%rbp), %rdi
callq 0x48f70
jmp 0x5d5ec
jmp 0x5d65d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x437f0
movq -0x50(%rbp), %rdi
callq 0x2e400
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x2e4a0
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x5d69c
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movq (%rax), %rsi
callq 0x43630
jmp 0x5d677
movq -0x78(%rbp), %rdi
callq 0x2e600
movq -0x50(%rbp), %rdi
callq 0x2e230
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)