// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
leaq (,%rdx,8), %rdi
callq 0x382d1
movq %rax, %r15
testq %rbp, %rbp
je 0x4f94f
xorl %ebx, %ebx
movq 0x10(%r12), %rdi
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %r14
testq %r13, %r13
je 0x4f917
callq 0x41ad6
testq %rax, %rax
jne 0x4f925
jmp 0x4fc63
callq 0x41acf
testq %rax, %rax
je 0x4fca4
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
movl $0x2, %edx
callq 0x40d47
testl %eax, %eax
jne 0x4fbe0
movq %r13, (%r15,%rbx,8)
incq %rbx
cmpq %rbx, %rbp
movq %r14, %r13
jne 0x4f8f2
leaq -0xf7(%rip), %rcx       # 0x4f85f
movl $0x8, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x26890
testq %r13, %r13
movq %r13, 0x30(%rsp)
je 0x4f985
movq $0x0, 0x28(%rsp)
callq 0x40a6c
movq %rax, 0x10(%rsp)
jmp 0x4f99b
movq %r12, %rdi
callq 0x3cee9
movq %rax, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
movq (%r15), %rdi
callq 0x4e3b6
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0x4e442
cmpl $-0x1, %eax
je 0x4fb55
movl %eax, %r13d
movq $0x0, 0x20(%rsp)
movq %r12, %rbx
movl $0x1, %r12d
movq 0x8(%rsp), %r14
cmpq $0x2, %rbp
jb 0x4faa8
movq (%r15,%r12,8), %rdi
cmpq (%r15), %rdi
je 0x4fa99
cmpl $0x2, %r13d
je 0x4fa60
cmpl $0x6, %r13d
jne 0x4fa99
movl (%rdi), %eax
movl %eax, %ecx
andl $0xf0, %ecx
cmpl $0x60, %ecx
jne 0x4fa2c
movq 0x8(%rdi), %rdi
movq (%rsp), %rsi
callq 0x64dc9
testb %al, %al
je 0x4faa8
movq (%r15,%r12,8), %rax
movl (%rax), %eax
andl $0xf0, %eax
cmpl $0x20, %eax
jne 0x4fa99
movq (%rsp), %rdi
callq 0x40753
movq %rax, %r14
movq (%r15,%r12,8), %rdi
movq %rax, %rsi
callq 0x4e335
testl %eax, %eax
je 0x4fb43
movq %r14, %rdi
callq 0x40c8f
jmp 0x4fa99
movl (%r14), %eax
movl $0xf0, %ecx
andl %ecx, %eax
cmpl $0x10, %eax
jne 0x4fa8d
movl (%rdi), %eax
andl %ecx, %eax
cmpl $0x60, %eax
jne 0x4fa8d
movq 0x8(%rdi), %rdi
movq 0x8(%r14), %rsi
callq 0x64dc9
testb %al, %al
je 0x4faa8
movq (%r15,%r12,8), %rdi
movq %r14, %rsi
callq 0x4e335
testl %eax, %eax
je 0x4faa8
incq %r12
cmpq %r12, %rbp
jne 0x4f9e5
movq %rbp, %r12
movq %r14, 0x8(%rsp)
cmpq %rbp, %r12
jne 0x4faeb
cmpq $0x0, 0x30(%rsp)
movq %rbx, %r12
je 0x4faf0
cmpl $0x6, %r13d
jne 0x4fb03
movq (%rsp), %rdi
callq 0x40753
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x4e054
movq %r14, %rdi
callq 0x40c8f
jmp 0x4fb23
movq %rbx, %r12
jmp 0x4fb23
cmpl $0x2, %r13d
jne 0x4fb12
movq %r12, %rdi
movq %r14, %rsi
callq 0x3d170
jmp 0x4fb1e
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x4e054
jmp 0x4fb23
movq (%rsp), %rsi
movq %r12, %rdi
callq 0x3d415
incq 0x20(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
callq 0x4e442
movl %eax, %r13d
cmpl $-0x1, %eax
jne 0x4f9cd
jmp 0x4fb5e
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x40c8f
jmp 0x4faad
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
callq 0x4e426
movq 0x30(%rsp), %rbx
testq %rbx, %rbx
je 0x4fbcc
movq 0x10(%r12), %rdi
movq %rbx, %rsi
callq 0x41e5f
movl %eax, %ebp
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x4e5b5
testq %rax, %rax
je 0x4fbf6
movq 0x10(%r12), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x41b77
movq %r14, %rdi
callq 0x4e5b5
movq %r12, %rdi
movq %rax, %rsi
callq 0x3d23f
movq 0x10(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x6ca8c(%rip), %rsi     # 0xbc651
movl $0x20, %edi
jmp 0x4fc29
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x3cf25
jmp 0x4fc4c
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x38413
movq %r14, %rdi
callq 0x40c8f
leaq 0xb270b(%rip), %rax     # 0x102310
movq 0x20(%rax), %rsi
movq %r12, %rdi
callq 0x3c854
testl %ebp, %ebp
je 0x4fc31
movq 0x10(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x6e904(%rip), %rsi     # 0xbe528
movl $0x4, %edi
movq %rbx, %rdx
callq 0x7c665
movq 0x10(%r12), %rdi
movq %rbx, %rsi
callq 0x41d90
leaq 0xb1c63(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
movq %r15, %rdi
callq 0x38413
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x38413
movq 0x10(%r12), %rdi
movq %r14, %rsi
callq 0x41e5f
testl %eax, %eax
je 0x4fc97
movq 0x10(%r12), %rdi
movq %r14, %rsi
callq 0x41d90
leaq 0xb1c18(%rip), %rax     # 0x1018a8
incq 0x720(%rax)
leaq 0xb2672(%rip), %rax     # 0x102310
movq 0x20(%rax), %rsi
jmp 0x4fcb7
movq %r15, %rdi
callq 0x38413
leaq 0xb265d(%rip), %rax     # 0x102310
movq 0x68(%rax), %rsi
movq %r12, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3c854