// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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, %r13
movq %rdx, %rbp
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
leaq (,%rdx,8), %rdi
callq 0x3b29d
movq %rax, %r15
testq %rbp, %rbp
je 0x5307c
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %r14
testq %r13, %r13
je 0x5303f
movq 0x10(%r12), %rdi
callq 0x44f66
jmp 0x53049
movq 0x10(%r12), %rdi
callq 0x44f5f
movq %rax, %r13
testq %rax, %rax
je 0x5331c
movq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0x441ab
testl %eax, %eax
jne 0x53369
movq %r13, (%r15,%rbx,8)
incq %rbx
cmpq %rbx, %rbp
movq %r14, %r13
jne 0x53022
leaq -0xf4(%rip), %rcx       # 0x52f8f
movl $0x8, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x268a0
testq %r13, %r13
movq %r13, 0x38(%rsp)
je 0x530b2
movq $0x0, 0x30(%rsp)
callq 0x43ed0
movq %rax, 0x18(%rsp)
jmp 0x530c8
movq %r12, %rdi
callq 0x402b0
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq (%r15), %rdi
callq 0x51af7
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rax, 0x20(%rsp)
movq %rax, %rdi
callq 0x51b83
cmpl $-0x1, %eax
je 0x53287
movl %eax, %r13d
movq $0x0, 0x28(%rsp)
movl $0xf0, %ebx
movq %r12, 0x40(%rsp)
movl $0x1, %r12d
movq 0x8(%rsp), %r14
cmpq $0x2, %rbp
jb 0x531ce
movq (%r15,%r12,8), %rax
cmpq (%r15), %rax
je 0x531bf
cmpl $0x2, %r13d
je 0x5318b
cmpl $0x6, %r13d
jne 0x531bf
movl (%rax), %ecx
andl %ebx, %ecx
cmpl $0x60, %ecx
jne 0x53153
movq 0x8(%rax), %rdi
movq 0x10(%rsp), %rsi
callq 0x689dc
testb %al, %al
je 0x531ce
movq (%r15,%r12,8), %rax
movl (%rax), %eax
andl %ebx, %eax
cmpl $0x20, %eax
jne 0x531bf
movq 0x10(%rsp), %rdi
callq 0x43bb7
movq %rax, %r14
movq (%r15,%r12,8), %rdi
movq %rax, %rsi
callq 0x51a76
testl %eax, %eax
je 0x53275
movq %r14, %rdi
callq 0x440f3
jmp 0x531bf
movl (%r14), %ecx
andl %ebx, %ecx
cmpl $0x10, %ecx
jne 0x531af
movl (%rax), %ecx
andl %ebx, %ecx
cmpl $0x60, %ecx
jne 0x531af
movq 0x8(%rax), %rdi
movq 0x8(%r14), %rsi
callq 0x689dc
testb %al, %al
je 0x531ce
movq (%r15,%r12,8), %rdi
movq %r14, %rsi
callq 0x51a76
testl %eax, %eax
je 0x531ce
incq %r12
cmpq %r12, %rbp
jne 0x5311b
movq %rbp, %r12
movq %r14, 0x8(%rsp)
cmpq %rbp, %r12
jne 0x53214
cmpq $0x0, 0x38(%rsp)
movq 0x40(%rsp), %r12
je 0x5321b
cmpl $0x6, %r13d
jne 0x53230
movq 0x10(%rsp), %rdi
callq 0x43bb7
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x51790
movq %r14, %rdi
callq 0x440f3
jmp 0x53253
movq 0x40(%rsp), %r12
jmp 0x53253
cmpl $0x2, %r13d
jne 0x53241
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x40537
jmp 0x5324e
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x51790
jmp 0x53253
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x407de
incq 0x28(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x51b83
movl %eax, %r13d
cmpl $-0x1, %eax
jne 0x53106
jmp 0x53290
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x440f3
jmp 0x531d3
movq $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rdi
callq 0x51b67
movq 0x38(%rsp), %rbx
testq %rbx, %rbx
je 0x53305
movq 0x10(%r12), %rdi
movq %rbx, %rsi
callq 0x452f3
movl %eax, %ebp
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x51cf6
testq %rax, %rax
je 0x5337f
movq 0x10(%r12), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x45007
movq %r14, %rdi
callq 0x51cf6
movq %r12, %rdi
movq %rax, %rsi
callq 0x40606
movq 0x10(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x6c306(%rip), %rsi     # 0xbf601
movl $0x20, %edi
jmp 0x533b2
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x402ec
jmp 0x533d5
movq %r15, %rdi
callq 0x3b3df
testq %r14, %r14
je 0x533ec
movq 0x10(%r12), %rdi
movq %r14, %rsi
callq 0x452f3
testl %eax, %eax
je 0x53359
movq 0x10(%r12), %rdi
movq %r14, %rsi
callq 0x45220
leaq 0xb3506(%rip), %rax     # 0x106858
incq 0x710(%rax)
leaq 0xb3f50(%rip), %rax     # 0x1072b0
movq 0x20(%rax), %rsi
jmp 0x533f7
movq %r15, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3b3df
movq %r14, %rdi
callq 0x440f3
leaq 0xb3f22(%rip), %rax     # 0x1072b0
movq 0x20(%rax), %rsi
movq %r12, %rdi
callq 0x3fc19
testl %ebp, %ebp
je 0x533ba
movq 0x10(%r12), %rax
movl 0x30(%rax), %ecx
leaq 0x6e109(%rip), %rsi     # 0xc14b6
movl $0x4, %edi
movq %rbx, %rdx
callq 0x800c9
movq 0x10(%r12), %rdi
movq %rbx, %rsi
callq 0x45220
leaq 0xb348a(%rip), %rax     # 0x106858
incq 0x710(%rax)
movq %r15, %rdi
callq 0x3b3df
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb3ebd(%rip), %rax     # 0x1072b0
movq 0x68(%rax), %rsi
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3fc19