// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%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 0x5c6f3
jmp 0x5cb4c
cmpq $0x0, -0x10(%rbp)
jl 0x5c706
movq -0x10(%rbp), %rax
movl %eax, %eax
movq %rax, -0x18(%rbp)
jmp 0x5c722
movq -0x8(%rbp), %rdi
leaq 0x11c52f(%rip), %rax    # 0x178c40
movq 0x90(%rax), %rsi
callq 0x42cc0
jmp 0x5cb4c
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x11c507(%rip), %rax    # 0x178c40
movq 0x68(%rax), %rdx
callq 0x4a730
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x5c763
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x2, %edx
callq 0x490a0
cmpl $0x0, %eax
je 0x5c768
jmp 0x5cb4c
cmpq $0x0, -0x18(%rbp)
jne 0x5c788
movq -0x8(%rbp), %rdi
leaq 0x11c4c6(%rip), %rax    # 0x178c40
movq 0x68(%rax), %rsi
callq 0x42cc0
jmp 0x5cb4c
movq -0x28(%rbp), %rdi
callq 0x5bf20
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x20, %edi
leaq 0xdade7(%rip), %rsi     # 0x13759f
callq 0x9ce20
movq -0x18(%rbp), %rcx
leaq 0x11ba10(%rip), %rax    # 0x1781d8
addq 0x720(%rax), %rcx
leaq 0x11ba02(%rip), %rax    # 0x1781d8
movq %rcx, 0x720(%rax)
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x5c8b5
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
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x4ac10
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x4, %edi
leaq 0xdcc50(%rip), %rsi     # 0x139498
callq 0x9ce20
movq -0x8(%rbp), %rdi
leaq 0x11c3e8(%rip), %rax    # 0x178c40
movq 0x128(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rcx
movl $0x2, %esi
movb $0x0, %al
callq 0x46840
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x4aa60
leaq 0x11b941(%rip), %rax    # 0x1781d8
movq 0x720(%rax), %rcx
addq $0x1, %rcx
leaq 0x11b92f(%rip), %rax    # 0x1781d8
movq %rcx, 0x720(%rax)
jmp 0x5cb4c
leaq 0xe1322(%rip), %rdi     # 0x13dbde
movl $0x4, %esi
callq 0x486a0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x437f0
movq -0x20(%rbp), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
imulq $0x5, -0x60(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x5c9af
jmp 0x5c904
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x18(%rbp)
cmpq $0x0, %rax
je 0x5c9aa
movq -0x28(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x5be60
movl %eax, -0x4c(%rbp)
cmpl $0x6, -0x4c(%rbp)
jne 0x5c946
movq -0x58(%rbp), %rdi
callq 0x486f0
movq %rax, -0x48(%rbp)
jmp 0x5c94f
movq -0x48(%rbp), %rdi
callq 0x487b0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x43630
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5ba40
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq 0x11b860(%rip), %rax    # 0x1781d8
movq 0x368(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %esi
leaq -0x40(%rbp), %rdx
movl $0x3, %r8d
movl %r8d, %ecx
callq 0x33bf0
movq -0x48(%rbp), %rdi
callq 0x48f70
jmp 0x5c904
jmp 0x5cb01
movq $0x0, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x60(%rbp)
cmpq $0x0, %rax
je 0x5ca37
movq -0x28(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x5be60
movl %eax, -0x4c(%rbp)
cmpl $0x6, -0x4c(%rbp)
jne 0x5c9f5
movq -0x58(%rbp), %rdi
callq 0x486f0
movq %rax, -0x48(%rbp)
jmp 0x5c9fe
movq -0x48(%rbp), %rdi
callq 0x487b0
cmpq $0x0, -0x68(%rbp)
jne 0x5ca12
movq -0x48(%rbp), %rdi
callq 0x5b710
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5b750
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x5ba40
movq -0x48(%rbp), %rdi
callq 0x48f70
jmp 0x5c9b7
movq -0x28(%rbp), %rdi
callq 0x487b0
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rsi
movq -0x68(%rbp), %rdx
callq 0x4a8e0
movq -0x28(%rbp), %rdi
callq 0x5bbf0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x5bce0
movl %eax, -0x4c(%rbp)
cmpl $-0x1, %eax
je 0x5caef
cmpl $0x6, -0x4c(%rbp)
jne 0x5ca98
movq -0x58(%rbp), %rdi
callq 0x486f0
movq %rax, -0x48(%rbp)
jmp 0x5caa1
movq -0x48(%rbp), %rdi
callq 0x487b0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x43630
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq 0x11b71b(%rip), %rax    # 0x1781d8
movq 0x368(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %esi
leaq -0x40(%rbp), %rdx
movl $0x3, %r8d
movl %r8d, %ecx
callq 0x33bf0
movq -0x48(%rbp), %rdi
callq 0x48f70
jmp 0x5ca6a
movq -0x70(%rbp), %rdi
callq 0x5bca0
movq -0x28(%rbp), %rdi
callq 0x48f70
movq -0x40(%rbp), %rdi
callq 0x48f70
movq -0x8(%rbp), %rdi
callq 0x33cf0
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x4aa60
leaq 0x11b6a5(%rip), %rax    # 0x1781d8
movq 0x720(%rax), %rcx
addq $0x1, %rcx
leaq 0x11b693(%rip), %rax    # 0x1781d8
movq %rcx, 0x720(%rax)
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)