// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/t_set.c
// Function: sinterGenericCommand
void sinterGenericCommand(client *c, robj **setkeys,
                          unsigned long setnum, robj *dstkey) {
    robj **sets = zmalloc(sizeof(robj*)*setnum);
    setTypeIterator *si;
    robj *eleobj, *dstset = NULL;
    int64_t intobj;
    void *replylen = NULL;
    unsigned long j, cardinality = 0;
    int encoding;

    for (j = 0; j < setnum; j++) {
        robj *setobj = dstkey ?
            lookupKeyWrite(c->db,setkeys[j]) :
            lookupKeyRead(c->db,setkeys[j]);
        if (!setobj) {
            zfree(sets);
            if (dstkey) {
                if (dbDelete(c->db,dstkey)) {
                    signalModifiedKey(c->db,dstkey);
                    server.dirty++;
                }
                addReply(c,shared.czero);
            } else {
                addReply(c,shared.emptymultibulk);
            }
            return;
        }
        if (checkType(c,setobj,OBJ_SET)) {
            zfree(sets);
            return;
        }
        sets[j] = setobj;
    }
    /* Sort sets from the smallest to largest, this will improve our
     * algorithm's performance */
    qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);

    /* The first thing we should output is the total number of elements...
     * since this is a multi-bulk write, but at this stage we don't know
     * the intersection set size, so we use a trick, append an empty object
     * to the output list and save the pointer to later modify it with the
     * right length */
    if (!dstkey) {
        replylen = addDeferredMultiBulkLength(c);
    } else {
        /* If we have a target key where to store the resulting set
         * create this key with an empty set inside */
        dstset = createIntsetObject();
    }

    /* Iterate all the elements of the first (smallest) set, and test
     * the element against all the other sets, if at least one set does
     * not include the element it is discarded */
    si = setTypeInitIterator(sets[0]);
    while((encoding = setTypeNext(si,&eleobj,&intobj)) != -1) {
        for (j = 1; j < setnum; j++) {
            if (sets[j] == sets[0]) continue;
            if (encoding == OBJ_ENCODING_INTSET) {
                /* intset with intset is simple... and fast */
                if (sets[j]->encoding == OBJ_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,intobj))
                {
                    break;
                /* in order to compare an integer with an object we
                 * have to use the generic function, creating an object
                 * for this */
                } else if (sets[j]->encoding == OBJ_ENCODING_HT) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    if (!setTypeIsMember(sets[j],eleobj)) {
                        decrRefCount(eleobj);
                        break;
                    }
                    decrRefCount(eleobj);
                }
            } else if (encoding == OBJ_ENCODING_HT) {
                /* Optimization... if the source object is integer
                 * encoded AND the target set is an intset, we can get
                 * a much faster path. */
                if (eleobj->encoding == OBJ_ENCODING_INT &&
                    sets[j]->encoding == OBJ_ENCODING_INTSET &&
                    !intsetFind((intset*)sets[j]->ptr,(long)eleobj->ptr))
                {
                    break;
                /* else... object to object check is easy as we use the
                 * type agnostic API here. */
                } else if (!setTypeIsMember(sets[j],eleobj)) {
                    break;
                }
            }
        }

        /* Only take action when all sets contain the member */
        if (j == setnum) {
            if (!dstkey) {
                if (encoding == OBJ_ENCODING_HT)
                    addReplyBulk(c,eleobj);
                else
                    addReplyBulkLongLong(c,intobj);
                cardinality++;
            } else {
                if (encoding == OBJ_ENCODING_INTSET) {
                    eleobj = createStringObjectFromLongLong(intobj);
                    setTypeAdd(dstset,eleobj);
                    decrRefCount(eleobj);
                } else {
                    setTypeAdd(dstset,eleobj);
                }
            }
        }
    }
    setTypeReleaseIterator(si);

    if (dstkey) {
        /* Store the resulting set into the target, if the intersection
         * is not an empty set. */
        int deleted = dbDelete(c->db,dstkey);
        if (setTypeSize(dstset) > 0) {
            dbAdd(c->db,dstkey,dstset);
            addReplyLongLong(c,setTypeSize(dstset));
            notifyKeyspaceEvent(NOTIFY_SET,"sinterstore",
                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++;
    } else {
        setDeferredMultiBulkLength(c,replylen,cardinality);
    }
    zfree(sets);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r13
leaq (,%rdx,8), %rdi
callq 0x327b9
movq %rax, %r15
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x4745c
movq %r13, %rbp
movq 0x10(%r13), %rdi
movq (%r14,%rbx,8), %rsi
cmpq $0x0, 0x8(%rsp)
je 0x4742d
callq 0x3aec2
jmp 0x47432
callq 0x3aebb
movq %rax, %r13
testq %rax, %rax
je 0x4748e
movq %rbp, %rdi
movq %r13, %rsi
pushq $0x2
popq %rdx
callq 0x3a311
testl %eax, %eax
jne 0x474de
movq %r13, (%r15,%rbx,8)
incq %rbx
movq %rbp, %r13
jmp 0x4740e
leaq -0xe3(%rip), %rcx       # 0x47380
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x26890
cmpq $0x0, 0x8(%rsp)
je 0x474f4
movq $0x0, 0x38(%rsp)
callq 0x3a0a1
movq %rax, 0x20(%rsp)
jmp 0x4750a
movq %r15, %rdi
callq 0x328f9
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x47733
movq 0x10(%rbp), %rdi
movq %rbx, %rsi
callq 0x3b1df
testl %eax, %eax
je 0x474ce
movq 0x10(%rbp), %rdi
movq %rbx, %rsi
callq 0x3b125
leaq 0xb23e1(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
leaq 0xb2e3b(%rip), %rax     # 0xfa310
movq 0x20(%rax), %rsi
jmp 0x4773e
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x328f9
movq %r13, %rdi
callq 0x36dad
movq %rax, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq (%r15), %rdi
callq 0x45fc4
movq %rax, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
movl $0xf0, %ebp
movq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x46049
cmpl $-0x1, %eax
je 0x476b0
movl %eax, %r14d
movq 0x30(%rsp), %r13
pushq $0x1
popq %rbx
cmpq %r12, %rbx
jae 0x47611
movq (%r15,%rbx,8), %rdi
cmpq (%r15), %rdi
je 0x47609
cmpl $0x2, %r14d
je 0x475d4
cmpl $0x6, %r14d
jne 0x47609
movl (%rdi), %eax
movl %eax, %ecx
andl $0xf0, %ecx
cmpl $0x60, %ecx
jne 0x4759f
movq 0x8(%rdi), %rdi
movq 0x28(%rsp), %rsi
callq 0x5a95d
testb %al, %al
je 0x47611
movq (%r15,%rbx,8), %rax
movl (%rax), %eax
andl $0xf0, %eax
cmpl $0x20, %eax
jne 0x47609
movq 0x28(%rsp), %rdi
callq 0x39e38
movq %rax, %r13
movq (%r15,%rbx,8), %rdi
movq %rax, %rsi
callq 0x45f47
testl %eax, %eax
je 0x4766b
movq %r13, %rdi
callq 0x3a265
jmp 0x47609
movl (%r13), %eax
andl %ebp, %eax
cmpl $0x10, %eax
jne 0x475fd
movl (%rdi), %eax
andl %ebp, %eax
cmpl $0x60, %eax
jne 0x475fd
movq 0x8(%rdi), %rdi
movq 0x8(%r13), %rsi
callq 0x5a95d
testb %al, %al
je 0x47611
movq (%r15,%rbx,8), %rdi
movq %r13, %rsi
callq 0x45f47
testl %eax, %eax
je 0x47611
incq %rbx
jmp 0x47552
movq %r13, 0x30(%rsp)
cmpq %rbx, %r12
jne 0x4752a
cmpq $0x0, 0x8(%rsp)
je 0x4767a
cmpl $0x6, %r14d
jne 0x47659
movq 0x28(%rsp), %rdi
callq 0x39e38
movq %rax, %r14
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x45c80
movq %r14, %rdi
callq 0x3a265
jmp 0x4752a
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x45c80
jmp 0x4752a
movq %r13, 0x30(%rsp)
movq %r13, %rdi
callq 0x3a265
jmp 0x47616
cmpl $0x2, %r14d
jne 0x4768f
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x36fd9
jmp 0x4769e
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x37206
movq 0x10(%rsp), %rax
incq %rax
movq %rax, 0x10(%rsp)
jmp 0x4752a
movq 0x40(%rsp), %rdi
callq 0x4602d
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x4771d
movq 0x18(%rsp), %rbx
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x3b1df
movl %eax, %ebp
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x461ae
testq %rax, %rax
je 0x47754
movq 0x10(%rbx), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3af45
movq %r12, %rdi
callq 0x461ae
movq %rbx, %rdi
movq %rax, %rsi
callq 0x370a6
movq 0x10(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x66e28(%rip), %rsi     # 0xae541
pushq $0x20
jmp 0x47783
movq 0x18(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x36de9
jmp 0x477a6
leaq 0xb2bd6(%rip), %rax     # 0xfa310
movq 0x68(%rax), %rsi
movq %rbp, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x36987
movq %r12, %rdi
callq 0x3a265
leaq 0xb2bad(%rip), %rax     # 0xfa310
movq 0x20(%rax), %rsi
movq %rbx, %rdi
callq 0x36987
testl %ebp, %ebp
je 0x4778c
movq 0x10(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x68c97(%rip), %rsi     # 0xb0418
pushq $0x4
popq %rdi
movq %r14, %rdx
callq 0x6fb58
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x3b125
leaq 0xb2109(%rip), %rax     # 0xf98a8
incq 0x720(%rax)
movq %r15, %rdi
callq 0x328f9
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq