// File: /workspace/github_c_makefile_star2_O3/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 0x41718
testl %eax, %eax
jne 0x4ecb3
movq 0x30(%rsp), %r15
testq %r15, %r15
js 0x4ec9d
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0xb37ca(%rip), %rbp     # 0x102310
movq 0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x41af9
testq %rax, %rax
je 0x4ecb3
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x40d47
testl %eax, %eax
jne 0x4ecb3
movl %r15d, %r15d
testq %r15, %r15
je 0x4ecc2
movq %r14, %rdi
callq 0x4e5b5
movq %rax, %r12
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x6da8c(%rip), %rsi     # 0xbc62f
movl $0x20, %edi
callq 0x7c665
leaq 0xb2cf4(%rip), %rax     # 0x1018a8
addq %r15, 0x720(%rax)
movq %r15, %r13
subq %r12, %r13
jae 0x4ecc8
leaq 0x749a8(%rip), %rdi     # 0xc3576
movl $0x4, %esi
callq 0x40744
movq %rax, 0x10(%rsp)
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d274
subq %r15, %r12
leaq (%r12,%r12,4), %rax
cmpq %r15, %rax
jbe 0x4ed51
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r14, %rdi
movq %rsp, %rsi
movq %r13, %rdx
callq 0x4e53a
cmpl $0x6, %eax
jne 0x4ec35
movq 0x8(%rsp), %rdi
callq 0x40753
movq %rax, %r12
movq %rax, (%rsp)
jmp 0x4ec41
movq (%rsp), %r12
movq %r12, %rdi
callq 0x407a8
decq %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3d170
movq %r14, %rdi
movq %r12, %rsi
callq 0x4e287
movq %r12, 0x20(%rsp)
leaq 0xb2c42(%rip), %rax     # 0x1018a8
movq 0x368(%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x3, %r8d
callq 0x32dcf
movq %r12, %rdi
callq 0x40c8f
testq %r15, %r15
jne 0x4ec0f
jmp 0x4ee7c
leaq 0xb366c(%rip), %rax     # 0x102310
movq 0x90(%rax), %rsi
movq %rbx, %rdi
callq 0x3c854
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x68(%rbp), %rsi
jmp 0x4ecab
movq 0x40(%rbx), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rax, %r14
callq 0x4eeb2
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x41e5f
movq 0x10(%rbx), %rax
movq 0x40(%rbx), %rcx
movq 0x8(%rcx), %rdx
movl 0x30(%rax), %ecx
leaq 0x6f81c(%rip), %rsi     # 0xbe528
movl $0x4, %edi
callq 0x7c665
movq 0x128(%rbp), %rdx
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rcx
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x3f61d
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x41d90
incq 0x720(%r14)
jmp 0x4ecb3
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq %r14, %rdi
movq %rsp, %rsi
movq %r12, %rdx
callq 0x4e53a
cmpl $0x6, %eax
jne 0x4ed7f
movq 0x8(%rsp), %rdi
callq 0x40753
movq %rax, %rbp
movq %rax, (%rsp)
jmp 0x4ed8b
movq (%rsp), %rbp
movq %rbp, %rdi
callq 0x407a8
testq %r15, %r15
jne 0x4ed9b
movq %rbp, %rdi
callq 0x4e03c
movq %rax, %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4e054
movq (%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4e287
movq %rbp, %rdi
callq 0x40c8f
incq %r13
jne 0x4ed59
movq %r14, %rdi
callq 0x407a8
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
movq %r15, %rdx
callq 0x41c77
movq %r14, %rdi
callq 0x4e3b6
movq %rax, %r15
leaq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r15, %rdi
movq %rsp, %rsi
movq %r13, %rdx
callq 0x4e442
cmpl $0x6, %eax
je 0x4ee19
cmpl $-0x1, %eax
je 0x4ee6c
movq (%rsp), %r12
movq %r12, %rdi
callq 0x407a8
jmp 0x4ee2a
movq 0x8(%rsp), %rdi
callq 0x40753
movq %rax, %r12
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3d170
movq %r12, 0x20(%rsp)
leaq 0xb2a67(%rip), %rax     # 0x1018a8
movq 0x368(%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
movq %rbp, %rdx
movl $0x3, %ecx
movl $0x3, %r8d
callq 0x32dcf
movq %r12, %rdi
callq 0x40c8f
jmp 0x4edf3
movq %r15, %rdi
callq 0x4e426
movq %r14, %rdi
callq 0x40c8f
movq 0x10(%rsp), %rdi
callq 0x40c8f
movq %rbx, %rdi
callq 0x32ea2
movq 0x10(%rbx), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x41d90
leaq 0xb2a02(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
jmp 0x4ecb3