// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/t_set.c
// Function: spopWithCountCommand
void spopWithCountCommand(client *c) {
    long l;
    unsigned long count, size;
    robj *set;

    /* Get the count argument */
    if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != C_OK) return;
    if (l >= 0) {
        count = (unsigned) l;
    } else {
        addReply(c,shared.outofrangeerr);
        return;
    }

    /* Make sure a key with the name inputted exists, and that it's type is
     * indeed a set. Otherwise, return nil */
    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk))
        == NULL || checkType(c,set,OBJ_SET)) return;

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

    size = setTypeSize(set);

    /* Generate an SPOP keyspace notification */
    notifyKeyspaceEvent(NOTIFY_SET,"spop",c->argv[1],c->db->id);
    server.dirty += count;

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

        /* Delete the set as it is now empty */
        dbDelete(c->db,c->argv[1]);
        notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],c->db->id);

        /* Propagate this command as an DEL operation */
        rewriteClientCommandVector(c,2,shared.del,c->argv[1]);
        signalModifiedKey(c->db,c->argv[1]);
        server.dirty++;
        return;
    }

    /* Case 2 and 3 require to replicate SPOP as a set of SERM commands.
     * Prepare our replication argument vector. Also send the array length
     * which is common to both the code paths. */
    robj *propargv[3];
    propargv[0] = createStringObject("SREM",4);
    propargv[1] = c->argv[1];
    addReplyMultiBulkLen(c,count);

    /* Common iteration vars. */
    robj *objele;
    int encoding;
    int64_t llele;
    unsigned long remaining = size-count; /* Elements left after SPOP. */

    /* If we are here, the number of requested elements is less than the
     * number of elements inside the set. Also we are sure that count < size.
     * Use two different strategies.
     *
     * CASE 2: The number of elements to return is small compared to the
     * set size. We can just extract random elements and return them to
     * the set. */
    if (remaining*SPOP_MOVE_STRATEGY_MUL > count) {
        while(count--) {
            encoding = setTypeRandomElement(set,&objele,&llele);
            if (encoding == OBJ_ENCODING_INTSET) {
                objele = createStringObjectFromLongLong(llele);
            } else {
                incrRefCount(objele);
            }

            /* Return the element to the client and remove from the set. */
            addReplyBulk(c,objele);
            setTypeRemove(set,objele);

            /* Replicate/AOF this command as an SREM operation */
            propargv[2] = objele;
            alsoPropagate(server.sremCommand,c->db->id,propargv,3,
                PROPAGATE_AOF|PROPAGATE_REPL);
            decrRefCount(objele);
        }
    } else {
    /* CASE 3: The number of elements to return is very big, approaching
     * the size of the set itself. After some time extracting random elements
     * from such a set becomes computationally expensive, so we use
     * a different strategy, we extract random elements that we don't
     * want to return (the elements that will remain part of the set),
     * creating a new set as we do this (that will be stored as the original
     * set). Then we return the elements left in the original set and
     * release it. */
        robj *newset = NULL;

        /* Create a new set with just the remaining elements. */
        while(remaining--) {
            encoding = setTypeRandomElement(set,&objele,&llele);
            if (encoding == OBJ_ENCODING_INTSET) {
                objele = createStringObjectFromLongLong(llele);
            } else {
                incrRefCount(objele);
            }
            if (!newset) newset = setTypeCreate(objele);
            setTypeAdd(newset,objele);
            setTypeRemove(set,objele);
            decrRefCount(objele);
        }

        /* Assign the new set as the key value. */
        incrRefCount(set); /* Protect the old set value. */
        dbOverwrite(c->db,c->argv[1],newset);

        /* Tranfer the old set to the client and release it. */
        setTypeIterator *si;
        si = setTypeInitIterator(set);
        while((encoding = setTypeNext(si,&objele,&llele)) != -1) {
            if (encoding == OBJ_ENCODING_INTSET) {
                objele = createStringObjectFromLongLong(llele);
            } else {
                incrRefCount(objele);
            }
            addReplyBulk(c,objele);

            /* Replicate/AOF this command as an SREM operation */
            propargv[2] = objele;
            alsoPropagate(server.sremCommand,c->db->id,propargv,3,
                PROPAGATE_AOF|PROPAGATE_REPL);

            decrRefCount(objele);
        }
        setTypeReleaseIterator(si);
        decrRefCount(set);
    }

    /* Don't propagate the command itself even if we incremented the
     * dirty counter. We don't want to propagate an SPOP command since
     * we propagated the command as a set of SREMs operations using
     * the alsoPropagate() API. */
    decrRefCount(propargv[0]);
    preventCommandPropagation(c);
    signalModifiedKey(c->db,c->argv[1]);
    server.dirty++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x3ab5e
