// File: /workspace/github_c_makefile_star2_O0/gplessis[P]dotdeb-redis/src/t_set.c
// Function: sunionDiffGenericCommand
void sunionDiffGenericCommand(client *c, robj **setkeys, int setnum,
                              robj *dstkey, int op) {
    robj **sets = zmalloc(sizeof(robj*)*setnum);
    setTypeIterator *si;
    robj *ele, *dstset = NULL;
    int j, cardinality = 0;
    int diff_algo = 1;

    for (j = 0; j < setnum; j++) {
        robj *setobj = dstkey ?
            lookupKeyWrite(c->db,setkeys[j]) :
            lookupKeyRead(c->db,setkeys[j]);
        if (!setobj) {
            sets[j] = NULL;
            continue;
        }
        if (checkType(c,setobj,OBJ_SET)) {
            zfree(sets);
            return;
        }
        sets[j] = setobj;
    }

    /* Select what DIFF algorithm to use.
     *
     * Algorithm 1 is O(N*M) where N is the size of the element first set
     * and M the total number of sets.
     *
     * Algorithm 2 is O(N) where N is the total number of elements in all
     * the sets.
     *
     * We compute what is the best bet with the current input here. */
    if (op == SET_OP_DIFF && sets[0]) {
        long long algo_one_work = 0, algo_two_work = 0;

        for (j = 0; j < setnum; j++) {
            if (sets[j] == NULL) continue;

            algo_one_work += setTypeSize(sets[0]);
            algo_two_work += setTypeSize(sets[j]);
        }

        /* Algorithm 1 has better constant times and performs less operations
         * if there are elements in common. Give it some advantage. */
        algo_one_work /= 2;
        diff_algo = (algo_one_work <= algo_two_work) ? 1 : 2;

        if (diff_algo == 1 && setnum > 1) {
            /* With algorithm 1 it is better to order the sets to subtract
             * by decreasing size, so that we are more likely to find
             * duplicated elements ASAP. */
            qsort(sets+1,setnum-1,sizeof(robj*),
                qsortCompareSetsByRevCardinality);
        }
    }

    /* We need a temp set object to store our union. If the dstkey
     * is not NULL (that is, we are inside an SUNIONSTORE operation) then
     * this set object will be the resulting object to set into the target key*/
    dstset = createIntsetObject();

    if (op == SET_OP_UNION) {
        /* Union is trivial, just add every element of every set to the
         * temporary set. */
        for (j = 0; j < setnum; j++) {
            if (!sets[j]) continue; /* non existing keys are like empty sets */

            si = setTypeInitIterator(sets[j]);
            while((ele = setTypeNextObject(si)) != NULL) {
                if (setTypeAdd(dstset,ele)) cardinality++;
                decrRefCount(ele);
            }
            setTypeReleaseIterator(si);
        }
    } else if (op == SET_OP_DIFF && sets[0] && diff_algo == 1) {
        /* DIFF Algorithm 1:
         *
         * We perform the diff by iterating all the elements of the first set,
         * and only adding it to the target set if the element does not exist
         * into all the other sets.
         *
         * This way we perform at max N*M operations, where N is the size of
         * the first set, and M the number of sets. */
        si = setTypeInitIterator(sets[0]);
        while((ele = setTypeNextObject(si)) != NULL) {
            for (j = 1; j < setnum; j++) {
                if (!sets[j]) continue; /* no key is an empty set. */
                if (sets[j] == sets[0]) break; /* same set! */
                if (setTypeIsMember(sets[j],ele)) break;
            }
            if (j == setnum) {
                /* There is no other set with this element. Add it. */
                setTypeAdd(dstset,ele);
                cardinality++;
            }
            decrRefCount(ele);
        }
        setTypeReleaseIterator(si);
    } else if (op == SET_OP_DIFF && sets[0] && diff_algo == 2) {
        /* DIFF Algorithm 2:
         *
         * Add all the elements of the first set to the auxiliary set.
         * Then remove all the elements of all the next sets from it.
         *
         * This is O(N) where N is the sum of all the elements in every
         * set. */
        for (j = 0; j < setnum; j++) {
            if (!sets[j]) continue; /* non existing keys are like empty sets */

            si = setTypeInitIterator(sets[j]);
            while((ele = setTypeNextObject(si)) != NULL) {
                if (j == 0) {
                    if (setTypeAdd(dstset,ele)) cardinality++;
                } else {
                    if (setTypeRemove(dstset,ele)) cardinality--;
                }
                decrRefCount(ele);
            }
            setTypeReleaseIterator(si);

            /* Exit if result set is empty as any additional removal
             * of elements will have no effect. */
            if (cardinality == 0) break;
        }
    }

    /* Output the content of the resulting set, if not in STORE mode */
    if (!dstkey) {
        addReplyMultiBulkLen(c,cardinality);
        si = setTypeInitIterator(dstset);
        while((ele = setTypeNextObject(si)) != NULL) {
            addReplyBulk(c,ele);
            decrRefCount(ele);
        }
        setTypeReleaseIterator(si);
        decrRefCount(dstset);
    } else {
        /* If we have a target key where to store the resulting set
         * create this key with the result set inside */
        int deleted = dbDelete(c->db,dstkey);
        if (setTypeSize(dstset) > 0) {
            dbAdd(c->db,dstkey,dstset);
            addReplyLongLong(c,setTypeSize(dstset));
            notifyKeyspaceEvent(NOTIFY_SET,
                op == SET_OP_UNION ? "sunionstore" : "sdiffstore",
                dstkey,c->db->id);
        } else {
            decrRefCount(dstset);
            addReply(c,shared.czero);
            if (deleted)
                notifyKeyspaceEvent(NOTIFY_GENERIC,"del",
                    dstkey,c->db->id);
        }
        signalModifiedKey(c->db,dstkey);
        server.dirty++;
    }
    zfree(sets);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movslq -0x14(%rbp), %rdi
shlq $0x3, %rdi
callq 0x3acd0
movq %rax, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x1, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5cc5f
cmpq $0x0, -0x20(%rbp)
je 0x5cbde
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x4a6f0
movq %rax, -0x80(%rbp)
jmp 0x5cbfb
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x4a6c0
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x5cc1c
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x5cc51
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl $0x2, %edx
callq 0x490a0
cmpl $0x0, %eax
je 0x5cc41
movq -0x30(%rbp), %rdi
callq 0x3afa0
jmp 0x5d10c
movq -0x60(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x5cbac
cmpl $0x1, -0x24(%rbp)
jne 0x5cd3c
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x5cd3c
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5ccdf
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x5cca7
jmp 0x5ccd4
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x5bf20
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5bf20
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x5cc8e
movq -0x68(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rsi
movl $0x2, %eax
movl $0x1, %ecx
cmpq %rsi, %rdx
cmovlel %ecx, %eax
movl %eax, -0x54(%rbp)
cmpl $0x1, -0x54(%rbp)
jne 0x5cd3a
cmpl $0x1, -0x14(%rbp)
jle 0x5cd3a
movq -0x30(%rbp), %rdi
addq $0x8, %rdi
movl -0x14(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
movl $0x8, %edx
leaq 0xa9b(%rip), %rcx       # 0x5d7d0
callq 0x26890
jmp 0x5cd3c
callq 0x48c10
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x5cdd6
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5cdd1
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x5cd6f
jmp 0x5cdc6
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5bbf0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5bdc0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x5cdbd
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x5b750
cmpl $0x0, %eax
je 0x5cdb2
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x48f70
jmp 0x5cd84
movq -0x38(%rbp), %rdi
callq 0x5bca0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x5cd56
jmp 0x5cf9c
cmpl $0x1, -0x24(%rbp)
jne 0x5ceb9
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x5ceb9
cmpl $0x1, -0x54(%rbp)
jne 0x5ceb9
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x5bbf0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5bdc0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x5ceab
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5ce7f
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x5ce3f
jmp 0x5ce74
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x5ce56
jmp 0x5ce7f
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x40(%rbp), %rsi
callq 0x5bb30
cmpl $0x0, %eax
je 0x5ce72
jmp 0x5ce7f
jmp 0x5ce74
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x5ce26
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x5ce9d
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x5b750
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x48f70
jmp 0x5ce08
movq -0x38(%rbp), %rdi
callq 0x5bca0
jmp 0x5cf9a
cmpl $0x1, -0x24(%rbp)
jne 0x5cf98
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x5cf98
cmpl $0x2, -0x54(%rbp)
jne 0x5cf98
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x5cf96
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x5cf02
jmp 0x5cf88
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5bbf0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5bdc0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x5cf75
cmpl $0x0, -0x4c(%rbp)
jne 0x5cf4d
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x5b750
cmpl $0x0, %eax
je 0x5cf4b
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5cf6a
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x5ba40
cmpl $0x0, %eax
je 0x5cf68
movl -0x50(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5cf6a
movq -0x40(%rbp), %rdi
callq 0x48f70
jmp 0x5cf17
movq -0x38(%rbp), %rdi
callq 0x5bca0
cmpl $0x0, -0x50(%rbp)
jne 0x5cf86
jmp 0x5cf96
jmp 0x5cf88
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x5cee2
jmp 0x5cf98
jmp 0x5cf9a
jmp 0x5cf9c
cmpq $0x0, -0x20(%rbp)
jne 0x5cfff
movq -0x8(%rbp), %rdi
movslq -0x50(%rbp), %rsi
callq 0x437f0
movq -0x48(%rbp), %rdi
callq 0x5bbf0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5bdc0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x5cfe8
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x43630
movq -0x40(%rbp), %rdi
callq 0x48f70
jmp 0x5cfbd
movq -0x38(%rbp), %rdi
callq 0x5bca0
movq -0x48(%rbp), %rdi
callq 0x48f70
jmp 0x5d103
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x4ac10
movl %eax, -0x74(%rbp)
movq -0x48(%rbp), %rdi
callq 0x5bf20
cmpq $0x0, %rax
jbe 0x5d08d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x4a7d0
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rdi
callq 0x5bf20
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0x43780
movl -0x24(%rbp), %ecx
leaq 0xda582(%rip), %rsi     # 0x1375e6
leaq 0xda569(%rip), %rax     # 0x1375d4
cmpl $0x0, %ecx
cmoveq %rax, %rsi
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x20, %edi
callq 0x9ce20
jmp 0x5d0d2
movq -0x48(%rbp), %rdi
callq 0x48f70
movq -0x8(%rbp), %rdi
leaq 0x11bb9f(%rip), %rax    # 0x178c40
movq 0x20(%rax), %rsi
callq 0x42cc0
cmpl $0x0, -0x74(%rbp)
je 0x5d0d0
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
movl $0x4, %edi
leaq 0xdc3cd(%rip), %rsi     # 0x139498
callq 0x9ce20
jmp 0x5d0d2
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x4aa60
leaq 0x11b0ee(%rip), %rax    # 0x1781d8
movq 0x720(%rax), %rcx
addq $0x1, %rcx
leaq 0x11b0dc(%rip), %rax    # 0x1781d8
movq %rcx, 0x720(%rax)
movq -0x30(%rbp), %rdi
callq 0x3afa0
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)