// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %r8d, %ebp
movq %rcx, 0x20(%rsp)
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movslq %edx, %r14
leaq (,%r14,8), %rdi
callq 0x382d1
movq %rax, %rbx
testl %r14d, %r14d
jle 0x4ef48
movl 0x8(%rsp), %r14d
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%r15,%r13,8), %rsi
cmpq $0x0, 0x20(%rsp)
je 0x4ef11
callq 0x41ad6
jmp 0x4ef16
callq 0x41acf
movq %rax, %r12
testq %rax, %rax
je 0x4ef39
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0x40d47
testl %eax, %eax
je 0x4ef3c
jmp 0x4f313
xorl %r12d, %r12d
movq %r12, (%rbx,%r13,8)
incq %r13
cmpq %r13, %r14
jne 0x4eef5
cmpl $0x1, %ebp
jne 0x4f094
movq 0x8(%rsp), %r13
testl %r13d, %r13d
setle %al
cmpq $0x0, (%rbx)
sete %cl
movl $0x1, %ebp
orb %al, %cl
jne 0x4efe2
movl %r13d, %r15d
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpq $0x0, (%rbx,%r13,8)
je 0x4ef96
movq (%rbx), %rdi
callq 0x4e5b5
addq %rax, %r12
movq (%rbx,%r13,8), %rdi
callq 0x4e5b5
addq %rax, %r14
incq %r13
cmpq %r13, %r15
jne 0x4ef78
movq %r12, %rax
shrq $0x3f, %rax
addq %r12, %rax
sarq %rax
xorl %ebp, %ebp
cmpq %r14, %rax
setg %bpl
incl %ebp
movq 0x8(%rsp), %r13
cmpl $0x2, %r13d
jl 0x4efe2
cmpq %r14, %rax
jg 0x4efe2
movq %rbx, %rdi
addq $0x8, %rdi
leal -0x1(%r13), %esi
leaq 0x8af(%rip), %rcx       # 0x4f887
movl $0x8, %edx
callq 0x26890
callq 0x40a6c
movq %rax, %r12
movq (%rbx), %rdi
testq %rdi, %rdi
sete %al
cmpl $0x1, %ebp
setne %cl
orb %al, %cl
jne 0x4f139
callq 0x4e3b6
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x4e4cf
testq %rax, %rax
je 0x4f1df
movq %rax, %r15
movl %r13d, %r14d
xorl %ebp, %ebp
movl $0x1, %r13d
movq 0x8(%rsp), %rax
cmpl $0x2, %eax
jl 0x4f063
movl $0x1, %r13d
movq (%rbx,%r13,8), %rdi
testq %rdi, %rdi
je 0x4f054
cmpq (%rbx), %rdi
je 0x4f05e
movq %r15, %rsi
callq 0x4e335
testl %eax, %eax
jne 0x4f05e
incq %r13
cmpq %r13, %r14
jne 0x4f03a
jmp 0x4f068
movq 0x8(%rsp), %rax
cmpl %eax, %r13d
jne 0x4f075
movq %r12, %rdi
movq %r15, %rsi
callq 0x4e054
incl %ebp
movq %r15, %rdi
callq 0x40c8f
movq 0x10(%rsp), %rdi
callq 0x4e4cf
movq %rax, %r15
testq %rax, %rax
jne 0x4f024
jmp 0x4f1e1
movq 0x8(%rsp), %r15
testl %r15d, %r15d
setle %r14b
callq 0x40a6c
movq %rax, %r12
testl %ebp, %ebp
setne %al
leaq 0x6d5b0(%rip), %rdx     # 0xbc664
leaq 0x6d5bb(%rip), %rcx     # 0xbc676
cmoveq %rdx, %rcx
orb %r14b, %al
jne 0x4f1d6
movl %r15d, %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x4f123
callq 0x4e3b6
movq %rax, %r15
movq %rax, %rdi
callq 0x4e4cf
testq %rax, %rax
je 0x4f11b
movq %rax, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x4e054
addl %eax, %ebp
movq %r13, %rdi
callq 0x40c8f
movq %r15, %rdi
callq 0x4e4cf
movq %rax, %r13
testq %rax, %rax
jne 0x4f0f6
movq %r15, %rdi
callq 0x4e426
incq %r14
cmpq 0x8(%rsp), %r14
jne 0x4f0d5
leaq 0x6d530(%rip), %rax     # 0xbc664
jmp 0x4f1f2
testq %rdi, %rdi
sete %al
testl %r13d, %r13d
setle %cl
cmpl $0x2, %ebp
setne %dl
leaq 0x6d524(%rip), %rsi     # 0xbc676
movq %rsi, 0x10(%rsp)
orb %al, %cl
orb %dl, %cl
jne 0x4f1db
movl %r13d, %eax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx,%r14,8), %rdi
testq %rdi, %rdi
je 0x4f1ca
callq 0x4e3b6
movq %rax, %r15
movq %rax, %rdi
callq 0x4e4cf
testq %rax, %rax
je 0x4f1be
movq %rax, %r13
movq %r12, %rdi
movq %r13, %rsi
testq %r14, %r14
je 0x4f19f
callq 0x4e287
subl %eax, %ebp
jmp 0x4f1a6
callq 0x4e054
addl %eax, %ebp
movq %r13, %rdi
callq 0x40c8f
movq %r15, %rdi
callq 0x4e4cf
movq %rax, %r13
testq %rax, %rax
jne 0x4f18b
movq %r15, %rdi
callq 0x4e426
testl %ebp, %ebp
je 0x4f1db
incq %r14
cmpq 0x8(%rsp), %r14
jne 0x4f16a
jmp 0x4f1f7
movq %rcx, 0x10(%rsp)
xorl %ebp, %ebp
jmp 0x4f1f7
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
callq 0x4e426
leaq 0x6d484(%rip), %rax     # 0xbc676
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %r13
testq %r13, %r13
je 0x4f25e
movq 0x18(%rsp), %r14
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0x41e5f
movl %eax, %r15d
movq %r12, %rdi
callq 0x4e5b5
testq %rax, %rax
je 0x4f2be
movq 0x10(%r14), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x41b77
movq %r12, %rdi
callq 0x4e5b5
movq %r14, %rdi
movq %rax, %rsi
callq 0x3d23f
movq 0x10(%r14), %rax
movl 0x30(%rax), %ecx
movl $0x20, %edi
movq 0x10(%rsp), %rsi
jmp 0x4f2f1
movslq %ebp, %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x3d274
movq %r12, %rdi
callq 0x4e3b6
movq %rax, %r15
movq %rax, %rdi
callq 0x4e4cf
testq %rax, %rax
je 0x4f2ac
movq %rax, %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x3d170
movq %r13, %rdi
callq 0x40c8f
movq %r15, %rdi
callq 0x4e4cf
movq %rax, %r13
testq %rax, %rax
jne 0x4f289
movq %r15, %rdi
callq 0x4e426
movq %r12, %rdi
callq 0x40c8f
jmp 0x4f313
movq %r12, %rdi
callq 0x40c8f
leaq 0xb3043(%rip), %rax     # 0x102310
movq 0x20(%rax), %rsi
movq %r14, %rdi
callq 0x3c854
testl %r15d, %r15d
je 0x4f2f9
movq 0x10(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x6f23c(%rip), %rsi     # 0xbe528
movl $0x4, %edi
movq %r13, %rdx
callq 0x7c665
movq 0x10(%r14), %rdi
movq %r13, %rsi
callq 0x41d90
leaq 0xb259c(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x38413