// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, 0x30(%rsp)
movl %edx, %r13d
movq %rsi, %r12
movq %rdi, 0x20(%rsp)
movslq %edx, %r14
leaq (,%r14,8), %rdi
callq 0x327b9
movq %rax, %rbx
xorl %r15d, %r15d
testl %r14d, %r14d
movl $0x0, %eax
movq %r13, 0x18(%rsp)
cmovgl %r13d, %eax
movq %rax, 0x10(%rsp)
pushq $0x2
popq %rbp
cmpq %r15, 0x10(%rsp)
je 0x46aff
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%r12,%r15,8), %rsi
cmpq $0x0, 0x30(%rsp)
je 0x46ace
callq 0x3aec2
jmp 0x46ad3
callq 0x3aebb
movq %rax, %r13
testq %rax, %rax
je 0x46af3
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x3a311
testl %eax, %eax
je 0x46af6
jmp 0x46e37
xorl %r13d, %r13d
movq %r13, (%rbx,%r15,8)
incq %r15
jmp 0x46aab
pushq $0x1
popq %rbp
movl 0xc(%rsp), %r15d
cmpl $0x1, %r15d
jne 0x46b8b
cmpq $0x0, (%rbx)
je 0x46b8b
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpq %r13, 0x10(%rsp)
je 0x46b46
cmpq $0x0, (%rbx,%r13,8)
je 0x46b41
movq (%rbx), %rdi
callq 0x461ae
addq %rax, %r12
movq (%rbx,%r13,8), %rdi
callq 0x461ae
addq %rax, %r15
incq %r13
jmp 0x46b1c
pushq $0x2
popq %rcx
movq %r12, %rax
cqto
idivq %rcx
xorl %ebp, %ebp
cmpq %r15, %rax
setg %bpl
incl %ebp
cmpl $0x2, 0x18(%rsp)
jl 0x46b86
cmpq %r15, %rax
jg 0x46b86
movq %rbx, %rdi
addq $0x8, %rdi
movq 0x18(%rsp), %rax
leal -0x1(%rax), %esi
leaq 0x82a(%rip), %rcx       # 0x473a8
pushq $0x8
popq %rdx
callq 0x26890
movl 0xc(%rsp), %r15d
callq 0x3a0a1
movq %rax, %r12
testl %r15d, %r15d
je 0x46c42
movl %r15d, %eax
xorl %r15d, %r15d
cmpl $0x1, %eax
jne 0x46d1e
movq (%rbx), %rdi
testq %rdi, %rdi
sete %al
cmpl $0x1, %ebp
setne %cl
orb %al, %cl
jne 0x46c99
callq 0x45fc4
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovgel %eax, %ecx
movl %ecx, 0x2c(%rsp)
xorl %r15d, %r15d
movq 0x10(%rsp), %rdi
callq 0x460cf
testq %rax, %rax
je 0x46d14
movq %rax, %r13
pushq $0x1
popq %rbp
cmpq %r14, %rbp
jge 0x46c1e
movq (%rbx,%rbp,8), %rdi
testq %rdi, %rdi
je 0x46c19
cmpq (%rbx), %rdi
je 0x46c24
movq %r13, %rsi
callq 0x45f47
testl %eax, %eax
jne 0x46c24
incq %rbp
jmp 0x46bfa
movl 0x2c(%rsp), %eax
movl %eax, %ebp
cmpl 0x18(%rsp), %ebp
jne 0x46c38
movq %r12, %rdi
movq %r13, %rsi
callq 0x45c80
incl %r15d
movq %r13, %rdi
callq 0x3a265
jmp 0x46be1
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq 0x10(%rsp), %r14
je 0x46d1e
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x46c94
callq 0x45fc4
movq %rax, %r13
movq %r13, %rdi
callq 0x460cf
testq %rax, %rax
je 0x46c8c
movq %rax, %rbp
movq %r12, %rdi
movq %rax, %rsi
callq 0x45c80
addl %eax, %r15d
movq %rbp, %rdi
callq 0x3a265
jmp 0x46c64
movq %r13, %rdi
callq 0x4602d
incq %r14
jmp 0x46c48
testq %rdi, %rdi
sete %al
cmpl $0x2, %ebp
setne %cl
orb %al, %cl
jne 0x46d1e
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq 0x10(%rsp), %r14
je 0x46d1e
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x46d0f
callq 0x45fc4
movq %rax, %r13
movq %r13, %rdi
callq 0x460cf
testq %rax, %rax
je 0x46cfe
movq %rax, %rbp
movq %r12, %rdi
movq %rax, %rsi
testq %r14, %r14
je 0x46cec
callq 0x45ea1
subl %eax, %r15d
jmp 0x46cf4
callq 0x45c80
addl %eax, %r15d
movq %rbp, %rdi
callq 0x3a265
jmp 0x46cc7
movq %r13, %rdi
callq 0x4602d
testl %r15d, %r15d
je 0x46e4d
incq %r14
jmp 0x46caf
movq 0x10(%rsp), %rdi
callq 0x4602d
movq 0x30(%rsp), %r13
testq %r13, %r13
je 0x46d93
movq 0x20(%rsp), %r14
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0x3b1df
movl %eax, %ebp
movq %r12, %rdi
callq 0x461ae
testq %rax, %rax
je 0x46de5
movq 0x10(%r14), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3af45
movq %r12, %rdi
callq 0x461ae
movq %r14, %rdi
movq %rax, %rsi
callq 0x370a6
cmpl $0x0, 0xc(%rsp)
leaq 0x677da(%rip), %rax     # 0xae554
leaq 0x677e5(%rip), %rsi     # 0xae566
cmoveq %rax, %rsi
movq 0x10(%r14), %rax
movl 0x30(%rax), %ecx
pushq $0x20
jmp 0x46e14
movslq %r15d, %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x370d9
movq %r12, %rdi
callq 0x45fc4
movq %rax, %r15
movq %r15, %rdi
callq 0x460cf
testq %rax, %rax
je 0x46dd3
movq %rax, %r13
movq %r14, %rdi
movq %rax, %rsi
callq 0x36fd9
movq %r13, %rdi
callq 0x3a265
jmp 0x46dae
movq %r15, %rdi
callq 0x4602d
movq %r12, %rdi
callq 0x3a265
jmp 0x46e37
movq %r12, %rdi
callq 0x3a265
leaq 0xb351c(%rip), %rax     # 0xfa310
movq 0x20(%rax), %rsi
movq %r14, %rdi
callq 0x36987
testl %ebp, %ebp
je 0x46e1d
movq 0x10(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x69606(%rip), %rsi     # 0xb0418
pushq $0x4
popq %rdi
movq %r13, %rdx
callq 0x6fb58
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0x3b125
leaq 0xb2a78(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x328f9
xorl %r15d, %r15d
jmp 0x46d1e