testl %eax, %eax
jne 0x46862
movq 0x30(%rsp), %r15
testq %r15, %r15
js 0x4684c
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xb3c16(%rip), %rbp     # 0xfa310
movq 0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x3aee5
testq %rax, %rax
je 0x46862
movq %rax, %r14
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3a311
testl %eax, %eax
jne 0x46862
movl %r15d, %r15d
testq %r15, %r15
je 0x46871
movq %r14, %rdi
callq 0x461ae
movq %rax, %r12
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x67dca(%rip), %rsi     # 0xae51f
pushq $0x20
popq %rdi
callq 0x6fb58
leaq 0xb3144(%rip), %rax     # 0xf98a8
addq %r15, 0x720(%rax)
movq %r15, %r13
subq %r12, %r13
jae 0x46877
leaq 0x6e38c(%rip), %rdi     # 0xb4b0a
pushq $0x4
popq %rsi
callq 0x39e29
movq %rax, 0x10(%rsp)
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x370d9
subq %r15, %r12
leaq (%r12,%r12,4), %rax
cmpq %r15, %rax
jbe 0x468fa
leaq 0x10(%rsp), %rbp
pushq $0x3
popq %r12
subq $0x1, %r15
leaq 0xb30e1(%rip), %r13     # 0xf98a8
jb 0x46a2a
movq %r14, %rdi
movq %rsp, %rsi
leaq 0x8(%rsp), %rdx
callq 0x46138
cmpl $0x6, %eax
jne 0x467f5
movq 0x8(%rsp), %rdi
callq 0x39e38
movq %rax, %r13
movq %rax, (%rsp)
jmp 0x46801
movq (%rsp), %r13
movq %r13, %rdi
callq 0x39e74
movq %rbx, %rdi
movq %r13, %rsi
callq 0x36fd9
movq %r14, %rdi
movq %r13, %rsi
callq 0x45ea1
movq %r13, 0x20(%rsp)
leaq 0xb3085(%rip), %rax     # 0xf98a8
movq 0x368(%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
movq %rbp, %rdx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x2e189
movq %r13, %rdi
callq 0x3a265
jmp 0x467bc
leaq 0xb3abd(%rip), %rax     # 0xfa310
movq 0x90(%rax), %rsi
movq %rbx, %rdi
callq 0x36987
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x68(%rbp), %rsi
jmp 0x4685a
movq 0x40(%rbx), %rsi
addq $0x8, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rax, %r14
callq 0x46a59
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3b1df
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x69b5f(%rip), %rsi     # 0xb0418
pushq $0x4
popq %rdi
callq 0x6fb58
movq 0x128(%rbp), %rdx
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rcx
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x38eb8
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3b125
incq 0x720(%r14)
jmp 0x46862
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq %r14, %rdi
testq %r13, %r13
je 0x46970
movq %rsp, %rsi
movq %r12, %rdx
callq 0x46138
cmpl $0x6, %eax
jne 0x4692d
movq 0x8(%rsp), %rdi
callq 0x39e38
movq %rax, %rbp
movq %rax, (%rsp)
jmp 0x46939
movq (%rsp), %rbp
movq %rbp, %rdi
callq 0x39e74
testq %r15, %r15
jne 0x46949
movq %rbp, %rdi
callq 0x45c68
movq %rax, %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x45c80
movq (%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x45ea1
movq %rbp, %rdi
callq 0x3a265
incq %r13
jmp 0x46902
callq 0x39e74
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
movq %r15, %rdx
callq 0x3b01d
movq %r14, %rdi
callq 0x45fc4
movq %rax, %r15
leaq 0x8(%rsp), %r13
pushq $0x3
popq %r12
movq %r15, %rdi
movq %rsp, %rsi
movq %r13, %rdx
callq 0x46049
cmpl $0x6, %eax
je 0x469c3
cmpl $-0x1, %eax
je 0x46a13
movq (%rsp), %rbp
movq %rbp, %rdi
callq 0x39e74
jmp 0x469d4
movq 0x8(%rsp), %rdi
callq 0x39e38
movq %rax, %rbp
movq %rax, (%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x36fd9
movq %rbp, 0x20(%rsp)
leaq 0xb2ebd(%rip), %rax     # 0xf98a8
movq 0x368(%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
leaq 0x10(%rsp), %rdx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x2e189
movq %rbp, %rdi
callq 0x3a265
jmp 0x4699d
movq %r15, %rdi
callq 0x4602d
movq %r14, %rdi
callq 0x3a265
leaq 0xb2e7e(%rip), %r13     # 0xf98a8
movq 0x10(%rsp), %rdi
callq 0x3a265
movq %rbx, %rdi
callq 0x2e22e
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3b125
incq 0x720(%r13)
jmp 0x46862