// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 0x44ba1
testl %eax, %eax
jne 0x523e7
movq 0x30(%rsp), %r15
testq %r15, %r15
js 0x523d1
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xb5037(%rip), %rbp     # 0x1072b0
movq 0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x44f89
testq %rax, %rax
je 0x523e7
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x441ab
testl %eax, %eax
jne 0x523e7
movl %r15d, %r15d
testq %r15, %r15
je 0x523f6
movq %r14, %rdi
callq 0x51cf6
movq %rax, %r12
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x6d309(%rip), %rsi     # 0xbf5df
movl $0x20, %edi
callq 0x800c9
leaq 0xb4571(%rip), %rax     # 0x106858
addq %r15, 0x710(%rax)
movq %r15, %r13
subq %r12, %r13
jae 0x523fc
leaq 0x741f5(%rip), %rdi     # 0xc64f6
movl $0x4, %esi
callq 0x43ba8
movq %rax, 0x10(%rsp)
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4063b
movq %r12, %rax
subq %r15, %rax
leaq (%rax,%rax,4), %rax
cmpq %r15, %rax
jbe 0x52485
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r14, %rdi
movq %rsp, %rsi
movq %r13, %rdx
callq 0x51c7b
cmpl $0x6, %eax
jne 0x52368
movq 0x8(%rsp), %rdi
callq 0x43bb7
movq %rax, (%rsp)
jmp 0x52371
movq (%rsp), %rdi
callq 0x43c0c
decq %r15
movq (%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x40537
movq %r14, %rdi
movq %r12, %rsi
callq 0x519c8
movq %r12, 0x20(%rsp)
leaq 0xb44be(%rip), %rax     # 0x106858
movq 0x368(%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x3, %r8d
callq 0x35e55
movq %r12, %rdi
callq 0x440f3
testq %r15, %r15
jne 0x52345
jmp 0x525b4
leaq 0xb4ed8(%rip), %rax     # 0x1072b0
movq 0x90(%rax), %rsi
movq %rbx, %rdi
callq 0x3fc19
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x68(%rbp), %rsi
jmp 0x523df
movq 0x40(%rbx), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rax, %r14
callq 0x525ea
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x452f3
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x6f076(%rip), %rsi     # 0xc14b6
movl $0x4, %edi
callq 0x800c9
movq 0x128(%rbp), %rdx
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rcx
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x42a6f
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x45220
incq 0x710(%r14)
jmp 0x523e7
cmpq %r15, %r12
jne 0x5248f
xorl %r15d, %r15d
jmp 0x524fc
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %rsp, %rsi
movq %r12, %rdx
callq 0x51c7b
cmpl $0x6, %eax
jne 0x524ba
movq 0x8(%rsp), %rdi
callq 0x43bb7
movq %rax, (%rsp)
jmp 0x524c3
movq (%rsp), %rdi
callq 0x43c0c
testq %r15, %r15
jne 0x524d4
movq (%rsp), %rdi
callq 0x51778
movq %rax, %r15
movq (%rsp), %rsi
movq %r15, %rdi
callq 0x51790
movq (%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x519c8
movq %rbp, %rdi
callq 0x440f3
incq %r13
jne 0x52497
movq %r14, %rdi
callq 0x43c0c
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
movq %r15, %rdx
callq 0x45107
movq %r14, %rdi
callq 0x51af7
movq %rax, %r15
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r15, %rdi
movq %rsp, %rsi
movq %r13, %rdx
callq 0x51b83
cmpl $0x6, %eax
je 0x52550
cmpl $-0x1, %eax
je 0x525a4
movq (%rsp), %rdi
callq 0x43c0c
jmp 0x5255e
movq 0x8(%rsp), %rdi
callq 0x43bb7
movq %rax, (%rsp)
movq (%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x40537
movq %r12, 0x20(%rsp)
leaq 0xb42df(%rip), %rax     # 0x106858
movq 0x368(%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x3, %r8d
callq 0x35e55
movq %r12, %rdi
callq 0x440f3
jmp 0x5252d
movq %r15, %rdi
callq 0x51b67
movq %r14, %rdi
callq 0x440f3
movq 0x10(%rsp), %rdi
callq 0x440f3
movq %rbx, %rdi
callq 0x35f2d
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x45220
leaq 0xb427a(%rip), %rax     # 0x106858
incq 0x710(%rax)
jmp 0x523e7